html - Flexbox 没有将所有元素垂直居中对齐

标签 html css

我试图让所有内容都集中在这个灰色的 div 中。我使用了我认为有效的 flexbox,但是除了文本之外其他内容并没有稍微居中 - 输入表单和按钮没有垂直居中。我认为这可能是外部邮件黑猩猩样式问题。

还有,我想把输入框的宽度控制成百分比,但是也不行。但是,固定工作。

为什么不是垂直居中对齐?为什么我不能设置框的百分比宽度?

#newsletter-wrap {
    width: 100%;
    height: 100px;
    background: lightgrey;
    float: left;
    display: flex;
	justify-content: center;
	align-items: center;
    font-size: 45px;
    font-weight: 300;
}
#newsletter-wrap > *, #newsletter-wrap > form > * {
    display: inline;
    padding: 0;
    margin: 0;
}
#newsletter-button {
    display: inline;
}
#mce-EMAIL {
    background: pink;
    width: 300px; /* want to make it %, not fixed */
}
    <section id="newsletter-wrap">
        Problem.
        
        <form action="https://outsetweb.us17.list-manage.com/subscribe/post?u=8c38151fa33f984127db5705c&amp;id=4c741659e1" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
	<input type="email" placeholder="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">
<div id="mce-responses">
		<div class="response" id="mce-error-response" style="display:none"></div>
		<div class="response" id="mce-success-response" style="display:none"></div>
	</div>
	<input type="submit" value="Subscribe" name="subscribe" id="newsletter-button">
</form>
    
    </section>

最佳答案

你正在使用 flex 来 #newsletter-wrap 在这种情况下 float: left; 是不需要的。您将 flex 应用于 #newsletter-wrap,因此属性将仅应用于其子元素,而不是子元素中的元素。据我所知,vertical-align: middle; 是一个与Table-cell相关的属性。它会起作用,但是当您使用 flex 时,您应该只使用与其相关的属性。

#newsletter-wrap {
  width: 100%;
  height: 100px;
  background: lightgrey;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 45px;
  font-weight: 300;
}

form {
  display: flex;
  align-items: center;
}

#newsletter-button {
  display: inline;
}

#mce-EMAIL {
  background: pink;
  width: 100%;
}
<section id="newsletter-wrap">
  Problem.

  <form action="https://outsetweb.us17.list-manage.com/subscribe/post?u=8c38151fa33f984127db5705c&amp;id=4c741659e1" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" novalidate>
    <input type="email" placeholder="email" value="" name="EMAIL" class="required email" id="mce-EMAIL">

    <div id="mce-responses">
      <div class="response" id="mce-error-response" style="display:none"></div>
      <div class="response" id="mce-success-response" style="display:none"></div>
    </div>

    <input type="submit" value="Subscribe" name="subscribe" id="newsletter-button">
  </form>

</section>

关于html - Flexbox 没有将所有元素垂直居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47014131/

相关文章:

jquery - 如何使div容器在IE11中自动适应其固定大小的子元素

javascript - 防止页面在后退按钮上重新加载(使用缓存)。或者向下滚动用户。 (问题,至少,iPhone)

css - Zurb Foundation 中的垂直对齐内容

html - 站点上的 css float 问题

HTML 选择和选项混合方向(ltr 和 rtl)

html - 如何删除我的无序列表左侧的尴尬边距?

javascript - 使用动画创建分段控件

css - 需要针对不同背景的 Firefox CSS hack

javascript - 退出搜索框事件时如何重置占位符?

html - 使用 css 的多个背景图像的 IE 兼容性问题