【问题标题】:How to display the first 3 li with 100% and per row 2 li with 33.33% width?如何以 100% 显示前 3 li,每行 2 li 以 33.33% 宽度显示?
【发布时间】:2019-11-22 05:39:40
【问题描述】:

我有一个设计。我在这里遇到了一个问题。

我所做的是,我必须以 100% 的宽度显示前 3 个 li 标签,并保持 33.33% 的 li 标签。

这是我的预期输出

li-100% width
li-100% width
li-100% width
li-33.33% width | li-33.33% width | big image here(not a part of loop)
li-33.33% width | li-33.33% width |
li-33.33% width | li-33.33% width |

但请注意,我正在使用while 循环,所以我会得到类似的输出

li-100% width
li-100% width
li-100% width
li-33.33% width | li-33.33% width | li-33.33% width
li-33.33% width | li-33.33% width | li-33.33% width
li-33.33% width | li-33.33% width | li-33.33% width

我必须每行只显示 2 li。

我正在使用以下逻辑

<div class="wrapperClass">
  <ul>
    <?php
      $index = 0;
      $check=0;
       while ( condition ) {
         if ($index < 3) {?>
      <li>
        <!--width 100%-->
      </li>
      <?php }
                else{?>
      <li>
        <!--width with 33.33-->
      </li>
      <?php } $index++;}?>
  </ul>
  <img src="">
</div>

css

.wrapperClass ul li:nth-child(1),.wrapperClass ul li:nth-child(2),.wrapperClass ul li:nth-child(3){
    width:100%;
}
.wrapperClass ul li{width:33.33%;display: inline-block;}

你能帮我解决这个问题吗?

【问题讨论】:

  • 创建li时,可以给不同的class,在合适的class中使用css
  • 请提供输出html或小提琴链接
  • @chandershekhar 小提琴链接由于逻辑原因在这里不起作用。我在上图中分享了我的预期输出。
  • @DevsiOdedra,你能分享一个例子吗?
  • @NarenVerma 提供输出 html

标签: php jquery html css html-lists


【解决方案1】:

使用这个 css

.wrapperClass ul li:nth-child(-n+3){
    width:100%;
}
.wrapperClass ul li{display: inline-block; width:33.33%;}
<div class="wrapperClass">
  <ul>
    <li>Some text</li>
    <li>Some text</li>
    <li>Some text</li>
    <li>Some text</li>
    <li>Some text</li>
  </ul>
</div>

【讨论】:

  • 这不是我的预期输出。
  • 我尝试了您的代码,但没有得到预期的输出。这是我得到的输出 prnt.sc/q0flhl 我必须在显示 3 li 后以 100% 显示 3 li 的行中仅显示 2 li
  • @NarenVerma,也许,你有一个影响提供答案的上层 css。这就是为什么即使它们运行良好,您也永远不会在这里找到答案。
  • @AksenP,我也在尝试,但我对 3 li 之后必须使用的逻辑感到困惑。我可以根据输出改变CSS。
【解决方案2】:

你可以做这样的事情。此代码仅供参考。

      
      var html = '';
      for(var i = 1; i<= 9; i++){
      
        if(i <= 3){
          html += "<li class='fullli'> full </li> ";
          
        }else {
           html += "<li class='halflli'> half </li> ";
        }
      }
      
      $('.mainul').html(html);
.fullli {
  width : 100%;
}
.halflli {
  width : 33%;
  display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapperClass">
  <ul class="mainul">
   
  </ul>
  <img src="">
</div>

【讨论】:

  • @Devshi,给我一些时间检查
  • 我尝试了您的代码,但没有得到预期的输出。这是我得到的输出prnt.sc/q0flhl 我必须在行中只显示 2 li instated of 3
【解决方案3】:

您可以使用网格布局解决此问题。这是一个例子:

HTML

<div class="layout">
  <ul>
    <li>one</li>
    <li>two</li>
    <li>three</li>
    <li>four</li>
    <li>five</li>
    <li>six</li>
    <li>seven</li>
  </ul>
  <div class="image-wrapper">
    <img src="some-image.png" alt="some image">
  </div>
</div>

CSS

.layout {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-column-gap: 10px;
  grid-row-gap: 5px;
}

ul {
  display: contents;
  list-style: none;
}

li:nth-child(-n + 3) {
  background-color: #bada55;
  grid-column: 1/4;
}

li:nth-child(2n + 4) {
  background-color: skyblue;
}

li:nth-child(2n + 5) {
  background-color: lightblue;
}

.image-wrapper {
  background-color: hotpink;
  grid-column: 3/4;
  grid-row: 4/7;
}

img {
  width: 100%;
  /*just some random height to demonstrate
  that the items on the left side will keep their height
  even with a much higher image on the right side.
  I would recomment setting the height to 'auto' though.*/
  height: 250px;
}

请注意,我在此处使用的 css 技术在很大程度上不受 Internet Explorer 11 的支持,因为该浏览器使用的是较旧的网格规范。如果您也想支持 Internet Explorer 11,您可能需要找到一种解决方法。

如果您可以更改 HTML 的结构,您也可以执行以下操作:

HTML

<div class="container">
  <div>one</div>
  <div>two</div>
  <div>three</div>
  <div class="columns">
    <div>
        <div>four</div>
        <div>five</div>
        <div>six</div>
        <div>seven</div>
        <div>eight</div>
        <div>nine</div>
        <div>ten</div>
        <div>eleven</div>
        <div>...and so on</div>
    </div>
    <div>
      <img src="some-image.png" alt="some image">
    </div>
  </div>
</div>

CSS

.columns {
  display: flex;
  flex-flow: row nowrap;
  align-items: flex-start;
}

.columns > *:first-child {
  width: 66%;
  display: flex;
  flex-flow: row wrap;
}

.columns > *:first-child > * {
  width: 50%;
}

.columns > *:last-child {
  width: 34%;
}

img {
  display: block;
  width: 100%;
  height: auto;
}

【讨论】:

  • 任何其他没有网格的解决方案?
  • 您可以更改 html 的结构。然后使用 flex-box 甚至浮动来构建它会很容易。
  • 改变结构意味着什么?我需要更改设计吗?
  • 在您的第二个解决方案中,如果我得到超过 6 条记录,它将无法像我预期的输出那样工作......我正在从数据库中获取记录
  • 是的,我检查了更新的答案,但动态它不起作用。
猜你喜欢
  • 2011-01-26
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 2015-06-14
  • 2017-04-22
  • 1970-01-01
  • 2011-03-25
  • 1970-01-01
相关资源
最近更新 更多