Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7816219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:47:18+00:00 2026-06-02T05:47:18+00:00

When should you add classes to elements and style them using class selectors and

  • 0

When should you add classes to elements and style them using class selectors and when should you instead add a class/id on the parent and use the elements just as is?

Example:

<div class="warning-dialog">
    <h3>This is the title</h3>
    <p>This is the message</p>
</div>

.warning-dialog {}
.warning-dialog h3 {}
.warning-dialog p {}

vs.

<div class="warning-dialog">
    <h3 class="warning-title">This is the title</h3>
    <p class="warning-message">This is the message</p>
</div>

.warning-dialog {}
.warning-title {}
.warning-message {}

Or should you do

.warning-dialog .warning-dialog {}
.warning-dialog .warning-title {}
.warning-dialog .warning-message {}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T05:47:20+00:00Added an answer on June 2, 2026 at 5:47 am

    Ask yourself this simple question:

    Do all <x> elements under this common ancestor mean the same thing?

    If the answer to that is yes, then you don’t need a class name on those elements. Taking your code as an example, the following is sufficient:

    <div class="warning-dialog">
        <h3>This is the title</h3>
        <p>This is the message</p>
    </div>
    

    Because inside of a .warning-dialog, all h3 elements (1) and all p elements (1) would mean the same, the title and the content of the dialog! Meaning, you don’t need to have any specific class names on them and they are easily accessible via .warning-dialog h3 or .warning-dialog p.

    If however, the answer to above question is “No”, that’s a whole different story:

    <div>Warning</div>
    <div>Info</div>
    <div>Error</div>
    

    You can’t (easily) designate each div with a CSS, they don’t all mean the same thing, so you need to use class names to make it better!

    <div class="warning-dialog">Warning</div>
    <div class="info-dialog">Info</div>
    <div class="error-dialog">Error</div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think it should be possible to add additional namespaces to the generated classes
I'm trying to use JQuery to add/remove classes as part of a function that
Our designer is calling for us to add the classes we use for JavaScript
I'm using the jQuery UI tabs widget to style my page: <div id=tabs> <ul>
What code I should add to accept null from WHERE statement. { int numApprovals
[Edit] And, I should add, most elegantly? I guess (but not sure how) I
I need to design a function to return negative numbers unchanged but should add
I have following code: os.chdir(os.path.dirname(os.path.realpath(__file__)) + /../test) path.append(os.getcwd()) os.chdir(os.path.dirname(os.path.realpath(__file__))) Which should add /../test to
Why should we add an object name to a QObject? I can still load
http://www.w3.org/QA/Tools/Icons If my code is valid then Should i add logo to all my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.