html - 高度 100 百分比背景不起作用?

标签 html twitter-bootstrap css

堆栈上有很多这样的问题,但答案并不能解决我的问题,如您所见,我正在尝试通过在文本的父 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>

最佳答案

这里有几个问题,但值得注意的是您的内容是绝对定位的,因此容器全部折叠并且高度为 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>

关于html - 高度 100 百分比背景不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55401753/

相关文章:

css - 页脚漂浮在 div 上

javascript - 如何使用 JavaScript/jQuery 选择/取消选择带有复选框的选择选项?

jquery - 打包 jquery 和 bootstrap

html - Font Awesome 图标元素空间有不需要的重叠

javascript - 如何在 HTML 属性内的函数调用中将变量作为参数传递?

c# - MVC/HTML - 当 HTML 位于文本区域时不会触发输入提交

javascript - bootboxjs 无效的提示类型 - 文档中的示例不起作用

javascript - Bootstrap 进度的 Jquery 动画宽度百分比不起作用

javascript - JQuery悬停切换相关选择器

javascript - 当总宽度超过 100% 时,表格单元格将隐藏