简体   繁体   English

CSS 使 UL 列表包装 LI

[英]CSS make UL list wrap LI

I want to make the UL (yellow) wrap the LI list elements (purple) horizontally without any fixed widths on the UL .我想让 UL(黄色)水平包裹 LI 列表元素(紫色) ,而 UL 上没有任何固定宽度 The wrap has been added for the example.为示例添加了包装。

HTML HTML

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

</div>

CSS CSS

.wrap {
  background: green;
  width: 500px;
  padding: 10px 20px;
}

li {
  display: inline-block;
  width: 70px;
  height: 50px;
  background: purple;
  list-style: none;
}

ul {
  background: yellow;
  margin: 0; padding: 0;
}

Currently目前

当前 CSS

Desired想要的

所需的 CSS

CodePen here: http://codepen.io/ptimson/pen/IrCHB CodePen 在这里: http ://codepen.io/ptimson/pen/IrCHB

To date, the only way you can do this with CSS alone is with media queries - one media query for each #columns layout necessary.迄今为止,您可以单独使用 CSS 做到这一点的唯一方法是使用媒体查询 - 每个 #columns 布局都需要一个媒体查询。

This can be quite tedious using css.使用 css 可能会非常乏味。

Fortunately, you can automate this using a preprocessor such as LESS.幸运的是,您可以使用预处理器(例如 LESS)自动执行此操作。

So say that I have basic markup of <li> 's within an <ul> ...所以说我在<ul><li>的基本标记......

Here's how to take advantage of LESS to set up the media queries:以下是如何利用 LESS 设置媒体查询:

CODEPEN - Resize the window to see this in action CODEPEN - 调整窗口大小以查看实际情况

First set up some less variables according to the design which you need:首先根据您需要的设计设置一些较少的变量:

@item-width:100px;
@item-height:100px;
@marginV: 4px;
@marginH: 2px;
@min-cols:2;
@max-cols:9; //set an upper limit of how may columns you want to write the media queries for

Then:然后:

Set up an iteration mixin like this: (You can paste this code into http://less2css.org )像这样设置迭代混合:(您可以将此代码粘贴到http://less2css.org

.loopingClass (@index-width) when (@index-width <= @item-width * @max-cols) {
    @media (min-width:@index-width) {
        .container{
            width: @index-width;
        }
    }

    .loopingClass(@index-width + @item-width + 2*@marginH);
}

.loopingClass (@item-width * @min-cols + @min-cols*@marginH*2);

Add this to the parent <ul> of the list将此添加到列表的父<ul>

ul {
  display: flex;  
  flex-wrap: wrap;
}

That will cause the <li> to wrap as you want.这将导致<li>根据需要进行换行。 Then add some margin/padding to the right of the <li> elements so they're reasonably spaced.然后在<li>元素的右侧添加一些边距/填充,使它们的间距合理。

<div class="wrap">
  <ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li><br>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
  </ul>
</div>

css css

.wrap {
  display:table;
  background: green;
  padding: 10px 20px;
}
ul {
    display:table;
  background: yellow;
  margin: 0;
  padding: 0;
    font-size: 0;
}
li {
  margin:0 4px 4px 0;
  display: inline-block;
  width: 70px;
  height: 50px;
  background: purple;
  list-style: none;
}
li:nth-child(7){
    margin-right: 0px;
}

****************************************************************** ****************************************************** ****************

other其他

<div class="wrap">
<ul>
    <li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
</ul>
</div>

also

<div class="wrap">
<ul>
    <li>
    <li>
    <li>
    <li>
    <li>
    <li>
    <li>
    <li>
    <li>
    <li>
    <li>
  </ul>
</div>

css css

.wrap {
  background: green;
  width: 520px;
  padding: 10px 20px;
}
ul {
  background: yellow;
  margin: 0;
  padding: 0;
  border:0;
}
li {
  position:relative;
  display: inline-block;
  width: 70px;
  height: 50px;
  background: purple;
  list-style: none;
  margin-right: 5px;
}
li:nth-child(7){
    margin-right: 0px;
}
li:nth-child(14){
    margin-right: 0px;
}

This isn't a "bug" (I don't think).这不是“错误”(我不认为)。 It's just the way setting elements on a line works.这只是在一行上设置元素的方式。 You want spaces between words that you type to be spaces right?您希望键入的单词之间的空格是空格吗? The spaces between these blocks are just like spaces between words.这些块之间的空格就像单词之间的空格。 That's not to say the spec couldn't be updated to say that spaces between inline-block elements should be nothing, but I'm fairly certain that is a huge can of worms that is unlikely to ever happen.这并不是说规范不能更新为内联块元素之间的空间应该没有,但我相当肯定这是一个不太可能发生的巨大蠕虫罐。

Here's some ways to fight the gap and get inline-block elements sitting directly next to each other.这里有一些方法可以弥补差距并使内联块元素直接相邻。

Remove the spaces The reason you get the spaces is because, well, you have spaces between the elements (a line break and a few tabs counts as a space, just to be clear).删除空格 得到空格的原因是,元素之间有空格(为了清楚起见,换行符和几个制表符也算作空格)。 Minimized HTML will solve this problem, or one of these tricks:最小化的 HTML 将解决这个问题,或者这些技巧之一:

<ul>
  <li>
   one</li><li>
   two</li><li>
   three</li>
</ul>

or或者

<ul>
  <li>one</li
  ><li>two</li
  ><li>three</li>
</ul

or with comments...或评论...

<ul>
  <li>one</li><!--
  --><li>two</li><!--
  --><li>three</li>
</ul>

They're all pretty funky, but it does the trick.它们都很时髦,但它确实有效。

Negative margin You can scoot the elements back into place with negative 4px of margin (may need to be adjusted based on font size of parent).负边距您可以使用负 4px 的边距将元素移回原位(可能需要根据父级的字体大小进行调整)。 Apparently this is problematic in older IE (6 & 7), but if you don't care about those browsers at least you can keep the code formatting clean.显然,这在较旧的 IE(6 和 7)中存在问题,但如果您不关心这些浏览器,至少您可以保持代码格式清晰。

display: inline-block;
  margin-right: -4px;

Skip the closing tag HTML5 doesn't care anyway.跳过结束标签 HTML5 无论如何都不在乎。 Although you gotta admit, it feels weird.虽然你不得不承认,但感觉很奇怪。

<ul>
  <li>one
  <li>two
  <li>three
</ul>

将 wrap 的宽度更改为 435px-ish,应该是这样!

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

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