26

I have created a CSS style class:

.btn { 
  color:#050; 
  font: bold 84% 'trebuchet ms',helvetica,sans-serif; 
  background-color:#fed; 
  border:1px solid; 
  border-color: #696 #363 #363 #696; 
} 

How can I apply this CSS style class to all buttons which are present in the page without adding class="btn" to every button?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ashu
  • 261
  • 1
  • 3
  • 3

5 Answers5

41

If your buttons are <input type="button"> or submit, then this should do it:

input[type="button"], input[type="submit"] { 
    color:#050; 
    font: bold 84% 'trebuchet ms',helvetica,sans-serif; 
    background-color:#fed; 
    border:1px solid; 
    border-color: #696 #363 #363 #696; 
} 

Otherwise, if your buttons are <button>:

button { 
    color:#050; 
    font: old 84% 'trebuchet ms',helvetica,sans-serif; 
    background-color:#fed; 
    border:1px solid; 
    border-color: #696 #363 #363 #696; 
}

To grab all three kinds of buttons:

button, input[type="button"], input[type="submit"] { 
    color:#050; 
    font: bold 84% 'trebuchet ms',helvetica,sans-serif; 
    background-color:#fed; 
    border:1px solid; 
    border-color: #696 #363 #363 #696; 
} 
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
12
button{ 
  color:#050; 
  font: bold 84% 'trebuchet ms',helvetica,sans-serif; 
  background-color:#fed; 
  border:1px solid; 
  border-color: #696 #363 #363 #696; 
} 

OR

input[type="button"]{ 
  color:#050; 
  font: bold 84% 'trebuchet ms',helvetica,sans-serif; 
  background-color:#fed; 
  border:1px solid; 
  border-color: #696 #363 #363 #696; 
} 
Aman
  • 4,786
  • 1
  • 17
  • 17
  • 2
    The second example is **not** CSS3. The attribute selector already exists in CSS2.1. It was only expanded in CSS3. http://www.w3.org/TR/CSS2/selector.html#attribute-selectors – Yi Jiang Aug 22 '10 at 06:04
  • IMO, this item deserves to be marked as the answer; IOW, I think it deserves a green check mark. – David A. Gray Jan 28 '16 at 02:19
3
input[type=submit], input[type=button], button {
   ...
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
2

If you don't want to explicitly set all the styles, but want to use the styles already defined in some pre-existing class (for example the bootstrap btn-default class), you could do something like this:

<script>
$("button").addClass("btn-default");
</script>

In my case this was what I was looking for, although I'm sure there is some sort of caveat about doing it this way, or you'd expect it was something doable directly from CSS.

Josh Sutterfield
  • 1,943
  • 2
  • 16
  • 25
1

just a note

input[type="button"]

isn't going to work on old browsers like IE6. If that's a problem you will unfortunately need to add your class to each item.

Stuart Burrows
  • 10,824
  • 2
  • 34
  • 33