简体   繁体   English

垂直居中的伪元素文本

[英]Vertically centering pseudo element text

I've been trying to figure out how to vertically center an ::after pseudo-element that contains text (a content attribute). 我一直在尝试找出如何垂直放置包含文本( content属性)的::after伪元素。

Previous questions : there are some previous questions which address this, but none of the solutions seem to work for this simple case. 先前的问题 :之前有一些问题可以解决这个问题 ,但是似乎没有一个解决方案适用于这种简单情况。

Here I basically have a simple DIV that should function as a "next" button. 在这里,我基本上有一个简单的DIV ,应将其用作“下一步”按钮。 It contains a simple right arrow (Unicode 0x203A ). 它包含一个简单的向右箭头(Unicode 0x203A )。

The markup is basically just: 标记基本上只是:

<div class = "next-button"></div>

And the CSS is: CSS是:

.next-button {
    position: absolute;
    height: 70px;
    line-height: 70px;
    text-align: center;
    background: black;
    cursor: pointer;
}

.next-button::after {
    content: "\203A";
    color: white;
    font-size: 3em;
    line-height: 0;

} }

JSFiddle link: https://jsfiddle.net/me6a3nq2/ JSFiddle链接: https ://jsfiddle.net/me6a3nq2/

So, I've been trying various things to center this. 因此,我一直在尝试各种方法来解决这个问题。 Firstly, vertical-align:middle doesn't work here. 首先, vertical-align:middle在这里不起作用。 Another CSS trick is to set the line-height of the parent element equal to the height of the parent element. 另一个CSS技巧是将父元素的line-height设置为等于父元素的height However, this doesn't seem to work either. 但是,这似乎也不起作用。

I can add position:relative to next-button::after , and then adjust top accordingly, but this won't exactly center the arrow unless I know the exact height of the arrow, which may change or vary across browsers. 我可以将position:relative添加到next-button::after ,然后相应地调整top ,但是除非我知道箭头的确切高度(这在浏览器中可能会发生变化或变化),否则它不会使箭头居中。

I also tried using a common CSS method, which is to use translateY , as specified here . 我还尝试使用一种常见的CSS方法,即使用此处指定的 translateY I usually have very good success with this method: 我通常用这种方法取得很好的成功:

.next-button::after {
    content: "\203A";
    color: white;
    font-size: 3em;
    line-height: 0;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

But even this doesn't work with the pseudo element. 但这对伪元素也不起作用。 It appears to move the arrow, but it places it off-centered towards the bottom, at least on Chrome: 它似乎在移动箭头,但至少在Chrome上使它偏心于底部。

在此处输入图片说明

I also notice, when using the Chrome Inspector tool, that the arrow itself, which is basically just a character of text, seems to have "padding" automatically above and below it, and that the "above" padding is seemingly larger than the below padding, causing the arrow to be uncentered vertically: 我还注意到,在使用Chrome Inspector工具时,箭头本身(基本上只是文本的一个字符)似乎在其上方和下方自动具有“填充”,并且“上方”填充似乎大于下方的填充填充,导致箭头在垂直方向上未居中:

在此处输入图片说明

...but, I can't really tell why this padding exists, nor do I seem to have any control of it. ...但是,我无法真正说出为什么存在这种填充,也似乎无法控制它。 I thought it might be line-height , so I set line-height to 0 , but that had no effect. 我以为可能是line-height ,所以我将line-height设置为0 ,但是没有效果。

So, I'm out of ideas. 所以,我没主意了。 I'm thinking I may need to give up on using a pseudo element and just make the arrow inside a nested DIV. 我在想我可能需要放弃使用伪元素,而只是将箭头放在嵌套的DIV中。

Question: Is there any way to vertically align a pseudo element? 问题:有什么方法可以垂直对齐伪元素? (And also, what is the cause of the "padding" above and below of the text in the pseudo element content ?) (而且,伪元素content本上方和下方的“填充”是什么原因?)

If it's OK for you, set line-height: 65px; 如果可以,请设置line-height: 65px; :

That's how I do it in my Projects. 这就是我在专案中做的方式。 But if the height changes you will have to change the line-height as well. 但是,如果高度发生变化,则还必须更改行高。 The problem is that these Symbols are not verticly centered at their center of look, but on the baseline for text. 问题在于这些符号不是垂直位于其外观中心,而是位于文本的基线上。 I don't think there is a better solution to this. 我认为没有更好的解决方案。

 .next-button { position: absolute; height: 70px; line-height: 70px; text-align: center; background: black; cursor: pointer; } .next-button::after { font-family: Arial; content: "\\203A"; color: white; font-size: 3em; line-height: 65px; } 
 <div class="next-button"></div> 

Is there any way to vertically align a pseudo element? 有什么办法可以垂直对齐伪元素?

Yes, and all of the ways you describe actually work. 是的,您描述的所有方式都有效。

Take for example the transform method - just add a border to the character - and you can clearly see that the character - which includes the entire area in the red border - is centered (Fiddle Demo) transform方法为例-仅在字符上添加边框-您就可以清楚地看到字符-包括红色边框中的整个区域-居中(小提琴演示)

So the problem here is that the character glyph itself isn't centered - and that's probably for a good reason - just like an underscore character isn't centered either. 因此,这里的问题是字符字形本身不居中-这可能是有充分的理由-就像下划线字符也不居中一样。

So this actually has nothing to do with pseudo elements but rather the specific character you have chosen to center. 因此,这实际上与伪元素无关,而与您选择居中的特定字符无关。

You'll either have to manually adjust the character so that it centers OR use a different character for your arrow. 你要么必须使其中心手动调整字符使用不同的字符为您的箭头。

Although line-height will do the job, I found out another solution.!! 尽管line-height可以胜任,但我发现了另一种解决方案。

Just give vertical-align:sub to the pseudo element of .next-button . 只需将vertical-align:sub赋予.next-buttonpseudo element .next-button

Here is the fiddle. 这是小提琴。

  .next-button { position: absolute; height: 70px; line-height: 70px; text-align: center; background: black; cursor: pointer; } .next-button::after { content: "\\203A"; color: white; font-size: 3em; line-height: 0; vertical-align: sub; } 
 <body> <div class = "next-button"></div> </body> 

You can use flexbox in CSS3. 您可以在CSS3中使用flexbox。

display: flex,
justify-content: center  // or you can also use space-around, it all depends.

// other ways:
align-item: center  or;
align-self: center

Here a link to a example: How do I vertically center text with CSS? 这里是一个示例的链接: 如何使用CSS将文本垂直居中?

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

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