高度100%在背景上不起作用?

时间:2019-03-28 15:45:21

标签: html css html5 twitter-bootstrap css3

有很多类似这样的问题,但是答案并不能解决我的问题,如您所见,我正在尝试通过在文本的父div上制作背景图像来在图像外观上创建此小文本。由于图像没有任何高度而没有显示图像,我给出100%的高度也不起作用,快速堆栈搜索说由于父级而给html 100%应该有一些高度,但是我这样做也不能解决问题。我想要一些响应高度控制,因此为什么要使用%。当然不想使用px值

我的代码

h1, h2, h3, h4, h5, h6 {
  font-family: 'Teko', sans-serif;
}
html {
  font-size: 18px;
}
@media (min-width: 576px) {
  .container {
    max-width: 540px;
 }
}
@media (min-width: 768px) {
  .container {
    max-width: 720px;
 }
}
@media (min-width: 992px) {
  .container {
    max-width: 960px;
 }
}
@media (min-width: 1200px) {
  .container {
    max-width: 1400px;
 }
}
h1, .h1 {
  font-size: 3.815rem;
}
h2, .h2 {
  font-size: 2.441rem;
}
h3, .h3 {
  font-size: 1.563rem;
}
h4, .h4 {
  font-size: 1.25rem;
}

html, body {
      height: 100%;
  }
  
.cta-location .cta-location-bg {
  background-image: url("https://www.dropbox.com/s/5x8p2z5cvip5u38/chicago.jpg?dl=1");
  height: 100%;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
}
.cta-location .cta-location-text-content {
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
}
<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>slick slider</title>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
   <link href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">

   <link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/css/bootstrap-select.min.css">
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

   <link rel="stylesheet" href="css/style.css">
</head>

<body>

   <div class="my-6">

      <div class="cta-location h-100">
         <div class="bg-dark text-white cta-location-bg">
            <div class="container d-flex align-items-center">
               <div class="row w-100">
                  <!-- <div class="text-left col-lg-4 col-sm-12 cta-location-text-content"> -->
                        <div class="cta-location-text-content">

                     <h2 class="card-title ">
                        Your Success, Without Borders
                     </h2>
                     <p class="card-text ">
                        Our global network of strategic locations puts us close to your operations, so you can get the
                        products, service and support you need fast.
                     </p>
                     <div class="">
                        <a href="" class="btn btn-primary">Find a location</a>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>






   <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
      integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
      crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
      integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
      crossorigin="anonymous"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
      integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
      crossorigin="anonymous"></script>
   <script src="./js/extra-jquery.js"></script>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

这里有几个问题,但值得注意的是,您的内容的绝对位置必须正确,以使容器全部折叠起来并具有0高度。这是我更改为至少要显示背景的规则,您可以看到我注释掉了其中的几乎所有内容:

.cta-location .cta-location-text-content {
  /* text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%); */
  color: white;
}

除了position: absolute之外,您还需要采取其他方法来使内容居中。

h1, h2, h3, h4, h5, h6 {
  font-family: 'Teko', sans-serif;
}
html {
  font-size: 18px;
}
@media (min-width: 576px) {
  .container {
    max-width: 540px;
 }
}
@media (min-width: 768px) {
  .container {
    max-width: 720px;
 }
}
@media (min-width: 992px) {
  .container {
    max-width: 960px;
 }
}
@media (min-width: 1200px) {
  .container {
    max-width: 1400px;
 }
}
h1, .h1 {
  font-size: 3.815rem;
}
h2, .h2 {
  font-size: 2.441rem;
}
h3, .h3 {
  font-size: 1.563rem;
}
h4, .h4 {
  font-size: 1.25rem;
}

html, body {
      height: 100%;
  }
  
.cta-location .cta-location-bg {
  background-image: url("https://www.dropbox.com/s/5x8p2z5cvip5u38/chicago.jpg?dl=1");
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
}
.cta-location .cta-location-text-content {
  /* text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%); */
  color: white;
}
<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>slick slider</title>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
   <link href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">

   <link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/css/bootstrap-select.min.css">
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

   <link rel="stylesheet" href="css/style.css">
</head>

<body>

   <div class="my-6">

      <div class="cta-location h-100">
         <div class="bg-dark text-white cta-location-bg">
            <div class="container d-flex align-items-center">
               <div class="row w-100">
                  <!-- <div class="text-left col-lg-4 col-sm-12 cta-location-text-content"> -->
                        <div class="cta-location-text-content">

                     <h2 class="card-title ">
                        Your Success, Without Borders
                     </h2>
                     <p class="card-text ">
                        Our global network of strategic locations puts us close to your operations, so you can get the
                        products, service and support you need fast.
                     </p>
                     <div class="">
                        <a href="" class="btn btn-primary">Find a location</a>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>






   <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
      integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
      crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
      integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
      crossorigin="anonymous"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
      integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
      crossorigin="anonymous"></script>
   <script src="./js/extra-jquery.js"></script>

</body>

</html>

答案 1 :(得分:-1)

height: 100%更改为height: 100vh可以解决您的问题,希望对您有所帮助。谢谢

h1, h2, h3, h4, h5, h6 {
  font-family: 'Teko', sans-serif;
}
html {
  font-size: 18px;
}
@media (min-width: 576px) {
  .container {
    max-width: 540px;
 }
}
@media (min-width: 768px) {
  .container {
    max-width: 720px;
 }
}
@media (min-width: 992px) {
  .container {
    max-width: 960px;
 }
}
@media (min-width: 1200px) {
  .container {
    max-width: 1400px;
 }
}
h1, .h1 {
  font-size: 3.815rem;
}
h2, .h2 {
  font-size: 2.441rem;
}
h3, .h3 {
  font-size: 1.563rem;
}
h4, .h4 {
  font-size: 1.25rem;
}

html, body {
      height: 100%;
  }
  
.cta-location .cta-location-bg {
  background-image: url("https://www.dropbox.com/s/5x8p2z5cvip5u38/chicago.jpg?dl=1");
  height: 100vh;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  position: relative;
}
.cta-location .cta-location-text-content {
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: white;
}
<!DOCTYPE html>
<html lang="en">

<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>slick slider</title>
   <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
      integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
   <link href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet">

   <link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.13.2/css/bootstrap-select.min.css">
   <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

   <link rel="stylesheet" href="css/style.css">
</head>

<body>

   <div class="my-6">

      <div class="cta-location h-100">
         <div class="bg-dark text-white cta-location-bg">
            <div class="container d-flex align-items-center">
               <div class="row w-100">
                  <!-- <div class="text-left col-lg-4 col-sm-12 cta-location-text-content"> -->
                        <div class="cta-location-text-content">

                     <h2 class="card-title ">
                        Your Success, Without Borders
                     </h2>
                     <p class="card-text ">
                        Our global network of strategic locations puts us close to your operations, so you can get the
                        products, service and support you need fast.
                     </p>
                     <div class="">
                        <a href="" class="btn btn-primary">Find a location</a>
                     </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
   </div>






   <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
      integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
      crossorigin="anonymous"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
      integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
      crossorigin="anonymous"></script>
   <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
      integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
      crossorigin="anonymous"></script>
   <script src="./js/extra-jquery.js"></script>

</body>

</html>