Styling Textarea
Styling buttons is an essential aspect of web development, as buttons are one of the primary ways users interact with a web application.
Basic Styling
Native Button Element
You can apply basic styles directly to the <button>
element to make it visually appealing and consistent with your application's design.
button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 4px;
background-color: #007bff;
color: white;
cursor: pointer;
}
Button Variants
You can create different button variants by using classes.
.button-primary {
background-color: #007bff;
}
.button-secondary {
background-color: #6c757d;
}
.button-success {
background-color: #28a745;
}
Interactive States
You can also style the button for different states like hover, focus, active, and disabled.
button:hover {
background-color: #0056b3;
}
button:focus {
outline: none;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
button:active {
background-color: #004085;
}
button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
Icon Buttons
You can also include icons in buttons for better visual cues. You can use icon libraries like FontAwesome or SVGs.
<button class="button-icon">
<i class="fa fa-play"></i> Play
</button>
.button-icon i {
margin-right: 5px;
}
Accessibility
- Labels: If the button's purpose is not clear from its content, use the
aria-label
attribute. - ARIA Attributes: Use ARIA attributes like
aria-pressed
for toggle buttons.
Example HTML
Here's how your HTML might look:
<button class="button-primary">Primary</button>
<button class="button-secondary">Secondary</button>
<button class="button-success">Success</button>
Complete Example
Combining all the above, here's a complete example:
- html
- css
index.html
<html>
<head>
<link rel="stylesheet" type="text/css" href="./main.css" >
</head>
<body>
<header></header>
<form>
<span>
<label for="fName">First Name</label>
<input id="fName" name="fName" pattern="[A-Za-z]{3,}" type="text" >
</span>
<div>
<p> Select your gender</p>
<span>
<input id="male" type="radio" name="gender" >
<label for="male">Male</label>
</span>
<span>
<input id="female" type="radio" name="gender" >
<label for="female">FeMale</label>
</span>
</div>
<div>
<p> Select languages you can speak</p>
<span>
<input id="english" type="checkbox" name="languages" >
<label for="english">English</label>
</span>
<span>
<input id="spanish" type="checkbox" name="languages" >
<label for="spanish">Spanish</label>
</span>
<span>
<input id="hindi" type="checkbox" name="languages" >
<label for="hindi">Hindi</label>
</span>
</div>
<div>
<p> Select your country</p>
<span>
<label for="country">Country:</label>
<select id="country">
<option>Australia</option>
<option>Finland</option>
<option>India</option>
<option>Spain</option>
</select>
</span>
</div>
<br/>
<div>
<span>
<label for="address">Enter your full address</label><br/>
<textarea id="address" placeholder="Enter your full address"></textarea>
</span>
</div>
<br/>
<div>
<button class="button-primary">Primary</button>
<button class="button-secondary">Secondary</button>
<button class="button-success">Success</button>
</div>
</form>
</body>
</html>
main.css
/** -------- Text Styles start here ------**/
input[type="text"] {
font-family: Arial, sans-serif;
font-size: 16px;
color: #333;
border: 1px solid #ccc;
border-radius: 4px;
outline: none;
background-color: #fff;
padding: 10px;
margin: 5px 0;
width: 100%;
height: 40px;
}
input[type="text"]:hover {
border-color: #777;
}
input[type="text"]:focus {
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
input[type="text"]::placeholder {
color: #aaa;
font-style: italic;
}
input[type="text"]:valid {
border-color: green;
}
input[type="text"]:invalid {
border-color: red;
}
input[type="text"]:disabled {
background-color: #eee;
cursor: not-allowed;
}
/** == Text Styles end here ==**/
/** -------- Radio button style starts here -------- **/
/* Hide native radio */
input[type="radio"] {
opacity: 0;
position: absolute;
}
/* Custom radio button */
input[type="radio"] + label {
position: relative;
padding-left: 35px;
cursor: pointer;
}
input[type="radio"] + label::before {
content: "";
position: absolute;
left: 0;
top: 0;
width: 25px;
height: 25px;
border: 2px solid #ccc;
border-radius: 50%;
}
input[type="radio"]:checked + label::after {
content: "";
position: absolute;
left: 9px;
top: 9px;
width: 11px;
height: 11px;
background-color: #007bff;
border-radius: 50%;
}
/* Interactive states */
input[type="radio"]:hover + label::before {
border-color: #007bff;
}
input[type="radio"]:focus + label::before {
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
input[type="radio"]:disabled + label {
opacity: 0.6;
cursor: not-allowed;
}
/** == Radio button styles end here ==**/
/** -------- Checkbox button style starts here -------- **/
/* Hide native checkbox */
input[type="checkbox"] {
opacity: 0;
position: absolute;
}
/* Custom checkbox */
input[type="checkbox"] + label {
position: relative;
padding-left: 35px;
cursor: pointer;
}
input[type="checkbox"] + label::before {
content: "";
position: absolute;
left: 0;
top: 0;
width: 25px;
height: 25px;
border: 2px solid #ccc;
border-radius: 4px;
}
input[type="checkbox"]:checked + label::after {
content: "";
position: absolute;
left: 9px;
top: 5px;
width: 8px;
height: 15px;
border: solid white;
border-width: 0 3px 3px 0;
transform: rotate(45deg);
background-color: #007bff;
}
/* Interactive states */
input[type="checkbox"]:hover + label::before {
border-color: #007bff;
}
input[type="checkbox"]:focus + label::before {
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
input[type="checkbox"]:disabled + label {
opacity: 0.6;
cursor: not-allowed;
}
/** == Checkbox button styles end here ==**/
/** -------- Select tag style starts here -------- **/
/* Basic styling */
select {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
appearance: none;
background-image: url('arrow.png');
background-repeat: no-repeat;
background-position: right center;
}
/* Custom wrapper */
.select-wrapper {
position: relative;
}
.select-wrapper::after {
content: "\25BC";
position: absolute;
right: 10px;
top: 50%;
transform: translateY(-50%);
pointer-events: none;
}
/* Interactive states */
select:hover {
border-color: #007bff;
}
select:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
select:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/** == Select tag styles end here ==**/
/** -------- textarea style starts here -------- **/
/* Basic styling */
textarea {
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
line-height: 1.5;
resize: vertical;
}
/* Placeholder */
textarea::placeholder {
color: #aaa;
font-style: italic;
}
/* Interactive states */
textarea:hover {
border-color: #007bff;
}
textarea:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
textarea:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/** == textarea styles end here ==**/
/** -------- button style starts here -------- **/
/* Basic styling */
button {
padding: 10px 20px;
font-size: 16px;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
}
/* Button variants */
.button-primary {
background-color: #007bff;
}
.button-secondary {
background-color: #6c757d;
}
.button-success {
background-color: #28a745;
}
/* Interactive states */
button:hover {
background-color: #0056b3;
}
button:focus {
outline: none;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);
}
button:active {
background-color: #004085;
}
button:disabled {
opacity: 0.6;
cursor: not-allowed;
}
/* Icon buttons */
.button-icon i {
margin-right: 5px;
}
/** == textarea styles end here ==**/