Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2013, 03:38 PM   PM User | #1
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 283
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
correct syntax for id and class

Hi there. I've tried searching for this answer but I can't find it anywhere. I know this is basic but,

Is there a difference or best practice preference between these?

a) p#mydiv {}

or

b) #mydiv p {}


And also just to make sure I'm doing this correctly, for multiple elements to be the same is this correct?

.myclass a, .myclass p {font-family: arial}

Many thanks. I just want to check now so I don't get two far into it and it's wrong.
paddyfields is offline   Reply With Quote
Old 01-30-2013, 03:56 PM   PM User | #2
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 906
Thanks: 4
Thanked 212 Times in 211 Posts
tracknut is an unknown quantity at this point
Quote:
Originally Posted by paddyfields View Post
Is there a difference or best practice preference between these?

a) p#mydiv {}

or

b) #mydiv p {}
a) targets a <p> tag with an id of mydiv
b) targets some tag with an id of mydiv with a <p> inside it. So two different things here.

Your second question is correct.

Dave
tracknut is offline   Reply With Quote
Old 01-30-2013, 04:41 PM   PM User | #3
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 283
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
Great, thanks.

In that case though when would you use div#mydiv {} as opposed to #mydiv{} to control an html element of <div id="mydiv"> ?
paddyfields is offline   Reply With Quote
Old 01-30-2013, 04:46 PM   PM User | #4
jerry62704
Senior Coder

 
jerry62704's Avatar
 
Join Date: Oct 2007
Location: Springfield, IL
Posts: 1,077
Thanks: 13
Thanked 84 Times in 84 Posts
jerry62704 is on a distinguished road
I use the #xyz format instead of the div#xyz. No point to the extra typing and it hides the darn thing more.
__________________
.
.
...and gladly would he learn and gladly teach

Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls
jerry62704 is offline   Reply With Quote
Old 01-30-2013, 05:29 PM   PM User | #5
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 906
Thanks: 4
Thanked 212 Times in 211 Posts
tracknut is an unknown quantity at this point
Quote:
Originally Posted by paddyfields View Post
Great, thanks.

In that case though when would you use div#mydiv {} as opposed to #mydiv{} to control an html element of <div id="mydiv"> ?
div#mydiv is a div tag with id mydiv
#mydiv is *any* tag with id mydiv

So again, they're two different things. I too will not typically use the first type for elements like divs. I do sometimes use them for text, where I might want to specifically target p.bold, and not just .bold.

Dave
tracknut is offline   Reply With Quote
Users who have thanked tracknut for this post:
paddyfields (01-31-2013)
Old 01-30-2013, 06:16 PM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by tracknut View Post
div#mydiv is a div tag with id mydiv
#mydiv is *any* tag with id mydiv

So again, they're two different things. I too will not typically use the first type for elements like divs. I do sometimes use them for text, where I might want to specifically target p.bold, and not just .bold.

Dave
As you are only allowed to have one id with that value in each page the additional qualification of which tag it is in is only necessary if you have the same div in different pages in different tags and want to target it in only some of the pages. If all of the id="mydiv" are in div tags then there is no difference between the two.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Users who have thanked felgall for this post:
paddyfields (01-31-2013)
Old 01-31-2013, 12:09 PM   PM User | #7
paddyfields
Regular Coder

 
Join Date: Dec 2010
Location: London
Posts: 283
Thanks: 60
Thanked 11 Times in 11 Posts
paddyfields is an unknown quantity at this point
Great, thanks guys. I understand how it works now.
paddyfields is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:46 PM.


Advertisement
Log in to turn off these ads.