使图像填满整个屏幕而无需边界框?

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

Make Image Fill Entire Screen Without Bounding Box?

问题

我目前正在使用以下代码来使图像填充整个屏幕,同时保持其宽高比。

div {
  position: fixed;
  top: 0;
  width: 100%;
  height: 100%;
}

img {
  object-fit: contain;
  width: 100%;
  height: 100%;
  border: thin solid black;
}

这段代码的作用是调整比屏幕小或大的图像大小,以填充整个屏幕,同时保持其宽高比。例如,在横向方向上使用显示器(宽度 > 高度)时,具有纵向比例的图像(高度 > 宽度)将与屏幕一样高,但左右会留有一些空间。我的示例应该能说明这一点。

在这些情况下,我想能够检测用户是否点击图像或图像外的区域。然而,使用这种方法,图像的边界框会填充整个div和整个屏幕,即使对用户来说看起来不同。

我的意思是:div被设置为占据其容器的100%宽度和高度。由于其位置被设置为fixed,它的大小将与屏幕相同。为了使object-fit属性在图像上起作用,我需要为图像分配宽度和高度。当我将这些值设置为100%时,图像的所谓边界框将填充整个父元素/屏幕,然后图像将占据尽可能多的空间,同时保持其宽高比(参见此答案)。这意味着图像可能看起来只具有与其父元素相同的高度,而不具有与其父元素相同的宽度,但实际上是一样的。您可以使用浏览器的开发工具来验证这一点,我还添加了一个边框以可视化它。这意味着,我无法将事件监听器附加到图像和div上,以区分点击图像和空白区域,因为图像被视为填充整个区域。

如何解决这个问题的最佳方法是什么?是否有仅使用CSS的方法?我唯一能想到的是使用JS来调整图像的大小,使其适应屏幕,同时保持其宽高比,并且在每次调整大小时都要这样做。这似乎对于如此简单的事情来说是很多工作。

英文:

I am currently using the following code to make images fill the entire screen while keeping their aspect ratio.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

div {
  position: fixed;
  top: 0;
  width: 100%;
  height: 100%;
}

img {
  object-fit: contain;
  width: 100%;
  height: 100%;
  border: thin solid black;
}

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

&lt;div&gt;
  &lt;img src=&quot;https://placekitten.com/200/300&quot; alt=&quot;&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

What this does is resize images which are smaller or larger than the screen to fill the entire screen while keeping their aspect ratio. For example, when using a display in landscape orientation (width > height) an image with portrait ratio (height > width) would be as high as the screen, while there is some space on the left and right. My example should illustrate this.

In these cases, I would like to be able to detect if the user clicks on the image or outside of it. However, with this approach the bounding box of the image fills the entire div and the entire screen, even if it looks different to the user.

What I mean by this is: The div is set to have 100 percent of the width and height of its container. Since its position is set to fixed, it will have the same size as the screen. For the object-fit property to work on the image, I need to assign it a width and height too. When I set those values to 100 percent, the image's so-called bounding box will fill the entire parent/screen, and then the image will take up as much space as it can inside this box while keeping its aspect ratio (see this answer for another explanation). That means it may look like the image only has the same height, and not the same width as its parent, but in reality, it does. You can verify this using the developer tools of your browser, and I added a border to the image to visualise it. This means, however, that I cannot attach an event listener to the image and one to the div to differentiate between clicks on the image and on the blank area, since the image counts as filling the entire area.

What would be the best approach to solve this? Is there a CSS-only-way? The only thing I can come up with is using JS to size the image, which means making it bigger/smaller to fit the screen while making it keep its aspect ratio, and also doing this on every resize. This seems like a lot of work for something so simple.

答案1

得分: 2

尝试使用 flex 布局。如果您更改 div.outer 的高度和宽度或图像的尺寸,它将保持在一个维度上居中,并在另一个维度上填充 div.outer。只有在图像本身上点击才会触发警告。

<div class="outer">
  <div class="inner">
    <img src="https://placekitten.com/200/300" onclick="alert('click')">
  </div>
</div>
英文:

Try using flex layout. If you change the height and width of div.outer or the dimensions of the image, it will remain centered in one dimension and fill the div.outer in the other. And only a click on the image itself will raise the alert.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

div.outer {
  height: 200px;
  width: 100px;
  border: solid 1px black;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

div.inner {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

div.inner,
img {
  max-width: 100%;
  max-height: 100%;
}

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

&lt;div class=&quot;outer&quot;&gt;
  &lt;div class=&quot;inner&quot;&gt;
    &lt;img src=&quot;https://placekitten.com/200/300&quot; onclick=&quot;alert(&#39;click&#39;)&quot;&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

一般情况下,如果图像覆盖整个屏幕,那么我们无法点击父div

但是,如果您选择在四个角的小空间上点击,那么我们可以在父元素上提供点击。

请参考下面的代码片段。

div上使用Flex来居中img(图像)标签。
我还提供了最大宽度和高度,以确保img(图像)标签在父元素的边界内。

我甚至还告诉您如何强制拉伸,如果需要的话

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-css -->
* { box-sizing:border-box;}
html,body {height:100%;}
body {margin:0;}

div {
  position: fixed;
  /*拉伸DIV*/
  top: 0;
  bottom:0;
  right:0;
  left: 0;
  /*添加背景颜色以便识别*/
  background-color: #eadefe;
  /*新属性*/
  display: flex;
  justify-content: center;
  align-items: center;
}

img {
  max-width: 100%; /** 将宽度更改为最大宽度 **/
  max-height: 100%; /** 将高度更改为最大高度 **/
  border: thin solid black;
}

/**以下是强制拉伸并保持图像比例的部分**/

div.force-stretch {
  padding: 10px; /*提供填充以使图像和div都能被点击*/
}
div.force-stretch img {
  object-fit:cover; /*添加10px间隙,以便即使拉伸也有父div点击的空间*/
  width: 100%;
  height: 100%;
}

<!-- 语言: lang-html -->
<div>
  <img src="https://placekitten.com/200/300" alt="">
</div>

<!-- 结束代码片段 -->
英文:

In general sense, if image covers the whole screen then we cant target the click on the parent div.

But if u go for the little space all on four corners, then we can provide the click on the parent.

Please refer to the snippet below.

Use the Flex on the div to center the img(image) tag.
I have also provided the maximum width and height so that, the img(image) tag will be in boundary of the parent.

I had even provided you on how to force stretch if needed

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* { box-sizing:border-box;}
html,body {height:100%;}
body {margin:0;}

div {
  position: fixed;
  /*stretching the DIV*/
  top: 0;
  bottom:0;
  right:0;
  left: 0;
  /*added the background-color for identification*/
  background-color: #eadefe;
  /*new-props*/
  display: flex;
  justify-content: center;
  align-items: center;
}

img {
  max-width: 100%; /** changed width to max-width **/
  max-height: 100%; /** changed height to max-height **/
  border: thin solid black;
}

/**Below is to force strech and keep image proportions**/

div.force-stretch {
  padding: 10px; /*provided padding so image and div can both get clicks*/
}
div.force-stretch img {
  object-fit:cover; /*added 10px gap so even if stretched there is space for parent div click*/
  width: 100%;
  height: 100%;
}

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

&lt;div&gt;
  &lt;img src=&quot;https://placekitten.com/200/300&quot; alt=&quot;&quot;&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 1

你可以利用 flex 的奇怪行为来实现所需的结果。

<div class="image-holder">
    <img
        src="https://images.unsplash.com/photo-1592194996308-7b43878e84a6?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=387&amp;q=80"
        alt="kitten" />
</div>

flex 会拉伸图像的高度,如果要拉伸宽度,请使用 flex-direction: column

英文:

You can use the strange behavior of flex to produce the desired result.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

document.querySelector(&#39;img&#39;).onclick = () =&gt; {alert(&#39;you pet the kitten.&#39;)}

<!-- language: lang-css -->

body {
	margin: 0;
}
.image-holder {
	height: 100vh;
	width: 100vw;
	display: flex;
	justify-content: center;
}

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

&lt;div class=&quot;image-holder&quot;&gt;
			&lt;img
				src=&quot;https://images.unsplash.com/photo-1592194996308-7b43878e84a6?ixlib=rb-4.0.3&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=387&amp;q=80&quot;
				alt=&quot;kitten&quot; /&gt;
		&lt;/div&gt;

<!-- end snippet -->

flex stretches the image's height, if you want to stretch the width use flex-direction: column.

答案4

得分: 0

这段代码解决了你的问题:

<div class="some-text"></div>
<div class="img-container">
  <img src="https://placekitten.com/500/100" alt="">
</div>
<div class="some-text"></div>

假设你在图像之前和之后有一些元素(类名为.some-text的元素)。

英文:

This code solves your problem:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.img-container {
  width: 100%;
  height: 100vh;
  text-align: center;
}

img {
  object-fit: contain;
  height: 100%;
  max-width: 100%;
}

.some-text {
  height: 1000px;
}

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

&lt;div class=&quot;some-text&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;img-container&quot;&gt;
  &lt;img src=&quot;https://placekitten.com/500/100&quot; alt=&quot;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;some-text&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

Supposing that you have some elements before and after the image (element .some-text).

答案5

得分: 0

你可以在CSS中使用background-size属性来设置背景图片的大小,以填充整个屏幕而不产生边界框。你可以将background-size属性的值设置为cover,以确保图像覆盖整个屏幕,同时保持其纵横比。

这是一个示例:

body {
    background-image: url('your-image-url.jpg');
    background-size: cover;
    background-repeat: no-repeat;
    background-attachment: fixed;
}

这将使背景图像填充整个屏幕,而不会出现边界框或图像拉伸。background-repeat属性设置为no-repeat,以防止图像重复出现,background-attachment属性设置为fixed,以使图像在屏幕上保持固定位置。

英文:

You can use the background-size property in CSS to set the size of the background image to fill the entire screen without having a bounding box. You can set the background-size property value to cover to make sure the image covers the entire screen, while maintaining its aspect ratio.

Here's an example:

body {
       background-image: url(&#39;your-image-url.jpg&#39;);
       background-size: cover;
       background-repeat: no-repeat;
       background-attachment: fixed;
    }

This will set the background image to fill the entire screen, without any bounding box or stretching of the image. The background-repeat property is set to no-repeat to prevent the image from repeating and the background-attachment property is set to fixed to keep the image in a fixed position on the screen.

huangapple
  • 本文由 发表于 2023年2月8日 21:40:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386642.html
匿名

发表评论

匿名网友

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

确定