简体   繁体   English

两列布局<ul>

[英]Two-column layout with <ul>

I have been trying to create a simple two column layout with css without using floats. 我一直在尝试使用css创建一个简单的两列布局而不使用浮点数。 I found several that suggested using an <ul> and each <li> would be a column. 我发现了一些建议使用<ul>建议,每个<li>都是一列。 I tried that and I can't seem to get it to work. 我尝试了一下,但似乎无法正常工作。 Here is my sample code. 这是我的示例代码。

<ul id="blogWrapper">
    <li id="testBlogCol1">Column 1</li>
    <li id="testBlogCol2">Column 2</li>
</ul>
#blogWrapper {
    width: 65%;
    height: auto;
    min-width: 45%;
    margin: 0 auto;
    border: 1px solid #cccccc;
    background-color: #fff;
}

#blogWrapper ul li {
    display: inline;
}

#testBlogCol1 {
    width: 40%;
    height: auto;
    background-color: #f5f5f5;
}

#testBlogCol1 {
    width: 60%;
    height: auto;
    background-color: #fff;
}

Instead of the <li> 's seperating into two columns like I want it to, they just stack on top of each other like normal. 而不是像我想要的那样将<li>分成两列,它们像通常一样彼此堆叠。 Any suggestions? 有什么建议么?

write inline-block instead of inline because inline element didn't take width, height, vertical margin & vertical padding . inline-block而不是inline因为inline元素没有占用width, height, vertical margin & vertical padding

write like this: 这样写:

#blogWrapper ul li {
    display: inline-block;
    *display:inline;/* For IE7*/
    *zoom:1;/* For IE7*/
    vertical-align:top;
}

Use the following CSS: 使用以下CSS:

#blogWrapper {
    width: 65%;
    height: auto;
    min-width: 45%;
    margin: 0 auto;
    border: 1px solid #cccccc;
    background-color: #fff;
}
#blogWrapper
{
  overflow: hidden;    
}
#blogWrapper li {
    float: left;
    display: block;
}

#testBlogCol1 {
    width: 40%;
    height: auto;
    background-color: #f5f5f5;
}

#testBlogCol2 {
    width: 50%;
    height: auto;
    background-color: #fff;
}

Currently using overflow: hidden to stretch the UL as long as the LI is. 当前正在使用溢出:只要LI处于隐藏状态就可以拉伸UL。 Otherwise the content after the UL will be shown on the same spot as the UL. 否则,UL之后的内容将显示在与UL相同的位置。

You currently write #blogWrapper ul while #blogWrapper is your UL. 您当前正在编写#blogWrapper ul#blogWrapper是您的UL。 So that CSS will have no effect. 这样CSS不会起作用。
The CSS you are using contains #testBlogCol1 twice. 您使用的CSS包含#testBlogCol1两次。

Live fiddle: http://jsfiddle.net/wHz94/ 现场小提琴: http : //jsfiddle.net/wHz94/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM