我正在尝试显示具有以下结构的媒体库的元素:



.mail-attach-list {
    list-style: none;
    padding: 0;
}
.mail-attach-list>li {
    display: inline-block;
    width: 190px;
}
.thumbnail {
    margin-bottom: 2px;
    border-radius: 3px;
    display: block;
    padding: 4px;
    border-color: rgba(0, 0, 0, 0.09);
    background-color: #fff;
    text-decoration: none;
}
.mail-attach-list>li .mail-file-img, .mail-attach-list>li .mail-file-icon {
    height: 120px;
    overflow: hidden;
    text-align: center;
    background-color: #f8f9fa;
}
.thumbnail .caption {
    padding: 9px;
    color: #333;
}

<ul class="mail-attach-list list-ov">
    <li class="item">
        <a href="#" class="thumbnail">
            <div class="mail-file-icon">
                <i class="pli-file-excel"></i>
            </div>
            <div class="caption">
                <p class="text-primary mar-no text-overflow">Icon Example</p>
                <small class="text-muted text-overflow">
                    Added: <time data-toggle="timeago">4 days ago</time>
                </small>
            </div>
        </a>
    </li>
    <li class="item">
        <a href="#" class="thumbnail">
            <div class="mail-file-img">
                <img class="image-responsive" src="/uploads/2017/09/bg-img-7-400x300.jpg" alt="image">
            </div>
            <div class="caption">
                <p class="text-primary mar-no text-overflow">Image example</p>
                <small class="text-muted text-overflow">
                    Added: <time data-toggle="timeago">4 days ago</time>
                </small>
            </div>
        </a>
    </li>
</ul>






  我为此解释删除了一些标签属性


这是预览:jquery - 动态更改&lt;li&gt;宽度以完全适合&lt;ul&gt;-LMLPHP


  所以我想删除右边的空白,我使用的是像素单位
  在li元素中,因为我无法使其工作。


如何动态更改li元素的宽度以完全适合ul父级?

最佳答案

您要做的就是在width上使用基于百分比的.mail-attached-list>li。您需要使用的确切值取决于要在单行中显示的项目数。

除此之外,您还可以在text-align-last: center上使用.mail-attach-list,这将使<li>元素的集合居中:



.mail-attach-list {
  list-style: none;
  padding: 0;
  text-align-last: center; /* Center the elements */
}

.mail-attach-list>li {
  display: inline-block;
  width: 49%; /* Dependant on the number of items per row */
}

.thumbnail {
  margin-bottom: 2px;
  border-radius: 3px;
  display: block;
  padding: 4px;
  border-color: rgba(0, 0, 0, 0.09);
  background-color: #fff;
  text-decoration: none;
}

.mail-attach-list>li .mail-file-img,
.mail-attach-list>li .mail-file-icon {
  height: 120px;
  overflow: hidden;
  text-align: center;
  background-color: #f8f9fa;
}

.thumbnail .caption {
  padding: 9px;
  color: #333;
}

<ul class="mail-attach-list list-ov">
  <li class="item">
    <a href="#" class="thumbnail">
      <div class="mail-file-icon">
        <i class="pli-file-excel"></i>
      </div>
      <div class="caption">
        <p class="text-primary mar-no text-overflow">Icon Example</p>
        <small class="text-muted text-overflow">
                    Added: <time data-toggle="timeago">4 days ago</time>
                </small>
      </div>
    </a>
  </li>
  <li class="item">
    <a href="#" class="thumbnail">
      <div class="mail-file-img">
        <img class="image-responsive" src="/uploads/2017/09/bg-img-7-400x300.jpg" alt="image">
      </div>
      <div class="caption">
        <p class="text-primary mar-no text-overflow">Image example</p>
        <small class="text-muted text-overflow">
                    Added: <time data-toggle="timeago">4 days ago</time>
                </small>
      </div>
    </a>
  </li>
</ul>





希望这可以帮助! :)

10-06 11:51