我正在创建Flexbox卡以显示三个元素。标题,<hr>
和段落。标题文本的长度各不相同,因此高度也有所不同。
我希望所有标题都具有相同的高度,以便每张卡都是统一的。如果无法做到这一点,我希望在每张卡上将段落水平对齐。
我尝试为标题提供flex: 1
属性以将段落下推,但它会占用过多空间。
这里是codepen和演示:
body {
max-width: 1180px;
margin: 0 auto;
}
h2,
p {
margin: 0;
padding: 0;
}
h2 {
font-size: 18px;
}
/*******************************************************/
.cards {
display: flex;
}
.card {
border: 1px solid;
display: flex;
flex-direction: column;
margin: 0 0 25px 0;
width: 50%;
}
.card-title {
text-transform: uppercase;
text-align: center;
margin: 0 0 25px 0;
}
.card-paragraph {
text-align: center;
}
hr {
height: 3px;
width: 100%;
background: #333;
}
/*******************************************************/
<body>
<div class="cards">
<div class="card">
<div class="card-title">
Title one IS LONGER THAN TITLE TWO
</div>
<hr>
<div class="card-paragraph">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text.
</div>
</div>
<div class="card">
<div class="card-title">
TITLE TWO
</div>
<hr>
<div class="card-paragraph">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
</div>
</body>
我想要的:
正在发生的事情:
答案 0 :(得分:2)
我建议使用CSS Grid这样的东西:
body {
max-width: 1180px;
margin: 0 auto;
}
h2,
p {
margin: 0;
padding: 0;
}
h2 {
font-size: 18px;
}
/*******************************************************/
.cards {
display: grid;
grid-auto-columns: auto;
grid-auto-rows: auto;
}
.card {
border: 1px solid;
grid-row: 1;
display: grid;
grid-auto-rows: 1fr;
}
.card-title {
text-transform: uppercase;
text-align: center;
margin: 0 0 25px 0;
}
.card-paragraph {
text-align: center;
}
hr {
height: 3px;
width: 100%;
background: #333;
}
/*******************************************************/
<body>
<div class="cards">
<div class="card">
<div class="card-title">
Title one IS LONGER THAN TITLE TWO Title one IS LONGER THAN TITLE TWO Title one IS LONGER THAN TITLE TWO
</div>
<hr>
<div class="card-paragraph">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text.
</div>
</div>
<div class="card">
<div class="card-title">
TITLE TWO
</div>
<hr>
<div class="card-paragraph">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
</div>
</body>
答案 1 :(得分:2)
可能无法对齐相邻的flexbox 容器-CSS网格可能是一个不错的选择:
在display: contents
上使用card
或完全移除包装器
使用2列网格,并使用card-title
grid-row: 1
置于第一行
使用card-paragraph
将grid-row: 3
放置到第三行,并使用hr
将grid-row: 2
放置在中间。
请参见下面的演示
body {
max-width: 1180px;
margin: 0 auto;
}
h2,
p {
margin: 0;
padding: 0;
}
h2 {
font-size: 18px;
}
.cards {
/* display: flex; */
display: grid;
grid-template-columns: 1fr 1fr;
}
.card {
/* border: 1px solid;
display: flex;
flex-direction: column;
margin: 0 0 25px 0;
width: 50%;*/
display: contents; /* added */
}
.card-title {
text-transform: uppercase;
text-align: center;
/* margin: 0 0 25px 0;*/
grid-row: 1; /* added */
border: 1px solid; /* added */
}
.card-paragraph {
text-align: center;
grid-row: 3; /* added */
border: 1px solid; /* added */
}
hr {
height: 3px;
/* width: 100%; */
background: #333;
margin: 0; /* added */
grid-row: 2; /* added */
}
<div class="cards">
<div class="card">
<div class="card-title">
Title one IS LONGER THAN TITLE TWO
</div>
<hr>
<div class="card-paragraph">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum is simply dummy text.
</div>
</div>
<div class="card">
<div class="card-title">
TITLE TWO
</div>
<hr>
<div class="card-paragraph">
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</div>
</div>
</div>
答案 2 :(得分:1)
只有通过设置标题的高度才能使用flexbox:
.card-title {
...
height: 50px;
}
Codepen