图像应该缩小,但不要超过其自然大小。

huangapple go评论65阅读模式
英文:

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&#39;s natural size */
  max-width: 100%;
}

section p {
  flex: 1;
}

<!-- language: lang-html -->

&lt;section&gt;
  &lt;figure&gt;
    &lt;h4&gt;The headline&lt;/h4&gt;
    &lt;img src=&quot;images/the-img.jpg&quot;&gt;
    &lt;figcaption&gt;
      The caption.
    &lt;/figcaption&gt;
  &lt;/figure&gt;

  &lt;p&gt;
    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.
  &lt;/p&gt;
&lt;/section&gt;

<!-- 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 -->

&lt;section&gt;
  &lt;figure&gt;
    &lt;h4&gt;The headline&lt;/h4&gt;
    &lt;div&gt;&lt;!-- added this --&gt;
      &lt;img src=&quot;https://picsum.photos/id/237/300/300&quot;&gt;
    &lt;/div&gt;&lt;!-- added this --&gt;
    &lt;figcaption&gt;
      The caption.
    &lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;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.&lt;/p&gt;
&lt;/section&gt;

<!-- end snippet -->

答案2

得分: 2

Solution 1: 使用max-widthalign-self属性的组合可以实现此目标。只需修改img元素的CSS如下:

<code>
    figure img {
      width: auto;
      height: auto;
      max-width: 100%;
      align-self: flex-start; 
    }
</code>

img选择器中,我们将widthheight属性设置为auto,以保持图像的纵横比。max-width属性限制了图像在水平方向上不会超过其自然大小。通过设置align-self: flex-start,我们将图像对齐到容器的顶部,允许它在可用空间内垂直伸展。

Solution 2: 也可以通过在figure的CSS上设置额外的align-items属性为flex-startcenterflex-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-width 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&#39;s natural size */
            max-width: 100%;
        }

        section p {
            flex-basis: 50%;
            flex-grow: 1;
            flex-shrink: 0;
        }

HTML as posted by @Adam

huangapple
  • 本文由 发表于 2023年6月5日 17:56:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405270.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定