【问题标题】:How do I "vertically center" two flex items?我如何“垂直居中”两个弹性项目?
【发布时间】:2017-01-07 04:10:11
【问题描述】:

这是我的项目的link

可以这么说,我希望他们两个都“居中”。但是,是的,我知道他们都可以占据 1 个位置。所以我认为是否有办法绘制一条垂直居中的(不可见的)水平线。然后,我可以将一个弹性项目放在这条线的正上方,另一个放在它的正下方。

但我认为有一种更好的方法可以用于居中超过 2 个弹性项目。

body{
  background:#4A6556;
  
}


body>div>hello,hey{
  background: #CCC;
  border-radius: 7px;
  margin: 5px;
  padding: 20px;
}

body>div{
  display: flex;
  flex: row wrap;
  flex-direction: column;
}

body>div>hey{
  order:2;
  
  font-family: 'Lato', sans-serif;
  color:#212121;
  
}
body>div>hello{
  order:1;
  
  font-family: 'Kumar One', cursive;
  color: #938653;
}

hello,
hey{
  text-align: center;
}
<link href="https://fonts.googleapis.com/css?family=Kumar+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kumar+One|Lato" rel="stylesheet">

<body>
  <div>
<hello>Welcome!</hello>
<hey>This is my portfolio page.</hey>
   </div>
</body>

【问题讨论】:

  • 如果使用类,则不需要使用nth-child ...
  • 我想要整个屏幕页面的高度。不管那是什么。
  • @Santi 那些元素是有效的并且被称为Custom Elements
  • @LGSon 是否在 JavaScript 中未定义的自定义元素上运行默认行为?我的意思是,他们是否在没有定义的情况下进行验证?
  • @AndreiGheorghiu 我不记得争论过,我相信我问过你“如果不需要,为什么要使用自定义标签?”期待一个实际的答案,这不是修辞。你似乎对这个话题很了解,所以我想你可以提供一些见解。不要那么自卫,这是一次谈话,而不是争论。 LGSon - 有趣,谢谢。我必须对它们进行一些研究!

标签: css flexbox


【解决方案1】:

您的问题尚不完全清楚,您希望这两个项目并排one-on-top-of-the-other显示。

A.并排

.va-twins {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.va-twins > * {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

body {
  margin: 0; padding: 0; // SO reset, you don't need this.
}
<div class="va-twins">
  <hello>Welcome!</hello>
  <hey>This is my portfolio page.</hey>
</div>

如果您需要,这里是完整的前缀代码(在本文发布时接近完整的浏览器兼容性97.38%

.va-twins {
  min-height: 100vh;
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
       -moz-box-pack: center;
        -ms-flex-pack: center;
            justify-content: center;
    -webkit-box-align: center;
    -webkit-align-items: center;
       -moz-box-align: center;
        -ms-flex-align: center;
            align-items: center;
  
}
.va-twins > * {
  -webkit-box-flex: 1;
  -webkit-flex-grow: 1;
     -moz-box-flex: 1;
      -ms-flex-positive: 1;
          flex-grow: 1;
    display: -webkit-box;
    display: -webkit-flex;
    display: -moz-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -webkit-flex-direction: column;
       -moz-box-orient: vertical;
       -moz-box-direction: normal;
        -ms-flex-direction: column;
            flex-direction: column;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
       -moz-box-pack: center;
        -ms-flex-pack: center;
            justify-content: center;
    -webkit-box-align: center;
    -webkit-align-items: center;
       -moz-box-align: center;
        -ms-flex-align: center;
            align-items: center;
}
<div class="va-twins">
  <hello>Welcome!</hello>
  <hey>This is my portfolio page.</hey>
</div>

B.一个在另一个之上

.va-twins {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: stretch;
}
.va-twins > * {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

body {
  margin: 0; padding: 0; // SO reset, you don't need this.
}
<div class="va-twins">
  <hello>Welcome!</hello>
  <hey>This is my portfolio page.</hey>
</div>

全前缀:

.va-twins {
  min-height: 100vh;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
     -moz-box-orient: vertical;
     -moz-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: stretch;
  -webkit-align-items: stretch;
     -moz-box-align: stretch;
      -ms-flex-align: stretch;
          align-items: stretch;
}
.va-twins > * {
  -webkit-box-flex: 1;
  -webkit-flex-grow: 1;
     -moz-box-flex: 1;
      -ms-flex-positive: 1;
          flex-grow: 1;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -webkit-flex-direction: column;
     -moz-box-orient: vertical;
     -moz-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
     -moz-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}
<div class="va-twins">
  <hello>Welcome!</hello>
  <hey>This is my portfolio page.</hey>
</div>

您可以使用@media queries 在不同的屏幕尺寸上切换A.B.

【讨论】:

  • > * 是什么意思?你有 display、justify-content 和 align-items 重复了两次。为什么?
  • 这就是为什么:w3.org/TR/CSS21/selector.html%23id-selectors &gt; 的意思是“直接的孩子”。所以&gt; * 适用于&gt; 之前的选择器的任何直接子代。这是基本的CSS
  • @user132522 我重新阅读了您的问题,但我不确定您是否希望它们并排显示或一个在另一个之上。我也编辑了答案以涵盖另一个选项。
  • 该网站说 >* 针对某物的孙子的孩子。你还能简要解释组件吗?谢谢
  • @user132522 我链接了官方文档,因为这是我有疑问时通常会去的地方。在掌握基础知识之前,您可能会发现 this resource 更有帮助。但请记住,官方是 w3.org。这就是源头。
猜你喜欢
  • 2017-06-11
  • 1970-01-01
  • 2017-11-28
  • 2015-03-31
  • 1970-01-01
  • 2016-01-27
  • 2014-12-29
  • 2016-10-28
相关资源
最近更新 更多