1

Over 2 years i have built my website where each JSP page has its own css defined. Now as part of performance optimization i would like to merge these CSS into one css file. How can i merge it without getting affected by same selector names?

For example i have multiple JSP pages define the following selector

table.myitemlisttable {
    width: 100%;
    margin: 10px auto;
    padding: 0px;
    border: 0px;
    border-collapse: collapse;
    background-color: rgba(0, 0, 0, .7);
}

where the name is same but there are differences in the attributes

3 Answers 3

4

One thing I do to avoid conflicts and keep the number of classes down is give each body tag an id. Then I can use those as limiting selectors to restrict the scope of my rules. Example, you have a Contact Us page, and a Products page. Products gets:

<body id="products">

And Contact Us gets

<body id="contactus">

Each page can have a table with the same class, and in your CSS, you'd just put the id in front of the rules.

#products table.myitemlisttable {
    width: 100%;
    margin: 10px auto;
    padding: 0px;
    border: 0px;
    border-collapse: collapse;
    background-color: rgba(0, 0, 0, .7);
}

#contactus table.myitemlisttable {
    width: 100%;
    margin: 10px auto;
    padding: 0px;
    border: 0px;
    border-collapse: collapse;
    background-color: rgba(255, 255, 255, .7);
}

You could even find things that are consistent on all pages, and pull them out to save lines.

table.myitemlisttable {
    width: 100%;
    margin: 10px auto;
    padding: 0px;
    border: 0px;
    border-collapse: collapse;
    background-color: rgba(0, 0, 0, .7);
}

#products table.myitemlisttable {
    background-color: rgba(0, 0, 0, .7);
}

#contactus table.myitemlisttable {
    background-color: rgba(255, 255, 255, .7);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Absolutely great idea
Is there a way we can do this for javascript functions?
With jQuery, you can add selectors like that. $('.myitemlisttable') to $(#products .myitemlisttable).
2
  1. Change the class or id by appending a page name to the end. E.g. table.myitemlisttable_page_items.
  2. Copy + Paste each snippet of CSS into a single CSS page.
  3. Rename classes and ids as appropriate and remove any duplicate CSS where possible.

Comments

2

Hmm.. I like @misterManSam's answer, but I'd slightly vary it to reduce changing classes in lots of places.

I'd prepend a class or id to my css selector, then wrap (all) my html on each page...

CSS

.page1 table.myitemlisttable
.page2 table.myitemlisttable

HTML

<div class='page1'> ...existing page1... </div>

1 Comment

Ha, and fools seldom differ. :)

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.