垂直居中容器内的许多div之一

时间:2017-06-13 19:19:30

标签: css

一个部分包含一个导航组,其中元素浮动到两侧,中心文本组和背景图像。未手动设置截面高度以适合背景图像。 如何垂直居中文本组?它是div.h1-container

Plunkr:https://plnkr.co/edit/rSWgdfPQEbIeXbPh4wO1?p=preview

body {
  font: 15px/1.5 Arial, Helvetica, sans-serif;
  padding: 0;
  margin: 0;
  background-color: #f4f4f4;
}


/* Global */

.container {
  width: 80%;
  margin: auto;
  overflow: hidden;
}

.clear {
  clear: both;
}

ul {
  margin: 0;
  padding: 0;
}

section h2 {
  line-height: 1.0;
}

.button_1 {
  height: 34px;
  background: tomato;
  border: 0;
  padding-left: 20px;
  padding-right: 20px;
  color: #ffffff;
  width: 200px;
}

.showcase-nav a {
  color: #ffffff;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 16px;
}

.showcase-nav {
  padding-top: 30px;
  margin-top: 10px;
  float: right;
}

.showcase-nav li.social i {
  padding: 0 4px;
}

.showcase-nav li {
  display: inline;
  padding: 0 20px 0 20px;
}

.showcase-branding {
  float: left;
}

.showcase-branding h1 {
  font-size: 26px;
  margin: 0;
  padding-top: 30px;
  color: #fff;
}

.showcase-branding .highlight {
  color: #e8491d;
  font-weight: bold;
}

.showcase-nav a:hover {
  color: #cccccc;
  font-weight: bold;
}

.h1-container {
  width: 70%;
  margin: auto;
}

.h1-container h1 {
  text-transform: uppercase;
  text-align: center;
  color: #fff;
}

.h1-container p {
  width: 81%;
  background: tomato;
  margin: auto;
  border-radius: 10px;
  text-align: center;
  color: #fff;
}


/* Showcase */

#showcase {
  background: url('http://i68.tinypic.com/2j5jho4.png') no-repeat;
  background-size: contain;
  width: 100%;
  display: inline-block;
  position: relative;
  margin: 0 auto;
}

#showcase:after {
  padding-top: 51.46%;
  display: block;
  content: '';
}

#showcase .container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}

#showcase .h1-container h1 {
  font-size: 55px;
  margin-bottom: 10px;
}

#showcase p {
  font-size: 20px;
  padding: 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<section id="showcase">
  <div class="container">
    <div class="showcase-branding">
      <h1>Title</h1>
    </div>
    <nav class="showcase-nav">
      <ul>
        <li class="current"><a href="about.html">About</a></li>
        <li><a href="contact.html">Contact</a></li>
        <li class="social"><a href=""><i class="fa fa-linkedin-square"></i></a><a href=""><i class="fa fa-facebook-official"></i></a><a href=""><i class="fa fa-twitter"></i></a></li>
      </ul>
    </nav>
    <div class="clear"></div>
    <div class="h1-container">
      <h1>Text on line 1<br>text on line 2</h1>
      <p>lorem ipsum lorem ipsum lorem ipsum lorem ipsum <i class="fa fa-caret-right"></i></p>
    </div>
  </div>
</section>

1 个答案:

答案 0 :(得分:2)

鉴于目前的情况,有两种简单的方法可以解决这个问题。

一种方法是提供.h1-container display: flexflex-direction: columnjustify-content: center

这是jsfiddle

但是,如果您这样做,.h1-container中放置的每个元素和每个新元素最终都会垂直堆叠并垂直居中于.h1-container,因此它将显示为如果.h1-container没有正确居中,则子元素不居中。

还要记住,justify-content始终将子元素相对于弹性框的方向定位。因此,如果未明确设置弹性框方向,并且由于默认值为flex-direction: rowjustify-content将水平放置子项。 align-items将始终与弹性框的方向属性相反。

body {
  font: 15px/1.5 Arial, Helvetica, sans-serif;
  padding: 0;
  margin: 0;
  background-color: #f4f4f4;
}


/* Global */

.container {
  width: 80%;
  margin: auto;
  overflow: hidden;
}

.clear {
  clear: both;
}

ul {
  margin: 0;
  padding: 0;
}

section h2 {
  line-height: 1.0;
}

.button_1 {
  height: 34px;
  background: tomato;
  border: 0;
  padding-left: 20px;
  padding-right: 20px;
  color: #ffffff;
  width: 200px;
}

.showcase-nav a {
  color: #ffffff;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 16px;
}

.showcase-nav {
  padding-top: 30px;
  margin-top: 10px;
  float: right;
}

.showcase-nav li.social i {
  padding: 0 4px;
}

.showcase-nav li {
  display: inline;
  padding: 0 20px 0 20px;
}

.showcase-branding {
  float: left;
}

.showcase-branding h1 {
  font-size: 26px;
  margin: 0;
  padding-top: 30px;
  color: #fff;
}

.showcase-branding .highlight {
  color: #e8491d;
  font-weight: bold;
}

.showcase-nav a:hover {
  color: #cccccc;
  font-weight: bold;
}

.h1-container {
  width: 70%;
  margin: auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.h1-container h1 {
  text-transform: uppercase;
  text-align: center;
  color: #fff;
}

.h1-container p {
  width: 81%;
  background: tomato;
  margin: auto;
  border-radius: 10px;
  text-align: center;
  color: #fff;
}


/* Showcase */

#showcase {
  background: url('http://i68.tinypic.com/2j5jho4.png') no-repeat;
  background-size: contain;
  width: 100%;
  display: inline-block;
  position: relative;
  margin: 0 auto;
}

#showcase:after {
  padding-top: 51.46%;
  display: block;
  content: '';
}

#showcase .container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}

#showcase .h1-container h1 {
  font-size: 55px;
  margin-bottom: 10px;
}

#showcase p {
  font-size: 20px;
  padding: 5px;
}
  <body>
    <section id="showcase">
			<div class="container">
        <div class="showcase-branding">
          <h1>Title</h1>
        </div>
        <nav class="showcase-nav">
          <ul>
            <li class="current"><a href="about.html">About</a></li>
            <li><a href="contact.html">Contact</a></li>
            <li class="social"><a href=""><i class="fa fa-linkedin-square"></i></a><a href=""><i class="fa fa-facebook-official"></i></a><a href=""><i class="fa fa-twitter"></i></a></li>
          </ul>
        </nav>
        <div class="clear"></div>
        <div class="h1-container">
          <h1>Text on line 1<br>text on line 2</h1>
          <p>lorem ipsum lorem ipsum lorem ipsum lorem ipsum <i class="fa fa-caret-right"></i></p>
        </div>
      </div>
    </section>
  </body>

另一种方法是提供.container position: relative,然后提供.h1-container position: absolutetop: 50%transform: translateY(-50%)

.container定位为相对允许您将.h1-container.container相对于整个文档进行绝对定位。

.container {
  position: relative;
}

.h1-container {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

这是jsfiddle

以下内联版本由于所有内容的尺寸而无法正常显示,但至少以.h1-container为中心。

body {
  font: 15px/1.5 Arial, Helvetica, sans-serif;
  padding: 0;
  margin: 0;
  background-color: #f4f4f4;
}


/* Global */

.container {
  width: 80%;
  margin: auto;
  overflow: hidden;
  position: relative;
}

.clear {
  clear: both;
}

ul {
  margin: 0;
  padding: 0;
}

section h2 {
  line-height: 1.0;
}

.button_1 {
  height: 34px;
  background: tomato;
  border: 0;
  padding-left: 20px;
  padding-right: 20px;
  color: #ffffff;
  width: 200px;
}

.showcase-nav a {
  color: #ffffff;
  text-decoration: none;
  text-transform: uppercase;
  font-size: 16px;
}

.showcase-nav {
  padding-top: 30px;
  margin-top: 10px;
  float: right;
}

.showcase-nav li.social i {
  padding: 0 4px;
}

.showcase-nav li {
  display: inline;
  padding: 0 20px 0 20px;
}

.showcase-branding {
  float: left;
}

.showcase-branding h1 {
  font-size: 26px;
  margin: 0;
  padding-top: 30px;
  color: #fff;
}

.showcase-branding .highlight {
  color: #e8491d;
  font-weight: bold;
}

.showcase-nav a:hover {
  color: #cccccc;
  font-weight: bold;
}

.h1-container {
  width: 70%;
  margin: auto;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.h1-container h1 {
  text-transform: uppercase;
  text-align: center;
  color: #fff;
}

.h1-container p {
  width: 81%;
  background: tomato;
  margin: auto;
  border-radius: 10px;
  text-align: center;
  color: #fff;
}


/* Showcase */

#showcase {
  background: url('http://i68.tinypic.com/2j5jho4.png') no-repeat;
  background-size: contain;
  width: 100%;
  display: inline-block;
  position: relative;
  margin: 0 auto;
}

#showcase:after {
  padding-top: 51.46%;
  display: block;
  content: '';
}

#showcase .container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
}

#showcase .h1-container h1 {
  font-size: 55px;
  margin-bottom: 10px;
}

#showcase p {
  font-size: 20px;
  padding: 5px;
}
<body>
    <section id="showcase">
			<div class="container">
        <div class="showcase-branding">
          <h1>Title</h1>
        </div>
        <nav class="showcase-nav">
          <ul>
            <li class="current"><a href="about.html">About</a></li>
            <li><a href="contact.html">Contact</a></li>
            <li class="social"><a href=""><i class="fa fa-linkedin-square"></i></a><a href=""><i class="fa fa-facebook-official"></i></a><a href=""><i class="fa fa-twitter"></i></a></li>
          </ul>
        </nav>
        <div class="clear"></div>
        <div class="h1-container">
          <h1>Text on line 1<br>text on line 2</h1>
          <p>lorem ipsum lorem ipsum lorem ipsum lorem ipsum <i class="fa fa-caret-right"></i></p>
        </div>
      </div>
    </section>
  </body>