如何在css中继承宽度?

时间:2012-05-20 09:42:51

标签: css

所以我有这个HTML:

<p>here is some <span>random</span> content that I might use</p>​

和这个css:

span{
    position: relative;
}

span:before{
    content: 'this is more content';
    position:absolute;
    color: red;
    top: -15px;
}

发生的情况是,使用css添加的内容会继承span(单词“random”)的宽度。有没有办法删除继承的宽度没有添加宽度值来覆盖继承

这是演示http://jsfiddle.net/7dBbZ/1/ 谢谢     

2 个答案:

答案 0 :(得分:4)

您要找的是white-space:nowrap;,因为该元素是使用display:inline呈现的。它实际上并没有继承width,因为BoltClock已经指出它是不是一个隐式继承的属性,而是用允许的包装填充父元素。

http://jsfiddle.net/7dBbZ/5/

尝试使用content的不同值进行游戏,并亲自查看(即文字短于父元素,文字不含空格等)。

答案 1 :(得分:0)

试试这个:

span:before {
    white-space: nowrap;
}