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 8873483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:28:34+00:00 2026-06-14T18:28:34+00:00

The basic question I’m wondering about is what is preferred in terms of readability,

  • 0

The basic question I’m wondering about is what is preferred in terms of readability, reuse, coding style, etc. One thing to note is that this JSP could be used in multiple parts of a page, for this naive example lets say its just a div that needs to be styled a certain way and put on a page several different times.

I realize I could externalize the class to a shared CSS file, but this class will only ever be used by this part of the page, say for instance this box is the only one that will ever need to be purple in the entire product, in my opinion it doesn’t make sense to dirty up a shared CSS in order to clean up my JSP. So what is better

Option 1 (Using ID selectors)

<% String contextName = request.getParameter("myContext"); %>

<style type="text/css">
    #<%=contextName %>_myDiv
    {
        font-weight: bold;
        background-color: purple;
        height: 20px;
    }
</style>

<div id="<%=contextName %>_myDiv">
    <div>Blah Blah Blah!</div>
</div>

Option 2 (Using class selectors)

<% String contextName = request.getParameter("myContext"); %>

<style type="text/css">
    .<%=contextName %>_myDiv
    {
        font-weight: bold;
        background-color: purple;
        height: 20px;
    }
</style>

<div class="<%=contextName %>_myDiv">
    <div>Blah Blah Blah!</div>
</div>

It seems to me that option 2 would make things easier to debug since they are using a shared class, however if there are (for example) 50 of these boxes on the page then it will result in this class being declared 50 times. Does this create extra leg work for the browser. Alternately if I use the ID selector method then I create 50 unique styles that do the exact same thing causing extra work for the browser to match up all the IDs.

So what is better? NOTE: both these ways work, I’m just looking for the pros and cons of each method.

  • 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-14T18:28:36+00:00Added an answer on June 14, 2026 at 6:28 pm

    You shouldn’t have style elements in the body anyway, so you should put the style in a style sheet. As an id should be unique in the page, you would use a class.

    If you don’t want to do that, then there is no reason to have a style tag either. Just put the style in the element:

    <div style="font-weight:bold;background-color:purple;height:20px;">
      <div>Blah Blah Blah!</div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basic question here, just got curious about coding a simple formula out, and wanted
Very basic question - how to get one value from a generator in Python?
Basic question: what design principles should one follow when choosing between using a class
Basic question before I get too far into coding. I was told once that
Basic question about the way git works. I clone a repo, make some changes,
Basic question : How to I create a bidirectional one-to-many map in Fluent NHibernate?
Basic question about regex mechanics: I have the following expression: [10]*1[10]* . Would this
Very basic question about iPhone memory management: Say I have a viewController with several
Very basic question. I have a portal containing several servlets, one of which takes
A basic question about AND logical operator. I'm trying to extract some fields in

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.