html - 背景大小 :contain on my hero image 遇到一些问题

标签 html css

试图将主图放在一起,但当我使用 background-size:contain 时,我的图像似乎会重复出现,如下面的屏幕截图所示: enter image description here

问题是,如果我使用 background-repeat:no-repeat 它会这样做 enter image description here

所有我想做的就是有一个 75vh 的英雄形象,该形象显示完整。不确定我做错了什么,但我已经很久没有接触 CSS 了。

HTML

<div class='header'>
    <div class='header__navbar'>

        <div class=header__navbar-media>
            <!-- used for first block to center logo -->
        </div>

        <div class='header__navbar-logo'>
            <a href="/"><img src="../img/logo-lospallos22.png" class='header__navbar-logo-img'></a>

        </div>

        <div class='header__navbar-order'>
            <div class='header__navbar-order-account'>
                <a href="#" class='login-nav'>Login</a>
                <a href="#" class='login-nav'>|</a>
                <a href="#" class='login-nav'>Register</a>
            </div>
            <a href='#' class='btn btn__orderButton'> Order Now</a>
        </div>  

    </div>

CSS

.header {
    background-image: url(../img/bg.jpg);
    height: 75vh;
    padding-top: 2rem;
    max-width: 100%;
    background-size: contain;
    background-repeat: no-repeat;
}

*, *::after, *::before{
    box-sizing: inherit;    
    padding: 0;
    margin: 0;
}

html{ /*Root font size is set in the html selector*/
    font-size: 62.5%; /* This is the font size we want(10px) divided by default root font size. 10/16 = .625*/
                      /*The reason we use percentage instead of hardcoded pixels is because it allows end users to change font size for vision purposes in their browser..*/
}

a:link, a:visited{
    text-decoration: none;
}

a{
    color: inherit;
}

header 中的所有元素都是 flex 元素,所以我省略了 CSS,因为我认为它不会增加任何值(value)。

最佳答案

如果您希望图像覆盖整个区域,请使用 background-size: cover 而不是 contain。

关于html - 背景大小 :contain on my hero image 遇到一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804642/

相关文章:

css - 刷新页面后链接不再变成 "unvisited"。这是为什么?

php - WordPress functions.php 不工作

javascript - 文本区域最大长度值不起作用

html - 如何使 Sundown 渲染 block 引用(以 ">"开头的行)

html - 水平形式的 CSS 响应列表项

asp.net - 清除属性未按预期工作

android - 我网站上的图片无法在移动浏览器中加载

html - 何时在 html/css 中使用 id 和 class

javascript - 在没有固定高度/虚拟化的情况下提高长列表的性能

html - 如何修复幻灯片?空白出现 HTML/CSS