CSS ID reconnize

Posted on 2012-08-14
Last Modified: 2012-08-22
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


Open in new window

Question by:Peiris
    LVL 9

    Expert Comment

    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

    Open in new window

    LVL 50

    Accepted Solution

    Yes you can try this
    *[id*='divItem_'] {
    	background: blue;

    Open in new window

    or if you specifically want to target div's
    div[id*='divItem_'] {
    	background: blue;

    Open in new window

    Full test code here
    <!doctype html>
    <style type="text/css">
    div {
    	width: 100px;
    	height: 100px;
    	float: left;
    	margin: 10px;
    	background: red;
    div[id*='divItem_'] {
    	background: blue;
    <div id="divItem_1"></div>
    <div id="divItem_2"></div>
    <div id="divItem_3"></div>
    <div id="divItem_4"></div>
    <div id="term"></div>

    Open in new window

    LVL 25

    Expert Comment

    by:Kyle Hamilton
    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:
    LVL 53

    Expert Comment

    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.


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
    In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
    In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now