Background and CSS Floats

。_饼干妹妹 提交于 2019-12-22 12:42:31

问题


So I am using the 960 grid system and found a few things that they don't support. I've considered switching to Blueprint, but I've got to come back to the design a bit later in the process. Anyway, I've simplified my code to show what I'm experiencing:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 

Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Test CSS</title>
<style type="text/css">
    .frame { width: 960px; margin-left: auto; margin-right: auto; }
    .column { display: inline; float: left; position: relative; margin: 10px 10px 10px 10px; width: 300px; background-color: silver; }
    #bkg { background-color: blue; }
</style>
</head>

<body>
    <div class="frame" id="bkg">
        <div class="column">Column A</div>
        <div class="column">Column B<div><br/><br/><br/><br/></div></div>
        <div class="column">Column C</div>
    </div>
</body>

</html>

I'm trying to see the background expand to cover all 3 columns. Is there anything I can do to expand the background so it covers all 3 columns?


回答1:


You can have the frame float as in:

.frame { width: 960px; margin-left: auto; margin-right: auto;float:left}

and this does it. Or you can put an extra element in the frame with a "clear fix". The pre-made 960 grid does this for you with a class "clear":

(css  .clearfix { clear: both } )
<div class="frame"
   ...
<div class="clearfix" /></div>



回答2:


Just add this extra rule to your selector:

div.frame {
     overflow:hidden;
}

You shouldn't need to rely on the clearfix unless you have elements coming outside of the frame, nor should you need to rely on any extraneous markup with clear:both.




回答3:


You need to apply the clearfix class to #bkg.

<div class="frame clearfix" id="bkg">



回答4:


What you want to do is "clear the float." There are many options out there, some easy, and some quite complex.

The easiest, is to add a clearing div after the last column, like this:

<div class="frame" id="bkg">
  <div class="column">Column A</div>
  <div class="column">Column B<div><br/><br/><br/><br/></div></div>
  <div class="column">Column C</div>
  <div style="clear: both;"></div>
</div>

And that should work.

Here are some other ways (some, pure css):

  • http://www.positioniseverything.net/easyclearing.html
  • http://www.quirksmode.org/css/clearing.html


来源:https://stackoverflow.com/questions/1495952/background-and-css-floats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!