1

I have a css class that is called button.blue:

button.blue { background-color: blue; ... }
button.blue:active { background-color: darkblue; ... }
button.blue:hover { background-color: lightblue; ... }

To use it, I do the following:

<button class="blue">hello</button>

Now this allows me to have a blue button that has the text "hello" on it. I want to have blue buttons of all different sizes. I don't want to repeat code. What is the best way to do this?

I've tried googling but my question is too vague I think for me to find an answer. I have tried searching for CSS nested classes, grouping, etc. and tried a few random things but nothing seems to work.

2
  • What exactly is your issue? The code you showed can be applied to many different buttons as is. Commented Sep 19, 2011 at 1:25
  • 1
    Oh, and you should use semantic class names which convey the actual function of the button instead of the intended formatting. Always think of the separation of content in HTML, behavior in Javascript and formatting in CSS. Commented Sep 19, 2011 at 1:28

4 Answers 4

2

You would just have a class for different widths so

.button_1 {width:100px;}
.button_2 {width:200px;}
.button_3 {width:300px;}
.button_4 {width:400px;}

And so forth. Or some other such naming system. You can have more than one class per element so this will work fine.

<button class="blue button_1">hello</button>
Sign up to request clarification or add additional context in comments.

Comments

0

You could have some classes that are just sizes like

.big { width: 100px, height: 100px }
.small { width: 20px, height: 20px }

Then you would do

<button class="big blue">hello</button>

2 Comments

this is what I was looking for ... I didn't know how to setup the HTML either actually (i.e. didn't know I could do class ="big blue"). Sad what happens when one learns web development as fast as possible through just tutorials :/
awesome... try learning from tutorials 10 years ago :( there was no stack overflow in those days.
0

All your properties from button.blue will be inherited by button.blue:active and button.blue:hover. There is no need to repeat code accept for the properties you wish to change.

Comments

0

You are able to chain selectors in css. Something like

button.blue.small { height: 20px; }

will only match if it is on a button with a blue class and a small class

Comments

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.