Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply different css stylesheet for different parts of the same web page [duplicate]

Tags:

css

stylesheet

I have a web page with different parts which require different css stylesheets to be applied to each. What I would like to know is how to specify which css stylesheet to use for each different part of the web page. if I leave them all together, the components do not get displayed properly.

like image 976
C.. Avatar asked Jul 21 '10 11:07

C..


People also ask

Should I use different CSS files for each page?

you should keep only one css file. Let me tell you in simple one line, once your website loads in client web browser the static resource can be cached that helped your website to boost and number of web request can be reduce when user browse multiple pages of your website.

Can you apply CSS to a part of HTML document only?

You can add inline CSS in a style attribute to style a single HTML element on the page. You can embed an internal stylesheet by adding CSS to the head section of your HTML doc. Or you can link to an external stylesheet that will contain all your CSS separate from your HTML.

Is it okay to have multiple CSS files?

Yes, It is possible to include one CSS file in another and it can be done multiple times. Also, import multiple CSS files in the main HTML file or in the main CSS file.


3 Answers

You can't apply different stylesheets to different parts of a page. You have a few options:

The best is to wrap the different parts of the page in divs with class names:

<div class='part1'>
    ....
</div>

<div class='part2'>
    ....
</div>

Then in your part1 CSS, prefix every CSS rule with ".part1 " and in your part2 CSS, prefix every CSS rule with ".part2 ". Note that by using classes, you can use a number of different "part1" or "part2" divs, to flexibly delimit which parts of the page are affected by which CSS rules.

like image 82
Ned Batchelder Avatar answered Nov 10 '22 01:11

Ned Batchelder


It is called Cascading Style Sheets (CSS) for a reason ..

use the specificity rules of CSS to target each section..

ie..

#section1 a{color:red}
#section2 a{color:blue}

this will make all links inside an element with id section1 be red, and all links inside an element with id section2 be blue..

<div id="section1">
 <a href="#">i will be red</a>
</div>
<div id="section2">
 <a href="#">i will be blue</a>
</div>
like image 34
Gabriele Petrioli Avatar answered Nov 10 '22 00:11

Gabriele Petrioli


An external stylesheet is applied to whole html page. In order to apply different stylesheets to different portions, Firstly, the html page has to be divided into different sections using the html tag ( with each html div to be assigned a unique id or class) Secondly, the external stylesheet to be applied to each div has to be designed according to the id/class assigned to it

Example as explained above by Gaby aka G. Petrioli...

like image 45
kapil Avatar answered Nov 10 '22 01:11

kapil