CSS 垂直显示内联 block 而不是水平显示

标签 css

我有一个类似于下面使用显示内嵌 block 的列表项

List of Blocks

有没有一种方法可以让元素列表以这种方式显示。

List of Blocks listed downwards

我已经尝试执行我在这个问题 CSS displaying elements vertically down instead of hortizontal straight 中找到的下面的代码 然而,我的元素只是在 div 之外的列表中进行,而不是按照我想要的列表样式进行

img {
display: block:}

.container > div {
float: left;}

感谢任何帮助

最佳答案

解决方案 A

一种方法是对元素进行分组,以便拥有不同的列。

.group {
  display:inline-block
}
.floatbox {
  border:1px solid red;
  padding:10px;
  box-sizing:border-box;
  margin:10px;
}
<div class="group">
    <div class ="floatbox">
    <h1>
    Floating Box1
    </h1>
    </div>
 <div class ="floatbox">
    <h1>
    Floating Box2
    </h1>
    </div>
</div>
<div class="group">
    <div class ="floatbox">
    <h1>
    Floating Box3
    </h1>
    </div>
 <div class ="floatbox">
    <h1>
    Floating Box4
    </h1>
    </div>
</div>

方案B

使用 column-count 在这里查看更多 > CSS3 Multiple Columns

.floatbox {
  border:1px solid red;
  box-sizing:border-box;

 
}
.container {
  -webkit-column-count: 2; /* Chrome, Safari, Opera */
    -moz-column-count: 2; /* Firefox */
    column-count: 2;
}
<div class="container">
    <div class ="floatbox">
    <h1>
    Floating Box1
    </h1>
    </div>
 <div class ="floatbox">
    <h1>
    Floating Box2
    </h1>
    </div>


    <div class ="floatbox">
    <h1>
    Floating Box3
    </h1>
    </div>
 <div class ="floatbox">
    <h1>
    Floating Box4
    </h1>
    </div>
</div>

方案 C

使用 flexbox 但这有点棘手,因为只有当组合元素的高度大于容器的高度时,它才会产生 2 列(或更多)

在下面的示例中,每个盒子的高度为 100pxcontainer 的高度为 200px 所以这就是为什么只有 2 个盒子适合里面一栏。它不像前两个解决方案那样响应

.floatbox {
  border:1px solid red;
  box-sizing:border-box;
  height:100px;

 
}
.container {
  display: -webkit-flex;
   display: flex;
   -webkit-align-items: center;
   align-items: center;
   -webkit-justify-content: center;
   justify-content: center;
   -webkit-flex-flow: column wrap;
   flex-flow: column wrap;
   -webkit-align-content: stretch;
   align-content: stretch;
   max-height:200px;
}
<div class="container">
    <div class ="floatbox">
    <h1>
    Floating Box1
    </h1>
    </div>
 <div class ="floatbox">
    <h1>
    Floating Box2
    </h1>
    </div>


    <div class ="floatbox">
    <h1>
    Floating Box3
    </h1>
    </div>
 <div class ="floatbox">
    <h1>
    Floating Box4
    </h1>
    </div>
</div>

如果有帮助请告诉我

关于CSS 垂直显示内联 block 而不是水平显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40017542/

相关文章:

html/css 响应式 3 列布局 - 将右列堆叠在左列下方

javascript - 创建心形进度加载器 | react 堆

html - html邮件中的位置相对/绝对问题

php - 浏览器不兼容请使用 Mozilla Firefox 3.2 以上或 IE 8.0 以上

javascript - 没有 "select"标签的 ComboBox 实现

html - 使正文占据可滚动页面的 100%

css - mozilla 中的样式不匹配

Css 类值替换

css - 高级自定义字段如何使用 css 设置我的自定义字段的样式?

asp.net - 在 asp.net 中显示多个状态消息