gpt4 book ai didi

HTML CSS flexbox 图像和文本

转载 作者:太空宇宙 更新时间:2023-11-04 08:07:33 24 4
gpt4 key购买 nike

我使用 flexbox 来显示一个 div,左边是一个 image,右边是一个 text。我希望 image 仅向 x 轴调整大小而不是 y 但保持其比例。对于 text div,我希望它在 x 轴上调整很多后显示在 image 下方。我使用:

<html>
<head>
<style>
.outer {
background-color: gray;
display: flex;
padding: 10px;
flex-wrap: wrap;
}
.img {
max-height: 468px;
max-width: 100%;
margin-right: 10px;
}
.txt {
border: 1px solid yellow;
}
</style>
</head>
<body>

<div class="outer">
<img class="img" src="http://treepicturesonline.com/ebonytreesunset.jpg">
<div class="txt">
In botany, a tree is a perennial plant with an elongated stem, or trunk, supporting branches and leaves in most species. In some usages, the definition of a tree may be narrower, including only woody plants with secondary growth, plants that are usable as lumber or plants above a specified height. Trees are not a taxonomic group but include a variety of plant species that have independently evolved a woody trunk and branches as a way to tower above other plants to compete for sunlight. Trees tend to be long-lived, some reaching several thousand years old. In looser definitions, the taller palms, tree ferns, bananas and bamboos are also trees. Trees have been in existence for 370 million years. It is estimated that there are just over 3 trillion mature trees in the world.[1]
</div>
</div>

</body>
</html>

现在,这部分起作用了。如果我用 Test 之类的小文字替换这面文字墙,那么它会显示在右侧,如果调整大小很多,它会根据需要放在底部但是对于这个它会直接放在底部......然后图像仅向 x 调整大小但不保持其比例...我该如何解决这两个问题?

谢谢

最佳答案

您可以使用媒体查询或像此处一样使用 min-width 来执行此操作。

当与 flex-grow 结合使用时,它们在换行时会占用全宽,而 flex-basis 会在不换行时让它们共享宽度。

为了最好的跨浏览器支持,我还包装了 img

Fiddle demo

堆栈片段

.outer {
background-color: gray;
display: flex;
flex-wrap: wrap;
}
.outer .img {
flex-grow: 1;
flex-basis: calc(50% - 20px);
margin: 10px;
min-width: 300px;
}
.img img {
display: block;
max-width: 100%;
}
.txt {
flex-grow: 1;
flex-basis: calc(50% - 22px); /* 22px = 2*10px margin + 2*1px border */
border: 1px solid yellow;
margin: 10px;
min-width: 300px;
}
<div class="outer">
<div class="img">
<img src="http://treepicturesonline.com/ebonytreesunset.jpg">
</div>
<div class="txt">
In botany, a tree is a perennial plant with an elongated stem, or trunk, supporting branches and leaves in most species. In some usages, the definition of a tree may be narrower, including only woody plants with secondary growth, plants that are usable
as lumber or plants above a specified height. Trees are not a taxonomic group but include a variety of plant species that have independently evolved a woody trunk and branches as a way to tower above other plants to compete for sunlight. Trees tend
to be long-lived, some reaching several thousand years old. In looser definitions, the taller palms, tree ferns, bananas and bamboos are also trees. Trees have been in existence for 370 million years. It is estimated that there are just over 3 trillion
mature trees in the world.[1]
</div>
</div>

关于HTML CSS flexbox 图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46519901/

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