【问题标题】:Centering inline-block DIV with CSS ":before" ruins text centering both vertical and horizontal使用 CSS ":before" 居中 inline-block DIV 会破坏垂直和水平居中的文本
【发布时间】:2016-07-12 18:41:53
【问题描述】:

大家好,我已经将一些基于 div 的按钮设置为居中,但在居中文本时出现了一些视觉故障(它不是真正居中):

#slide2
{
 text-align: center;
 text-align-last: center;	
 -moz-text-align-last: center;
}

#container2
{
height:100%;
}

.pagesContainer
{

        width:100%;
        height:50px;
        top:0%;
 }


.pageButtonContent
{
	width: 38px;
	height: 38px;
	background-color:white;
	border: solid 1px black;
	-webkit-transition: all .5s;
	-moz-transition: all .5s;
	transition: all .5s;
    display: inline-block;
    font-family: 'Century Gothic', CenturyGothic, AppleGothic, sans-serif;
     font-size: 20px;
     color:darkgray;
     border-radius: 50%;
     

}

.pageButtonContent:before {
  content: '';
  display: inline-block;
  vertical-align: middle;
  text-align: center;
  height:42px;
}


.pagesContainer:before {
  content: "";
  display: inline-block;
  vertical-align: middle;
  height: 100%;
}
<div class='Slide' id='slide2' onscroll='changePosition()'>
	<div id = "container2" class="containert">

	<div id='' class='pagesContainer'>
		
			<div id='previous' class='pageButtonContent'>
      1
			</div>
		
		
		
			<div id='' class='pageButtonContent'>
			2
			</div>
		
	
	
			<div id='' class='pageButtonContent'>
			3
			</div>
		
		
	 	
			<div id='next' class='pageButtonContent'>
      4
			</div>
		

	</div>
</div>
</div>

我知道有很多不同的方法可以做到这一点,但到目前为止,这种设置是唯一有效的方法,我不想回去尝试解释所有内容以保持主题。

由于某种原因,在应用 css :before 后,居中的文本似乎有点不对劲。看起来我通过设置 42px 高度(它的容器是 50px 高)设法垂直修复它,但我还没有弄清楚如何水平修复它,你们有什么想法吗?

【问题讨论】:

    标签: javascript html css centering


    【解决方案1】:

    看来你真的不需要 :before 来让它工作。 您可以添加 line-height 以使数字垂直居中:

    .pageButtonContent { line-height: 38px;}
    

    【讨论】:

    • 我真的很想将其标记为答案,但它不是 100% 完美,它仍然相差一个像素。让我们拭目以待,看看是否有人能弄清楚我做错了什么。
    • 您应该尝试使用不同的值,直到它适合您。使用 line-height 而不是 :before 来垂直调整文本是合乎逻辑的做法。
    • 你是对的,问题实际上是字体平滑。如果你看图片,你会注意到它。如果没有 inline-block,它的呈现方式会有所不同。谢谢你。 imgur.com/fgvrrSa
    猜你喜欢
    • 2015-08-29
    • 1970-01-01
    • 2012-03-14
    • 2017-06-28
    • 1970-01-01
    • 2013-10-05
    • 2012-01-07
    • 2012-01-01
    相关资源
    最近更新 更多