Html 如何覆盖外部css?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12828327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:17:55  来源:igfitidea点击:

How to override external css?

csshtmloverridingpadding

提问by zefs

I have an external css file which applies 35px padding to my content div. All my html pages are loaded inside that div but for one of them I want to use 0 padding-right.

我有一个外部 css 文件,它将 35px 填充应用于我的内容 div。我所有的 html 页面都加载在该 div 中,但对于其中之一,我想使用 0 padding-right。

I tried inline css, applying it directly on the body of that page and also using !important but nothing worked.

我尝试了内联 css,将它直接应用于该页面的正文,并使用 !important 但没有任何效果。

What I am I doing wrong?

我做错了什么?

index.html:

索引.html:

<div id="content"><?php include "page.html"?></div>

main.css:

主文件:

#content{
    margin-top: 303px;
    padding: 35px;
    z-index:1;
}

page.html:

页面.html:

<body style="padding:0px;">

采纳答案by zefs

Fluidbyte was right, I was being stupid trying to apply 0 padding on the wrong element. Adding

Fluidbyte 是对的,我试图在错误的元素上应用 0 填充是愚蠢的。添加

<style>
#content{
    padding-right:0px;  
}
</style>

on page.html worked fine, overrides the external css.

在 page.html 上工作正常,覆盖了外部 css。

回答by Armando Pe?a

To override a css setting you must use the keyword important.

要覆盖 css 设置,您必须使用关键字 important。

<body style="padding:0px ! important;"> 

回答by Fluidbyte

CSS reads top-down, therefore anything lower in the code should be overwritten, see: http://jsfiddle.net/PFx9h/

CSS 自上而下读取,因此应覆盖代码中较低的任何内容,请参阅:http: //jsfiddle.net/PFx9h/

If something isn't taking effect it's because there's some other code overriding it. Use an element inspector such as Webkit Dev Tools or Firebug to see what styles are being applied and how.

如果某些东西没有生效,那是因为有一些其他代码覆盖了它。使用元素检查器(例如 Webkit Dev Tools 或 Firebug)查看正在应用哪些样式以及如何应用。

EDIT:

编辑:

Since you posted your code, bodystyling won't apply to a div.

由于您发布了代码,因此body样式不适用于 div。

回答by j08691

Either apply the style="padding:0px;"on the content div inline (not recommended), or load your style after you load your external style sheet. Applying style="padding:0px;"to the body will only affect the body, and not apply to every element within it.

要么style="padding:0px;"在内联内容 div 上应用(不推荐),要么在加载外部样式表后加载样式。应用于style="padding:0px;"body 只会影响 body,而不适用于其中的每个元素。