How can you achieve a vertical list display for a label while preserving its inline-block properties?

I have chosen to use labels for my radio buttons because I enjoy the convenience of being able to click on the text to select the option. However, I want these labels to appear in a vertical list format. Currently, they are displaying as blocks, causing them to spread across the entire page and allowing users to accidentally activate an option by clicking on the white space next to the label.

I am aware that I can wrap the labels in a div and set the display property to block while setting the contained label's display property to inline-block. While this solution works, it would require me to make adjustments to hundreds of labels, so I would prefer to achieve the desired layout using only CSS if possible.

Alternatively, I tried using float: left and clear: both to achieve the desired effect. However, this caused issues with other elements on the page not displaying properly and being significantly impacted by these changes.


                label {
                  display: block;
                  cursor: pointer;
                }
           

                <label>
                  <input type="radio"/>
                  answer
                 </label>
                 <label>
                  <input type="radio"/>
                  answer
                 </label>
                 <label>
                  <input type="radio"/>
                  answer
                 </label>
                 <label>
                  <input type="radio"/>
                  answer
                 </label>
             

Answer №1

SOLUTION:

To fill the rest of the line, you can use the ::after pseudo element in this way:

label::after {
  content: "";
  display: block;
}

Check out the solution on jsFiddle.


CODE SNIPPET:

label {
  cursor: pointer;
}
label::after {
  content: "";
  display: block;
}
<label>
  <input type="radio" />answer
</label>
<label>
  <input type="radio" />answer
</label>
<label>
  <input type="radio" />answer
</label>
<label>
  <input type="radio" />answer
</label>

SIDE NOTE:

If you require older browser support, make use of :after instead of ::after. Remember that one is CSS2 syntax and the other CSS3. In CSS3, there was a change to distinguish between psuedo-elements and pseudo-classes.

Take note that IE8 only supports the single-colon CSS 2.1 syntax (i.e., :pseudo-class) and does not recognize the double-colon CSS3 syntax (i.e., ::pseudo-element).

Answer №2

experiment with including
tag after every element and delete display:block property..

label::after {
    content: "<br>";
}

Answer №3

To ensure the label stretches to its parent's width, apply the following CSS properties: display:block, width:100%, and text-align: center; for horizontal alignment of text within the label.

label{
  display:block; 
  width:100%; 
  text-align: center;
}

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

Struggling to implement custom media queries in a bootstrap theme

I've customized the Twitter Bootstrap layout for my website, and everything is looking great except for the @media queries. Can anyone help me figure out what's going wrong? HTML: <section id="lessons" class="bg-black no-padding lesson-conte ...

Building a bespoke search input for the DataTables JQuery extension

As the title indicates, I am in the process of developing a custom search box for the DataTables JQuery plugin. The default options do not suit my needs, and configuring the DOM is also not ideal as I aim to integrate it within a table row for display purp ...

Tips on effectively centering a wide div

After much experimentation, this is what I came up with: (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) return; js = d.createElement(s); js.id = id; js.src = "//connect.facebook.net/en ...

Conceal the server's internal file structure to prevent it from being displayed in sources

When I visit my website and inspect the elements in Google Chrome, I am able to see a list of all the folders on my site under the sources tab. This reveals their original names and allows for the downloading of the entire website code. I am concerned abo ...

Incorporate the HTML CTA on top of the background image within the email template

In my email template, I am trying to place an HTML CTA over a background image. The code works perfectly on all email clients including mobile and desktop, except for Outlook where the alignment of the CTA copy is off. Below is the code snippet along with ...

Saving Selected Radio button values into Jquery Array

In my HTML table, there are multiple rows with radio buttons representing the sexes in the 0th position of <td>. I am trying to store the values of sex (either 1 or 0) in an array. Below is a snippet for a table with 3 rows. HTML Code: <table> ...

CSS - Absolute positioning appears to be slightly choppy in Microsoft Edge

I have successfully implemented a slip scrolling function to reveal/hide a fixed logo on scroll, but I am facing some issues with Microsoft Edge browser. While testing in various browsers, everything works smoothly except for Microsoft Edge. In this brows ...

Delete all HTML functionalities

Is there a way to strip HTML functions when using a text area so that the content shows as plain text in a new post (div)? For example, if I type "Hello", I want it to appear as "< b > Hello < b / >", showing the whole code. Here is the code snippe ...

The parent element is not able to apply the CSS text-decoration in jQuery

Is there a way to retrieve a specific CSS property of an element, even if it is inherited from its parent? The standard jQuery method .css() can help with this. Consider the following HTML structure: <div id="container"> some text<br> ...

The functionality of jQuery .rotate() appears to be malfunctioning

I'm struggling to get this code working properly. My jQuery version is 2.1.0. Here's the code snippet: $("a.shownav img").rotate(180); Any suggestions on how to make it function correctly without relying on a plugin? ...

Using CSS to duplicate images in multiple repetitions horizontally

In the process of developing a UHD project that involves showcasing large images, I have encountered limitations with iOS and certain mobile browsers. While stationary browsers pose no issue, iOS only supports PNG images up to 5 megapixels in resolution, w ...

Discover the art of concurrently listening to both an event and a timer in JavaScript

Upon loading the page, a timer with an unpredictable duration initiates. I aim to activate certain actions when the countdown ends and the user presses a button. Note: The action will only commence if both conditions are met simultaneously. Note 2: To cl ...

Leveraging the power of Material-UI and React, modify the appearance of the "carrot" icon within the

Currently implementing MUI's textfield based on the information found at this link: https://mui.com/components/text-fields/. While there are numerous resources available for changing the font of the input text, I have not come across any documentation ...

Is it possible to apply two ngClass directives to a single div element in Angular 4?

When I click to start editing, a component is called for the editing process. In this component, I am unable to click on anything and the background turns black, which is okay. However, I would like for each ID that I select to edit to become active with a ...

The width of an HTML input and button elements do not match

Currently, I am facing an unusual issue where my input and button tags seem to have the same width assigned to them (width: 341.5px; calculated from $(window).width() / 4) in the code, but visually they appear to be of different widths. Here is a visual re ...

Seeking assistance in optimizing my Javascript code for more efficient canvas rendering

I've created a script for generating random moving lines as a background element for my portfolio. While it runs smoothly on its own, I encounter frame drops when combining it with other CSS animations and effects, especially at the beginning (althoug ...

Using Jquery for a Second Timer

I have a jQuery function that looks like the one below. The result is displayed in a span, but when the page is reloaded, this span briefly disappears and then reappears. Is there a way to prevent this from happening? I need it to stay visible at all tim ...

Tips on updating the arrow icon after clicking on a dropdown menu

HTML: <div class="customSelectContainer"> <ul> <li class="initial">Choose</li> <li data-value="value 1">Option 1</li> <li data-value="value 2">Option 2& ...

Make dark mode the default setting in your Next JS application

In my Next JS application, I have implemented both isDarkMode and handleDarkMode functions. Within the Header component, there is a toggle button that allows users to switch between light and dark modes. <ThemeContainer> <label classN ...

Adjust the stroke and fill color of an SVG upon hovering over it

I have a unique SVG image with an intricate stroke around it that matches the color of a filled icon. Positioned on a black background within the image, you can view my example here: https://jsfiddle.net/o48629qs/ The challenge I am facing involves changi ...