CSS Forms
In a form example, we use CSS to render HTML form elements:
CSS example
input
[
type
=
text
]
,
select
{
width:
100
%
;
padding:
12
px
20
px
;
margin:
8
px
0
;
display:
inline-block
;
border:
1
px
solid
#ccc
;
border-radius:
4
px
;
box-sizing:
border-box
;
}
input
[
type
=
submit
]
{
width:
100
%
;
background-color:
#4CAF50
;
color:
white
;
padding:
14
px
20
px
;
margin:
8
px
0
;
border:
none
;
border-radius:
4
px
;
cursor:
pointer
;
}
input
[
type
=
submit
]
:hover
{
background-color:
#45a049
;
}
div
{
border-radius:
5
px
;
background-color:
#f2f2f2
;
padding:
20
px
;
}
Try it »
Input field (input) styles
Use the width property to set the width of the input field:
CSS example
input
{
width:
100
%
;
}
Try it »
In the example above, all elements have a width of 100%, but if you only want to set a specific type of input, you can use the following attribute selectors:
input[type=text]- Selects text input fieldsinput[type=password]- Selects password input fieldsinput[type=number]- Selects number input fields- ...
Input field padding
Use padding property can add padding to the input field.
CSS example
input
[
type
=
text
]
{
width:
100
%
;
padding:
12
px
20
px
;
margin:
8
px
0
;
box-sizing:
border-box
;
}
Try it »
Note that we set box-sizing attribute to border-box. This ensures that the browser renders the input field with the specified width and height, including the border and padding in the calculation.
For more content, you can read CSS3 Box Sizing 。
Input field (input) border
Use border property can modify the size or color of the input border, and use the border-radius property to add rounded corners to the input:
CSS example
input
[
type
=
text
]
{
border:
2
px
solid
red
;
border-radius:
4
px
;
}
Try it »
If you only want to add a bottom border, you can use border-bottom Properties:
CSS example
input
[
type
=
text
]
{
border:
none
;
border-bottom:
2
px
solid
red
;
}
Try it »
Input field (input) color
You can use the background-color property to set the background color of the input field, and thecolor property to modify the text color:
CSS example
input
[
type
=
text
]
{
background-color:
#3CBC8D
;
color:
white
;
}
Try it »
Input field (input) focus
By default, some browsers show a blue outline when an input field gains focus (when clicked). We can set the input style to outline: none; to ignore this effect.
Use :focus selector can set the style of the input field when it gains focus:
CSS example
input
[
type
=
text
]
:focus
{
background-color:
lightblue
;
}
Try it »
CSS example
input
[
type
=
text
]
:focus
{
border:
3
px
solid
#555
;
}
Try it »
Input field (input) icon
If you want to add an icon to the input field, you can use the background-image property and thebackground-position property for positioning. Note the left margin set for the icon to give it some space:
CSS example
input
[
type
=
text
]
{
background-color:
white
;
background-image:
url
('
searchicon
.
png
')
;
background-position:
10
px
10
px
;
background-repeat:
no-repeat
;
padding-left:
40
px
;
}
Try it »
Animated search box
The following example uses the CSS transition property, which makes the input field expand to the right when it gains focus. You can see more in the CSS Animations chapter.
CSS example
input
[
type
=
text
]
{
webkit-transition:
width
0.4
s
ease-in-out
;
transition:
width
0.4
s
ease-in-out
;
}
input
[
type
=
text
]
:focus
{
width:
100
%
;
}
Try it »
Textarea styles
Note: Use resize property to disable the textarea's resizing feature (normally you can resize by dragging the bottom-right corner).
CSS example
textarea
{
width:
100
%
;
height:
150
px
;
padding:
12
px
20
px
;
box-sizing:
border-box
;
border:
2
px
solid
#ccc
;
border-radius:
4
px
;
background-color:
#f8f8f8
;
resize:
none
;
}
Try it »
Dropdown (select) styles
CSS example
select
{
width:
100
%
;
padding:
16
px
20
px
;
border:
none
;
border-radius:
4
px
;
background-color:
#f1f1f1
;
}
Try it »
Button styles
CSS example
input
[
type
=
button
]
,
input
[
type
=
submit
]
,
input
[
type
=
reset
]
{
background-color:
#4CAF50
;
border:
none
;
color:
white
;
padding:
16
px
32
px
;
text-decoration:
none
;
margin:
4
px
2
px
;
cursor:
pointer
;
}
/*
Tip: Use width: 100% to set a full-width button
*/
Try it »
For more content, you can refer to our CSS Buttons the tutorial.
Responsive forms
Responsive forms can rearrange elements based on the browser window size; we can see the effect by resizing the browser window:
Advanced: The following example usesCSS3 Media Queries to create a responsive form.
CSS example
{
box-sizing:
border-box
;
}
input
[
type
=
text
]
,
select
,
textarea
{
width:
100
%
;
padding:
12
px
;
border:
1
px
solid
#ccc
;
border-radius:
4
px
;
resize:
vertical
;
}
label
{
padding:
12
px
12
px
12
px
0
;
display:
inline-block
;
}
input
[
type
=
submit
]
{
background-color:
#4CAF50
;
color:
white
;
padding:
12
px
20
px
;
border:
none
;
border-radius:
4
px
;
cursor:
pointer
;
float:
right
;
}
input
[
type
=
submit
]
:hover
{
background-color:
#45a049
;
}
.container
{
border-radius:
5
px
;
background-color:
#f2f2f2
;
padding:
20
px
;
}
.col-25
{
float:
left
;
width:
25
%
;
margin-top:
6
px
;
}
.col-75
{
float:
left
;
width:
75
%
;
margin-top:
6
px
;
}
/*
Clear floats
*/
.row
:after
{
content:
""
;
display:
table
;
clear:
both
;
}
/*
Responsive layout - when the screen width is less than 600px, set elements to stack vertically
*/
@media
screen
and
(max-width: 600
px
)
{
.col-25
,
.col-75
,
input
[
type
=
submit
]
{
width:
100
%
;
margin-top:
0
;
}
}
Try it »