7 replies [Last post]
nmduc073
Offline
Regular
Viet Nam
Last seen: 13 years 12 weeks ago
Viet Nam
Timezone: GMT+7
Joined: 2007-06-26
Posts: 27
Points: 0

Hi,
I would like to ask a question about selector's syntax. I have read a book and the syntax example is

div#example
{
....
}

I wonder if we can write in the following way or not.

div #example
{
....
}

There is a space between element and id selector. Also, I have the same question

p .warning
{
....
}

Thank in advance,
Duc

wolfcry911
wolfcry911's picture
Offline
Guru
MA, USA
Last seen: 6 years 34 weeks ago
MA, USA
Timezone: GMT-5
Joined: 2004-09-01
Posts: 3224
Points: 237

they have different

they have different meanings.
div#example refers to a div with an id of example while
div #example refers to any element with an id of example which is a descendant of a div

the same holds true for your other selector.
p .warning addresses an element with a class of warning which is a descendant of a p.
p.warning addresses a p with a class of warning

nmduc073
Offline
Regular
Viet Nam
Last seen: 13 years 12 weeks ago
Viet Nam
Timezone: GMT+7
Joined: 2007-06-26
Posts: 27
Points: 0

Thank you very much.

Thank you very much.

nmduc073
Offline
Regular
Viet Nam
Last seen: 13 years 12 weeks ago
Viet Nam
Timezone: GMT+7
Joined: 2007-06-26
Posts: 27
Points: 0

I have one more example. We

I have one more example. We set the declaration as follow,

div #example
{
color:red;
}

Hello world!

What color is the text?

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 5 years 41 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

You could probably work out

You could probably work out the answer to this from wolfies reply earlier, but it depends it could well be, and most probably would be the case that the text would be red, but the question is a false one as it is simply not possible to give an answer, what the color would be would entirely depend on the markup you had (likely the id would be a child of a div, if not then it would't apply), there is nothing wrong per se with what you have written.

It's always best to read through the specs to get the best and acurate detail on the subject and don't rely too much on a single book always google as much as possible as this way you will find many sites explaining the subject.

http://www.w3.org/TR/REC-CSS2/selector.html

Edit: Of course it would have helped if I had observed the markup example, but I think TPH (a.k.a 'Hot Stuff' or 'Lambykins' *sigh* ) has covered things pretty much, but have a read of the W3C link as well.

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

thepineapplehead
thepineapplehead's picture
Offline
Guru
Last seen: 31 weeks 3 days ago
Joined: 2004-06-30
Posts: 9668
Points: 801

Without seeing the rest of

Without seeing the rest of the code, we can't say.

For example:


Hello world!

the text would inherit the body color



Hello world!

text would be red



Hello world!


text would also be red


Hello world!

text would NOT be red.

Verschwindende wrote:
  • CSS doesn't make pies

nmduc073
Offline
Regular
Viet Nam
Last seen: 13 years 12 weeks ago
Viet Nam
Timezone: GMT+7
Joined: 2007-06-26
Posts: 27
Points: 0

Thank you very much.

Thank you very much.

thepineapplehead
thepineapplehead's picture
Offline
Guru
Last seen: 31 weeks 3 days ago
Joined: 2004-06-30
Posts: 9668
Points: 801

You're quite welcome

You're quite welcome Wink

Verschwindende wrote:
  • CSS doesn't make pies