Unable to apply styling to border when clicking on radio button in React

I've created these divs to act like radio buttons:

[![these 4divs][1]][1]

When I click on one of them, I want the border to turn blue like this: border: 2px solid blue;

However, instead, the issue shown below is happening:

[![the problem][2]][2]

How can I fix this and make the border color turn blue as intended?

Code from studies.js:

<ul>
    <li>
        <input type='radio' value= '1' name ='radio' id='radio1'/>
        <label for='radio1'><div className="rcorners2 " ></div></label>
    </li>
    <li>
        <input type='radio' value='2' name='radio'  id='radio2'/>
        <label for='radio2'><div className="rcorners2 " ></div></label>
    </li>
    <li>
        <input type='radio' value='3' name='radio'  id='radio3'/>
        <label for='radio3'><div className="rcorners2 " ></div></label>
    </li>
</ul>

Code from studies.css:

ul {
    list-style:none;
}
li{
    display:inline-block;
    margin-right: 15px;
}
input{
    visibility:hidden;
}
label{
    cursor:pointer;
}
input:checked + label {
    border: 2px solid blue;
}

Answer №1

To achieve the desired output, you can style the label without using an extra div.

ul {
  list-style: none;
}

li {
  display: inline-block;
}

input {
  visibility: hidden;
}

label {
  cursor: pointer;
  background: #ccc;
  padding: 2px 20px;
  border-radius: 4px;
  border: 2px solid transparent;
}

input:checked+label {
  border-color: blue;
}
<ul>
  <li>
    <input type='radio' value='1' name='radio' id='radio1' />
    <label for='radio1'>Radio 1</label>
  </li>
  <li>
    <input type='radio' value='2' name='radio' id='radio2' />
    <label for='radio2'>Radio 2</label>
  </li>
  <li>
    <input type='radio' value='3' name='radio' id='radio3' />
    <label for='radio3'>Radio 3</label>
  </li>

</ul>

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

Creating an infinite scroll feature with Next.js and Firebase

I am currently facing a challenge in implementing infinite scroll elements in next.js using firebase firestore. My approach involves utilizing "my api" and integrating firestore within my api files. At the moment, my API call is structured as follows. I ...

Design: A stationary text box positioned on the left side alongside a selection of scrollable MDL cards on the right

I'm currently experiencing a challenge with adjusting the text on two specific pages. On one side, there is a box with text, while on the other are two MDL cards displaying dialogues. The trouble arises when I try to set a fixed position for the text ...

How to Keep Button Highlighted After Clicking

I've experimented with various methods like using :active, :focus, adding a class, and modifying CSS rules through jQuery to maintain the button highlight, but none of them have been successful. Please take a look at my code and point out any mistakes ...

Unable to set the state in the componentDidMount function

While working on my componentDidMount method, I encountered an issue with creating an array of objects using fetch. In theory, it seems straightforward - I have a variable "loading" in my state set to true by default, and once the fetching is complete, it ...

Issue: App(...) is not rendering anything. This typically indicates that a return statement is missing in discord.js using react and typescript

I am struggling with an error while working on Discord OAuth2, Typescript, and React. I have tried to troubleshoot it myself but couldn't find a solution. Can someone please assist me? The issue involves being redirected constantly to the entered addr ...

React Native error - "Invalid element type: expected a string or class/function, but received undefined" - encountering issue with importing a custom library?

Alright, I'm looking to make some modifications to this library, so I am attempting to import the non-transpiled version by downloading the repository and importing it from here: https://github.com/nicotroia/react-native-floating-action-menu#readme A ...

Steps to apply styles to an iframe element directly from its inner HTML code

Looking for help with this line of code: <iframe src="http://www.google.com" name="google" style="Z-INDEX: 0; MARGIN-TOP: -1px; WIDTH: 100%; HEIGHT: 1070px"> <html> <head></head> <body> < ...

What is the best way to manage multiple production environments on servers?

Currently, there is a project on React that has both production and development environments. The challenge at hand involves launching another version of the project with different environment settings on a virtual server. The issue arises from the inabi ...

Ways to incorporate horizontal scrolling for a flex box container

I'm utilizing flexbox to design a horizontal slider in HTML. However, when using the code below: <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <meta http-equiv="X-UA-Compatible" content="IE=edge> <met ...

Exploring the Uncharted Territory: Mapbox GL JS Controls Wander Beyond the Bounds while Resizing a styled-components Container in React

I'm currently working on a Mapbox GL Map component in React, where the container is styled using styled-components. One of the application actions triggers a resize function for the map container based on user input. However, when the map resizes, I ...

Chrome fails to load webfont upon page initialization

Encountering a problem with loading a WebFont specifically on Chrome version "48.0.2564.82 m (64-bit)". The issue arises from the webfont not being applied upon page load or navigation, unless I manually refresh the page. Below is the snippet of code fro ...

What is the best way to transfer values or fields between pages in ReactJS?

Is there a way to pass the checkbox value to the checkout.js page? The issue I'm facing is on the PaymentForm page, where my attempts are not yielding the desired results. Essentially, I aim to utilize the PaymentForm fields in the checkout.js page as ...

Can a screenshot of a YouTube video be printed exactly as it appears on a website?

Is there a way to print a page with an embedded YouTube video without it showing up as a blank area? I want the printed version to display the same still shot that appears on the web. If this isn't possible, I will use a noprint stylesheet. Here is ho ...

Error message: Unexpected end of input found in HTML, CSS, or JavaScript files

I am currently working on a card game project but I keep encountering an error that says "unexpected end of input". This error is completely new to me, so any assistance or guidance would be greatly appreciated. Additionally, the codepen isn't display ...

What is the best way to limit the number of options displayed in the vue-multiselect

I'm looking to truncate multiple values on the vue-multiselect component. I attempted to override various classes without success, as shown in this example: .multiselect__content-wrapper { overflow-x: hidden; text-overflow: ellipsis; } ...

Navigating JSON data with unexpected fields in Typescript and React.js

Looking to parse a JSON string with random fields in Typescript, without prior knowledge of the field types. I want to convert the JSON string into an object with default field types, such as strings. The current parsing method is: let values = JSON.parse ...

"Enhancing User Interactions: Implementing React useState with NextUI's Pressable Card and

In my project, I am utilizing Next.js along with the NextUI component library to develop a pressable NextUI card that, upon clicking, should trigger a NextUI modal to appear. The card component has been customized in myCard.js, while the modal component ha ...

Best Practices for Uploading Images to Google Cloud Storage

I am fairly new to utilizing Google Cloud Storage and am seeking advice on the best approach for uploading a user profile picture on GCS. Should I directly upload my image from the frontend and then update my User model with the GCS URL linking to the upl ...

Guide to vertically aligning text within a row using Bootstrap 5

I am currently struggling to achieve vertical alignment of text in the center of a row. Despite my efforts, I have not been successful in achieving the desired vertical alignment. What steps can be taken to ensure that the text is vertically centered with ...

Accessing the previous path in ReactJS using React-Router

Currently, I'm working on building a single-page application that consists of three components: Index, BluePage and GreenPage. To navigate between these components, I am utilizing React and React Router. The layout is designed in such a way that it r ...