负边距流体布局

Creating Liquid Layouts with Negative Margins

I was recently confronted with the task of creating a two-column liquid layout with a header and footer in which the content needed to come before the sidebar in the source code. I took opportunity to demonstrate an under-used aspect of CSS: negative margins. Negative margins allow us to push the content area away from the sides of the browser, leaving room for the sidebar.

Starting out simple

To show how negative margins can be helpful in creating liquid layouts, let’s start by creating a liquid two-column layout with a header and footer. The main content area will be on the left, with the sidebar on the right. This would normally be a very simple process, but we are changing things around a bit because we want our source code to be structured in a certain way.

Given the way that floats work, it would be easiest to put the sidebar before the content area, allowing it to float to the right of the rest of the content. However, since it is preferable to have the page’s content come before the sidebar in text browsers, screen readers, and legacy browsers that will not display our styles, we want to come up with a solution that allows the content to come first in the source — and one that works in a vast majority of browsers.

The source code we want

Let’s take a look at what we want our source code to look like for our two-column layout, with a header and a footer:

<div id="header">header</div>

<div id="container">
  <h1>content</h1>
  <p>Lorem ipsum dolor sit amet,
  consectetuer adipiscing elit.
  Phasellus varius eleifend.</p>
  <p class="last">Donec euismod.
  Praesent mauris mi, adipiscing non,
  mollis eget, adipiscing ac, erat.
  Integer nonummy mauris sit.</p>
</div>

<div id="sidebar">
  <h1>sidebar</h1>
  <ul>
    <li>link one</li>
    <li>link two</li>
  </ul>
</div>

<div id="footer">footer</div>

View Example 1 to see how we want the un-styled content to appear. Once you’ve seen that, it will become more obvious why we want the content to come before the sidebar, as browsers that do not correctly read our CSS will be displaying it in this way.

We know that we need the left content area to take up the entire available width minus the space that the right sidebar requires. So what if we could specify 100% -200px as our width? Using negative margins, we can create just that effect. No, really!

The CSS we’ll need

Let’s take a stab at the CSS we are going to need to pull this off. As promised, we are going to set the width of our container div to 100%, float it left, and give it a negative right margin of -200px. It is very important that we float this element, as margins (and thus, negative margins) are handled differently for floated and inline elements than they are for non-floated block-level elements.

The negative right margin has more to do with allowing the sidebar to float up into this element’s space than it does with any positioning/appearance of the element itself, as can be seen in Example 2 below. We float our sidebar to the right and set its width to the 200px we just created for it. Finally, we give our footer div a clear: both style to ensure that it remains below the rest of our content, regardless of which side is longer. We are also going to give our header and footer background colors to set them apart from the rest of the page.

#header {
  background: #d7dabd;
}
#container {
  width: 100%;
  float: left;
  margin-right: -200px;
}
#sidebar {
  width: 200px;
  float: right;
}
#footer {
  background: #d7dabd;
  clear: both; 
}

That CSS will give us the results in Example 2. As you can see, we now need to push the content area away from the right side of the document. We’ve chosen to use another div to do this, as browser support will be better for this method than there is for some of the alternatives. Thus we change our XHTML to look like this:

<div id="container">
  <div id="content">
    <h1>content</h1>
    <p>Lorem ipsum dolor sit amet,
    consectetuer adipiscing elit.
    Phasellus varius eleifend.</p>
    <p class="last">Donec euismod.
    Praesent mauris mi, adipiscing non,
    mollis eget, adipiscing ac, erat.
    Integer nonummy mauris sit.</p>
  </div>
</div>

We now add right margin and a background color to the content div, which should place it just where we need it and separate it from the sidebar.

#content {
  background: #f1f2ea;
  margin-right: 200px;
}

Heading off problems

Jumping a bit ahead of the visual examples, we’re going to fix another problem. We need to set the first element in our content div to have no top margin and the last element to have no bottom margin. In our case, we are simply going to set the margin-top of the h1 element to 0, and set up a class for the last paragraph of our content div, which will set the margin-bottom to 0. Now that we’ve done that,Example 3 is ready to look at.

It’s getting better, but of course you’ll have noticed that the right sidebar does not extend all the way to the bottom. Thanks to the little trick of using images as background, as shown by Dan Cederholm’s “Faux Columns,” we can easily fix this problem.

First, we create the below image. It is 200 pixels wide because the width must correspond to the width of our sidebar.

the image we'll use to create the faux-column background

To pull off Dan’s trick, we add a wrapper div around the container and sidebar divs, and also add a div with clear: both set on it below them. Our XHTML will now look like this:

<div id="wrapper">
  <div id="container">
    <div id="content">
      <h1>content</h1>
      <p>Lorem ipsum dolor sit amet,
      consectetuer adipiscing elit.
      Phasellus varius eleifend.</p>
      <p class="last">Donec euismod.
      Praesent mauris mi, adipiscing non,
      mollis eget, adipiscing ac, erat.
      Integer nonummy mauris sit.</p>
    </div>
  </div>
	
  <div id="sidebar">
    <h1>sidebar</h1>
    <ul>
      <li>link one</li>
      <li>link two</li>
    </ul>
  </div>
  <div class="clearing">&nbsp;</div>
</div>

Now that we’ve done that, we can add the background to this wrapper div. We set the background to repeat-y and to be positioned to the right. To fix a bug in Internet Explorer, we also need to add the same background to our container div.

#wrapper {
  background:
    #f1f2ea url(background.gif) repeat-y right;
}
#container {
  width: 100%;
  background:
    #f1f2ea url(background.gif) repeat-y right;
  float: left;
  margin-right: -200px;
}

W’ll also set up the styles for our clearing div to ensure that the footer will fall below both columns and that they will display correctly:

.clearing {
  height: 0;
  clear: both;
}

That will give us a very nice-looking two-column liquid layout that degrades very well in the absence of CSS. Take a look at Example 4. Simply adding some borders and padding to the elements can give you a fine start to a liquid two-column layout. Of course, if we would have wanted the sidebar on the right, we would simply have to change the floats and margins to their opposites. Where we see float: left, we would change to float: right; where we see margin-right: 200px, we would change to margin-left: 200px, and so on.

Getting trickier: the three-column version

But what if we can take this even further and attempt a three-column layout with a fluid content area? Not only can we do this, we can do it surprisingly easily! We’ll need to make a few final tweaks to our XHTML by adding a few more divs — and then we’ll be ready to write some more CSS.

<div id="outer_wrapper">

  <div id="wrapper">
    <div id="container">
      <div id="content">
      
        <div id="left">
          <h1>navbar</h1>
          <ul>
            <li>link one</li>
            <li>link two</li>
          </ul>
        </div>
        
        <div id="main">
          <h1>content</h1>
          <p>Lorem ipsum dolor sit amet,
          consectetuer adipiscing elit.
          Phasellus varius eleifend.</p>
          <p class="last">Donec euismod.
          Praesent mauris mi, adipiscing non,
          mollis eget, adipiscing ac, erat.
          Integer nonummy mauris sit.</p>
        </div>
        
      </div>
    </div>
		
    <div id="sidebar">
      <h1>sidebar</h1>
      <p>Here be your sidebar.
      Add whatever content you may desire.</p>
    </div>
		
    <div class="clearing">&nbsp;</div>
  </div>
  
</div>

Re-implementing faux columns

Again, since we will want all our columns to have equal heights, we are going to create more faux columns. We’ve created the following two images:

the images we'll use to create faux-column backgrounds

As you can see by the XHTML above, we’ve added another wrapper div in addition to the left sidebar div, and a div around the middle content. Our new wrapper div will contain the background image for our new column, positioned to the left and repeated down to the bottom of the div. Also, we’ve removed the background from the content div and will now add the desired background color to our outer_wrapper div:

#outer_wrapper {
  background: #fff url(background_3.gif) repeat-y left;
}
#wrapper {
  background: url(background_2.gif) repeat-y right;
}

The white background will show through where the image is not being displayed, thus coloring our middle column. We are also going to add the backgrounds to our inner elements to avoid some ugly gaps that are present in most versions of Internet Explorer.

#container {
  width: 100%;
  float: left;
  margin-right: -200px;
  background: url(background_2.gif) repeat-y right;
}
#content {
  margin-right: 200px;
  background: url(background_3.gif) repeat-y left;
}

We then add these simple styles to again use margins to position our left and middle columns where needed.

#main {
  margin-left: 150px;
}
#left {
  width: 150px;
  float: left;
}

Finally, we’ve added the following styles to our header and footer divs to give the layout a more finished look:

border: 1px solid #cecea5;

Take a look at Example 5 and feel free to view the source to see it in its entirety.

Of course, using the @import rule on your final sites would be a good idea to keep your site from getting displayed with its pants down in legacy browsers.

In conclusion

As you can see, negative margins are an under-utilized aspect of CSS that add another layout option for those who want to control the order of elements in the source code and who don’t mind adding a non-semantic wrapper div to do so.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值