Link to home
Start Free TrialLog in
Avatar of Peiris
PeirisFlag for United States of America

asked on

CSS ID reconnize

I have list of div tags are generated using a php loop. I id each div tag using "divItem_+(some random number)". My question is is it possible to set a CSS tag for these divs using the starting name if the tags. for example

#divItem*{
//styling
}

Open in new window

Avatar of WebDevEM
WebDevEM
Flag of United States of America image

I'd suggest adding a class to them all, in addition to the ID, so that you can target anything with that class.  For example:
<div class="allofthem" id="divItem_1"></div>
<div class="allofthem" id="divItem_2"></div>
<div class="allofthem" id="divItem_3"></div>

Open in new window

That way you can use
.allofthem{
//styling
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you're trying to target all of them, then absolutely go with a class per WebDevEM's post.

Beware of using general selectors like * or div or regex: [class^="grid-"]. They are the slowest to render. It won't make much of a difference in a small website with a couple of divs, but the minute you start adding more DOM elements, you'll be asking for trouble.  Have a look here for more on CSS preformance:

http://csswizardry.com/2011/09/writing-efficient-css-selectors/
I'd suggest adding a class to them all, in addition to the ID, so that you can target anything with that class.

WebDevEM is 100% correct. That is best practice.  If you like nightmare maintenance easily broken pages, and looking foolish then by all means explore other options, but not only is this best practice it is no more work than trying something screwy. You are already generating with php so you have one line of code to change, and a simple non-complex and efficient rule to put in the CSS.

Cd&