如何使用 CSS 将框垂直对齐到中心?

How do I vertically align a box to the center using CSS?

我一直在学习 CSS,现在正在通过尝试复制基本网站进行练习,但我遇到了一个问题!

我正在尝试垂直对齐一个框,使其始终位于中间,如果我使浏览器垂直变小,它会自动缩放。到目前为止,我已经尝试过水平复制我所做的(通常边距:0 auto;)但它不起作用。

我的相关 HTML 和 CSS 到目前为止看起来像这样:

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html{
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body{
  background-color: #f9f9f9;
  height: 100%;
}

.container{
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  
  min-height: 100px; 
  height: 100%;  
  
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
}


header{
  border: solid;
  min-height: 100px;
  margin: auto 5%;
}
<body>
    
    <div class="container">
      
      <header>
        
        <h1>Jon Phillips</h1>
        <p>User Interface Designer</p>
        
        <nav class="contact">
          
          <a href="mailto:jon@jonphillips.ca" target="_blank"><p>Contact</p></a>

        </nav>
      
      </header>
      
    </div>

  </body>

我正在显示边框,以便我可以看到发生了什么,并确保(如水平居中)我的边距需要是自动的。当我水平地完成这个时,它工作得很好......

有人对做什么有建议吗?

提前致谢!

Flexbox 是你的朋友!您需要将以下内容添加到您的容器样式中

  display: flex;
  align-items: center;
  justify-content: center;

我们使用 display: flex; 将显示设置为 flex/flexbox 并使用 align-items: center;justify-content: center;

将所有内容对齐到中心

所有供应商前缀看起来像:

  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html{
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body{
  background-color: #f9f9f9;
  height: 100%;
}

.container{
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  
  min-height: 100px; 
  height: 100%;  
  
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
  
   display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
       -ms-flex-align: center;
          align-items: center;
    -webkit-box-pack: center;
       -ms-flex-pack: center;
     justify-content: center;
}


header{
  border: solid;
  min-height: 100px;
  margin: auto 5%;
}
<body>
    
    <div class="container">
      
      <header>
        
        <h1>Jon Phillips</h1>
        <p>User Interface Designer</p>
        
        <nav class="contact">
          
          <a href="mailto:jon@jonphillips.ca" target="_blank"><p>Contact</p></a>

        </nav>
      
      </header>
      
    </div>

  </body>

遗憾的是,自动边距技巧仅适用于水平方向。在CSS3中,你可以使用另一个技巧。您从 top: 50%; 开始,然后使用 transform: translateY(-50%); 减去容器高度的一半。 perspective(1px) 只是为了更正整个像素的计算并防止某些浏览器出现问题。查看我添加到您的 CSS:

的三行

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html {
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body {
  background-color: #f9f9f9;
  height: 100%;
}

.container {
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  min-height: 100px;
  height: 100%;
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
}

header {
  border: solid;
  min-height: 100px;
  margin: auto 5%;
  position: relative;
  top: 50%;
  transform: perspective(1px) translateY(-50%);
}
<body>
  <div class="container">
    <header>
      <h1>Jon Phillips</h1>
      <p>User Interface Designer</p>
      <nav class="contact">
        <a href="mailto:jon@jonphillips.ca" target="_blank">
          <p>Contact</p>
        </a>
      </nav>
    </header>
  </div>
</body>

在您尝试居中的项目的父项上,您可以只使用 display: flex; 并且子项应该在内部居中(使用您已经设置的其余样式)。

这也可以使用绝对定位来实现,尽管会多几行:

 .parent {
      position: relative;
 }

 .child {
      /* Vertically center - will still need left / right & width adjustment otherwise */
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
 }

*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* Global Values */

html{
  font-family: "Arial";
  font-size: 16px;
  line-height: 1.2;
  height: 100%;
}

body{
  background-color: #f9f9f9;
  height: 100%;
}

.container{
  display: block;
  min-width: 240px;
  max-width: 768px;
  width: 100%;
  display: flex;
  
  /*----------------------------------------------*/
  /* This is to make sure that the container height is always the same size as the browser. */
  
  min-height: 100px; 
  height: 100%;  
  
  /*----------------------------------------------*/
  margin: 0 auto;
  text-align: center;
  border-style: solid;
}


header{
  border: solid;
  min-height: 100px;
  margin: auto 5%;
  width: 100%;
}
<body>
    
    <div class="container">
      
      <header>
        
        <h1>Jon Phillips</h1>
        <p>User Interface Designer</p>
        
        <nav class="contact">
          
          <a href="mailto:jon@jonphillips.ca" target="_blank"><p>Contact</p></a>

        </nav>
      
      </header>
      
    </div>

  </body>

有不同的方法来解决这个问题,最简单且兼容性好的是使用 "table-cell display",所以在你的情况下将它添加到你的 CSS:

.container {
  display:table-cell;
  height:100%;
  vertical-align:middle
}

关键是 table-cell 的正确实现应该将父元素视为 table,您的父元素是正文本身,因此请更改您的 CSS 进入:

body{
  background-color: #f9f9f9;
  height: 100%;
  width:100%;
  display:table
}

http://jsfiddle.net/9tghnydg/

虽然 .container 上的高度适当并不是绝对必要的,但它作为主体的唯一单元格 table:

.container {
  display:table-cell;
  vertical-align:middle
}

http://jsfiddle.net/9tghnydg/1/

检查这个:https://codepen.io/danieldd/pen/pdooVN

<div class="container">
  <div class="item-centered"></div>
</div>
.container {
  height: 400px;
  width: 100%;
  background-color: cyan;

  display: flex; /*this is needed for centering*/
  align-items: center; /*this center vertically childred elements*/
  justify-content: center; /*this center horizontally childred elements*/
}

.item-centered {  
  height: 200px;
  width: 200px;
  background-color: red;
}