简体   繁体   English

Flexbox不将所有项目垂直对齐

[英]Flexbox not aligning all item vertically middle

I'm trying to get everything to be centered inside this grey div . 我正在尝试使所有内容都集中在此灰色div I have Used flexbox which I thought work but then the other content is not slightly in the center except the text - the input form and button are not vertically centered. 我曾经使用过我认为可以工作的flexbox,但是除了文本以外,其他内容不在中间居中-输入表单和按钮未垂直居中。 I assume it could be an external mail chimp styling issue. 我认为这可能是外部邮件黑猩猩的样式问题。

Also, I want to control the width of the input box to be a percentage, but it will not allow. 另外,我想将输入框的宽度控制为百分比,但不允许这样做。 However, fixed works. 但是,固定工程。

Why is it not aligning vertically centered? 为什么它没有垂直居中对齐? And why can't I set a percentage width of the box? 为什么不能设置盒子的百分比宽度?

 #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> 

Add just the following css to align form component vertically, This will solve your issue. 仅添加以下CSS,即可垂直对齐表单组件,这将解决您的问题。

#mc-embedded-subscribe-form{
 vertical-align: middle;
}

vertical align: middle would do the trick. vertical align: middle就能解决问题。

 #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; vertical-align: middle; } #newsletter-button { display: inline; } #mce-EMAIL { background: pink; width: 50%; /* 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> 

One more variant is to add display flex to the form, because form's child elements is not flex elements towards #newsletter-wrap : 另一种变体是向表单添加display flex,因为表单的子元素不是向#newsletter-wrap的 flex元素:

 #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 */ } #mc-embedded-subscribe-form { display: flex; } 
  <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> 

You are using flex to #newsletter-wrap in that case float: left; 在这种情况下,您正在使用flex来#newsletter-wrap float: left; is not needed. 不需要。 You applied flex to the #newsletter-wrap so the properties will be applied only to its children not the elements inside the child. 您已将flex应用于#newsletter-wrap因此属性将仅应用于其子级,而不应用于子级中的元素。 As far as I know vertical-align: middle; 据我所知vertical-align: middle; is a property related to Table-cell . 是与Table-cell相关的属性。 It will work but as you are using flex you should only use the properties related to it. 它将起作用,但是当您使用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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM