CSS调整背景图片的大小

时间:2020-05-19 16:32:23

标签: html css flexbox resize

我在flexbox中调整背景图片的大小时遇到​​问题。 要做的是在调整浏览器窗口大小的同时,背景图像水平缩小,而不是垂直缩小。 我尝试了不同的背景大小属性,并尝试了最大和最小高度/宽度以及flex-shrink,但对我来说没有任何用处。 预先感谢!

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
    .image-header {
        display: flex;
        min-height: 75vh;
        background-image: linear-gradient(rgba(0, 0, 0, 0.25), rgba(0, 0, 0, 0.25)), 
        url('https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/190403-pancakes-066-copy-1554497284.jpeg?crop=0.777xw:0.584xh;0.115xw,0.315xh&resize=1200:*');
        background-repeat: no-repeat;
        background-attachment: fixed;
        max-width: 100%;
        background-size: contain;
        position: relative;
    }
    
    .image-header h1 {
        color: white;
        padding: 25px;
        text-shadow: 2px 2px 2px black;
        position: absolute;
        bottom: 0;
        left: 0;
        margin-bottom: 20px;
    }
        </style>
    </head>
    <body>
        <div class="main-image">
            <div class="image-header">
            <h1>Easy Fluffy American Pancakes</h1>
            </div>
    </body>
    </html>

1 个答案:

答案 0 :(得分:-1)

这应该在下面为您工作。

https://jsfiddle.net/pa5dbk8j/#&togetherjs=mk3zhMtk7M

.image-header 我为图片增加了高度

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
.image-header {
    display: flex;
    min-height: 75vh;
    background-image: linear-gradient(rgba(0, 0, 0, 0.25), rgba(0, 0, 0, 0.25)), 
    url('https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/190403-pancakes-066-copy-1554497284.jpeg?crop=0.777xw:0.584xh;0.115xw,0.315xh&resize=1200:*');
    background-repeat: no-repeat;
    background-attachment: fixed;
    max-width: 100%;
    max-height:100%;
    background-size: contain;
    position: relative;
}

.image-header h1 {
    color: white;
    padding: 25px;
    text-shadow: 2px 2px 2px black;
    position: absolute;
    bottom: 0;
    left: 0;
    margin-bottom: 20px;
}
    </style>
</head>
<body>
    <div class="main-image">
        <div class="image-header">
        <h1>Easy Fluffy American Pancakes</h1>
        </div>
        </div>
</body>
</html>