简体   繁体   English

如何并排渲染li项目

[英]how to render li item side by side

在此处输入图片说明 I am trying to have a list of items(li tags) side by side, but it is not working. 我正在尝试并排列出项(li标签),但是它不起作用。 Basically I want the first 7 items to float on the left and the rest of items to float on the right, but all be at the same level. 基本上,我希望前7个项目在左侧浮动,其余项目在右侧浮动,但所有项目都处于同一水平。 Although one group of list is on the left and the other group of list is on the right but they are not aligned properly, the right group is not parallel to the one on the left. 尽管一组列表在左侧,而另一组列表在右侧,但它们排列不正确,但右侧组与左侧的列表不平行。 Here is the html code 这是HTML代码

<ul class="sidenav nav navbar-nav">
          <li class="list-title active" data-group="Article"><a href="#api-Article">Article</a></li>
        <li data-version="1.0.0" data-name="GetArticlesId" data-group="Article" class="">
          <a href="#api-Article-GetArticlesId">Get an article's basic content</a>
        </li>
        <li data-version="1.0.0" data-name="GetArticleDocumentsId" data-group="Article" class="">
          <a href="#api-Article-GetArticleDocumentsId">Get an article's documents</a>
        </li>
        <li data-version="1.0.0" data-name="GetArticleLinksId" data-group="Article" class="">
          <a href="#api-Article-GetArticleLinksId">Get an article's links</a>
        </li>
        <li data-version="1.0.0" data-name="GetArticlePhotosId" data-group="Article" class="">
          <a href="#api-Article-GetArticlePhotosId">Get an article's photos</a>
        </li>
        <li data-version="1.0.0" data-name="GetArticleVideosId" data-group="Article" class="">
          <a href="#api-Article-GetArticleVideosId">Get an article's videos</a>
        </li>
          <li class="list-title" data-group="Articles"><a href="#api-Articles">Articles</a></li>
        <li data-version="1.0.0" data-name="GetArticleKeywordsKeyword" data-group="Articles" class="">
          <a href="#api-Articles-GetArticleKeywordsKeyword">Get articles by keyword</a>
        </li>
        <li data-version="1.0.0" data-name="GetNewsId" data-group="Articles" class="">
          <a href="#api-Articles-GetNewsId">Get articles by organization</a>
        </li>
        <li data-version="1.0.0" data-name="GetHeadlines" data-group="Articles" class="">
          <a href="#api-Articles-GetHeadlines">Get articles from the announcment section</a>
        </li>
        <li data-version="1.0.0" data-name="GetLeads" data-group="Articles" class="">
          <a href="#api-Articles-GetLeads">Get articles from the featured news section</a>
        </li>
  </ul>

Here is the css part: 这是CSS部分:

#sidenav {
  #scrollingNav {
   background-color: #F9FAFA;
   height: 100%;
   ul.sidenav {
    @include breakpoints(mobile nav tablet lg_tablet) {
      li:nth-child(1) {
        float: left;
        display: inline;
      }
       li:nth-child(2) {
        float: left;
        display: inline;
      }
       li:nth-child(3) {
        float: left;
        display: inline;
      }
       li:nth-child(4) {
        float: left;
        display: inline;
      }
       li:nth-child(5) {
        float: left;
        display: inline;
      }
      li:nth-child(6) {
        float: left;
        display: inline;
      }
       li:nth-child(7) {
        float: right;
        display: inline;
      }
       li:nth-child(8) {
        float: right;
        display: inline;
      }
       li:nth-child(9) {
        float: right;
        display: inline;
      }
       li:nth-child(10) {
        float: right;
        display: inline;
      }
       li:nth-child(11) {
        float: right;
        display: inline;
      }
    }.......

I am not sure how to put to make the first group li:nth-child(1) to li:nth-child(7) to float on the left and the rest float on the right. 我不确定如何使第一个组li:nth-​​child(1)到li:nth-​​child(7)浮在左边,其余的浮在右边。

There a different things you should consider. 您应该考虑其他问题。

  • display:inline has no effect on floated elements (but may be used to fix the IE6 double-margin bug). display:inline对浮动元素没有影响(但可以用于修复IE6双重漏洞)。
  • you can simplify your code by using a "smarter" selector, for example :nth-child(-n+6) to select the first 6 li elements. 您可以使用“更智能”的选择器来简化代码,例如:nth-child(-n+6)选择前6个li元素。 These selectors have a quite good range of browser compatibility 这些选择器具有很好的浏览器兼容性

Combining this, you can float your li elements like so: 结合这一点,您可以像这样浮动li元素:

li {
    float: right;

    // float elements 1-7, while 0 is the first child
    &:nth-child(-n+6) {
           float: left;
    }
}

DEMO - Keep in mind, that the browser window needs to be wide enough to display all elements in the same line. 演示 -请记住,浏览器窗口必须足够宽才能在同一行中显示所有元素。

Maybe you should consider using a more responsive approach, using a newer display: flex method. 也许您应该考虑使用响应更快的方法,使用更新的display: flex方法。

Last, if you simply want to create to columns, you could consider the use of the columns: 2 property, as suggested by @dippas. 最后,如果您只想创建列,则可以考虑使用columns: 2属性,如@dippas所建议。 But be aware of the leck of support by . 但是要注意缺乏支持。

This could be helpful to you. 这可能对您有帮助。

li{
   line-height:1.5em;
   border-bottom:1px solid #ccc;
   float:left;
   display:inline;
}

JSFIDDLE JSFIDDLE

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

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