我在引导程序中使用超大容器,并尝试使用图像作为背景。但是,似乎所有图像都会随着屏幕尺寸的变化而失真,或者无法显示照片的正确部分。我应该设置保存图像的正确尺寸吗?还是一种消除/最小化屏幕调整大小失真的方法?如有必要,我可以向您展示我的代码。

提前致谢!

CSS:

*{
    margin: 0;
    padding: 0;
}

.navbar-default{
    background-color: rgb(66, 134, 244);
}
.under {
    border-bottom: 15px solid rgb(66, 134, 244);
}
.size {
    width: 300px;
}
.jumbotron{
    height: 400px;
    background: url("../images/parakeet.jpg");
    background-size: cover;
}

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Case</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <link href="css/custom.css" rel="stylesheet">
</head>
<body>

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">

<!-- Button nav for small screens -->

    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
<!-- Brand name -->

      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>

<!-- Makes nav not expand on resize -->

    <div class="collapse navbar-collapse" id="myNavbar">

<!-- Menu -->
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Home</a></li>

<!-- Dropdown menu -->
      <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Page 1-1</a></li>
          <li><a href="#">Page 1-2</a></li>
          <li><a href="#">Page 1-3</a></li>
        </ul>
      </li>
<!-- Non dropdown -->
      <li><a href="#">Page 2</a></li>
      <li><a href="#">Page 3</a></li>
    </ul>
  </div>
    </div>
</nav>
      <div class="jumbotron text-center under">
    <h1>Website Heading</h1>
      <p>Information about the website!</p>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-sm-4 text-center">
                <h2>Text</h2>
                <p>More text describing the heading!</p>
                <span class="glyphicon glyphicon-leaf size"></span>

            </div>
                    <div class="col-sm-4 text-center">
                <h2>Text</h2>
                <p>More text describing the heading!</p>
                <span class="glyphicon glyphicon-hourglass btn-lg"></span>

            </div>

            <div class="col-sm-4 text-center">
                <h2>Text</h2>
                <p>More text describing the heading!</p>
                <span class="glyphicon glyphicon-plus btn-lg"></span>
            </div>

        </div>

    </div>
</body>
</html>

最佳答案

很好。现在您需要玩background-position
现在,如果您运行此代码。您会看到一些可爱的狗的图像。现在,在较小的设备上,您将看到3条中狗。为什么?因为background-position在x上设置为50%,在y上设置为50%(图像的中心)

假设您要在手机上显示最合适的黑头小狗。只需将background-position设置为100%和50%即可。



*{
    margin: 0;
    padding: 0;
}

.navbar-default{
    background-color: rgb(66, 134, 244);
}
.under {
    border-bottom: 15px solid rgb(66, 134, 244);
}
.size {
    width: 300px;
}
.jumbotron{
    height: 400px;
    background: url("https://puppydogweb.com/wp-content/uploads/2015/05/54_2383_Cute-Puppies-puppies-16094619-1280-800.jpg");
    background-size: cover;
    background-position: 0 50%;
}

<body>

<nav class="navbar navbar-default navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">

<!-- Button nav for small screens -->

    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
<!-- Brand name -->

      <a class="navbar-brand" href="#">WebSiteName</a>
    </div>

<!-- Makes nav not expand on resize -->

    <div class="collapse navbar-collapse" id="myNavbar">

<!-- Menu -->
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Home</a></li>

<!-- Dropdown menu -->
      <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Page 1 <span class="caret"></span></a>
        <ul class="dropdown-menu">
          <li><a href="#">Page 1-1</a></li>
          <li><a href="#">Page 1-2</a></li>
          <li><a href="#">Page 1-3</a></li>
        </ul>
      </li>
<!-- Non dropdown -->
      <li><a href="#">Page 2</a></li>
      <li><a href="#">Page 3</a></li>
    </ul>
  </div>
    </div>
</nav>
      <div class="jumbotron text-center under">
    <h1>Website Heading</h1>
      <p>Information about the website!</p>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-sm-4 text-center">
                <h2>Text</h2>
                <p>More text describing the heading!</p>
                <span class="glyphicon glyphicon-leaf size"></span>

            </div>
                    <div class="col-sm-4 text-center">
                <h2>Text</h2>
                <p>More text describing the heading!</p>
                <span class="glyphicon glyphicon-hourglass btn-lg"></span>

            </div>

            <div class="col-sm-4 text-center">
                <h2>Text</h2>
                <p>More text describing the heading!</p>
                <span class="glyphicon glyphicon-plus btn-lg"></span>
            </div>

        </div>

    </div>
</body>





因此,所有分辨率小于平板电脑分辨率的东西都会看到黑头小狗。

@media (max-width: 768px) {
  .jumbotron{
    background-position: 100% 50%;
  }
}

07-28 02:20
查看更多