CSS:colorstylesolutionwithcsscontexualselectors

來源:互聯網
上載者:User

Your web site always contains several modules which navigated by a menu or sth, just like this: forums | Blog | Photos. You may want each module has its own color style, so the users can recognize each module easily and your site also be cool. This article supplies a easier way to accomplish this aim by using the contexual selector feature of css.

1. What are contextual selectors of css?

Contextual selectors can look for element types, CLASS attributes, ID attributes or combinations of these:

DIV P           { font: small sans-serif }.reddish H1     { color: red }#x78y CODE      { background: blue }DIV.sidenote H1 { font-size: large }

Reference to W3C

 

Of course, .class1 .class2 { .... }  is also a contexual selector, so is this .class1 .class2 .class3 {...}. We mainly use this style in this article.

 

2. Samples.

Suppose you got two pages: page1.htm and page2.htm. How can we define two color styles for the two page with minimus code typing?

Below is page1 and page2's html. (to be simple, I just list a few elements)

page1:

<body class=page1>

  <div class=primaryPalette> this is page1.</div>

</body>

 

page2:

<body class=page1>

  <div class=primaryPalette> this is page2.</div>

</body>

 

Define css classes in the css file and link it to your pages.

.page1 .primaryPalette{

 BORDER-LEFT-COLOR: #747e96; BORDER-BOTTOM-COLOR: #747e96; BORDER-TOP-COLOR: #747e96; BACKGROUND-COLOR: #747e96; BORDER-RIGHT-COLOR: #747e96

}

.page2 .primaryPalette{

 BORDER-LEFT-COLOR: #44a12c; BORDER-BOTTOM-COLOR: #44a12c; BORDER-TOP-COLOR: #44a12c; BACKGROUND-COLOR: #44a12c; BORDER-RIGHT-COLOR: #44a12c

}

By doing this, you got two pages with different colors. This is just a simple sample. Actually you can define as complex styles as you can, just in this format: <[.page1|. page2[|...] ]  [other classes inner]>. For example:

.page1 .a1 .b1 {}

.page2 .a1 .b1 {}

In the html of your pages, you just add or change the body's css class, then you can get a new color style of your page.

The idea of this article came from www.salesforce.com. You can check the source html and css of this web site and get the details.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.