包含float元素的css中包含元素的边框

时间:2012-10-06 20:46:04

标签: css border

在这个2列布局中,我想在整个页面上放置一个边框。我想如果我在<body>标签内放置'border:solid'就能完成工作,但事实并非如此。它只在标题<h1>上添加了边框。有人可以帮忙吗? 谢谢!

http://jsfiddle.net/v5gSt/

<!DOCTYPE html>
<html>
    <head>
        <title>Two Column Layout</title>
        <style type="text/css">
            body { 
                width: 960px;
                font-family: Arial, Verdana, sans-serif;
                color: #665544;
                border: solid;
                }
            .column1of2 {
                float: left;
                width: 620px;
                margin: 10px;}
            .column2of2 {
                float: left;
                width: 300px;
                margin: 10px;}
        </style>
    </head>
    <body>
        <h1>The Evolution of the Bicycle</h1>
        <div class="column1of2">
            <h3>The First Bicycle</h3>
            <p>In 1817 Baron von Drais invented a walking machine that would help him get around the royal gardens faster: two same-size in-line wheels, the front one steerable, mounted in a frame upon which you straddled. The device was propelled by pushing your feet against the ground, thus rolling yourself and the device forward in a sort of gliding walk.</p>
            <p>The machine became known as the Draisienne (or "hobby horse"). It was made entirely of wood. This enjoyed a short lived popularity as a fad, not being practical for transportation in any other place than a well maintained pathway such as in a park or garden.</p>
            <h3>Further Innovations</h3>
            <p>The next appearance of a two-wheeled riding machine was in 1865, when pedals were applied directly to the front wheel. This machine was known as the velocipede (meaning "fast foot") as well as the "bone shaker," since it's wooden structure combined with the cobblestone roads of the day made for an extremely uncomfortable ride. They also became a fad and indoor riding academies, similar to roller rinks, could be found in large cities.</p>
            <p>In 1870 the first all-metal machine appeared. (Prior to this, metallurgy was not advanced enough to provide metal which was strong enough to make small, light parts out of.) The pedals were attached directly to the front wheel with no freewheeling mechanism. Solid rubber tires and the long spokes of the large front wheel provided a much smoother ride than its predecessor.</p>
        </div>
        <div class="column2of2">
            <h3>Bicycle Timeline</h3>
            <ul>
                <li>1817: Draisienne</li>
                <li>1865: Velocipede</li>
                <li>1870: High-wheel bicycle</li>
                <li>1876: High-wheel safety</li>
                <li>1885: Hard-tired safety</li>
                <li>1888: Pneumatic safety</li>
            </ul>
        </div>

    </body>
</html>

4 个答案:

答案 0 :(得分:1)

虽然不是语义,但在使用样式clear:both的结束正文标记之前添加空div会修复它。

<强> jsFiddle example

答案 1 :(得分:1)

实际上你的border: solid;有效:)但是这两个div设置为float: left所以删除了文档流,你必须在一个元素上使用clear CSS属性最后一个div。

这个非常基本的例子远非完美但有效:

<style type="text/css">
clear
{
  clear: both;
}
</style>
<div class="column2of2">
...
</div>
<br class="clear"></span>

有很多解决方案可以让您更清楚地了解Quirksmode

中的一些解决方案

答案 2 :(得分:1)

将此行放在正文结束标记

之前
<div style="clear:both "></div>


<html>
<head>
    <title>Two Column Layout</title>
    <style type="text/css">
        body {      
        width: 970px;
        font-family: Arial, Verdana, sans-serif;
        color: #665544;
        border:medium black inset;
        }           
        .column1of2 {
        float: left;
        width: 620px;
        margin: 10px;
    }
        .column2of2 {
        float: left;
        width: 300px;
        margin: 10px;
    }
    </style>
</head>

<body>    
    <h1>The Evolution of the Bicycle</h1>
    <div class="column1of2">        
        <h3>The First Bicycle</h3>
        <p>In 1817 Baron von Drais invented a walking machine that would help him get around the royal gardens faster: two same-size in-line wheels, the front one steerable, mounted in a frame upon which you straddled. The device was propelled by pushing your feet against the ground, thus rolling yourself and the device forward in a sort of gliding walk.</p>
        <p>The machine became known as the Draisienne (or "hobby horse"). It was made entirely of wood. This enjoyed a short lived popularity as a fad, not being practical for transportation in any other place than a well maintained pathway such as in a park or garden.</p>
        <h3>Further Innovations</h3>
        <p>The next appearance of a two-wheeled riding machine was in 1865, when pedals were applied directly to the front wheel. This machine was known as the velocipede (meaning "fast foot") as well as the "bone shaker," since it's wooden structure combined with the cobblestone roads of the day made for an extremely uncomfortable ride. They also became a fad and indoor riding academies, similar to roller rinks, could be found in large cities.</p>
        <p>In 1870 the first all-metal machine appeared. (Prior to this, metallurgy was not advanced enough to provide metal which was strong enough to make small, light parts out of.) The pedals were attached directly to the front wheel with no freewheeling mechanism. Solid rubber tires and the long spokes of the large front wheel provided a much smoother ride than its predecessor.</p>
    </div>
    <div class="column2of2">
        <h3>Bicycle Timeline</h3>
        <ul>
            <li>1817: Draisienne</li>
            <li>1865: Velocipede</li>
            <li>1870: High-wheel bicycle</li>
            <li>1876: High-wheel safety</li>
            <li>1885: Hard-tired safety</li>
            <li>1888: Pneumatic safety</li>
        </ul>            
    </div>        
    <div style="clear:both "></div> 
</body>

答案 3 :(得分:-1)

浮动元素从文档流中取出,因此它们的容器对它们的宽度或高度一无所知,除非它们浮动。

将其视为2D并且实际上漂浮在容器顶部。

你有两个主要选项来解决这个问题:

  • 漂浮身体。
  • 将div包装在页面容器div中并浮动