如何放置容器

时间:2018-10-03 08:24:47

标签: html css

我得到了一个问题,并提出了如何将元素放在css中的中心。

我想将标题设置为背景图像的宽度为100%,高度为100%,并在标题中添加一些导航,文本/图像div,如下所示。

@import url('https://fonts.googlrapis.com/css?family=poppins:400,500,700');
html,
body {
  height: 100%;
  width: 100%;
  font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, "sans-serif";
}

.navbar {
  background-color: rgba(38, 38, 38, 0.30);
}

.nav-background {
  background-image: url(http://localhost/newsystem/images/background.jpg);
  repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.header-contain {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  position: relative;
  border: 1px solid red;
}

.contain-one {
  padding: 5px;
  background-color: rgba(0, 116, 217, 0.50);
  position: absolute;
  color: #fff;
  top: 50%;
  left: 50%;
  transform: translate(-50%);
}

.header-contain img {
  position: absolute;
  bottom: 0;
  height: 50%;
}
<body>
  <div class="nav-background">
    <nav class="navbar navbar-expand-lg">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
        </button>

      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
        </ul>
      </div>
    </nav>
    <div class="header-contain">
      <img src="images/umbrella.png">
      <div class="contain-one">
        <h5>Let's start with us</h5>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam, est.</p>
      </div>
    </div>
  </div>
</body>

问题是为什么我的.header-container高度从导航下方开始。我希望.header-container与我的.nav-background图片大小相同。因此,我可以将.header-container img修复到nav-background的div元素的底部。

下面是我现在面临的图像问题。

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以在容器上使用flex并使标头包含增长以填充其余空间,而不是使用高度100%:

@import url('https://fonts.googlrapis.com/css?family=poppins:400,500,700');
* {
  box-sizing: border-box;  /* add this */
}

html,
body {
  height: 100%;
  width: 100%;
  font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, "sans-serif";
  margin:0;              /* add this */
}

.navbar {
  background-color: rgba(38, 38, 38, 0.30);
}

.nav-background {
  background-image: url(http://localhost/newsystem/images/background.jpg);
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: flex;          /* add this */
  flex-direction: column; /* aligns child items in a column */
}

.header-contain {
  width: 100%;
  margin: 0;
  padding: 0;
  position: relative;
  border: 1px solid red;
  flex-grow: 1;           /* add this */
}

.contain-one {
  padding: 5px;
  background-color: rgba(0, 116, 217, 0.50);
  position: absolute;
  color: #fff;
  top: 50%;
  left: 50%;
  transform: translate(-50%);
}

.header-contain img {
  position: absolute;
  bottom: 0;
  height: 50%;
}
<body>
  <div class="nav-background">
    <nav class="navbar navbar-expand-lg">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
        </button>

      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
          </li>
        </ul>
      </div>
    </nav>
    <div class="header-contain">
      <img src="images/umbrella.png">
      <div class="contain-one">
        <h5>Let's start with us</h5>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam, est.</p>
      </div>
    </div>
  </div>
</body>