宽度和高度似乎不起作用:在伪元素之前

问题描述 投票:46回答:5

Here是一个小提琴。

<p>foo <a class="infolink" href="#">bar</a> baz</p>

a.infolink::before
{
    content: '?';
    background: blue;
    color: white;
    width: 20ex;
    height: 20ex;
}

'?'出现,但显然没有20ex大小。为什么不?在Firefox和Chrome中测试过。

css pseudo-element
5个回答
88
投票

:before:after伪元素实际上是display: inline;默认情况下。

将显示值更改为inline-block而不是宽度和高度生效:

a.infolink::before {
    content: '?';
    display: inline-block;
    background: blue;
    color: white;
    width: 20px;
    height: 20px;
}

http://jsfiddle.net/C7rSa/3/


10
投票

::before::after伪元素默认为内联,因此widthheight不会生效。

设置为display: inline-blockit should work


1
投票

添加display:block;

demo

p > a.infolink::before {
    display:block;
    content:'?';
    background: blue;
    color: white;
    width: 20ex;
    height: 20ex;
    border:1px solid #000;
}

0
投票

我可以让它工作但不使用宽度的百分比。像素工作正常

visibility: visible;
content: "stuff";
min-width: 29px;
width: 100%;
float: left;
position: relative;
top: -15px;
left: 0;

0
投票

如果您在容器或CSS空白区域中有图像,请使用此选项:nowrap;属性

body::before{ 
    content:url(https://i.imgur.com/LJvMTyw.png);
    transform: scale(.3);
}
© www.soinside.com 2019 - 2024. All rights reserved.