CSS:如何使用封闭的div覆盖类样式

时间:2013-02-04 14:50:30

标签: html css

我有一个P元素的风格,我无法改变。 我想用DIV将它括起来强制执行一个新的字体大小。 为什么内部P忽略div字体大小?

示例:

<html>
<head>
    <style>
        .para1 { font-size:small; }
    </style>
</head>

<div style="font-size:300% !important">
    <p class="para1">I must have been asleep, for certainly if I had been fully awake I must have noticed the approach to such a remarkable place. In the gloom the courtyard looked of considerable size, and as several dark ways led from it under great round arches it perhaps seemed bigger than it really is. I have not yet been able to see it by daylight.</p>
</div>

</html>

3 个答案:

答案 0 :(得分:4)

您可以将类设置为包装div,就像我在此处所做的那样: http://jsfiddle.net/3Zvrg/

HTML:

<div class="out_of_para1">
    <p class="para1">

CSS:

.out_of_para1 p {font-size: 300%;}

编辑:基于OP的最后评论

答案 1 :(得分:1)

我知道你不能改变课程,但为什么你不能这样做。

<div style="font-size:300% !important">
<p>I must have been asleep</p>
</div>

并且不将您的“p”与任何类相关联?

答案 2 :(得分:0)

如果属性的值为inherit,则仅继承样式。在段落上,属性的值为small。因此, div 的字体大小为300%,但段落的字体大小为small

您必须在段落元素上明确设置字体大小。

您可以使用样式表中的后代选择器执行此操作:

div .para1 {
    font-size: 300%;
}