为什么我的背景图片会被放大?

时间:2017-02-14 10:02:56

标签: html css

我正在尝试设置1920 * 1080的背景图像,并且由于某种原因,图片会被放大并被裁剪。

此外,我想知道是否有可能将<navbar>设置为不覆盖背景的顶部。是否有机会分开它?

这是我的CSS文件的背景设置:

body {
  background: url(images/back.jpg) no-repeat center center fixed;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

body,
html {
  font-family: 'Caveat Brush', cursive;
  color: white;
}

这就是image looks like on my browser

的方式

谢谢。

4 个答案:

答案 0 :(得分:1)

这是因为你使用的是background-size:cover; 这将缩放背景图像以适应它所在的html元素的宽度和高度,同时拉伸和/或裁剪图像。 考虑导航栏: 如果您的html中有导航栏并且它具有给定的高度,例如50px,则添加

body { 
    padding-top: 50px;
    background-position: top 50px center;
}

到你的css

答案 1 :(得分:0)

根据您的建议进行一些更改之后,导线栏本身会随着填充物而降低(图像将被附加)。

html的代码是:

    </head>
    <body>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Brand and toggle get grouped for better mobile display -->
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" 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="#">Eyal Younger </a>
    </div>

    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">HOME <span class="sr-only">(current)</span></a></li>
        <li><a href="#">HTML COURSE</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">One more separated link</a></li>
          </ul>
        </li>
      </ul>
      <form class="navbar-form navbar-left">
        <div class="form-group">
          <input type="text" class="form-control" placeholder="Search">
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
      </form>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#">Link</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
            <li role="separator" class="divider"></li>
            <li><a href="#">Separated link</a></li>
          </ul>
        </li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

css代码:

body { 

    padding-top: 50px;
    background-position: top 50px center;   
    background: url(images/back.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: height: 1920 width: 1080px;

}

body,
html {

    font-family: 'Caveat Brush', cursive;
    color:white;    
}

h1 {
    font-weight: 700;
    font-size: 5em;

}

h3 {
    padding-top: 0px;
    padding-bottom: 0px;
    margin-top: 5px;
    margin-bottom: 10px;


}

#mainimg {
    width: 180px;
    height: 180px;
    border-radius: 50%;
    border: 8px double red;
    box-shadow: 5px 5px 6px #353232;

}



.content {
    padding: 6%;
    text-align: center;
    text-shadow: 0px 4px 3px rgba(0,0,0,0.9),
                 0px 8px 13px rgba(0,0,0,0.1),
                 0px 18px 23px rgba(0,0,0,0.1);


}

enter image description here

答案 2 :(得分:0)

尝试使用完整的背景图像

body{
margin: 0;
background:url('images/back.jpg') no-repeat fixed 0 -300px;
background-size:100%;

答案 3 :(得分:0)

这就是它如何缩放我的背景图像 How it looks like