使用 CSS 将 Div 拆分为两列

我一直在尝试使用 CSS 将一个 div 分成两列,但是我还没有设法让它工作。我的基本结构如下:

<div id="content">
<div id="left">
<div id="object1"></div>
<div id="object2"></div>
</div>


<div id="right">
<div id="object3"></div>
<div id="object4"></div>
</div>
</div>

如果我尝试将右边和左边的 div 浮动到它们各自的位置(右边和左边) ,它似乎忽略了内容 div 的背景颜色。其他代码,我已经尝试从各种网站似乎不能翻译到我的结构。

谢谢你的帮助!

480105 次浏览

When you float those two divs, the content div collapses to zero height. Just add

<br style="clear:both;"/>

after the #right div but inside the content div. That will force the content div to surround the two internal, floating divs.

Another way to do this is to add overflow:hidden; to the parent element of the floated elements.

overflow:hidden will make the element grow to fit in floated elements.

This way, it can all be done in css rather than adding another html element.

Floats don't affect the flow. What I tend to do is add a

<p class="extro" style="clear: both">possibly some content</p>

at the end of the 'wrapping div' (in this case content). I can justify this on a semantic basis by saying that such a paragraph might be needed. Another approach is to use a clearfix CSS:

#content:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}


#content {
display: inline-block;
}
/*  \*/
* html #content {
height: 1%;
}


#content {
display: block;
}
/*  */

The trickery with the comments is for cross-browser compatibility.

For whatever reason I've never liked the clearing approaches, I rely on floats and percentage widths for things like this.

Here's something that works in simple cases:

#content {
overflow:auto;
width: 600px;
background: gray;
}


#left, #right {
width: 40%;
margin:5px;
padding: 1em;
background: white;
}


#left  { float:left;  }
#right { float:right; }

If you put some content in you'll see that it works:

<div id="content">
<div id="left">
<div id="object1">some stuff</div>
<div id="object2">some more stuff</div>
</div>


<div id="right">
<div id="object3">unas cosas</div>
<div id="object4">mas cosas para ti</div>
</div>
</div>

You can see it here: http://cssdesk.com/d64uy

This works good for me. I have divided the screen into two halfs: 20% and 80%:

<div style="width: 20%; float:left">
#left content in here
</div>


<div style="width: 80%; float:right">
#right content in there
</div>

The most flexible way to do this:

#content::after {
display:block;
content:"";
clear:both;
}

This acts exactly the same as appending the element to #content:

<br style="clear:both;"/>

but without actually adding an element. ::after is called a pseudo element. The only reason this is better than adding overflow:hidden; to #content is that you can have absolute positioned child elements overflow and still be visible. Also it will allow box-shadow's to still be visible.

Best way to divide a div vertically --

#parent {
margin: 0;
width: 100%;
}
.left {
float: left;
width: 60%;
}
.right {
overflow: hidden;
width: 40%;
}

This is best answered here Question 211383

These days, any self-respecting person should be using the stated "micro-clearfix" approach of clearing floats.

  1. Make font size equal to zero in parent DIV.
  2. Set width % for each of child DIVs.

    #content {
    font-size: 0;
    }
    
    
    #content > div {
    font-size: 16px;
    width: 50%;
    }
    

*In Safari you may need to set 49% to make it works.

Make children divs inline-block and they will position side by side:

#content {
width: 500px;
height: 500px;
}


#left, #right {
display: inline-block;
width: 45%;
height: 100%;
}

See Demo

Divide a division in two columns is very easy, just specify the width of your column better if you put this (like width:50%) and set the float:left for left column and float:right for right column.

None of the answers given answer the original question.

The question is how to separate a div into 2 columns using css.

All of the above answers actually embed 2 divs into a single div in order to simulate 2 columns. This is a bad idea because you won't be able to flow content into the 2 columns in any dynamic fashion.

So, instead of the above, use a single div that is defined to contain 2 columns using CSS as follows...

.two-column-div {
column-count: 2;
}

assign the above as a class to a div, and it will actually flow its contents into the 2 columns. You can go further and define gaps between margins as well. Depending on the content of the div, you may need to mess with the word break values so your content doesn't get cut up between the columns.

Pure old school CSS

I know this post is old, but if any of you still looking for a simpler solution.

#container .left,
#container .right {
display: inline-block;
}


#container .left {
width: 20%;
float: left;
}
#container .right {
width: 80%;
float: right;
}

You can use flexbox to control the layout of your div element:

* { box-sizing: border-box; }


#content {
background-color: rgba(210, 210, 210, 0.5);
border: 1px solid #000;
padding: 0.5rem;
display: flex;
}


#left,
#right {
background-color: rgba(10, 10, 10, 0.5);
border: 1px solid #fff;
padding: 0.5rem;
flex-grow: 1;
color: #fff;
}
<div id="content">
<div id="left">
<div id="object1">lorem ipsum</div>
<div id="object2">dolor site amet</div>
</div>


<div id="right">
<div id="object3">lorem ipsum</div>
<div id="object4">dolor site amet</div>
</div>
</div>

If you don't care old browser and need a simple way.

#content {
display: flex;
}


#left,
#right {
flex: 50%;
}