Results 1 to 3 of 3

Thread: Which CSS selector?

  1. #1
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default Which CSS selector?

    Sometimes I find it difficult to choose the right CSS selector. Consider this, for example:

    Code:
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <title>Which CSS selector</title>
        <style>
            [for="married"] {
                outline: 1px solid red;
            }
        </style>
    </head>
    
    <body>
        <form>
            <label for="name">Name</label>
            <input type="text" id="name">
            <label for="married">Married</label>
            <input type="checkbox" id="married">
        </form>
    </body>
    
    </html>
    I can get the same result with:

    • label:nth-child(3)
    • label:nth-of-type(2)
    • #name + label
    • etc.


    I can even give the element an ID and get it directly. But isn't it better to keep your HTML tidier/smaller?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Each method has its own merits depending upon the situation.

    There are various goals to keep in mind (just off the top of my head, in no particular order, there are probably others):

    • Simple and concise code (both HTML and CSS)
    • Code (both HTML and CSS) that's easy to understand with the human eye/mind
    • Code (both HTML and CSS) that's reusable across as many pages of the site as possible
    • CSS code that establishes a visual theme that can also be easily tweaked in just a few places to create dramatic and/or subtle variations on that theme
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    Rain Lover (03-29-2014)

  4. #3
    Join Date
    Nov 2009
    Location
    Isfahan, Iran
    Posts
    229
    Thanks
    46
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by jscheuer1 View Post
    Each method has its own merits depending upon the situation.

    There are various goals to keep in mind (just off the top of my head, in no particular order, there are probably others):

    • Simple and concise code (both HTML and CSS)
    • Code (both HTML and CSS) that's easy to understand with the human eye/mind
    • Code (both HTML and CSS) that's reusable across as many pages of the site as possible
    • CSS code that establishes a visual theme that can also be easily tweaked in just a few places to create dramatic and/or subtle variations on that theme
    Thanks for the answer!

Similar Threads

  1. Various Ways of Using The Selector
    By flame-d in forum JavaScript
    Replies: 3
    Last Post: 09-18-2013, 12:59 AM
  2. PHP Regex for CSS Selector
    By JAB Creations in forum PHP
    Replies: 8
    Last Post: 11-18-2011, 12:47 PM
  3. Image Selector
    By dalbeck in forum JavaScript
    Replies: 10
    Last Post: 12-12-2007, 03:11 PM
  4. CSS Stylesheet Selector
    By mrmesells in forum Bug reports
    Replies: 0
    Last Post: 08-13-2006, 06:58 PM
  5. Image Selector Help
    By Trin in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 07-07-2005, 08:07 AM

Bookmarks

Posting Permissions

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