CSS Basic
CSS :default Selector
Photo Credit to CodeToFun
🙋 Introduction
The :default
selector in CSS is a pseudo-class used to target the default form element in a group of similar form elements.
It applies specifically to form controls that are pre-selected or set as the default option when the page loads, such as the default radio button in a group or the default option in a dropdown menu.
💡 Syntax
The signature of the :default
Selector is as follows:
:default {
/* CSS properties */
}
The :default
pseudo-class can be applied to elements like radio buttons, checkboxes, and options within a <select> element that are marked as the default.
📝 Example
Here is an example of how to use the :default
selector in CSS:
☠️ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :default Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<form>
<p>Choose your preferred contact method:</p>
<input type="radio" id="email" name="contact" value="email" checked>
<label for="email">Email</label>
<input type="radio" id="phone" name="contact" value="phone">
<label for="phone">Phone</label>
<p>Select a country:</p>
<select>
<option value="us" selected>United States</option>
<option value="uk">United Kingdom</option>
<option value="in">India</option>
</select>
</form>
</body>
</html>
🎨 CSS
/* Style for the default radio button or select option */
:default {
border: 2px solid blue;
background-color: #e0f0ff;
}
In this example:
- The radio button labeled "Email" is marked as the default and receives a blue border and a light blue background.
- The "United States" option in the dropdown is also the default and gets the same styles.
💬 Usage Tips
- The
:default
pseudo-class is particularly useful when you want to highlight the default choice in a group of options or inputs. - You can use
:default
in combination with other pseudo-classes or selectors to fine-tune the styling of default elements, such asinput:default
oroption:default
.
⚠️ Common Pitfalls
- Not all form elements support the
:default
pseudo-class. It is primarily useful for radio buttons, checkboxes, and options within a<select>
element. - Avoid confusing
:default
with thechecked
orselected
attributes, which reflect user interaction. The:default
selector only applies to the default state as defined in the HTML markup when the page loads.
🎉 Conclusion
The :default
selector allows you to style the pre-selected form elements, making it easier for users to identify the default choices on a webpage.
By using :default
, you can ensure that default options stand out visually, improving usability and making the form easier to navigate.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS :default Selector), please comment here. I will help you immediately.