【问题标题】:CSS resizing background imageCSS 调整背景图片大小
【发布时间】:2020-05-19 16:32:23
【问题描述】:

我在调整 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>

【问题讨论】:

  • background-size;auto 100% 应该可以工作但是它需要一个非常大的图像,因为它需要足够宽以覆盖整个屏幕宽度,
  • 不幸的是,它不起作用。基本上,我想获得与本网站相同的效果:hairybikers.com/recipes/view/beef-goulash 在调整大小时图像不会在其下方留下这个空白区域。

标签: html css flexbox resize


【解决方案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>

【讨论】:

    猜你喜欢
    • 2021-02-23
    • 2011-03-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-28
    • 2020-04-23
    • 2014-02-17
    • 2014-01-16
    • 2014-06-16
    相关资源
    最近更新 更多