How to position a label above a radio button using CSS

I have been attempting to position a label above a radio button using CSS.

My desired outcome:

This is the code I am currently using:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
label {
    display:block;
}
</style>
</head>
<body>
<form>
<input type="radio" name="opinions" id="radio-1" />
<label for="radio-1">Yes</label>

<input type="radio" name="opinions" id="radio-2" />
<label for="radio-2">No</label>

<input type="radio" name="opinions" id="radio-3"/>
<label for="radio-3">I don't know</label>
</form>
</body>
</html>

Answer №1

Here is an example of a styled list you can use:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
label {
    display:block;
}
ul {
    list-style: none;
    display: block;
}
ul li {
    float: left;
    display: block;
    width: 85px;
    text-align: center;
}
</style>
</head>
<body>
<form>
<ul>
<li><input type="radio" name="opinions" id="radio-1" />
    <label for="radio-1">Yes</label>
</li>
<li>
<input type="radio" name="opinions" id="radio-2" />
<label for="radio-2">No</label>
</li><li>
<input type="radio" name="opinions" id="radio-3"/>
<label for="radio-3">I don't know</label>
</li>
<ul>
</form>
</body>
</html>

Answer №2

place the content in a div and align it to the center.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style type="text/css">
    label {
        display:block;
    }
    </style>
    </head>
    <body>
    <form>
<div>
    <input type="radio" name="opinions" id="radio-1" />
    <label for="radio-1">Yes</label>
</div>    
    <input type="radio" name="opinions" id="radio-2" />
    <label for="radio-2">No</label>

    <input type="radio" name="opinions" id="radio-3"/>
    <label for="radio-3">I don't know</label>
    </form>
    </body>
    </html>


div{
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

Looking to design this chart using CSS and HTML?

Could someone please help me with resizing the circle border in this code? I am facing difficulties adjusting the size and would appreciate any assistance. By running the code snippet, you can see the issue I am encountering and how it differs from the pro ...

Transferring the offset of jQuery from one element to another results in their positions being misaligned

In the quest for aligning content vertically between two divs, I have encountered a challenge. Despite my efforts to adjust offsets and heights, the new element in the second div still does not align perfectly with the first one. This is evident in the cod ...

Preventing special characters in an input field using Angular

I am trying to ensure that an input field is not left blank and does not include any special characters. My current validation method looks like this: if (value === '' || !value.trim()) { this.invalidNameFeedback = 'This field cannot ...

Having trouble accessing data attribute value with PHP in jQuery

Currently, I am utilizing both Jquery and PHP. In my code, there is a text box which includes a "data attribute" (data-vals) value that I am attempting to pass using jQuery. However, the response I am receiving is always "undefined." I have included my c ...

Is there a way to customize the background color when the Bootstrap 4 toggle switch is in the "checked" position?

I'm struggling to find a way to adjust the background color of the "checked" state for this toggle switch in Bootstrap 4. It utilizes an additional library for toggling between dark and light modes - you can find it here on github. While that function ...

"Challenges encountered while trying to format a table responsively with Bootstrap CSS

My objective is to display two tables side by side, with the right table overflowing horizontally. However, I am currently facing an issue where the second table is being pushed below the first one. Any assistance on resolving this problem would be highly ...

The iPhone screen is unresponsive, causing the webpage to zoom in to the top left corner repeatedly

I've been wracking my brain trying to solve this issue, searching high and low on this site and others for a solution. I've attempted various configurations with the viewport meta tag, removing the fb-root div, ensuring there is no height=100%, b ...

What's the difference in width between Inline-Block and Float?

HTML <ul> <li><a href="#">Item #1</a></li> <li><a href="#">Item #2</a></li> <li><a href="#">Item #3</a></li> <li><a href="#">Item #4</a></li> & ...

Retrieve the value of a nested JSON object using the name of an HTML form field, without the use of eval

I am facing a similar issue to Convert an HTML form field to a JSON object with inner objects, but in the opposite direction. Here is the JSON Object response received from the server: { company : "ACME, INC.", contact : { firstname : "Da ...

Incorporating invisible surprises into a fixed menu that reveal themselves as you scroll

I am working on implementing a top navigation and a sticky sub-navigation feature. The goal is to have the sticky nav become the top nav when the user scrolls down the page, similar to the functionality on this website - The issue I'm facing is with ...

Troubleshooting problem with printing iframes on Firefox

When attempting to print an iframe with a large amount of data using the code below, I'm encountering an issue in Firefox where only the first page is printed. The rest of the pages are not being included in the printout. window.frames[frameID]. ...

send a variable across multiple web pages at the same time

Currently, I have a variable that needs to be passed through multiple PHP pages. The code below effectively sends the variable to one page (index.php). echo "<a href='index.php?PHOTO=".$i."'>select</a>"; However, I am wondering how ...

Differences between Animation Easing in iOS and CSS3 Cubic Bezier Functions

I am currently working on an iOS app and I am trying to improve the animations within it. Having a lot of experience with CSS3 animation and transitions, I want to be able to define my animations in iOS with the same precision that is possible with CSS3 ( ...

What is the best method for clearing DOM variables and storage when a new innerHTML is loaded dynamically in a Single Page Application?

When I load Dynamic HTMLs with JS into a div on an index page, the content of the div gets updated dynamically based on user actions using the JQuery.load function. The loaded HTML includes JS files that are also added to the DOM and work as expected. How ...

css malfunctioning user interface

I'm struggling to figure out how to design a CSS toolbar. My goal is to create a 22x22 button toolbar with 4 buttons. I have this PNG image: and the following code: <style> #nav {background: url(content/images/crtoolbar.png) no-repeat;height: ...

Removing the new line using v-if in Vue.js

I want to insert a v-if in the middle of a sentence without creating a new line. This is what I desire: A / B / C / D However, my current output is: A / B / C / D This is the code I am using: <div> {{ A }} / {{ B }} <p v-if="functi ...

Adjust image size according to the browser's window resize using JQuery

I am trying to make an image resize based on the browser size changes using JQuery. My goal is for the image to scale without losing its aspect ratio or getting cropped. I have incorporated Bootstrap in my HTML structure: <div class="container-fluid" ...

What is the best way to hide my rectangle in HTML?

Just starting out with Wix for my website. I have a block of HTML code that creates a blue rectangle, but I want it to disappear automatically after 30 seconds. What additional code do I need to achieve this effect? Also, is there any unnecessary code I c ...

The collapsible section expands upon loading the page

Trying to implement Disqus as a plugin on my test webpage, I created a collapsible section with a "Show comments" button. The idea was to hide the plugin by default and reveal it only when users click on "Show comments". Despite using the w3schools example ...

Is it beneficial or detrimental to utilize a HTML Form?

Is it permissible to have 1 text box and 2 Label elements that are associated with that one textbox? Imagine the Textbox as a search field. The two Label elements represent two distinct categories you can conduct searches in. Clicking on Label 1 would foc ...