简体   繁体   English

如何在图像上同时使用宽度:100% 和高度:自动

[英]How to use both width: 100% and height: auto on an image

I would like to automatically adjust the height of my image according to the height of my text.我想根据文本的高度自动调整图像的高度。

I tried using height: auto and width: 100% on my image (which seemed to make the most sense to me) but it doesn't work.我尝试在我的图像上使用height: autowidth: 100% (这对我来说似乎最有意义),但它不起作用。

What I have我有的

我实际拥有的架构

CodePen Example: https://codepen.io/JeremyWeb/pen/wvJPboW CodePen 示例: https://codepen.io/JeremyWeb/pen/wvJPboW

 * { padding: 0; margin: 0; }.container { display: flex; align-items: flex-start; border: solid 4px red; padding: 5px; }.text { width: 50%; border: solid 4px black; padding: 5px; }.img-container { width: 50%; height: auto; max-height: 100%; box-sizing: border-box; border: solid 4px black; padding: 5px; }.img { height: auto; width: 100%; object-fit: cover; }
 <div class="container"> <p class="text">Simultaneously we had a problem with prisoner drunkenness that we couldn't figure out. I mean, the guards searched cells multiple times to no avail, that we couldn't figure out.</p> <div class="img-container"> <img class="img" src="https://images.unsplash.com/photo-1621166856483-1725976c4e21?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max"/> </div> </div>

What I want我想要的是

我需要的架构

In HTML/CSS vanilla if possible.如果可能的话,在 HTML/CSS vanilla 中。 Thank you.谢谢你。

I solve your problem with javascript.我用 javascript 解决了你的问题。 You can try my solution-你可以试试我的解决方案——

CSS CSS

.img {
  height: 100%;
  width: 100%;
  object-fit: cover;
}

JS JS

imgDiv = document.getElementsByClassName('img-container')[0];
textHeight = document.getElementsByClassName('text')[0].offsetHeight;
imgDiv.style.height = textHeight + 'px';

Working fiddle - https://jsfiddle.net/os9ht6fe/工作小提琴 - https://jsfiddle.net/os9ht6fe/

One way of doing this is to put the image as a background to an after pseudo element to the text element.这样做的一种方法是将图像作为背景放置到文本元素的伪元素之后。 That way you are sure of getting the same height.这样你就可以确保获得相同的高度。

This snippet uses the CSS calc function to work out the required dimensions and sets the image to cover as requested.此代码段使用 CSS 计算 function 来计算所需的尺寸并根据要求设置要覆盖的图像。

在此处输入图像描述

 * { padding: 0; margin: 0; }.container { --ppx: 5px; /* padding */ --bpx: 4px; /* border */ display: flex; align-items: flex-start; border: solid var(--bpx) red; padding: var(--ppx); }.text { width: 50%; border: solid var(--bpx) black; padding: var(--ppx); position: relative; }.text::after { position: absolute; background-image: url(https://images.unsplash.com/photo-1621166856483-1725976c4e21?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max); background-size: cover; background-repeat: no-repeat no-repeat; background-position: center center; content: ''; top: calc(var(--bpx) * -1); left: calc(100% + var(--bpx) + var(--ppx)); width: calc(100% - (4 * var(--bpx)) - (7 * var(--ppx))); height: calc(100% - (2 * var(--ppx))); border: solid var(--bpx) black; padding: var(--ppx); }
 <div class="container"> <p class="text">Simultaneously we had a problem with prisoner drunkenness that we couldn't figure out. I mean, the guards searched cells multiple times to no avail, that we couldn't figure out.</p> </div>

The image is centered both horizonatlly and vertically.图像水平和垂直居中。 Obviously that can be changed if required.显然,如果需要,可以更改。 And if the padding/border values differ between the main container and the text element then further CSS variables can be defined and used in the calculations.如果主容器和文本元素之间的填充/边框值不同,则可以在计算中定义和使用进一步的 CSS 变量。

I wasn't sure about the padding around the image.我不确定图像周围的填充。 Is that required?这是必需的吗? If so further calculation is required and we can't probably use the convenience of cover.如果需要进一步计算,我们可能无法使用覆盖的便利。 (Or we introduce two pseudo elements to allow them to be sized differently). (或者我们引入两个伪元素以允许它们的大小不同)。 Let me know if required.如果需要,请告诉我。

Based on Haworth's method, another way would be to use a background image on an empty div and using the min-height property.基于 Haworth 的方法,另一种方法是在空 div 上使用背景图像并使用min-height属性。

图片示例

 .container { display: flex; }.text { width: 50%; }.img-container { width: 50%; min-height: 100%; background-image: url("https://images.unsplash.com/photo-1621166856483-1725976c4e21?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max"); background-size: cover; background-position: center; }
 <div class="container"> <p class="text">Simultaneously we had a problem with prisoner drunkenness that we couldn't figure out. I mean, the guards searched cells multiple times to no avail, that we couldn't figure out.</p> <div class="img-container"></div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM