phasco phaisco
phasco phaisco

Reputation: 17

CSS: style radio button with label next to it?

I'm trying to style a radio button and place a label inline with the radio button using CSS.

But the radio button doesn't look right and its label is not inline with it.

I need to achieve this:

enter image description here

This is what I have so far:

input[type="radio"]{
    display:none;
}

input[type="radio"] + label
{
    background: #fff;
    border: solid 2px #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
	border-radius:50%;
}

input[type="radio"]:checked + label
{
    background: #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
	border-radius:50%;
}
<input type="radio" id="male" name="gender" value="M">
<label for="male">Male</label>

<input type="radio" id="female" name="gender" value="F">
<label for="female">Female</label>

EDIT:

I need the all the radio buttons and their labels all in one line.

Upvotes: 0

Views: 3714

Answers (5)

SpaceDogCS
SpaceDogCS

Reputation: 2978

You can before at the label, so it will look like the image

input{
  display: none;
}

label{
  float: left;
  margin-right: 10px
}

label span{
  width: 16px;
  height: 16px;
  background-color: #fff;
  border-radius: 50%;
  border: 1px solid #222;
  float: left;
  margin-right: 5px;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
     -moz-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
  -webkit-align-items: center;
     -moz-box-align: center;
      -ms-flex-align: center;
          align-items: center;
}

input:checked + label span:before{
  content: '';
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #222;
}
<input type="radio" id="male" name="gender" value="M"/>
<label for="male"><span></span>Male</label>

<input type="radio" id="female" name="gender" value="F"/>
<label for="female"><span></span>Female</label>

Upvotes: 2

Vij7
Vij7

Reputation: 1

.container {
    display: inline-block;
    position: relative;
    padding-left: 35px;
    margin-bottom: 12px;
    cursor: pointer;
    font-size: 22px;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
.container input {
    position: absolute;
    opacity: 0;
    cursor: pointer;
}
.checkmark {
    position: absolute;
    top: 0;
    left: 0;
    height: 25px;
    width: 25px;
    background-color: #eee;
    border-radius: 50%;
}
input:checked ~ .checkmark {
    background-color: #2196F3;
}
.checkmark:after {
    content: "";
    position: absolute;
    display: none;
}
input:checked ~ .checkmark:after {
    display: block;
}
.checkmark:after {
 	top: 9px;
	left: 9px;
	width: 8px;
	height: 8px;
	border-radius: 50%;
	background: white;
}
<label class="container">One
  <input type="radio" checked="checked" name="radio">
  <span class="checkmark"></span>
</label>
<label class="container">Two
  <input type="radio" name="radio">
  <span class="checkmark"></span>
</label>

Hope this helps.... :)

Upvotes: 0

sol
sol

Reputation: 22959

If you're open to some HTML adjustments you might find this more flexible:

Make sure to include a focus state for the input to ensure it is accessible for keyboard users.

input[type="radio"] {
  position: absolute;
  opacity: 0;
  cursor: pointer;
}

label {
  position: relative;
  cursor: pointer;
  padding-left: 30px;
  display: inline-block;
  line-height: 1.6;
  margin-right: 15px;
}

span {
  position: absolute;
  left: 0;
  top: 0;
  height: 20px;
  width: 20px;
  background: #FFF;
  border: 2px solid black;
  border-radius: 50%;
}

span:after {
  content: '';
  position: absolute;
  left: 5px;
  top: 5px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: black;
  display: none;
}

input[type="radio"]:checked~span:after {
  display: block;
}

input[type="radio"]:focus~span {
  outline: 2px solid orange;
}
<label for="male">Male
  <input type="radio" id="male" name="gender" value="M">
   <span></span>
</label>


<label for="female">Female
  <input type="radio" id="female" name="gender" value="F">
   <span></span>
</label>

Upvotes: 0

Jorjon
Jorjon

Reputation: 5434

Just add a ::before to the input + label.

input[type="radio"]{
    display:none;
}

input[type="radio"] + label::before
{
    content:'';
    background: #fff;
    border: solid 2px #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
	  border-radius:50%;
}

input[type="radio"]:checked + label::before
{
    background: #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
	  border-radius:50%;
}
<input type="radio" id="male" name="gender" value="M">
<label for="male">Male</label>

<input type="radio" id="female" name="gender" value="F">
<label for="female">Female</label>

Upvotes: 0

Katz
Katz

Reputation: 175

If you are using your label both for displaying the actual radio button and for the actual label, you will indeed run into a problem : Right now, you tell you <label> to be a circle 20px width, and to at the same time contain your text. Try with this, instead :

input[type="radio"] + label::before
{
    background: #fff;
    border: solid 2px #000;
    height: 20px;
    width: 20px;
    display:inline-block;
    padding: 0 0 0 0px;
    border-radius:50%;
    margin-right:5px;
}

This adds a pseudo-element to your label, which you can style as you would any <div> or <span>. Also, for you checked radio button, I would simplify :

input[type="radio"]:checked + label::before
{
    background: #000;
}

Upvotes: 0

Related Questions