Loading...

Go Back

Next page
Go Back Course Outline

CSS Full Course


Styling Forms in Css

Styling Forms - CSS Example

This page demonstrates various techniques to style form elements using CSS. Below are examples of form styling, custom controls, validation styles, input types, and focus/active states.

1. Form Element Styling

Here is an example of styling form elements (inputs and buttons):


form {
  width: 300px;
  margin: 0 auto;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
}

input, button {
  width: 100%;
  padding: 10px;
  margin-bottom: 15px;
  border: 1px solid #ccc;
  border-radius: 5px;
  font-size: 16px;
}

input:focus, button:focus {
  border-color: #3498db;
}

button {
  background-color: #3498db;
  color: white;
  cursor: pointer;
}

button:hover {
  background-color: #2980b9;
}
    

Example Output:



2. Custom Form Controls

Creating custom checkboxes and radio buttons:


input[type="checkbox"], input[type="radio"] {
  display: none;
}

.custom-checkbox, .custom-radio {
  position: relative;
  padding-left: 30px;
  cursor: pointer;
}

.custom-checkbox input[type="checkbox"]:checked + .checkbox-label::before,
.custom-radio input[type="radio"]:checked + .radio-label::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 20px;
  height: 20px;
  background-color: #3498db;
  border-radius: 50%;
}

.checkbox-label, .radio-label {
  display: inline-block;
  font-size: 16px;
  padding-left: 10px;
}

.custom-checkbox input[type="checkbox"]:checked + .checkbox-label::before {
  background-color: green;
}

.custom-radio input[type="radio"]:checked + .radio-label::before {
  background-color: orange;
}
    

Example Output:

3. Validation Styles

Styling invalid and valid inputs:


input:valid {
  border: 2px solid green;
}

input:invalid {
  border: 2px solid red;
}

input:focus:invalid {
  background-color: #f8d7da;
}

input:focus:valid {
  background-color: #d4edda;
}
    

Example Output:



4. Styling Input Types

Styling text and number input fields:


input[type="text"], input[type="number"] {
  width: 100%;
  padding: 12px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

input[type="number"] {
  background-color: #f0f0f0;
}

button {
  padding: 10px 20px;
  background-color: #3498db;
  color: white;
  border: none;
  cursor: pointer;
  width: 100%;
}

button:hover {
  background-color: #2980b9;
}
    

Example Output:

5. Focus and Active States

Styling form elements when focused or active:


input {
  width: 100%;
  padding: 12px;
  margin-bottom: 10px;
  border: 1px solid #ccc;
  border-radius: 5px;
  transition: border 0.3s;
}

input:focus {
  border: 2px solid #3498db;
  background-color: #eaf3fe;
}

button:active {
  background-color: #2980b9;
  transform: scale(0.98);
}
    

Example Output:

Go Back

Next page