开发者

CSS Layout places text right next to it instead of below

开发者 https://www.devze.com 2023-01-25 06:23 出处:网络
I have a problem with my CSS layout seen below: #wrapper { width: 80%; } #content { float: left; background: #FFFF00;

I have a problem with my CSS layout seen below:

#wrapper { 
 width: 80%;
} 

#content { 
 float: left;
 background: #FFFF00;
 height: 350px;
 width: 70%;
 display: inline;
}

#rightcolumn { 
 background: #EBE3CD;
 height: 开发者_如何学Go350px;
 width: 30%;
 height: 250px;
 float: left;
}

#legendcolumn {
 background: #FF00FF;
 height: 100px;
 width: 30%;
 float: left;
}

The body of my HTML is as follows:

 <div id="wrapper">
  <div id="content">
   Main Content.
  </div>
  <div id="rightcolumn">
   Right Column.
  </div>
  <div id="legendcolumn">
   Here comes the legend.
  </div>
 </div>

 Lorem ipsum dolor sit amet.

Unfortunately, the text lorem ipsum dolor sit amet is placed next to the layout. However, I would like to place the text right below the layout. How can I achieve this without introducing a new div container?


The float and inline contents are causing the issue, but you can control that with The Power of Overflow:

#wrapper 
{  
  width: 80%; 
  overflow: auto;  // overflow + float = magic happy land
}  

Edit: You can see why display:block and clear:both won't work if you add a border: 1px solid red; to #wrapper. One of the consequences of float is that the container will collapse to the height of it's non-floating children (zero here). Block and Clear will have zero apparent affect (block would have anyway - divs are natively block) if the element they reference is zero height. Overflow auto overcomes this.


Because your #wrapper is set to only 80% in width, those text element will try to fill the rest 20% of the screen with it. No exact solution without new div, but I think following may workaround:

#wrapper {
    width: 100%;
    padding-right: 20%:
}


Update your CSS like:

#wrapper { 
 width: 80%;
 display: block;
} 


why do you have #wrapper width as 80%? make it 100% instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消