1

For example:

<html>
 <head>
  <link href="css/style1.css" type="text/css" />
  <link href="css/style2.css" type="text/css" />
 </head>

 <body>

 <div>I want to use style1.css within this div<div>
 <div>I want to use style2.css within this div<div>

 <body>

Is there any posible way to do like that ?

Thank you.

2
  • Just use one .css file and specify your formations via ID's Commented Dec 15, 2011 at 13:04
  • Did you get a solution you were satisfied with? Commented Jan 24, 2012 at 16:22

3 Answers 3

1

In your two files define different classes of div.

For instance, in style1.css you might have:

div.class1
{
background-color: red;
}

And in style2.css you might have:

div.class2
{
background-color: blue;
}

Then change your code to reflect where you want each style, ie:

<div class="class1">I want to use style1.css within this div<div>
<div class="class2">I want to use style2.css within this div<div>
Sign up to request clarification or add additional context in comments.

5 Comments

Which is an horrible inefficiant and uncommon way !
Say that he's using a template CSS file that exists for every page, and would not like to clutter that with page-specific styling. Doesn't seem that uncommon to me.
@EvilP, your comment is horribly inefficient and unhelpful by not explaining why the answer is as you say it is.
then I can add that for you. First of all it is extremly slow if you load 2 css files. You loose overview where to configure which element in which css file when you think of huge websites and by choosing only one css file its much better because you don't loose the overview and its faster loaded.
I know this way is not the best, but it can save time.
0

As you wrote, this is not possible but you can give the div-tags id and format for the id only. So you only have to add one css file which gives you a better overview ,structre and the website is loaded faster. The HTML Markup

<div id='first'></div>
<div id='second'></div>

and in the css

#first{
   background-color:red;
}

#second{
   background-color:green;
}

By using id's you ensure that the access is faster than by using classes. If you want to style the content of the div's differently you could also do that.

Comments

0

If I were you, I also would use classes to define which styles go to which div. However I would not use a separate stylesheet for each class. I would combine the two classes into one stylesheet, because like EvilP said, loading two separate css files can be slow. Also, I would avoid using ids where a class can do the job as effectively, because an id is only used to target one specific element, and a class doesn't have to, but can target more than one element. So a class is more versatile overall.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.