垂直对齐 CSS :before 和 :after 内容
我试图将链接与图像居中,但似乎无法以任何方式垂直移动内容。
<h4>More Information</h4>
<a href="#" class="pdf">File Name</a>
图标为 22 x 22 像素
.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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我不是 CSS 专家,但是如果将
vertical-align: middle;
放入.pdf:before
指令中会发生什么?I'm no CSS expert, but what happens if you put
vertical-align: middle;
into your.pdf:before
directive?我认为更简洁的方法是继承垂直对齐:
在 html 中:
在 css 中:
这样图标将在任何分辨率/字体大小组合中正确对齐。非常适合与图标字体一起使用。
I think a cleaner approach is to inherit the vertical alignment:
In html:
And in css:
This way the icon will align properly in any resolution/font-size combination. Great for use with icon fonts.
使用弹性盒对我来说很有效:
Using flexboxes did the trick for me:
这对我有用:
This is what worked for me:
搞乱 line-height 属性应该可以解决问题。我还没有测试过这个,所以确切的值可能不正确,但从 1.5em 开始,并以 0.1 的增量调整它,直到它对齐。
Messing around with the line-height attribute should do the trick. I haven't tested this, so the exact value may not be right, but start with 1.5em, and tweak it in 0.1 increments until it lines up.
我今天花了很多时间试图解决这个问题,但无法使用行高或垂直对齐来使事情正常工作。我能找到的最简单的解决方案是将 设置为相对定位,因此它将包含绝对值,并且 :after 绝对定位,将其从流中取出。
在这种情况下,后像似乎会自动居中,至少在 Firefox/Chrome 下是这样。对于不支持 :after 的浏览器来说,由于 上的空格过多,这可能会有点草率。
I spent a good amount of time trying to work this out today, and couldn't get things working using line-height or vertical-align. The easiest solution I was able to find was to set the <a/> to be relatively positioned so it would contain absolutes, and the :after to be positioned absolutely taking it out of the flow.
The after image seemed to automatically center in that case, at least under Firefox/Chrome. Such may be a bit sloppier for browsers not supporting :after, due to the excess spacing on the <a/>.
我认为我刚刚找到了一个非常巧妙的解决方案。诀窍是设置图像(或任何内容)
height
的line-height
。text
使用 CSS:
如果没有跨度,这可能也可以工作。
I just found a pretty neat solution, I think. The trick is to set the
line-height
of image (or any content)height
.text
Using CSS:
That would probably also work without the span.
我有类似的问题。这就是我所做的。由于我尝试垂直居中的元素的高度 = 60px,因此我设法使用以下方法将其垂直居中:
top: calc(50% - 30px);
I had a similar problem. Here is what I did. Since the element I was trying to center vertically had height = 60px, I managed to center it vertically using:
top: calc(50% - 30px);
阅读关于
vertical-align
CSS 属性的建议后。After reading the advice on the
vertical-align
CSS attribute.您还可以使用表格来完成此操作,例如:
这是一个示例: https://jsfiddle.net/ar9fadd0/ 2/
编辑:
您还可以使用 flex 来完成此操作:
这是一个示例: https://jsfiddle.net/ctqk0xq1/1/
You can also use tables to accomplish this, like:
Here is an example: https://jsfiddle.net/ar9fadd0/2/
EDIT:
You can also use flex to accomplish this:
Here is an example: https://jsfiddle.net/ctqk0xq1/1/