Tips for modifying the text color of radio button choices in HTML and CSS

I have set a black background image for my HTML page. How can I change the radio button labels/texts to white, similar to the questions? This is the code snippet that shows how it currently looks on the page: View Image

<hr>
<label for="" style="color:white">Cigarette smoking status</label><br><br>
<input type="radio" name="cig-stat" style="color:white" value="0" id="never-smoke" required>Never Smoked Cigarettes<br>
<input type="radio" name="cig-stat" style="color:white" value="1" id="curr-smoker">Current Cigarette Smoker<br>
<input type="radio" name="cig-stat" style="color:white" value="2" id="former-smoker">Former Cigarette Smoker<br>
<hr>

Answer №1

Make sure to enclose the input elements within label tags, which should also include the text for each radio button, and then style them accordingly.

By the way, it is always recommended to use an external stylesheet for styling rather than inline styles. This not only helps in keeping the code clean but also avoids unnecessary repetition of the same styles across different elements.

body {
  background: #555;
}
<hr>
<label for="" style="color:white">Cigarette smoking status</label><br><br>
<label for="cig-stat" style="color:white"><input type="radio" name="cig-stat" value="0" id="never-smoke" required>Never Smoked Cigarettes</label><br>
<label for="cig-stat" style="color:white"><input type="radio" name="cig-stat" value="1" id="curr-smoker">Current Cigarette Smoker</label><br>
<label for="cig-stat" style="color:white"><input type="radio" name="cig-stat" value="2" id="former-smoker">Former Cigarette Smoker</label><br>
<hr>

Answer №2

To begin, I recommend structuring your code in the following way:

<fieldset>
  <legend>Cigarette smoking status</legend>

  <div>
    <input type="radio" name="cig-stat" value="0" id="never-smoke" required />
    <label for="never-smoke">Never Smoked Cigarettes</label>
  </div>
  
  <div>
    <input type="radio" name="cig-stat" value="1" id="curr-smoker" />
    <label for="curr-smoker">Current Cigarette Smoker</label>
  </div>

  <div>
    <input type="radio" name="cig-stat" value="2" id="former-smoker" />
    <label for="former-smoker">Former Cigarette Smoker</label>
  </div>
</fieldset>

After setting up the structure, you can customize the appearance of the <legend> and <label> elements by changing the text color to white. It's advisable to separate the CSS styling from the HTML markup for better organization, but if needed, you can include them inline.

For a demonstration, check out this live example:

https://jsfiddle.net/1k3gte76/

If you prefer not to have a white border around the <fieldset> element, simply add a rule like border: none to remove it.

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

Navigate through JSON data to add to an HTML table

HTML <table id="id_kbdata" cellspacing="0" cellpadding="0" > </table> JSON [ { "id":"3", "title":"Doing Business In...", "businessSubjectAreas":[ { "businessSubjectArea":"Market and Sell Products/Service" }, ...

Inserting Multiple Inputs

I have a unique application interface that is structured in the following manner: The rows within the application consist of either group headings (displayed as rows with single inputs) or ingredient forms (comprising a small input field, followed by a se ...

What is the best way to extract value from an input text that is being repeated using ng-repeat within a Tr

Having a table with repeated rows in angular, you can also dynamically add new rows by clicking a button. If there are already 3 repeated rows and 2 extra rows are added with data entered, how can we retrieve all the data from the table in the controller ...

The method window.scrollTo() may encounter issues when used with overflow and a height set to 100vh

Suppose I have an HTML structure like this and I need to create a button for scrolling using scrollTo. However, I've come across the information that scrollTo doesn't work well with height: 100vh and overflow: auto. What would be the best way to ...

Prevent users from copying and pasting text or right-clicking on the website

I have been struggling to completely disable the ability for users to copy and paste or select text on my website across all devices. After much searching, I came across a solution that I implemented on my site. Below the <body> tag, I inserted the ...

What is the best way to dynamically update the selected option in a dropdown menu within a Rails application using a variable?

Working on a project that involves a select drop-down menu containing a list of currencies. Want to enhance user experience by automatically selecting the default value in the dropdown based on the user's country (will be utilizing the geoip gem). To ...

Having trouble getting the Click Element with CSS selector to function properly in Google Tag Manager?

How can I detect a click on the specified element by using the "Click Element" variable in Google Tag Manager? <a href="https://amazon.in/product-id" target="_blank" class="amazon-btn"> <div> <span&g ...

"Trouble with the external CSS file - it's not

I have a question regarding my jsp file, top.jsp, and css file, top.css. Despite specifying a background color in top.css, the color is not showing up on my top.jsp file. Code snippet from top.css: @CHARSET "ISO-8859-1"; body { background-color: bl ...

Animate the smooth transition of a CSS element when it is displayed as a block using JQuery for a

I have implemented the collapsible sections code from W3 School successfully on my website. Now, I am trying to achieve a specific functionality where the "Open Section 1 Button" should slide down with a margin-top of 10px only if the first section "Open S ...

Tips for removing the error hint when focusing on input fields in VueJS

Having an issue with an error popping up when I focus on my input. Here's the code snippet: <v-text-field v-model="models.codePostal" label="Code postal" :rules="regles.codePostal" :hint="this.models.communeRe ...

I can't seem to figure out why I constantly struggle with adding a check mark to a checkbox using

Take a look at the code I've provided below : HTML : <input type="checkbox" name="xyz[1][]" id="sel_44" style="margin:2px;" value="12345" onclick="myClick(this)"> Javascript : <script> $('#sel_44').attr("checked", true); < ...

I'm struggling to find a way to turn off the scroll bar

I'm having trouble disabling the scroll bar on my website for specific reasons. Can anyone provide me with some guidance? <iframe src="video" width="960"height="540" frameborder=0 ></iframe> ...

The CSS for the UI Grid is malfunctioning

I have been working on creating a grid using UI Grid (recent version of ngGrid) within my current project. However, I am facing some issues with the display. The icons like angle down and row selected icon are not showing up correctly when I include the CS ...

What is the method to retrieve the selected value from a drop-down menu that is connected to JSON keys?

I am just starting to learn AngularJS and I need help with binding column names (keys from key-value pairs) to a select list. I want to be able to retrieve the key name when the selection in the select list is changed. The select list displays: name, snip ...

PHP overall outcome

I have created a form that includes checkboxes, but I am facing difficulty in calculating the total result based on the selections. Ideally, if a user selects three checkboxes, the total should be $3, and if only one checkbox is selected, the total should ...

Exploring how to display JSON objects containing spaces in an HTML table

Sorry if this question has been asked already, but I can't find the answer. I'm brand new to html/java/django and struggling with a seemingly simple issue. I am developing a web application that retrieves json data from firebase using python/pyre ...

Content not aligned in the center of the page on Internet Explorer

Recently, I've taken on the responsibility of managing the content for this website after it was passed down from the previous developer. Each page's content is contained within a div element with the class "ct", which has auto margins applied t ...

Guide to finding the parent component of a particular element?

When the add button is clicked, I am dynamically adding lists and a new button labeled as "New Button" to a web page. My goal is to have a variable printed inside the parent list when this "New Button" is clicked. How can I achieve this functionality? Bel ...

Navigating between divs with a 100% height using up and down movements

I am working on a website that is structured into different sections using divs with shared classes but unique IDs. Each div is set to take up 100% of the viewport height. To navigate between these sections, I want to provide Up/Down arrow buttons for user ...

Is there a problem with css3 opacity transition in Webkit browsers?

While exploring alternative ways to modify the Bootstrap 3 carousel, I stumbled upon a technique that achieves a 'fade' effect instead of the usual 'slide'. However, in webkit browsers, the transition may appear choppy or flicker betwee ...