html - 如何在引导模式中有响应列表?

标签 html css flexbox

我想使用 Bootstrap 创建具有以下结构的响应式模式。

enter image description here

所以每个列表项都有相同的空间和文本上方的图像,所有元素垂直和水平对齐。我这里有一个示例:“https://jsfiddle.net/dq0ygh44/ ”,但它无法正常工作,因为图像和文本并非全部水平和垂直居中。你知道怎么做吗?

HTML:

<a  data-toggle="modal" data-target=".bd-example-modal-lg" href="">Open</a>
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="container">

        <ul class="categories-list">

          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">All Options</a>
          </li>
           <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option1</a>
          </li>
          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option2</a>
          </li>
          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option Option</a>
          </li>
           <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option</a>
          </li>
          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option</a>
          </li>
          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option</a>
          </li>
          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option</a>
          </li>
          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option</a>
          </li>
          <li>
            <img src="http://via.placeholder.com/30x40"/>
            <a class="">Option</a>
          </li>
        </ul>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>

CSS:

.categories-list{

  display: flex;
  flex-wrap:wrap;
  max-height:50px;
  overflow-y:auto;
  justify-content: space-between;
  li{
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    padding: 1rem;
    border: 10px solid gray;
    margin-right: 1rem;
    border-radius: 5px;

    &:hover{
      background-color: $blue;
      color: $white;
      border-radius: 5px;
      cursor: pointer;

    }

    img{
      width: 30px;
      height: 30px;
    }
  }

}

最佳答案

我能够复制你的图像,我将 SASS 转换为 CSS 并添加了一个 min-width 这样所有的元素都会具有相同的宽度。还向 ul 标记添加了一个 clearfix,以便它占用子项的宽度。

JSFiddle Demo

CSS:

.clearfix:after {
  visibility: hidden;
  display: block;
  font-size: 0;
  content: " ";
  clear: both;
  height: 0;
}

* html .clearfix {
  zoom: 1;
}

/* IE6 */
*:first-child + html .clearfix {
  zoom: 1;
}

/* IE7 */
.categories-list {
  display: flex;
  flex-wrap: wrap;
  max-height: 50px;
  overflow-y: auto;
  justify-content: space-between;
}
.categories-list li {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 1rem;
  border: 10px solid gray;
  margin-right: 1rem;
  border-radius: 5px;
  min-width:150px;
  min-height:100px;
  margin:10px;
}
.categories-list li:hover {
  background-color: blue;
  color: white;
  border-radius: 5px;
  cursor: pointer;
}
.categories-list li img {
  width: 30px;
  height: 30px;
}

关于html - 如何在引导模式中有响应列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46220813/

相关文章:

css - 使用 flexbox 垂直居中元素

html - 使用 flexbox 时,文本不会在 IE 10 中换行

javascript - 如何将 5000+ JS 文件合并为 HTML head 部分的单个 JS 文件?

javascript - 为什么我收到错误 "Must provide query string."express-graphql?

css - 列表高度 100% 在 Safari 中不起作用

javascript - Highcharts:如何突出显示 yAxis 值?

html - CSS - 模糊所有的 child ,除了悬停的 child ,当鼠标悬停在 child 身上时

html - 为什么两个div之间有空隙并且没有重叠

c# - 在 .net 代码中在运行时编辑 HTML 电子邮件模板

jquery - 仅定位选择器中具有子菜单的链接