Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS Buttons

CSS Buttons

In this chapter we introduce how to create buttons with CSS.


Basic Button Style

Example

.button

{

   

background-color:

#4CAF50;

/* Green */

   

border:

none;

   

color:

white;

   

padding:

15px 32px;

   

text-align:

center;

   

text-decoration:

none;

   

display:

inline-block;

   

font-size:

16px;

}

Try it »


Button Color

We can use the background-color property to set the button color:

Example

.button1

{

background-color:

#4CAF50

;

}

/*

Green

*/

.button2

{

background-color:

#008CBA

;

}

/*

Blue

*/

.button3

{

background-color:

#f44336

;

}

/*

Red

*/

.button4

{

background-color:

#e7e7e7

;

color:

black

;

}

/*

gray

*/

.button5

{

background-color:

#555555

;

}

/*

black

*/

Try it »


Button sizes

We can use the font-size property to set the button size:

Example

.button1

{

font-size:

10

px

;

}

.button2

{

font-size:

12

px

;

}

.button3

{

font-size:

16

px

;

}

.button4

{

font-size:

20

px

;

}

.button5

{

font-size:

24

px

;

}

Try it »


Rounded buttons

We can use the border-radius property to set rounded buttons:

Example

.button1

{

border-radius:

2

px

;

}

.button2

{

border-radius:

4

px

;

}

.button3

{

border-radius:

8

px

;

}

.button4

{

border-radius:

12

px

;

}

.button5

{

border-radius:

50

%

;

}

Try it »


Button Border Color

We can use the border property to set the button border color:

Example

.button1

{

background-color:

white

;

color:

black

;

border:

2

px

solid

#4CAF50

;

/*

Green

*/

}

...

Try it »


Button Hover

We can use the :hover selector to change the style of a button when the mouse hovers over it.

Tip: We can use the transition-duration property to set the speed of the "hover" effect:

Example

.button

{

webkit-transition-duration:

0.4

s

;

/*

Safari

*/

transition-duration:

0.4

s

;

}

.button

:hover

{

background-color:

#4CAF50

;

/*

Green

*/

color:

white

;

}

...

Try it »


Button Shadow

We can use the box-shadow property to add a shadow to a button:

Example

.button1

{

box-shadow:

0

8

px

16

px

0

rgba

(

0

,

0

,

0

,

0.2

),

0

6

px

20

px

0

rgba

(

0

,

0

,

0

,

0.19

)

;

}

.button2

:hover

{

box-shadow:

0

12

px

16

px

0

rgba

(

0

,

0

,

0

,

0.24

),

0

17

px

50

px

0

rgba

(

0

,

0

,

0

,

0.19

)

;

}

Try it »


Disabled button

We can use the opacity property to add transparency to a button (looks similar to the effect of the "disabled" property).

Tip: We can add the cursor property and set it to "not-allowed" to set a disabled image:

Example

.disabled

{

opacity:

0.6

;

cursor:

not-allowed

;

}

Try it »


Button Width

By default, the size of a button is determined by its text content (matching the length of the text). We can use the width property to set the width of a button:

Tip: To set a fixed width you can use pixels (px) as the unit; for a responsive button you can set a percentage.

Example

.button1

{

width:

250

px

;

}

.button2

{

width:

50

%

;

}

.button3

{

width:

100

%

;

}

Try it »


Button group

Remove the margins and add float:left to set up a button group:

Example

.button

{

float:

left

;

}

Try it »


Bordered Button Group

We can use the border property to set a bordered button group:

Example

.button

{

float:

left

;

border:

1

px

solid

green

}

Try it »

Example

.btn-group

button

{

background-color:

#04AA6D

;

/*

Green background

*/

border:

1

px

solid

green

;

/*

Green border

*/

color:

white

;

/*

White text

*/

padding:

10

px

24

px

;

/*

Inner spacing,

*/

cursor:

pointer

;

/*

Pointer/hand icon

*/

float:

left

;

/*

Side-by-side floating buttons

*/

}

Try it »


Button Animation

Example

Add an arrow marker when the mouse moves over the button:

Try it »

Example

Add a "ripple" effect on click:

Try it »

Example

Add a "pressed" effect on click:

Try it »

For more button styles you can use the CSS Button Generator:https://c.runoob.com/front-end/6222/