css 使一个div在另一个Div中浮动

kjthegm6  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(263)

我需要一个div在另一个div中浮动。尝试使用位置:已修复,但div现在浮动在父div之外。
下面是示例代码。我需要“Div to Float”浮动在“Div 1”内部。现在它浮动在“Div 1”外部并位于“Div 2”后面。
下面是代码。

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style id="compiled-css" type="text/css">
.wrapper {<!--from  www  .j  av a2s.c  o  m-->
   width:100%;
   height: 200px;
   overflow-y: scroll;
}

.container {
   width: 301px;
  margin: 0px auto;
  height: 1501px;
  background: green;
  position: relative;
}

.element {
   background:yellow;
   position:fixed;
   width:101px;
   height:71px;
   top:51px;
   right:0px;
   left:769px;
   border:2px solid blue;
}
</style> 
 </head> 
 <body> 
  <div class="wrapper"> 
   <div class="container"> Div 1
    <div class="element">
      Div to float
    </div> 
   </div> 
  </div>  
  <div class="container" style="margin-top: 30px;background: purple;"> 
     Div 2
   </div>
 </body>
</html>

"我尝试过的一切"
https://codepen.io/vrnreg/pen/poKdvBJ
"我所期待的“
我需要“Div-to-Float”在“Div 1”中浮动。
"现在的结果是什么"
现在,它漂浮在'分区1'外面,并在'分区2'后面

whlutmcx

whlutmcx1#

.container {
    position:relative;
}
.element{
position:absolute;
}

我不完全理解“float”是什么意思,但是这段代码会把你的div.element放在div.container里面

相关问题