【问题标题】:How can I make my background image 'img-responsive'?我怎样才能让我的背景图片“img-responsive”?
【发布时间】:2017-08-12 11:36:23
【问题描述】:

我想知道如何使用引导程序“img-responsive”制作背景图片?背景图片在herodiv

<!DOCTYPE html>
<html>

<head>
  <html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Liam Docherty Digital Portfolio</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="css/style.css">

  <style>
    body {
      margin: 0;
      padding: 0;
    }
    
    .navbar.navbar-default {
      background-color: #4D5061;
      height: 10vh;
      z-index: 100;
    }
    
    .navbar.navbar-default ul {
      list-style-type: none;
      text-align: right;
    }
    
    .navbar.navbar-default ul li {
      display: inline-block;
    }
    
    .dropdown-menu li {
      text-align: center
    }
    
    .dropdown .dropdown-menu {
      background-color: #4D5061;
    }
    
    .dropdown .dropdown-menu a {
      color: white;
    }
    
    .navbar.navbar-default ul li a {
      display: inline-block;
      padding: 3.5vh 8px 4px;
      color: white;
      text-decoration: none;
      font-size: 14pt;
      font-family: 'Roboto', sans-serif;
    }
    
    .navbar.navbar-default ul li:after {
      content: '';
      position: absolute;
      right: 50%;
      bottom: 0;
      left: 50%;
      height: 3px;
      background-color: #FFFFFF;
      border-radius: 9px;
      transition: all .2s;
    }
    
    .navbar.navbar-default ul li a:hover {
      color: white;
    }
    
    .nav.navbar-nav,
    .nav.navbar-nav>li {
      float: none;
    }
    
    .navbar.navbar-default ul li:hover:after {
      right: 0;
      left: 0;
    }
    
    .nav-title {
      font-size: 14pt;
      margin: 0;
      top: 35px;
      left: 50px;
      width: 100%;
      position: absolute;
      text-align: center;
      color: white;
      font-family: 'Roboto', sans-serif;
    }
    
    .navbar.navbar-default ul.dropdown-menu li,
    .navbar.navbar-default ul.dropdown-menu li a {
      position: relative;
      display: block;
    }
    
    #logo {
      padding-top: 2vh;
      padding-left: 20px;
      float: left;
    }
    
    .hero {
      background-image: url("https://static.pexels.com/photos/48727/pexels-photo-48727.jpeg");
      background-attachment: fixed;
      position: relative;
      text-align: center;
      width: 100%;
      max-width: 100%;
      width: 100vw;
      height: 100%;
    }
    
    section {
      position: relative;
      height: 95vh;
    }
    
    .section1 {
      height: 100vh;
      text-align: center;
      color: white;
    }
    
    .section2 {
      background-color: #11B5E4;
      text-align: center;
      color: white;
    }
    
    .section3 {
      background-color: #FFFFFF;
      text-align: center;
      color: white;
    }
    
    .fa-angle-down {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    
    .fa-angle-up {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    
    .footer {
      height: 6vh;
      background-color: #4D5061;
      padding: 0;
      right: 0;
      bottom: 0;
      left: 0;
    }
    
    .footertext {
      font-size: 14pt;
      color: white;
      font-family: 'Roboto', sans-serif;
      text-align: center;
    }
    
    .profile.box {
      bottom: 0;
      height: 50%;
      left: 0;
      margin: auto;
      position: absolute;
      right: 0;
      top: 0;
      width: 50%;
    }
    
    .shape {
      border-radius: 25px;
      background: #4D5061;
      content: url(http://i1126.photobucket.com/albums/l611/ldocherty1/IMG_0730_zpsiz4dqc47.jpg);
      color: white;
      height: 250px;
      margin: auto;
      padding: 3px;
      width: 250px;
    }
    
    .shape2 {
      background: linear-gradient(35deg, #4D5061, #4D5061);
      border-radius: 85px;
      color: white;
      height: 40px;
      margin: 1% auto;
      opacity: 0.9;
      padding: 0px;
      width: 250px;
    }
  </style>

</head>

<body>
  <nav class="navbar navbar-default navbar-fixed-top">
    <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="#"><img src="http://placehold.it/60x60" alt="Your Brand Name"></a>
        <h1 class="nav-title">Liam Docherty's Digital Portfolio</h1>
      </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><a href="#">Home</a>
          </li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">About Me <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a>
              </li>
              <li><a href="#section3">Contact</a>
              </li>
              <li><a href="#">Something else here</a>
              </li>
              <li><a href="#">Separated link</a>
              </li>
              <li><a href="#">One more separated link</a>
              </li>
            </ul>
          </li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Units <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><a href="#">Separated link</a>
              </li>

              <li><a href="#">One more separated link</a>
              </li>
            </ul>
          </li>
          <li><a href="#">Clients</a>
          </li>
          <li><a href="#contact-me">Contact Me</a>
          </li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
  </nav>


  <section id="section1" class="section1">
    <div class="hero">
      <div class="profile box">
        <div class="shape"></div>
        <div class="shape2">
          <p>kjjjjjjjjjkjjjkkjkj</p>
        </div>
      </div>
    </div>





    <a href="#section2"><i class="fa fa-angle-down" style="font-size:100px;"></i></a>

  </section>
</html>

【问题讨论】:

标签: html css


【解决方案1】:

我个人使用 Jquery 的 Backstretch。该代码响应迅速且易于使用:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

然后将样式/图像添加到 .css 文档中,如下所示:

<style>
.content-wrapper {
    /* This image will be displayed fullscreen */

    background: url('http://site.rockbottomgolf.com/blog_images/Hole%2012%20-%20Imgur.jpg') no-repeat center fixed;

    scroll-x /* Ensure the html element always takes up the full height of the browser window */ min-height: 100%;
    /* The Magic */
    background-size: cover;
}
</style>

编辑!! - 我发布了一个指向 Jquery 的链接,但没有发布实际的响应式 backstretch 插件。请注意,您必须保留上面所需的脚本,然后在其后添加 backstretch,因为它是一个依赖项。 Download The Backstretch Plugin Here

然后您将按如下方式添加脚本:

<script src="example.com/relative-Path/jquery.backstretch.min.js"></script>

【讨论】:

  • 您好,感谢您的帮助。现在我已经添加了您推荐的代码并且它可以工作,我可以删除原始图像的 div 还是仍然相关?
  • 没关系,不需要了就删掉。
【解决方案2】:

使用background-size: cover

https://developer.mozilla.org/en-US/docs/Web/CSS/background-size

cover - 与包含相反的关键字。尽可能大地缩放图像并保持图像纵横比(图像不会被压扁)。图像“覆盖”了容器的整个宽度或高度。当图像和容器的尺寸不同时,图像会被裁剪为左/右或上/下。

或者,您也可以根据您想要的行为尝试contain,但从您的投资组合的外观来看,cover 是您想要的

包含:尽可能大地缩放图像并保持图像纵横比(图像不会被压扁)的关键字。图像在容器内加了信箱。当图像和容器的尺寸不同时,空白区域(左/右的顶部/底部)将使用背景色填充。

* 编辑 - 还在 .hero 的简写 background 属性中添加了 background-position: center center; background-repeat: no-repeat;,因为从您的投资组合的外观来看,您想要这些。

<!DOCTYPE html>
<html>

<head>
  <html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Liam Docherty Digital Portfolio</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <link rel="stylesheet" href="css/style.css">

  <style>
    body {
      margin: 0;
      padding: 0;
    }
    
    .navbar.navbar-default {
      background-color: #4D5061;
      height: 10vh;
      z-index: 100;
    }
    
    .navbar.navbar-default ul {
      list-style-type: none;
      text-align: right;
    }
    
    .navbar.navbar-default ul li {
      display: inline-block;
    }
    
    .dropdown-menu li {
      text-align: center
    }
    
    .dropdown .dropdown-menu {
      background-color: #4D5061;
    }
    
    .dropdown .dropdown-menu a {
      color: white;
    }
    
    .navbar.navbar-default ul li a {
      display: inline-block;
      padding: 3.5vh 8px 4px;
      color: white;
      text-decoration: none;
      font-size: 14pt;
      font-family: 'Roboto', sans-serif;
    }
    
    .navbar.navbar-default ul li:after {
      content: '';
      position: absolute;
      right: 50%;
      bottom: 0;
      left: 50%;
      height: 3px;
      background-color: #FFFFFF;
      border-radius: 9px;
      transition: all .2s;
    }
    
    .navbar.navbar-default ul li a:hover {
      color: white;
    }
    
    .nav.navbar-nav,
    .nav.navbar-nav>li {
      float: none;
    }
    
    .navbar.navbar-default ul li:hover:after {
      right: 0;
      left: 0;
    }
    
    .nav-title {
      font-size: 14pt;
      margin: 0;
      top: 35px;
      left: 50px;
      width: 100%;
      position: absolute;
      text-align: center;
      color: white;
      font-family: 'Roboto', sans-serif;
    }
    
    .navbar.navbar-default ul.dropdown-menu li,
    .navbar.navbar-default ul.dropdown-menu li a {
      position: relative;
      display: block;
    }
    
    #logo {
      padding-top: 2vh;
      padding-left: 20px;
      float: left;
    }
    
    .hero {
      background: url("https://static.pexels.com/photos/48727/pexels-photo-48727.jpeg") center center no-repeat;;
      background-attachment: fixed;
      position: relative;
      background-size: cover;
      text-align: center;
      width: 100%;
      max-width: 100%;
      width: 100vw;
      height: 100%;
    }
    
    section {
      position: relative;
      height: 95vh;
    }
    
    .section1 {
      height: 100vh;
      text-align: center;
      color: white;
    }
    
    .section2 {
      background-color: #11B5E4;
      text-align: center;
      color: white;
    }
    
    .section3 {
      background-color: #FFFFFF;
      text-align: center;
      color: white;
    }
    
    .fa-angle-down {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    
    .fa-angle-up {
      color: #4D5061;
      position: absolute;
      bottom: 0px;
    }
    
    .footer {
      height: 6vh;
      background-color: #4D5061;
      padding: 0;
      right: 0;
      bottom: 0;
      left: 0;
    }
    
    .footertext {
      font-size: 14pt;
      color: white;
      font-family: 'Roboto', sans-serif;
      text-align: center;
    }
    
    .profile.box {
      bottom: 0;
      height: 50%;
      left: 0;
      margin: auto;
      position: absolute;
      right: 0;
      top: 0;
      width: 50%;
    }
    
    .shape {
      border-radius: 25px;
      background: #4D5061;
      content: url(http://i1126.photobucket.com/albums/l611/ldocherty1/IMG_0730_zpsiz4dqc47.jpg);
      color: white;
      height: 250px;
      margin: auto;
      padding: 3px;
      width: 250px;
    }
    
    .shape2 {
      background: linear-gradient(35deg, #4D5061, #4D5061);
      border-radius: 85px;
      color: white;
      height: 40px;
      margin: 1% auto;
      opacity: 0.9;
      padding: 0px;
      width: 250px;
    }
  </style>

</head>

<body>
  <nav class="navbar navbar-default navbar-fixed-top">
    <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="#"><img src="http://placehold.it/60x60" alt="Your Brand Name"></a>
        <h1 class="nav-title">Liam Docherty's Digital Portfolio</h1>
      </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><a href="#">Home</a>
          </li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">About Me <span class="caret"></span></a>
            <ul class="dropdown-menu">
              <li><a href="#">Action</a>
              </li>
              <li><a href="#section3">Contact</a>
              </li>
              <li><a href="#">Something else here</a>
              </li>
              <li><a href="#">Separated link</a>
              </li>
              <li><a href="#">One more separated link</a>
              </li>
            </ul>
          </li>
          <li class="dropdown">
            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Units <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><a href="#">Separated link</a>
              </li>

              <li><a href="#">One more separated link</a>
              </li>
            </ul>
          </li>
          <li><a href="#">Clients</a>
          </li>
          <li><a href="#contact-me">Contact Me</a>
          </li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
  </nav>


  <section id="section1" class="section1">
    <div class="hero">
      <div class="profile box">
        <div class="shape"></div>
        <div class="shape2">
          <p>kjjjjjjjjjkjjjkkjkj</p>
        </div>
      </div>
    </div>





    <a href="#section2"><i class="fa fa-angle-down" style="font-size:100px;"></i></a>

  </section>
</html>

【讨论】:

  • @OusmaneMahyDiaw 的行为方式不同。如果 OP 的浏览器是纵向的,则图像将重复或在顶部/底部创建空白间隙,因为图像是横向的。
  • 您还必须包含background-position: center,否则较小的尺寸将无法响应。
  • @sequential 很好,但它在小屏幕上仍然响应,因为它可以缩放。他们想对background-position 使用什么值取决于他们希望看到图像的哪个部分。但同意他们可能想要中心,我会补充一点。
【解决方案3】:

你可以试试

<div>
    <img src="IMAGE LINK" style='width:100%;' border="0" alt="Null">
</div>

来源:Make an image responsive - simplest way

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 2017-09-02
    • 1970-01-01
    相关资源
    最近更新 更多