简体   繁体   English

Flexbox CSS垂直对齐方式未完全居中

[英]Flexbox CSS Vertical Alignment not Completely Central

I've used flexbox before for vertical alignment, but for the elements I'm using it on at the moment the alignment is very off-centre, particularly for the div in the JSFiddle example below, where adding a button to the same div that contains the rest of the text is making the alignment too low. 我之前使用过flexbox进行垂直对齐,但是对于当前对齐时我正在使用的元素,它们的对齐方式非常偏离中心,尤其是对于下面的JSFiddle示例中的div而言,在该div中向该div添加一个按钮包含其余文本会使对齐方式过低。

Code: 码:

 #servicesGrid { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .service { width: 24.85%; height: 45%; background: #262626; display: block; float: left; margin: 0 0.2% 0 0; text-align: center; } #servicesGrid .service:nth-of-type(1) { width: 100%; margin: 0 0 0.2% 0; height: 80%; } #servicesGrid .service:nth-of-type(5) { margin-right: 0; } .serviceContent { margin: 0; width: 100%; height: 100%; display: flex; align-items: center; color: #fff; text-align: center; } .button { color: #fff; border: 2px solid #fff; border-radius: 15px; padding: 10px 20px 10px 20px; text-transform: uppercase; font-family: 'Effra-Regular'; text-decoration: none; transition: all linear 0.3s; -webkit-transition: all linear 0.3s; -moz-transition: all linear 0.3s; } .button:hover { color: #000; background: #fff; } 
 <div id="servicesGrid"> <div class="service"> <div class="serviceContent"> <div style="margin-left:auto; margin-right:auto"> <h1 style="font-size:40px; font-family:'Effra-Light'; margin-bottom:-10px">Service</h1> <h4>Lorem ipsum dolor sit amet</h4> <br/> <a href="#contactUs" class="button" alt="Find Out More Button">Find Out More</a> </div> </div> </div> </div> 

JSFiddle: https://jsfiddle.net/f92udw9v/2/ . JSFiddle: https ://jsfiddle.net/f92udw9v/2/。

You have a few issues there: 您那里有一些问题:

  1. Extra padding and margin (your button runs outside the .serviceContent). 额外的填充和边距(您的按钮在.serviceContent外部运行)。

  2. The H1 at the top has a margin-top/bottom (probably user agent values) that distances it from the top of the .serviceContent. 顶部的H1具有距顶部/底部(可能是用户代理值)的边距,使其与.serviceContent的顶部保持一定距离。

Have a look at the changes I made to your jsfiddle . 看看我对您的jsfiddle所做更改

          <div style="margin-left:auto; margin-right:auto;border: 1px solid yellow;"> 

            <h1 style="font-size:40px; font-family:'Effra-Light';margin-top: 0px;">Service</h1>

            <h4>Lorem ipsum dolor sit amet</h4>

            </br>

            <a href="#contactUs" class="button" alt="Find Out More Button" style="display: inline-block;">Find Out More</a>

        </div>

What I did was: 我所做的是:

  1. Set the link () display to inline-block 将link()显示设置为inline-block
  2. Removed the top margin on H1 删除了H1的上边距

This still feels a bit of a hack to me, yet works slightly better. 对我来说,这仍然有些破绽,但效果略好一些。 You might want to consider to using a column based flex box for the content (with the proper spacing) without element level margin/padding. 您可能要考虑对内容(具有适当的间距)使用基于列的弹性框,而没有元素级别的边距/填充。

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

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