gpt4 book ai didi

html - 以可延展的列数居中

转载 作者:太空宇宙 更新时间:2023-11-04 06:52:26 25 4
gpt4 key购买 nike

我正在尝试列出一系列相同大小的元素。我希望这些元素在左右(垂直居中?)上以均匀的间距显示,并且彼此之间的间距均匀。最大的问题是列表需要能够适应屏幕大小的变化和元素数量的变化。因此,每行的宽度和元素需要根据需要更新。最下面的行也应该与上面的行对齐。

这是迄今为止我能得到的最接近的。

HTML

<div class="outer">
<div class="inner">
</div>
</div>
... repeated as any times as there are blocks.
<div class="outer">
<div class="inner">
</div>
</div>

CSS

body {
text-align: justify;
margin:0;
width: auto;
}

.outer {
background:blue;
width: 100px;
height: 90px;
display: inline-block;
text-align: center;
}

.inner {
background:red;
width: 90px;
height: 90px;
display: inline-block;
text-align: center;
}

JSFiddle

最佳答案

听起来像是 flexbox 的工作。其中一个对你有用吗? https://codepen.io/anon/pen/VEpbjv

HTML

  <ul class="flex-container space-between">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>

<ul class="flex-container space-around">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>

<ul class="flex-container space-evenly">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>

CSS

.flex-container {
padding: 0;
margin: 0;
list-style: none;
display: flex;
}

.flex-start {
justify-content: flex-start;
}

.flex-end {
justify-content: flex-end;
}
.flex-end li {
background: gold;
}

.center {
justify-content: center;
}
.center li {
background: deepskyblue;
}

.space-between {
justify-content: space-between;
}
.space-between li {
background: lightgreen;
}

.space-around {
justify-content: space-around;
}
.space-around li {
background: hotpink;
}

.space-evenly {
justify-content: space-evenly;
}
.space-evenly li {
background: #bada55;
}

.flex-item {
background: tomato;
padding: 5px;
width: 60px;
height: 50px;
margin: 5px;
line-height: 50px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}

关于html - 以可延展的列数居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52729567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com