背景和 CSS 浮动

发布于 2024-08-06 00:03:19 字数 1105 浏览 2 评论 0原文

所以我使用 960 网格系统,发现了一些他们不支持的东西。我考虑过切换到蓝图,但我必须在此过程中稍后再回到设计。不管怎样,我已经简化了我的代码来展示我正在经历的事情:

<!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>

我试图看到背景扩展以覆盖所有 3 列。我可以做些什么来扩展背景,使其覆盖所有 3 列吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ˉ厌 2024-08-13 00:03:19

只需将这个额外的规则添加到您的选择器中即可:

div.frame {
     overflow:hidden;
}

除非您有元素位于框架之外,否则您不需要依赖clearfix,也不应该依赖clear:both 的任何无关标记。

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.

您可以让框架浮动,如下所示:

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

这样就可以了。或者,您可以通过“明确修复”在框架中放置额外的元素。预制的 960 网格通过“clear”类为您完成此操作:

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

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>
雄赳赳气昂昂 2024-08-13 00:03:19

您需要将 clearfix 类应用于#bkg。

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

You need to apply the clearfix class to #bkg.

<div class="frame clearfix" id="bkg">
难得心□动 2024-08-13 00:03:19

你要做的就是“清除浮动”。有很多选择,有些简单,有些则相当复杂。

最简单的方法是在最后一列之后添加一个清除 div,如下所示:

<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>

应该可以工作。

这里有一些其他方法(一些,纯CSS):

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):

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文