Reputation: 1337
I am trying to align my custom radio buttons in a line but I am having an issue. If the text is really long but radio button overlap each other. I don't want to assign a fix width to any of them. Also, I need to have .radio .custom
styling there if I want to change the border of the radio button for error handling. I am open to changing the html structure but I need the Text to be inside custom
span. I can get rid of the inner-span if needed.
I have tried using:
display:flex;
on label
but it ended up giving me each option right over each other.
* {
box-sizing: border-box;
}
input[type="radio"] {
display: none;
cursor: pointer;
}
label {
cursor: pointer;
min-width: 50px;
display: inline-block;
position: relative;
}
.radio span.custom > span {
margin-left: 24px;
margin-right: 10px;
}
.radio .custom {
content: '';
height: 20px;
left: 0;
top: 0;
width: 20px;
background-color: #fff;
border: 1px solid #ccc;
border-radius: 100%;
position: absolute;
}
.radio input:checked + .custom:before {
background-color: blue;
border-radius: 100%;
border: 3px solid #fff;
content: "";
display: block;
height: 12px;
position: absolute;
top: 0;
width: 12px;
z-index: 1;
left: 0;
}
.radio input + .custom:after {
border-radius: 100%;
}
.checkbox input:checked .custom {
background-color: blue;
border-color: blue;
}
<label class="radio">
<input type="radio">
<span class="custom"><span>Onasasasase</span></span>
</label>
<label class="radio">
<input type="radio">
<span class="custom"><span>Two</span></span>
</label>
Upvotes: 1
Views: 981
Reputation: 46785
Here is one way of doing it. I simplified your HTML slightly (removed extra nested span
) and used the following CSS.
Your custom button is 18px square, so I allowed for a 25px margin on the child element span.custom
.
This allows space for the :before
pseudo-element that appears to the left of .custom
.
I colored the background on the label
so that you can see the unchecked button, but otherwise, you can mark-up gives you a lot of control over the borders, spacing and so on.
input[type="radio"] {
cursor: pointer;
display: none;
}
label {
cursor: pointer;
min-width: 50px;
display:inline-block;
position:relative;
background-color: #F0F0F0; // for demo only
}
.radio .custom {
margin-left: 25px;
margin-right: 25px;
}
.radio input + .custom:before {
content: "";
background-color: #fff;
border-radius: 100%;
border: 3px solid #fff;
display: block;
height: 12px;
width: 12px;
position: absolute;
top: 0;
left: 0;
}
.radio input:checked + .custom:before {
background-color: blue;
border: 3px solid #fff;
}
<label class="radio">
<input type="radio">
<span class="custom">One is very long</span>
</label>
<label class="radio">
<input type="radio">
<span class="custom">Two</span>
</label>
Upvotes: 3
Reputation: 3117
I just wrapped everything in a parent div and made it a flexbox, while centering it. Seems to work for me.
<div style="display: flex; justify-content: space-around">
<label class="radio">
<input type="radio">
<span class="custom"><span>Onasasasase</span></span>
</label>
<label class="radio">
<input type="radio">
<span class="custom"><span>Two</span></span>
</label>
</div>
Upvotes: 0