2012-10-10 269 views
2
設置

我的頁面引用的CSS樣式表的定義:覆蓋CSS樣式的HTML標籤

html {background-color:#FFFFFF;background-image:url('../images/Background.png');background-repeat:repeat-x; } 

如何覆蓋在頁面級別的background-image元素?我需要在應用程序中覆蓋只有一頁的設置。

回答

5

添加一個類的HTML元素,然後使用一些聯樣式覆蓋外部樣式表的造型。您也可以將內聯樣式放置在最佳實踐的外部樣式表中。

<html class="myHtmlTag"> 
    <head> 
    <link href="externalStyle.css" rel="stylesheet"/> 
    <style> 
    html.myHtmlTag{ 
     background: none !important; 
     background-image:none !important; 
    } 
    </style> 
    </head> 
    <body></body> 
</html> 
+0

減的空間。 ('html.myHtmlTag') – ACJ

+0

感謝您的更正,我更新了答案。 –

0

使用!important財產上backgroundbackground-image風格像這樣:

html{background-color:#000000 !important;background-image:url('your/image/path') !important;...} 
0

你能申請一個類到body標籤嗎? IE:

<body class="home"> 
<body class="articlepage"> 

否則,你在技術上可以使用jQuery,假設你有機會獲得掉落在網頁代碼做的功能,但被鎖定

<script>$("body").addClass("home");</script> 

----或--- ---

<script>$("body").addClass("articlepage");</script> 

這允許你這樣做的類:

body.home {background-image:url(homebg.jpg);} 
body.articlepage {background-image:url(articlebg.jpg);} 
1

如果您僅針對html標籤,而沒有任何其他選擇器,則只能在主css後包含另一個html樣式。按照CSS的特殊性 - 它們每個只有1個標記的值(沒有ids,沒有類) - 所以後者將設置元素的樣式。

<html> 
<head> 

<link rel="stylesheet" href="main.css" /> 

<!-- anywhere from here down you can include a style to over ride html --> 

這裏有一個快速演示:

html {background-color:#000;background-image:url('http://lxmpro.com/cms/wp-content/uploads/2012/01/site-background-pattern-07.jpeg');background-repeat:repeat-x; } 


/* second reference to html tag overrides the first */ 
html {background-image:url('http://www.noupe.com/wp-content/uploads/2009/10/wallpaper-pattern.jpg');} 

工作演示 - 類背景顏色的http://jsfiddle.net/K68D3/

0

基本內嵌樣式覆蓋:

<!DOCTYPE html> 
<html> 
<head> 
<style> 
.city { 
    background-color: orange; 
    color: white; 
    padding: 10px; 
} 
</style> 
</head> 

<body> 

<h2 class="city" style="background-color: tomato;">London</h2> 
<p>London is the capital of England.</p> 

</body> 
</html>