左右浮动多个li

时间:2013-04-01 13:32:57

标签: css wordpress css3 html-lists

我正在尝试在wordpress中制作自定义侧边栏,我拥有li中的所有元素和信息,我想要做的是尝试将总li的一半移到左边,一半移动到右边...

我正在使用的是左/右浮动/清除,这似乎不像我想要的那样......

HTML结构: -

<ul>
    <li class="left">Left</li>
    <li class="left">Left</li>
    <li class="left">Left</li>
    <li class="left">Left</li>
    <li class="left">Left</li>
    <li class="right">Right</li>
    <li class="right">Right</li>
    <li class="right">Right</li>
    <li class="right">Right</li>
    <li class="right">Right</li>
</ul>

CSS: -

.left { float:left; width:50%; clear:left; }
.right { float:right; width:50%; clear:right }

jsFiddle

4 个答案:

答案 0 :(得分:11)

如果您愿意放弃列表样式光盘(取决于浏览器),您可以轻松完成此操作而无需浮动或修改标记。

http://jsfiddle.net/Duejc/2/

ul {
    -webkit-columns: 2;
    -moz-columns: 2;
    columns: 2;
}

答案 1 :(得分:6)

随机播放你的HTML。当一个元素为clear时,它会对先前浮动的元素进行处理;在你的情况下,它是清除第五个元素的第六个元素:

<ul>
    <li class="left">Left</li>
    <li class="right">Right</li>
    <li class="left">Left</li>
    <li class="right">Right</li>
    <li class="left">Left</li>
    <li class="right">Right</li>
    <li class="left">Left</li>
    <li class="right">Right</li>
    <li class="left">Left</li>
    <li class="right">Right</li>
</ul>

jsFiddle

答案 2 :(得分:6)

为什么不按照这种情况将它制成这种情况:)

<ul class="left">
    <li>Left</li>
    <li>Left</li>
    <li>Left</li>
    <li>Left</li>
    <li>Left</li>
</ul>

<ul class="right">

    <li>Right</li>
    <li>Right</li>
    <li>Right</li>
    <li>Right</li>
    <li>Right</li>
</ul>

.left{
float: left;
width: 50%; }

.right{
float: right;
width: 50%; }

或者以@Xander说:)

答案 3 :(得分:1)

使用简单的标记和样式完成它 http://codepen.io/ruinunes/pen/bpgpZV

HAML:

%ul.chat
  %li.stamp 
    Saturday
    %span 20:32
  %li.left Who is it?
  %li.right It's Little Nero's, sir. I have your pizza.
  %li.left Leave it on the doorstep and get the hell outta here
  %li.stamp 
    Saturday
    %span 20:33
  %li.right Okay, but what about the money?
  %li.left What money?
  %li.right Well, you have to pay for your pizza, sir.
  %li.left Is that a fact? How much do I owe you?
  %li.left Keep the change, ya filthy animal!
  %li.right Cheapskate.

SCSS

ul.chat {
  font-family: sans-serif;
  list-style:none;
  margin: 0 auto;
  padding: 0;
  width: 50%;

  li {
    margin-bottom: 10px;
    display: inline-block;
    border-radius: 8px;
    padding: 10px;

    &.left { 
      background: #e3e3e3;
      float:left;
      margin-right: 50%;
      width:50%;
      border-top-left-radius: 0;
    }

    &.right {
      background: #6CCE66;
      color: #fff;
      float: right;
      width: 50%;
      border-top-right-radius: 0;
    }

    &.stamp {
      color: #666;
      font-size: 80%;
      text-align: center;
      width: 100%;
      span {
        color: #999;
      }
    }
  }

}