An illustration of a custom radio button on w3schools demo

Check out this custom radio buttons example on the w3schools.com website: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_custom_radio

I am curious about why there is a separate .container .checkmark:after statement in the code. Wouldn't it be more concise to include these styles directly in the .checkmark:after statement?

Here's the full code example:

/* The container */
.container {
    display: block;
    position: relative;
    padding-left: 35px;
    margin-bottom: 12px;
    cursor: pointer;
    font-size: 22px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* Hide the browser's default radio button */
.container input {
    position: absolute;
    opacity: 0;
}

/* Create a custom radio button */
.checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 25px;
    width: 25px;
    background-color: #eee;
    border-radius: 50%;
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
    background-color: #ccc;
}

/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
    background-color: #2196F3;
}

/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
    content: "";
    position: absolute;
    display: none;
}

/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after {
    display: block;
}

/* Style the indicator (dot/circle) */
.container .checkmark:after {
 top: 9px;
left: 9px;
width: 8px;
height: 8px;
border-radius: 50%;
background: white;
}
<h1>Custom Radio Buttons</h1>
<label class="container">One
  <input type="radio" checked="checked" name="radio">
  <span class="checkmark"></span>
</label>
<label class="container">Two
  <input type="radio" name="radio">
  <span class="checkmark"></span>
</label>
<label class="container">Three
  <input type="radio" name="radio">
  <span class="checkmark"></span>
</label>
<label class="container">Four
  <input type="radio" name="radio">
  <span class="checkmark"></span>
</label>

Answer №1

There is no definitive explanation for this phenomenon, but it seems that w3schools has a few key reasons behind their actions:

  • The initial rule involving .checkmark:after serves mainly to define the appearance, position, and display of elements without affecting other styles. This allows for consistent styling across all checkboxes on the site, even when additional changes are made.

  • In the case of a larger website with multiple input fields, using an invisible pseudo element is not problematic since modern browsers do not fully render elements with display: none upon page load. This approach enables precise control over which inputs receive specific styles.

This strategy revolves around separating style implementation from functional logic.

Answer №2

In the example above, it is possible to exclude ".container" because all radio buttons are enclosed within the container class, allowing for the same CSS styling to be applied.

However, what if you want different styles and the radio buttons are not within the same container class?

If you use "input:checked ~ .checkmark:after" without specifying a specific container, the styling will apply to all input elements rather than just those inside the container class.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that there are no automatic spaces between the texts entered in the input fields of an HTML CSS form

After designing an HTML CSS form, I incorporated JavaScript and jQuery for the website layout. However, I am encountering difficulty in adding spaces while inputting information on the contact form. You can locate the contact form at the bottom of this p ...

Tips for utilizing Selenium in Python or Requests to retrieve the <img src links and additional data

Hello there, I am looking to gather details about the speakers listed on the website mentioned above. Specifically, I require their Name, Title, Company, image source link, and Description. https://i.stack.imgur.com/66Pn9.png Unfortunately, my current co ...

Remove the excess white space surrounding the header background image on the website above the fold

How do I eliminate the white spaces on the left, right, and top of my background image that covers the header area above the fold on my webpage? I am currently testing in Google Chrome and using VS Code as my editor. html, body { width: 100%; height: ...

The issue persists with the customized Bootstrap carousel featuring thumbnails

By combining http://codepen.io/srkimir/pen/mGbrf and http://codepen.io/transportedman/pen/NPWRGq , I successfully created a fading carousel with thumbnails similar to the one in http://codepen.io/kikibres/pen/gpZoXz . However, when trying to implement ...

Require assistance in getting a slider operational

Hello there! I could really use your assistance with this code. I have been trying to make it work using JavaScript, but I'm determined not to incorporate any external JS files. Here is the snippet of JavaScript code I am working with: "use strict"; ...

Can you provide instructions on how to vertically adjust a bootstrap div?

Here is the code snippet: <div class="container"> <div class="row"> <div class="container1 col-lg-4 col-md-4 col-sm"> <h1 class="status">Status:</h1> </div> & ...

A minimalist web page appearing as blank in Internet Explorer

After testing my simple web page in various browsers, I discovered that it only showed a blank white page in Internet Explorer. I combed through my CSS code and identified background = rgba(some stuff) as the unsupported line by IE. However, this particula ...

Pause for a few moments prior to initializing Bootstrap and Angular 2

I am currently developing a space/gravity game using Angular 2, and I am looking to incorporate a splash screen before the main menu component is loaded. I believe that the easiest approach would be to utilize the pre-bootstrapped contents of the index.ht ...

What is the method for animating the hiding of <details>?

Having successfully used @keyframes to animate the opening of the details tag, I am now facing the challenge of animating the closing. Unfortunately, I have not been able to find a CSS solution for this. The @keyframes for closing are not working as expect ...

Arrange two icons side by side on the same line

How can I align two icons inside the "col" class in a horizontal line rather than stacked on top of each other? Additionally, I want to reorder them using the bootstrap "order" class. Can someone please assist me with this? I've attempted wrapping th ...

using a CSS tag to vertically center text within an element

After using a tag and setting the background color, height, and width for my content, I decided to align the text in the center along the x-axis using text-align: center;. However, I am now trying to figure out how to align it in the middle along the y-ax ...

Shifting an image outside of the container in a Bootstrap 4 card header

I am attempting to create a design similar to this: https://i.sstatic.net/OR4Em.png I am currently using Bootstrap 4 cards, but I am struggling to figure out the best way to move the icon/image outside of the Cards header. I have experimented with differe ...

Guiding the way with headings listed beneath every point

Hello there! I am trying to achieve a specific layout using this image as a reference. In the image, there are 3 different background images (represented by three Divs) and I want to add 3 List Points labeled as "LV", "RP", "IP" with centered descriptions ...

Tips for making text flow around an image

How can I achieve a design where the text wraps around an image positioned in the bottom right corner? [Image Removed by Owner] Your guidance on implementing this effect is greatly appreciated. Thank you. ...

Unresponsive Toggle Button on Bootstrap Navigation Bar

Whenever I resize the window and try to click the navigation toggle button, it doesn't seem to work. The button is unresponsive. Can someone please help me figure out what the issue could be? <nav class="navbar fixed-top navbar-expand-lg navba ...

Display a <div> element styled using CSS3

Question: I am facing a challenge with CSS3 styling. I have one class and one div. The class is currently set to display:none, but I want it to show when the mouse hovers over it (display:block). I have attempted the following: .1:hover + div#2 { dis ...

Masonry List with Next.js Image Fill

I am encountering an issue where the normal HTML img tag works perfectly fine inside my Masonry List. However, when I use the Next.js Image Component with a layout of fill, it fails to render anything. Custom Styles: const ImageList = styled('ul&apos ...

You are unable to apply multiple customizations to Bootstrap in a React application

I'm currently working on a React application that consists of two distinct sections, referred to as Part A and Part B. Each part requires its own set of SCSS rules and Bootstrap Grid System Customization. Specifically, I need Part A to have a 24-colu ...

Creating visually appealing error messages with HTML and CSS

As part of a CSS/HTML challenge, I am trying to customize a validation error message without using JavaScript. My goal is to make it look like this image This is the code I have so far. HTML <p> It only takes a minute to sign up and our free sta ...

When the mouse hovers over the text inside a div, the link does not work in Internet Explorer

Here is some code for demonstration. I'm facing an issue where the link spans over the entire div, which works perfectly in Firefox. However, in IE, when I hover over the text, the cursor changes to a text cursor and the link doesn't work there. ...