Results 1 to 8 of 8

Thread: [RESOLVED] Apply a css class to all instances of an element?

  1. #1

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Resolved [RESOLVED] Apply a css class to all instances of an element?

    I have a css class "shadow" defined which specifies a default box-shadow for easily adding a shadow to individual elements. Is it possible to apply this class to all elements of a specific type (eg. all blockquote elements) without having to re-type the stuff inside .shadow{} into blockquote{} for each element? If it can't be done without javascript then I won't bother about it.
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  2. #2
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: Apply a css class to all instances of an element?

    well if you type div { border:1px solid black; } then all divs will have a 1 pixel border thats solid black... so if i get what you mean is then just type the elements type for example ul { something} like this:

    this adds a border around the blackquote
    Code:
    <html>
    <head>
    <style type="text/css">
    blockquote {
    border:1px solid black;
    }
    </style>
    </head>
    <body>
    
    <blockquote>
    This is a long quotation. This is a long quotation. This is a long quotation. This is a long quotation. This is a long quotation.
    </blockquote>
    
    </body>
    </html>

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Apply a css class to all instances of an element?

    Code:
    .shadow,
    blockquote {
      /* this style will be applied to both elements with a .shadow class and blockquote elements */
    }

  4. #4

    Thread Starter
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Apply a css class to all instances of an element?

    Quote Originally Posted by kows View Post
    Code:
    .shadow,
    blockquote {
      /* this style will be applied to both elements with a .shadow class and blockquote elements */
    }
    Thanks. Don't know why I didn't think of that earlier
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  5. #5
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: [RESOLVED] Apply a css class to all instances of an element?

    oh sorry now i see kows has the class of the element i get what you mean

    Code:
    //class + id
    .form #textbox { }
    
    //multiple classess
    .form .inputs { }
    //multiple ids is same as classes... 
    
    //class and elements
    .form input { /*code for all inputs such as text, password, submit buttons...*/ }
    
    //multiple elements like h1, h2...
    h1, h2, h3, h4, h5, h6 { color:green; }
    i got a good site that you can learn fast of: w3schools

  6. #6
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [RESOLVED] Apply a css class to all instances of an element?

    Code:
    //class + id
    .form #textbox { }
    
    //multiple classess
    .form .inputs { }
    //multiple ids is same as classes...
    These two are not right. The top one is actually "an element with id 'textbox' within an element with class 'form'." If you want to specify "an element with class 'form' and id 'textbox'," you cannot have whitespace between them (and I'm not sure, but the ID may have to go first):
    Code:
    #textbox.form{}
    Same for multiple classes: no whitespace or else it's "class within a class."
    Code:
    .form.inputs{}

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Apply a css class to all instances of an element?

    from his comment about header tags in his code (h1, h2, h3, ...), I would assume he just forgot the commas in his example. but I'm not entirely sure.

    anyhow, I've never seen the double class notation (.classone.classtwo), but I know I could find it handy.

  8. #8
    Fanatic Member
    Join Date
    Jun 2008
    Posts
    1,023

    Re: [RESOLVED] Apply a css class to all instances of an element?

    if there is no comma then it will take the element h2 inside h1 and the element h3 inside h2 which is inside h1 which is not very good, therefore a comma(, not seperator xD).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width