高度100%的背景不起作用?

问题描述 投票:0回答:2

堆栈上有很多这样的问题,但答案并没有解决我的问题,正如你所看到的,我试图通过在文本的父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>
html css html5 twitter-bootstrap css3
2个回答
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
投票

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>
© www.soinside.com 2019 - 2024. All rights reserved.