html - 根据元素数量以不同宽度显示列表元素

标签 html css

我的 CSS 需要修改以显示 5 个元素,但如果元素少于 5 个,它应该覆盖整个宽度。

/* new css added */

.product-section {
  width: 100%;
  float: left;
  padding: 108px 80px 0;
}

.product-section h6 {
  width: 100%;
  float: left;
  font-family: 'Open Sans';
  font-weight: 600;
  font-size: 17px;
  line-height: 19px;
  color: #585253;
  text-transform: uppercase;
  border-bottom: 1px solid #d0d0d0;
  padding: 0 0 6px;
}

.veiwed-related {
  width: 100%;
  float: left;
  padding: 66px 0 0;
}

.recently-veiwed-related ul {
  width: 100%;
  float: left;
  list-style: none;
  margin: 0 0 0 -33px;
  padding: 0;
  position: relative;
}

.veiwed-related ul li {
  width: 16.5%;
  float: left;
  list-style: none;
  margin: 0 0 20px 33px;
  padding: 0;
}

.veiwed {
  width: 100%;
  float: left;
}


/* new css added */
<div class="product-section">
  <h6>you may also like</h6>
  <div class="veiwed-related">
    <ul>
      <li>One</li>
      <li>Two</li>
      <li>Three</li>
      <li>Four</li>
      <li>Five</li>
    </ul>
  </div>
</div>

如果有 5 个 <li> 我怎样才能做到s,它低于该宽度,但如果我只有 1 或 3,它会覆盖宽度?

最佳答案

我认为在 view-related 中你想这样做:

.veiwed-related ul li{
max-width:16.5%; //adjust the width by using max width so that it do not take unwanted place
float:left;
list-style: none;
margin: 0 0 20px 33px;
padding: 0;
}

max-width 将限制您的宽度,如果元素较少,尺寸将自动减小,因为您指定的是最大值而不是固定值

关于html - 根据元素数量以不同宽度显示列表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45972108/

相关文章:

html - 在大小(宽度和高度)不同的各种窗口上查找点位置的数学公式

jquery - html 和 css - 多级元素显示

javascript - 如果我在文本框中输入一些值,它应该使用 html 和 js 同时显示在另一个文本框中

javascript - setInterval 没有添加 .addClass

html - 为什么 box-shadow 属性在 SharePoint 中无法正常工作?

html - 按钮在所有浏览器中呈现的效果不一样

javascript - getElementById 返回 null(Internet Explorer 中除外)

html - CSS 中左浮动 div 的负右边距有什么用?

如果值已动态更改,则检查元素不透明度的 Jquery 函数将失败

javascript - 当元素进入视口(viewport)时,在离开视口(viewport)之前水平滚动其全部内容