gpt4 book ai didi

css - 如果不向 HTML 结构添加更多内容,是否可以实现这种 flexbox 布局?

转载 作者:行者123 更新时间:2023-11-28 16:05:52 26 4
gpt4 key购买 nike

我想知道我在下图上绘制的布局是否可以使用 flexbox 和这种 HTML 结构:

<div class="box">
<img src="url..." alt="" />
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>

如果这可以用 flexbox 完成并且不在里面添加更多盒子,那就太好了。

Flexbox Layout

最佳答案

对于固定的图像宽度和高度,这是可能的。主要思想在以下片段中。

.box {
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-content: space-between;
height: 300px;
}

.img {
width: 300px; height: 300px;
}

h5, p {
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}

由于父级的高度与图像的高度相同,因此内容会溢出并向右包裹。然后我们必须手动设置宽度,否则它将达到父级的 100%。

.box {
background-color: #ddd;
padding: 16px;
margin-bottom: 24px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
align-content: space-between;
}

.img {
background-color: #333;
color: #ddd;
width: 300px;
height: 300px;
}

h5, p {
padding: 0;
margin: 0;
background-color: #ccc;
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}

h5 {
font-size: 18px;
margin-bottom: 8px;
}

.box:nth-child(even) .img {
order: 3;
}
<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus</p>
</div>

<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>

<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a </p>
</div>

全高段落

您可能希望将您的段落一直延伸到底部(可以在那里添加一些链接)。扩展它很容易,因此您可以将 p 变成 flexbox 以将某些东西捕捉到底部,例如(这个例子不在下面的演示中,您只能通过灰色背景注意到这一点)。

.box {
/* ... */
justify-content: space-between;
}

p {
flex-grow: 1;
}

.box {
background-color: #ddd;
padding: 16px;
margin-bottom: 24px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
align-content: space-between;
justify-content: space-between;
}

.img {
background-color: #333;
color: #ddd;
width: 300px;
height: 300px;
}

h5, p {
padding: 0;
margin: 0;
background-color: #ccc;
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}

h5 {
font-size: 18px;
margin-bottom: 8px;
}

p {
flex-grow: 1;
}

.box:nth-child(even) .img {
order: 3;
}
<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Version 2</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus</p>
</div>

<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>

<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. </p>
</div>

两个版本在文本无法容纳时中断(尝试更改浏览器窗口大小)。

不显示全部,但从不中断

如果您不想让overflow: hidden 不适合段落,您可以设置段落的max-height

p {
max-height: 250px;
overflow: hidden;
}

.box {
background-color: #ddd;
padding: 16px;
margin-bottom: 24px;
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 300px;
align-content: space-between;
justify-content: space-between;
}

.img {
background-color: #333;
color: #ddd;
width: 300px;
height: 300px;
}

h5, p {
padding: 0;
margin: 0;
background-color: #ccc;
/* 100% - image width - margin between */
width: calc(100% - 300px - 16px);
}

h5 {
font-size: 18px;
margin-bottom: 8px;
}

p {
flex-grow: 1;
}

.box:nth-child(even) .img {
order: 3;
}

p {
max-height: 250px;
overflow: hidden;
}
<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Version 3</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus</p>
</div>

<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Lorem Ipsum</h5>
<p>Lorem Ipsum Dolor Sit Amet...</p>
</div>

<div class="box">
<div class="img">&lt;img&gt;</div>
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur mollis cursus hendrerit. Suspendisse potenti. Aliquam posuere ex ut lacus euismod dictum. Proin et ligula posuere leo viverra tempor a in tellus. </p>
</div>

关于css - 如果不向 HTML 结构添加更多内容,是否可以实现这种 flexbox 布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39269920/

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