垂直对齐 CSS :before 和 :after 内容

发布于 2024-09-01 09:02:37 字数 413 浏览 13 评论 0原文

我试图将链接与图像居中,但似乎无法以任何方式垂直移动内容。

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

北音执念 2024-09-08 09:02:38

我不是 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?

-残月青衣踏尘吟 2024-09-08 09:02:38

我认为更简洁的方法是继承垂直对齐:

在 html 中:

<div class="shortcut"><a href="#">Download</a></div>

在 css 中:

.shortcut {
    vertical-align: middle;
}
.shortcut > a:after {
    vertical-align: inherit;
{

这样图标将在任何分辨率/字体大小组合中正确对齐。非常适合与图标字体一起使用。

I think a cleaner approach is to inherit the vertical alignment:

In html:

<div class="shortcut"><a href="#">Download</a></div>

And in css:

.shortcut {
    vertical-align: middle;
}
.shortcut > a:after {
    vertical-align: inherit;
{

This way the icon will align properly in any resolution/font-size combination. Great for use with icon fonts.

念﹏祤嫣 2024-09-08 09:02:38

使用弹性盒对我来说很有效:

.pdf:before {
    display: flex;
    align-items: center;
    justify-content: center;
}

Using flexboxes did the trick for me:

.pdf:before {
    display: flex;
    align-items: center;
    justify-content: center;
}
金兰素衣 2024-09-08 09:02:38

这对我有用:

.pdf::before {
    content: url('path/to/image.png');
    display: flex;
    align-items: center;
    justify-content: center;
    height: inherit;
}

This is what worked for me:

.pdf::before {
    content: url('path/to/image.png');
    display: flex;
    align-items: center;
    justify-content: center;
    height: inherit;
}
久而酒知 2024-09-08 09:02:38

搞乱 line-height 属性应该可以解决问题。我还没有测试过这个,所以确切的值可能不正确,但从 1.5em 开始,并以 0.1 的增量调整它,直到它对齐。

.pdf{ line-height:1.5em; }

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.

.pdf{ line-height:1.5em; }
养猫人 2024-09-08 09:02:38

我今天花了很多时间试图解决这个问题,但无法使用行高或垂直对齐来使事情正常工作。我能找到的最简单的解决方案是将 设置为相对定位,因此它将包含绝对值,并且 :after 绝对定位,将其从流中取出。

a{
    position:relative;
    padding-right:18px;
}
a:after{
    position:absolute;
    content:url(image.png);
}

在这种情况下,后像似乎会自动居中,至少在 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.

a{
    position:relative;
    padding-right:18px;
}
a:after{
    position:absolute;
    content:url(image.png);
}

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/>.

许一世地老天荒 2024-09-08 09:02:38

我认为我刚刚找到了一个非常巧妙的解决方案。诀窍是设置图像(或任何内容)heightline-height

text

使用 CSS:

div{
  line-height: 26px; /* height of the image in #submit span:after */
}

span:after{
    content: url('images/forward.png');
    vertical-align: bottom;
}

如果没有跨度,这可能也可以工作。

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:

div{
  line-height: 26px; /* height of the image in #submit span:after */
}

span:after{
    content: url('images/forward.png');
    vertical-align: bottom;
}

That would probably also work without the span.

背叛残局 2024-09-08 09:02:38

我有类似的问题。这就是我所做的。由于我尝试垂直居中的元素的高度 = 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);

本王不退位尔等都是臣 2024-09-08 09:02:37

阅读关于 vertical-align CSS 属性的建议后。

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

After reading the advice on the vertical-align CSS attribute.

.pdf:before {
    padding: 0 5px 0 0;
    content: url(../img/icon/pdf_small.png);
    vertical-align: -50%;
}
小巷里的女流氓 2024-09-08 09:02:37

您还可以使用表格来完成此操作,例如:

.pdf {
  display: table;
}
.pdf:before {
  display: table-cell;
  vertical-align: middle;
}

这是一个示例: https://jsfiddle.net/ar9fadd0/ 2/

编辑:
您还可以使用 flex 来完成此操作:

.pdf {
  display: flex;
}
.pdf:before {
  display: flex;
  align-items: center;
}

这是一个示例: https://jsfiddle.net/ctqk0xq1/1/

You can also use tables to accomplish this, like:

.pdf {
  display: table;
}
.pdf:before {
  display: table-cell;
  vertical-align: middle;
}

Here is an example: https://jsfiddle.net/ar9fadd0/2/

EDIT:
You can also use flex to accomplish this:

.pdf {
  display: flex;
}
.pdf:before {
  display: flex;
  align-items: center;
}

Here is an example: https://jsfiddle.net/ctqk0xq1/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文