无法垂直对齐列表中的 div
Trouble vertically aligning divs in list
我对 css 比较陌生,抱歉,如果这真的很简单。我一直在寻找一个多小时的解决方案,但似乎无法弄清楚。我想垂直对齐时间线面板和时间线图像。请注意,h4 可以有多行,从而改变 timeline-panel 的高度。
谢谢
编辑
我想我没有正确解释自己。这是我正在构建的页面。你可以在关于部分看到它。 http://belyza.com/test437/
我现在为文本添加了上边距,但是当文本在较小的屏幕上变成多行时,这不适用于响应式主题。
.timeline {
list-style: none;
padding: 0;
position: relative
}
.timeline>li {
margin-bottom: 10px;
position: relative;
min-height: 50px
}
.timeline>li .timeline-panel {
position: relative;
text-align: center;
}
.timeline>li .timeline-image {
left: 50%;
margin-left: 15px;
width: 50px;
height: 50px;
position: absolute;
z-index: 100;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center
}
.timeline>li .timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px
}
.timeline .timeline-heading h4 {
margin-top: 0;
color: inherit
}
.timeline .timeline-heading h4.subheading {
text-transform: none
}
.timeline .timeline-body>p,
.timeline .timeline-body>ul {
margin-bottom: 0
}
<ul class="timeline">
<li>
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
</div>
</li>
</ul>
不能 100% 确定 objective 的确切结尾是什么样子。我假设您想要在某些文本上方画一个圆圈。
由于您为 .timeline-image
设置了宽度,因此您可以继续使用 auto
作为左右边距,一个版本是 margin: 0 auto;
。然后我从 .timeline-image
.
中删除了 position: absolute;
、z-index: 100;
和 left: 50%;
如果您不熟悉它的工作原理,使用绝对定位可能会做一些奇怪的事情。
.timeline {
list-style: none;
padding: 0;
}
.timeline > li {
margin-bottom: 10px;
}
.timeline-panel {
position: relative;
text-align: center;
}
.timeline-image {
margin: 0 auto;
width: 50px;
height: 50px;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center
}
.timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px margin-top: 0;
color: inherit
}
<ul class="timeline">
<li>
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
</div>
</li>
</ul>
如果您正在寻找圆圈左侧的单行文本,那么您需要稍微重新安排您的标记。将 .timeline-image
放在 .timeline-heading
之后。将两者都设置为 display: inline-block; vertical-align: middle;
,并将 .timeline-heading
设置为 line-height: 60px;
(.timeline-image
的总高度)。
.timeline {
list-style: none;
padding: 0;
}
.timeline > li {
margin-bottom: 10px;
}
.timeline-panel {
position: relative;
text-align: center;
}
.timeline-heading {
display: inline-block;
line-height: 60px;
vertical-align: middle;
}
.timeline-image {
display: inline-block;
width: 50px;
height: 50px;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center;
vertical-align: middle;
}
.timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px margin: 0;
color: inherit
}
<ul class="timeline">
<li>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
<div class="timeline-image"></div>
</div>
</li>
</ul>
如果你想使用 flexbox,你可以这样设置:
编辑
好的,现在我已经清楚地看到您想要做什么,我已经编辑了代码,我想这就是您要找的。
.timeline {
list-style: none;
padding: 0;
}
.timeline>li {
align-items: center;
display: flex;
justify-content: center;
margin-bottom: 10px;
min-height: 50px
}
.timeline-panel-left:after,
.timeline-panel-right:before {
content: "";
flex: 1 calc(50% - 80px);
max-width: calc(50% - 80px);
}
.timeline-panel-left {
text-align: right;
}
.timeline>li .timeline-panel {
flex: 1 calc(50% - 80px);
max-width: calc(50% - 80px);
}
.timeline>li .timeline-image {
margin-left: 15px;
margin-right: 15px;
width: 50px;
height: 50px;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
}
.timeline .timeline-heading h4 {
margin: 0;
color: inherit
}
<ul class="timeline">
<li class="timeline-panel-left">
<div class="timeline-panel">
<div class="timeline-heading">
<h4>SERVICE DE RÉPARATION</h4>
</div>
</div>
<div class="timeline-image">
</div>
</li>
<li class="timeline-panel-right">
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>DIAGNOSTIQUE</h4>
</div>
</div>
</li>
</ul>
添加了 display:inline-block
和 vertical-align:middle
。注释掉不需要的属性。 Display:flex
不支持低版本IE
.timeline {
list-style: none;
padding: 0;
position: relative
}
.timeline>li {
margin-bottom: 10px;
min-height: 50px;
text-align: center;
}
.timeline>li .timeline-panel {
position: relative;
text-align: center;
display: inline-block;
vertical-align: initial;
}
.timeline>li .timeline-image {
/* left: 50%; */
/* margin-left: 15px; */
width: 50px;
height: 50px;
/* position: absolute; */
/* z-index: 100; */
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center;
display: inline-block;
vertical-align: middle;
}
.timeline>li .timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px
}
.timeline .timeline-heading h4 {
margin-top: 0;
color: inherit
}
.timeline .timeline-heading h4.subheading {
text-transform: none
}
.timeline .timeline-body>p,
.timeline .timeline-body>ul {
margin-bottom: 0
}
<ul class="timeline">
<li>
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
</div>
</li>
</ul>
我对 css 比较陌生,抱歉,如果这真的很简单。我一直在寻找一个多小时的解决方案,但似乎无法弄清楚。我想垂直对齐时间线面板和时间线图像。请注意,h4 可以有多行,从而改变 timeline-panel 的高度。
谢谢 编辑
我想我没有正确解释自己。这是我正在构建的页面。你可以在关于部分看到它。 http://belyza.com/test437/ 我现在为文本添加了上边距,但是当文本在较小的屏幕上变成多行时,这不适用于响应式主题。
.timeline {
list-style: none;
padding: 0;
position: relative
}
.timeline>li {
margin-bottom: 10px;
position: relative;
min-height: 50px
}
.timeline>li .timeline-panel {
position: relative;
text-align: center;
}
.timeline>li .timeline-image {
left: 50%;
margin-left: 15px;
width: 50px;
height: 50px;
position: absolute;
z-index: 100;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center
}
.timeline>li .timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px
}
.timeline .timeline-heading h4 {
margin-top: 0;
color: inherit
}
.timeline .timeline-heading h4.subheading {
text-transform: none
}
.timeline .timeline-body>p,
.timeline .timeline-body>ul {
margin-bottom: 0
}
<ul class="timeline">
<li>
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
</div>
</li>
</ul>
不能 100% 确定 objective 的确切结尾是什么样子。我假设您想要在某些文本上方画一个圆圈。
由于您为 .timeline-image
设置了宽度,因此您可以继续使用 auto
作为左右边距,一个版本是 margin: 0 auto;
。然后我从 .timeline-image
.
position: absolute;
、z-index: 100;
和 left: 50%;
如果您不熟悉它的工作原理,使用绝对定位可能会做一些奇怪的事情。
.timeline {
list-style: none;
padding: 0;
}
.timeline > li {
margin-bottom: 10px;
}
.timeline-panel {
position: relative;
text-align: center;
}
.timeline-image {
margin: 0 auto;
width: 50px;
height: 50px;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center
}
.timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px margin-top: 0;
color: inherit
}
<ul class="timeline">
<li>
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
</div>
</li>
</ul>
如果您正在寻找圆圈左侧的单行文本,那么您需要稍微重新安排您的标记。将 .timeline-image
放在 .timeline-heading
之后。将两者都设置为 display: inline-block; vertical-align: middle;
,并将 .timeline-heading
设置为 line-height: 60px;
(.timeline-image
的总高度)。
.timeline {
list-style: none;
padding: 0;
}
.timeline > li {
margin-bottom: 10px;
}
.timeline-panel {
position: relative;
text-align: center;
}
.timeline-heading {
display: inline-block;
line-height: 60px;
vertical-align: middle;
}
.timeline-image {
display: inline-block;
width: 50px;
height: 50px;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center;
vertical-align: middle;
}
.timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px margin: 0;
color: inherit
}
<ul class="timeline">
<li>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
<div class="timeline-image"></div>
</div>
</li>
</ul>
如果你想使用 flexbox,你可以这样设置:
编辑
好的,现在我已经清楚地看到您想要做什么,我已经编辑了代码,我想这就是您要找的。
.timeline {
list-style: none;
padding: 0;
}
.timeline>li {
align-items: center;
display: flex;
justify-content: center;
margin-bottom: 10px;
min-height: 50px
}
.timeline-panel-left:after,
.timeline-panel-right:before {
content: "";
flex: 1 calc(50% - 80px);
max-width: calc(50% - 80px);
}
.timeline-panel-left {
text-align: right;
}
.timeline>li .timeline-panel {
flex: 1 calc(50% - 80px);
max-width: calc(50% - 80px);
}
.timeline>li .timeline-image {
margin-left: 15px;
margin-right: 15px;
width: 50px;
height: 50px;
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
}
.timeline .timeline-heading h4 {
margin: 0;
color: inherit
}
<ul class="timeline">
<li class="timeline-panel-left">
<div class="timeline-panel">
<div class="timeline-heading">
<h4>SERVICE DE RÉPARATION</h4>
</div>
</div>
<div class="timeline-image">
</div>
</li>
<li class="timeline-panel-right">
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>DIAGNOSTIQUE</h4>
</div>
</div>
</li>
</ul>
添加了 display:inline-block
和 vertical-align:middle
。注释掉不需要的属性。 Display:flex
不支持低版本IE
.timeline {
list-style: none;
padding: 0;
position: relative
}
.timeline>li {
margin-bottom: 10px;
min-height: 50px;
text-align: center;
}
.timeline>li .timeline-panel {
position: relative;
text-align: center;
display: inline-block;
vertical-align: initial;
}
.timeline>li .timeline-image {
/* left: 50%; */
/* margin-left: 15px; */
width: 50px;
height: 50px;
/* position: absolute; */
/* z-index: 100; */
background-color: #E80B10;
color: #fff;
border-radius: 100%;
border: 5px solid #ff9600;
text-align: center;
display: inline-block;
vertical-align: middle;
}
.timeline>li .timeline-image h4 {
font-size: 10px;
margin-top: 12px;
line-height: 14px
}
.timeline .timeline-heading h4 {
margin-top: 0;
color: inherit
}
.timeline .timeline-heading h4.subheading {
text-transform: none
}
.timeline .timeline-body>p,
.timeline .timeline-body>ul {
margin-bottom: 0
}
<ul class="timeline">
<li>
<div class="timeline-image">
</div>
<div class="timeline-panel">
<div class="timeline-heading">
<h4>test</h4>
</div>
</div>
</li>
</ul>