··
441
·
2024-05-01 16:15

Vertically aligning CSS :before and :after content

I am trying to centre the link with the image, but can't seem to move the content vertically in any way.

<h4>More Information</h4>
<a href="#" class="pdf">File Name</a>

The icon is 22 x 22px

.pdf {
    font-size: 12px;
}
.pdf:before {
    padding:0 5px 0 0;
    content: url(../img/icon/pdf_small.png);
}
.pdf:after {
    content: " ( .pdf )";
    font-size: 10px;
}
.pdf:hover:after {
    color: #000;
}


Share Favourite Flag
Ricciflows
Consider using flex: stackoverflow.com/a/22218694/435605 - the answer is for text but works on all elements.
2024-10-29 20:14:46 Reply
Ricciflows
Thanks to CSS3's substring matching attribute selectors, you can now do a[href$=".pdf"] { … } and not even need the class.
2024-10-29 20:16:40 Reply
Ricciflows
This seems to be the best answer for vertically centering content of :before/:after pseudo-elements: stackoverflow.com/a/14524161/470749
2024-10-29 20:17:07 Reply
Ricciflows
@Ricciflows: 🤔🤗
2024-10-29 20:29:50 Reply
Ricciflows
@Ricciflows: 😣😥
2024-10-29 20:38:26 Reply
View all 6 replies

2 answers

在我阅读了关于vertical-align这个CSS属性的建议后。

.pdf:before {
    padding: 0 5px 0 0;
    content: url(../img/icon/pdf_small.png);
    vertical-align: -50%;
}


Answer at 2024-08-23 11:21:28
Flag
Ricciflows
太棒了,中间不起作用,所以我不得不使用负百分比,谢谢你的提醒!
2024-10-29 13:55:48 Reply
Nekomusume
🤣
2024-10-29 23:14:47 Reply

我不是CSS方面的专家,但如果你将vertical-align: middle; 放到你的 .pdf:before令里会怎么样?

Edited at 2024-10-29 13:25:54
Flag
Ricciflows
很乐意帮忙。“中间派”什么都没做,还是做了什么,但没有做你想要的?
2024-10-29 14:44:36 Reply
Ricciflows
太棒了,中间不起作用,所以我不得不使用负百分比,谢谢你的提醒!🤣
2024-10-29 14:45:41 Reply
Ricciflows
🤗🤗
2024-10-29 17:12:13 Reply
Ricciflows
@Ricciflows: 🗺$\Bbb Z$
2024-10-29 21:14:14 Reply
View all 13 replies
Get connected with us on social networks! Twitter

©2024 Guangzhou Sinephony Technology Co., Ltd All Rights Reserved