4

I'm new on this site, and I don't even know if I'm posting on the right section but here we go.

I'm writing a simple HTML file with a CSS attached, in the HTML, I have a piece of code that is like this:

<ul id="thing_list">

    <li>
        <div>
            <header>
                <h2>Name of the thing</h2>
                <p>Some thing here</p>
            </header>
            <nav>
                <ul id="actions">
                    <li>Edit</li>
                    <li>Move</li>
                    <li>Delete</li>
                </ul>
            <nav>
        </div>
    </li>

And the CSS for this is like:

#thing_list li > div > nav li {
    list-style-type: none;
    display: inline;
    vertical-align: bottom;
}

#actions li {
    position: absolute;
    bottom: 0;
}

So, the question is, which way is better for me to name things? Is is better to define an ID/Class for everything or to define a few and use #thing_list li > div > nav li?

I'm not so sure if you guys can understand, english is not my main language, so please bear with me on that.

6
  • 4
    There are lot of resources online to help you with this, for example css-tricks.com/the-difference-between-id-and-class One key thing to remember is that ID must be unique Commented Aug 11, 2014 at 16:21
  • 1
    it depends what you are planning to do with your site. Otherwise whatever is more comfortable for you. Commented Aug 11, 2014 at 16:22
  • id 's must be unique, hence the name id. A class attribute can be attached to multiple elements. Classes are a way of grouping similar elements, e.g a <li class='nav-list' id='home'>Home</li> <li class='nav-list' id='about'>About Me</li> Commented Aug 11, 2014 at 16:24
  • First you really need to understand how selector works. what if you use > in your CSS. do you understand it. Commented Aug 11, 2014 at 16:24
  • 1
    My second suggestion is don't use so long group #thing_list li > div > nav li because it will take more time to Browser read this full syntax. Commented Aug 11, 2014 at 16:26

2 Answers 2

2

The big difference between CSS classes and CSS IDs is that a CSS class name can be used several times, while the CSS IDs are only used once. CSS IDs are used to apply CSS styles to the element and only element identified by the CSS ID. CSS classes should be used for items that you know you are going to use a lot. For example, using a common CSS class name, you can give the same styling to all tables on your webpage. The use of #thing_list li > div > nav li is not recommended as it takes extra time to process. Hope it useful!

Sign up to request clarification or add additional context in comments.

2 Comments

theres no such thing as css classes or ids. they are html classes and ids
I know. But it's more easy to explain it this way. I've should used terms like CSS selectors instead!
0

First, use correct HTML (you better us dl/dt/dd for your example).

See css is "Cascading", so need to override styles in growing specificy. Depending of your choice of specificy.

Note: ID's shall be unique in a document because of the url-anchor.

If you say: Every input field must be blue but saluation must be red, you write this:

 <label>Name: <input></label>
 <label>Saluation: <input class="sal" ></label>

 <style>
 input{
     background-color:blue;
 }
 input.sal{ 
     background-color:red;
 }
 </style>

In this example you have the freedom to be more specific to the second input. Maybe you need the freedom in the future.

So exceptions shall always be more specific than generals.

1 Comment

You will see, techniques like a normalizer.css or a reset.css are of very general specifity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.