简体   繁体   English

使带有 img 的 div 适合页面高度

[英]Fit div with img to page height

I have a simple container with images that looks like a table, so HTML and CSS would look like:我有一个简单的容器,其中包含看起来像表格的图像,因此 HTML 和 CSS 看起来像:

 .cab.net { padding: 0; margin: 0; display: flex; flex-direction: column; }.module { display: table-cell; background-color: black; }.module > button > img { width: 100%; }.cab.net-info-button { padding: 0; border: none; background: none; }
 <div class="cab.net"> <div> <div class="module"> <button class="cab.net-info-button"> <img src="https://images.unsplash.com/photo-1500577329392-f0af6d7dd827?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGlnaHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80" /> </button> </div> <div class="module"> <button class="cab.net-info-button"> <img src="https://images.unsplash.com/photo-1500577329392-f0af6d7dd827?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGlnaHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80" /> </button> </div> </div> <div> <div class="module"> <button class="cab.net-info-button"> <img src="https://images.unsplash.com/photo-1500577329392-f0af6d7dd827?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGlnaHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80" /> </button> </div> <div class="module"> <button class="cab.net-info-button"> <img src="https://images.unsplash.com/photo-1500577329392-f0af6d7dd827?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGlnaHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80)" /> </button> </div> </div> <div> <div class="module"> <button class="cab.net-info-button"> <img src="https://images.unsplash.com/photo-1500577329392-f0af6d7dd827?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGlnaHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80" /> </button> </div> <div class="module"> <button class="cab.net-info-button"> <img src="https://images.unsplash.com/photo-1500577329392-f0af6d7dd827?ixid=MXwxMjA3fDB8MHxzZWFyY2h8MXx8aGlnaHxlbnwwfHwwfA%3D%3D&ixlib=rb-1.2.1&w=1000&q=80" /> </button> </div> </div> </div>

I would like this to auto-fit my browser's height , I managed to manipulate the width of the element, but it's not exactly what I expected.我希望它能自动适应我的浏览器的高度,我设法操纵了元素的宽度,但这并不完全符合我的预期。
In fact, if I flip the screen that is higher than it's width, it will (for example) fit to 25% width, leaving lots of empty space beneath.事实上,如果我翻转高于其宽度的屏幕,它将(例如)适合 25% 的宽度,在下方留下大量空白空间。

I tried setting the height at 100vh or 100%, but it gives me nothing, is there any way?我尝试将高度设置为 100vh 或 100%,但它什么也没给我,有什么办法吗?

You can try to put inside the image tag object-fit:cover so whenever the height of the div is adjusted the image will also cover the whole div width and height.您可以尝试将图像标签放入object-fit:cover中,这样每当调整 div 的高度时,图像也会覆盖整个 div 的宽度和高度。 Alternatively, though this might take time by using child elements of your class module you can alternately set each images to backgrounds of each module class.或者,尽管使用 class modulechild elements可能会花费一些时间,但您可以交替地将每个图像设置为每个module class 的背景。

Like this:像这样:

.module {
    background-position: center top;
    background-repeat: no-repeat;
    background-size: cover;
    width: ; // set your own width
    height: ; // set your own height
}

.module:first-child {
    background-image:url(images/image1.png);
}

.module:nth-child(2) {
    background-image:url(images/image2.png);
}

.module:nth-child(3) {
    background-image:url(images/image3.png);
}

.module:nth-child(4) {
    background-image:url(images/image4.png);
}

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

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