【问题标题】:background image not centering / keeps repeating背景图像不居中/不断重复
【发布时间】:2018-02-20 13:12:19
【问题描述】:

我目前正在尝试创建一个测试站点,只是为了练习,一方面,我的背景图像不会居中/并且每当我尝试使其具有响应性时都会不断重复。整个图像也不存在于浏览器中。任何解决方案都会有所帮助。

我试过了 背景尺寸:封面; 背景位置:中心;

它仍然无法正常工作。

body {
    font-family: Nunito;
    background-image: url(https://images.unsplash.com/photo-1517048676732-d65bc937f952?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1005f3d059e15847f5b8e818aafe7b51&auto=format&fit=crop&w=2550&q=80);
    background-size: cover;
    background-position: center;
}

.navbar {
    background-color: transparent;
    border: 0px;
}

.jumbotron {
    color: #C7DB8A;
    background-color: transparent;
    text-align-last: center;
    padding-top: 15%;
}

.jumbotron p {
    color: #5E9589;
}
<!--lorem: ctrl+shift+L -->
<!--hr styles-->
<!DOCTYPE html>
<html>

<head>
    <title>asdfs Home</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="js/home.js"></script>

    <link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/home.css">
    <link rel="stylesheet" type="text/css" href="css/fa-svg-with-js.css">
    <link href="https://fonts.googleapis.com/css?family=Nunito:700" rel="stylesheet">

</head>

<body>
    <!--nav-->
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav-demo" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <a class="navbar-brand" href="#">ABXCH</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-nav-demo">
                <ul class="nav navbar-nav">
                    <li><a href="#">
                        <i class="fas fas fa-home"></i>
                        Home
                        </a></li>
                    <li><a href="#">
                        <i class="far fa-question-circle"></i>
                        About
                        </a></li>
                    <li><a href="#">
                        <i class="far fa-handshake"></i>
                        Contact
                        </a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">
                        <i class="fas fa-sign-in-alt"></i>
                        Login
                        </a></li>
                </ul>
            </div>
        </div>
    </nav>
    <!--jumbo-->

    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="jumbotron">
                    <h1>asdfas</h1>
                    <p>Your Online Insurance Sales Office</p>
                    <hr>
                    <p><a class="btn btn-default btn-lg" href="#" role="button">Learn More</a></p>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

【问题讨论】:

  • 你试过加background-repeat: no-repeat;

标签: javascript html css twitter-bootstrap-3


【解决方案1】:

问题不在于background-repeat,而是您面临背景传播。正如您在下面的屏幕截图中看到的那样,您的 body 元素没有占据整个屏幕高度,当我们遇到这种情况时,有一些特殊的背景规则:

正如您在specification 中看到的那样:

对于根元素是 HTML HTML 元素或 XHTML 的文档 html 元素 [HTML]:如果 背景图像在上的计算值 root elementnone 并且它的 background-colortransparent,用户 代理必须改为传播背景的计算值 来自该元素的第一个 HTML BODY 的属性 或 XHTML 正文子元素 元素。该 BODY 元素的背景属性的使用值 是它们的初始值,传播的值被视为 它们是在根元素上指定的。建议 HTML 文档的作者为 BODY 指定画布背景 元素而不是 HTML 元素。

这意味着背景被传播以覆盖body 未覆盖的区域,因此您拥有重复的背景。为避免此类行为,您可以通过添加 min-height:100vh 或将 background-color 应用到 html 元素并保持图像仅覆盖内容部分来确保正文占据全屏高度:

html {
  background:white;
}

body {
    font-family: Nunito;
    background-image: url(https://images.unsplash.com/photo-1517048676732-d65bc937f952?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1005f3d059e15847f5b8e818aafe7b51&auto=format&fit=crop&w=2550&q=80);
    background-size: cover;
    background-position: center;
}

.navbar {
    background-color: transparent;
    border: 0px;
}

.jumbotron {
    color: #C7DB8A;
    background-color: transparent;
    text-align-last: center;
    padding-top: 15%;
}

.jumbotron p {
    color: #5E9589;
}
<!--lorem: ctrl+shift+L -->
<!--hr styles-->
<!DOCTYPE html>
<html>

<head>
    <title>asdfs Home</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    <script src="js/home.js"></script>

    <link href="https://use.fontawesome.com/releases/v5.0.6/css/all.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/home.css">
    <link rel="stylesheet" type="text/css" href="css/fa-svg-with-js.css">
    <link href="https://fonts.googleapis.com/css?family=Nunito:700" rel="stylesheet">

</head>

<body>
    <!--nav-->
    <nav class="navbar navbar-default">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-nav-demo" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <a class="navbar-brand" href="#">ABXCH</a>
            </div>
            <div class="collapse navbar-collapse" id="bs-nav-demo">
                <ul class="nav navbar-nav">
                    <li><a href="#">
                        <i class="fas fas fa-home"></i>
                        Home
                        </a></li>
                    <li><a href="#">
                        <i class="far fa-question-circle"></i>
                        About
                        </a></li>
                    <li><a href="#">
                        <i class="far fa-handshake"></i>
                        Contact
                        </a></li>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">
                        <i class="fas fa-sign-in-alt"></i>
                        Login
                        </a></li>
                </ul>
            </div>
        </div>
    </nav>
    <!--jumbo-->

    <div class="container">
        <div class="row">
            <div class="col-lg-12">
                <div class="jumbotron">
                    <h1>asdfas</h1>
                    <p>Your Online Insurance Sales Office</p>
                    <hr>
                    <p><a class="btn btn-default btn-lg" href="#" role="button">Learn More</a></p>
                </div>
            </div>
        </div>
    </div>
</body>

</html>


更多信息的一些相关链接:

What's the meaning of "propagated to the viewport" in the CSS spec?

Applying a background to <html> and/or <body>

https://css-tricks.com/just-one-of-those-weird-things-about-css-background-on-body/

【讨论】:

    【解决方案2】:

    因为你没有设置background-repeat:no-repeat;试试下面的代码:

    body {
    font-family: Nunito;
    background-image: url(https://images.unsplash.com/photo-1517048676732-d65bc937f952?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=1005f3d059e15847f5b8e818aafe7b51&auto=format&fit=crop&w=2550&q=80);
    background-size: cover;
    background-position: center;
    background-repeat:no-repeat; // Added
    min-height:100vh; // Added for ensure capture window size
    }
    

    【讨论】:

    • 是的,我也试过背景没有重复,我认为 min-height:100vh;是我正在寻找的谢谢
    • @hellothere 这是错误的,它与背景重复无关...... min-height 解决了这个问题还有一个原因,请查看我的答案以获取更多信息
    【解决方案3】:

    只需放置 background-repeat: no-repeatbackground-size: cover 。它会工作

    background-image: url(http://placehold.it/800x300);
    background-repeat: no-repeat;
    background-size: cover;
    background-position: center;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2020-01-07
      • 1970-01-01
      • 2021-08-07
      • 2011-01-28
      • 2013-08-15
      相关资源
      最近更新 更多