英文:
Image should shrink but not grow above it's natural size
问题
我想要图片缩小,但不要超出其自然大小。这里提供的解决方案有效:https://stackoverflow.com/questions/3463664/make-an-image-width-100-of-parent-div-but-not-bigger-than-its-own-width 但是,在不同上下文中使用时,它仍然会超出其自然大小。
section {
display: flex;
}
figure {
margin: 0;
display: flex;
flex-direction: column;
flex: 1;
}
figure img {
/* 这个图片应该以响应方式缩小,但不要超出其自然大小 */
max-width: 100%;
}
section p {
flex: 1;
}
<section>
<figure>
<h4>标题</h4>
<img src="images/the-img.jpg">
<figcaption>
图片说明。
</figcaption>
</figure>
<p>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</p>
</section>
有没有一种通过 CSS 模拟增长的方法?通过 JavaScript 来实现这一点不会有问题。
英文:
I want an image to shrink but not grow above it's natural size. The solution given here is working fine:
https://stackoverflow.com/questions/3463664/make-an-image-width-100-of-parent-div-but-not-bigger-than-its-own-width
However when using it in a different context it's still growing above it's natural size.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
section {
display: flex;
}
figure {
margin: 0;
display: flex;
flex-direction: column;
flex: 1;
}
figure img {
/* This image should shrink in a responsive way
but not grow above it's natural size */
max-width: 100%;
}
section p {
flex: 1;
}
<!-- language: lang-html -->
<section>
<figure>
<h4>The headline</h4>
<img src="images/the-img.jpg">
<figcaption>
The caption.
</figcaption>
</figure>
<p>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</p>
</section>
<!-- end snippet -->
Is there a way to pretend growing by CSS? Achieving this by Javascript would be no problem.
答案1
得分: 2
只需将您的图像包装在一个div中,max-width: 100%
将基于该div而不是flex容器。
<section>
<figure>
<h4>标题</h4>
<div><!-- 添加这部分 -->
<img src="https://picsum.photos/id/237/300/300">
</div><!-- 添加这部分 -->
<figcaption>
图片说明。
</figcaption>
</figure>
<p>Lorem ipsum dolor sit amet,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,sed diam voluptua。At vero eos et accusam et justo duo dolores et ea rebum。Stet clita kasd gubergren,no sea takimata sanctus est Lorem ipsum dolor sit amet。Lorem ipsum dolor sit amet,consetetur sadipscing elitr,sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,sed diam voluptua。At vero eos et accusam et justo duo dolores et ea rebum。Stet clita kasd gubergren,no sea takimata sanctus est Lorem ipsum dolor sit amet。</p>
</section>
英文:
Just wrap your image in a div and the max-width:100%
will be based on that rather than the flex container.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
section {
display: flex;
}
figure {
margin: 0;
display: flex;
flex-direction: column;
flex: 1;
}
figure img {
max-width: 100%;
}
section p {
flex: 1;
}
<!-- language: lang-html -->
<section>
<figure>
<h4>The headline</h4>
<div><!-- added this -->
<img src="https://picsum.photos/id/237/300/300">
</div><!-- added this -->
<figcaption>
The caption.
</figcaption>
</figure>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
</section>
<!-- end snippet -->
答案2
得分: 2
Solution 1: 使用max-width
和align-self
属性的组合可以实现此目标。只需修改img元素的CSS如下:
<code>
figure img {
width: auto;
height: auto;
max-width: 100%;
align-self: flex-start;
}
</code>
在img选择器中,我们将width
和height
属性设置为auto
,以保持图像的纵横比。max-width
属性限制了图像在水平方向上不会超过其自然大小。通过设置align-self: flex-start
,我们将图像对齐到容器的顶部,允许它在可用空间内垂直伸展。
Solution 2: 也可以通过在figure
的CSS上设置额外的align-items
属性为flex-start
、center
或flex-end
来实现相同的效果,这也会防止内部的img元素在其自然宽度之外被拉伸。
<code>
figure {
margin: 0;
display: flex;
flex-direction: column;
flex: 1;
align-items: flex-start;
}
</code>
英文:
You want an image to shrink but not grow above its natural size. you can achieve this by
Solution 1: using a combination of max-widt
h and align-self
properties.
just modify the css of img element as:
<pre>
figure img {
width: auto;
height: auto;
max-width: 100%;
align-self: flex-start;
}
</pre>
In the figure img selector, we set the width
and height
properties to auto
to maintain the aspect ratio of the image. The max-width
property restricts the image from growing horizontally beyond its natural size. By setting align-self: flex-start
, we align the image to the top of the container, allowing it to stretch vertically within the available space
Solution 2: the same can be achieved by setting an additional align-items
property to flex-start
, center
or flex-end
on the figure
css. that will also prevent the stretching of inner img element beyond its natural width.
<pre>
figure {
margin: 0;
display: flex;
flex-direction: column;
flex: 1;
align-items: flex-start;
}
</pre>
答案3
得分: 0
我稍微调整了一下,最终去掉了间隙:将容器的 flex-basis 设置为右侧的 50%,并允许它增长。
section {
display: flex;
}
figure {
margin: 0;
display: flex;
flex-direction: column;
flex-basis: auto;
flex-grow: 0;
flex-shrink: 1;
}
figure img {
/* 此图像应以响应方式收缩,但不得增长超出其自然大小 */
max-width: 100%;
}
section p {
flex-basis: 50%;
flex-grow: 1;
flex-shrink: 0;
}
HTML 由 @Adam 发布。
英文:
I fiddled a bit more and in the end got rid of the gap: Set flex-basis of the container at the right to 50% and allow it to grow.
section {
display: flex;
}
figure {
margin: 0;
display: flex;
flex-direction: column;
flex-basis: auto;
flex-grow: 0;
flex-shrink: 1;
}
figure img {
/* This image should shrink in a responsive way
but not grow above it's natural size */
max-width: 100%;
}
section p {
flex-basis: 50%;
flex-grow: 1;
flex-shrink: 0;
}
HTML as posted by @Adam
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论