负边距流体布局

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
    评论
图像识别技术在病虫害检测中的应用是一个快速发展的领域,它结合了计算机视觉和机器学习算法来自动识别和分类植物上的病虫害。以下是这一技术的一些关键步骤和组成部分: 1. **数据收集**:首先需要收集大量的植物图像数据,这些数据包括健康植物的图像以及受不同病虫害影响的植物图像。 2. **图像预处理**:对收集到的图像进行处理,以提高后续分析的准确性。这可能包括调整亮度、对比度、去噪、裁剪、缩放等。 3. **特征提取**:从图像中提取有助于识别病虫害的特征。这些特征可能包括颜色、纹理、形状、边缘等。 4. **模型训练**:使用机器学习算法(如支持向量机、随机森林、卷积神经网络等)来训练模型。训练过程中,算法会学习如何根据提取的特征来识别不同的病虫害。 5. **模型验证和测试**:在独立的测试集上验证模型的性能,以确保其准确性和泛化能力。 6. **部署和应用**:将训练好的模型部署到实际的病虫害检测系统中,可以是移动应用、网页服务或集成到智能农业设备中。 7. **实时监测**:在实际应用中,系统可以实时接收植物图像,并快速给出病虫害的检测结果。 8. **持续学习**:随着时间的推移,系统可以不断学习新的病虫害样本,以提高其识别能力。 9. **用户界面**:为了方便用户使用,通常会有一个用户友好的界面,显示检测结果,并提供进一步的指导或建议。 这项技术的优势在于它可以快速、准确地识别出病虫害,甚至在早期阶段就能发现问题,从而及时采取措施。此外,它还可以减少对化学农药的依赖,支持可持续农业发展。随着技术的不断进步,图像识别在病虫害检测中的应用将越来越广泛。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值