Tips for enhancing the appearance of a label with CSS styling for increased focus

I'm attempting to create a button-like element with a radio button embedded within it. Upon selection of an option, I aim to trigger an active state on the button, changing its background color to #CCC.

Below is the relevant code snippet:

.butOption {
  display: block;
  width: 400px;
  margin: 10px auto;
  border: 1px solid #ccc;
  border-radius: 5px;
}
.butOption:hover {
  background-color: #ccc;
}
.butOption input {
  margin: 20px;
  float: left;
  overflow: auto;
}
<label class="butOption">
  <div class="radioBut">
    <input type="radio" name="item" value="" checked />
  </div>
  <div class="text">
    <p>text</p>
    <p>text</p>
  </div>
</label>
<label class="butOption">
  <div class="radioBut">
    <input type="radio" name="item" value="" checked />
  </div>
  <div class="text">
    <p>text</p>
    <p>text</p>
  </div>
</label>

Answer №1

If you want to achieve this effect, javascript is the way to go. Unfortunately, I have yet to discover a method using only CSS and HTML.

(Check out the demo here)

With javascript, you can handle the click event by removing the active class from the currently active option, and then add the active class to the clicked item's class definition.

JAVASCRIPT

document.getElementById("button1").onclick = function(){
    document.getElementsByClassName("active")[0].className = 'butOption';
    this.className += ' active';
};
document.getElementById("button2").onclick = function(){
    document.getElementsByClassName("active")[0].className = 'butOption';
    this.className += ' active';
};

CSS

.active {
    background: #CCC;
}

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

How to refresh the page when pausing the YouTube iframe API

I'm encountering an issue with my code where I am trying to refresh the page when exiting or pausing full screen mode. Exiting full screen is working as expected, however, pausing using the YouTube iframe API's "onstatechange" event does not seem ...

Retrieve the URL from an iframe using Python and Selenium

On my website's main page, there is an iframe containing the text Code: LWBAD that I need to retrieve. For a clearer visual, refer to the image below: https://i.sstatic.net/DJgbd.png Below is the source code of my main HTML page which includes the ...

Button background must be grayscale, while the text should remain unaffected

I have a variety of buttons with different background images. My goal is to apply a grayscale filter to the backgrounds by default and remove the filter on hover, without affecting the color of the button text. Currently, when I apply the grayscale filter ...

Is there a way to prevent the virtual keyboard from covering up input fields on a webview?

I am currently dealing with an issue on my Genero web-app where the webview of an Angular app is not scrolling under an input when it gets the focus. This makes it difficult for users to see what they are typing. Interestingly, loading the page in a regula ...

Utilizing Javascript in Spotfire for enhanced functionality

Currently, I have a dropdown menu with two options ("START" & "END"). Depending on the selected value from this dropdown, I want to display a specific DIV element. I attempted to use the code below, but unfortunately, it is not functioning as expected ...

Refreshing HTML Form upon Submit using JavaScript

I've been searching through various discussions without any luck, but I'm encountering an issue with a form that successfully submits data to a Google Sheet, yet the input fields retain their content after submission. Here is the code: <form ...

How to use AngularJS directives to replace text with stars (*) in HTML

I am in need of a textarea control that has the ability to be masked, meaning that the text displayed should appear as stars instead of the actual characters. Since I might have multiple textareas in my form, saving the actual text in one variable and usi ...

What is preventing me from adjusting the height of this DIV element?

I'm completely stumped by what's happening here. I've been trying to increase the height of a DIV element with an id of #titleStrip, but it just won't budge. It's beyond frustrating. I thought I knew my way around this kind of thin ...

Crafting a dynamic CSS 'trail' effect when hovering

I am attempting to create a visually appealing CSS menu using primarily CSS, with just a touch of jQuery as well: Here is the concept: +------------------------+ | | | | | +---+ | | | ...

Is the navigation item only clickable within its designated area?

Apologies in advance for the confusion, but I'm facing an issue with the navigation bar on my website. It works perfectly on the Home page when hovered over, but on the login and register pages, it requires hovering over a specific spot under the item ...

Slider that is always being updated cannot be moved by dragging

Currently, I am utilizing React wrapped with cljs+reagent to develop a small application. One key feature I require is a slider that updates roughly every second and can be adjusted by the user. At present, my implementation looks like this: [:div.scrubb ...

Dragging objects on a map is done using jQuery and it causes them to bounce

Currently, I am working on implementing the JQuery Draggable feature. My idea is to create a map where I can attach image Divs to it dynamically using Jquery. So far, I have been successful in adding the Divs and making them draggable around the map effici ...

The image sequence animation only loads once and does not continue indefinitely

Here is my code snippet: I have a sequence of 15 images that should load one after the other in a loop with a specific time interval. However, I am facing a bug where the images keep playing infinitely when the page loads, but I want them to play only once ...

Is it possible for a parent element to inherit the margin of a child element in HTML and

strong text It's puzzling to me why my header shifts downward when I add a margin-top to its child element (.navigation). When I apply padding, the header remains at the top. Can someone please explain why this happens? I've read that setting ov ...

What could be causing the lack of styling in my Express application?

The styles are not rendering correctly on the specified route: 'http://localhost:5000/exams/add-new' However, they are working fine on the following routes: 'http://localhost:5000' 'http://localhost:5000/exams' 'http:// ...

Expanding the last row to ensure its proper width with flex-grow

I have a flexbox with columns that each take up one third of the width. However, due to using flex-grow, the last element does not stick to its column size. I understand why this is happening, but I don't want to use max-width as it's not always ...

Using the Twit package on the client side: a step-by-step guide

I have been utilizing the Twit package for searching tweets. After executing the js file in the console, I am able to see the tweets but when trying to use them on my webpage, an error 'Uncaught ReferenceError: require is not defined' pops up. Th ...

Creating a Vue component that renders within an iframe, even without specifying a src attribute

<iframe id="frame" width="100%" height="100%"> </ifrme> Is there a way to render a component within this iframe by creating an HTML element or using another method? new Vue({ el:'#frame', store:store, router:router, ren ...

Unable to retrieve list using Selenium in C# due to issues with FindElements method

Currently, I am working with C# and NUnit. I have encountered an issue where I need to extract the value from a span element and store it in an ArrayList. This is the code I am using to retrieve the price list: var productprice = driver.FindElements(By. ...

Employing the power of JavaScript to enable the seamless toggle of an overlay

I have a div named "navbar_menu". On clicking this div, I want the visibility of the div named "nav_overlay" to fade in. Upon another click, I want it to fade back and become invisible again. Currently, I have set the div 'nav_overlay" to have ' ...