0

I have a website with a corresponding CSS file that defines all its styles. However, there is a specific DIV container on one of the webpages that I would like the styles to be different.

Here is my newbie question: Is there a way to specify another CSS file that applies to just within that DIV??? If so, how? If not, are there any other way of achieving a similar effect?

Thank you very much.

hpy
  • 1,989
  • 7
  • 26
  • 56

3 Answers3

3

You can achieve it in the following way :
1. Assign a class to the concerned div like

<div class="different"> 

And then style this class different in your default css, for example :

.different{
    font-size:12px;
}

2.The other way is to use IDs , since ids are unique to one element and on page only one element can have that ID .

<div id="diff">

And than in your current css file you can write something like :

#diff{
    font-size:12px;
}

They both will serve your purpose but classes can be used multiple time and IDs are unique to a given element. So it's up to you to decide which method you follow.

ultranaut
  • 2,132
  • 1
  • 17
  • 22
Shail
  • 3,639
  • 1
  • 18
  • 29
1

You can write another class of your existing css file or create a new one(in case of new one, you need to add reference of it in your head tag and) and write their css properties for that div like this

.MyDivClass
{
// your properties here
}

and use it with your div like this

<div class="MyDivClass">
</div>
Sachin
  • 40,216
  • 7
  • 90
  • 102
0

I don't know. I will do it like this

<div id="special-div"></div>

and I will create a special css file and include #special-div{/*styles here*/}

It is best to include the stylesheet in the <head> I think.

madhushankarox
  • 1,448
  • 1
  • 13
  • 17
  • 2
    creating another css file will increase the overall http request and its not advisable in his case since he only have to style one element . – Shail Mar 09 '13 at 02:04
  • Please note he wanted to use a different CSS file. And my answer is not only for a single element. – madhushankarox Mar 09 '13 at 02:08
  • Friend he only wants to style one element , so providing him with the idea of different stylesheet is wrong.Thanks – Shail Mar 09 '13 at 02:13
  • Have to agree with Shail trying to reduce HTTP requests is a very important best practice. Even if they were two long CSS files it would still be a good idea to merge them into one. – Aurelio Mar 09 '13 at 02:16
  • Me too agree. But my answer for his question. I don't know whether he is going to do a special style up with an another special stylesheet or styling a single element. And I think he already knows how to use id and class to style an element already. :) – madhushankarox Mar 09 '13 at 02:20