且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止子元素继承CSS样式

更新时间:2023-12-01 23:42:28

在你的情况下,***的办法是< p> 元素是继承:



所以在你的CSS文件,你应该使用,而不是内联样式:

  .parent-div {
font-size:10pt;
color:red;
}

.child-paragraph {
font-size:12pt;
color:black;
}


I have a <div> element with a <p> element inside:

<div style="font-size:10pt; color:red;">
    This is my Parent Div.
    <p>This is my Child Paragraph.</p>
    My parent Div Ends Here.
</div>

How can I prevent my paragraph from inheriting the CSS properties set to the <div>?

In your case the best way is to just over right the styles that the <p> element is inheriting:

So in your CSS file, which you should be using instead of inline styles:

.parent-div {
    font-size: 10pt;
    color: red;
}

.child-paragraph {
    font-size: 12pt;
    color: black;
}