Note that we have set the box-sizing property to
border-box. This makes sure that the padding and eventually borders are included in the
total width and height of the elements.
Bordered Inputs
You can modify the size and color of a border by using the "border" property. Additionally, you can create rounded corners for an element using the "border-radius" property.
To give a color to the area behind your input, use the 'background-color' property. And if you want to change the color of the text, use the 'color' property.
In some web browsers, when you click on an input field, it may have a blue border around it. You can make this border go away by using this code: outline: none;.
You can apply changes to the input field when someone clicks on it by using the ":focus" selector in your HTML code.
If you'd like to place an icon inside an input field, you can achieve this by using the 'background-image' property. You can control the icon's placement within the input using the 'background-position' property. It's important to mention that we include a significant amount of left padding to make room for the icon.
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 1px solid #555;
outline: none;
}
input[type=text]:focus {
background-color: lightblue;
}
</style>
</head>
<body>
<h2>Input fields with color on :focus</h2>
<p>In this case, the input field changes color when it is selected (clicked on).</p>
<form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="John">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname" value="Doe">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
box-sizing: border-box;
border: 3px solid #ccc;
-webkit-transition: 0.5s;
transition: 0.5s;
outline: none;
}
input[type=text]:focus {
border: 3px solid #555;
}
</style>
</head>
<body>
<h2>Input fields with black border on :focus</h2>
<p>In this code, the input field turns black around its border when someone clicks on it. Additionally, we've included the CSS transition property to create a smooth animation effect for the border color change. This transition takes 0.5 seconds to complete when the input field is clicked.</p>
<form>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname" value="John">
<label for="lname">Last Name</label>
<input type="text" id="lname" name="lname" value="Doe">
</form>
</body>
</html>