【问题标题】:Vertically centering in flex-direction: column在 flex-direction 中垂直居中:column
【发布时间】:2020-07-26 05:52:36
【问题描述】:

我正在尝试使用 Flexbox 将页面左侧的元素垂直居中,但无法弄清楚。它们是通过flex-direction: column 组织的,我不确定在设置时如何水平居中。请参阅下面的代码以及到目前为止我网站的实时版本。我已经将两个 flex 容器分成左右两部分,这样我就可以分别处理和居中它们。我现在只处理左侧。所以,我将首先展示它的 CSS。

我尝试过align-itemsjustify-contentalign-self,但这些都没有奏效。

谢谢,提前!

直播网站:https://huddle-single-landing-page.jordanchude.now.sh/

HTML

<div class="container">
<div class="left-side">
  <img id="huddle" src="https://i.ibb.co/FnJS8vM/logo.png" alt="logo" border="0">
  <img id="illustration" src="https://i.ibb.co/L9HBmDZ/illustration-mockups.png" alt="illustration-mockups" border="0">
</div>
<div class="right-side">
  <p id="headline">Build The Community Your Fans Will Love</p>

  <p id="subtitle">
          Huddle re-imagines the way we build communities. You have a voice, but so does your audience. 
          Create connections with your users as you engage in genuine discussion.
  </p> 

  <button>Register</button>

  <div class="social-icons">
    <span class="fa-stack fa-lg">
        <i class="far fa-circle fa-stack-2x"></i>
        <i class="fab fa-facebook-f fa-1x"></i>
    </span>
    <span class="fa-stack fa-lg">
        <i class="far fa-circle fa-stack-2x"></i>
        <i class="fab fa-twitter fa-1x"></i>
    </span>
    <span class="fa-stack fa-lg">
        <i class="far fa-circle fa-stack-2x"></i>
        <i class="fab fa-instagram fa-1x"></i>
    </span>
  </div>
</div>

CSS

 .container {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    flex-wrap: wrap;
}

/* Left Side */
.left-side {
    display: flex;
    flex-direction: column; 
    width: 800px;
}

#huddle {
    width: 200px;
    align-self: flex-start; 
}

#illustration {
   width: 100%; 
}

【问题讨论】:

  • 我不明白你想要的样子。请提供示例或说明以使其清楚。

标签: html css flexbox centering


【解决方案1】:

您的容器中没有额外的高度,因此没有用于居中的可用空间。

进行此调整:

.container {
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    /* align-items: center */ /* vertically centers both left and right elements */
    flex-wrap: wrap;
    height: 100vh; /* child would inherit height through align-content: stretch default */
}

.left-side {
    display: flex;
    flex-direction: column;
    width: 800px;
    align-self: center; /* vertically centers the flex item */        
    /* justify-content: center; */ /* vertically centers the content of the flex item */
}

【讨论】:

  • 太棒了,我能够把所有东西都放在中心。有没有办法只集中某些项目?例如,我希望“Huddle”保持在左上角,但它会随着下面的插图移动。
  • 如果您从left-side 中删除除width: 800px 之外的所有内容,然后添加display: grid,这似乎可行。
  • 知道了。我看到这两个元素现在通过网格分开,但我仍然无法让“Huddle”悬停在容器左上角附近的插图上方。它实际上不需要位于视口的左上角,特别是因为我希望它与插图的左侧对齐。它只需要向上移动。我尝试使用 margin-bottom,但这会使整个页面变大并将所有内容向下推。
  • 我以为你指的是容器的左上角。你的意思是视口的左上角?
  • 抱歉,当你发送你的评论时,我正在编辑我的评论。正确,容器的左上角。
猜你喜欢
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 2018-11-04
相关资源
最近更新 更多