html - 正确的 Jumbotron 图像尺寸

标签 html css image twitter-bootstrap

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

提前致谢!

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%;
  }
}

关于html - 正确的 Jumbotron 图像尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42960618/

相关文章:

javascript - 如何使用Javascript读取本地文本文件并逐行读取?

javascript - 获取很多复选框的值,然后通过foreach循环打印出来

windows - FFMPEG 在引用图像文件时使用了错误的参数

javascript - 如何添加菜单以在 Bootstrap 中切换导航栏

html - 如何将视频放入平板电脑的图像中

javascript - 我想在单个 html 页面中拥有多个选项卡,但我的代码无法正常工作

javascript - 如何做支持水平和垂直方面的比例图库?

javascript - html/JS 中其他输入的只读等价物

javascript - svg 上的 Bootstrap 弹出窗口

javascript - 将图像缓冲区发送到 node.js tcp 服务器