I have some problems with this CSS.
- How can I make each li’s size increases or decreases according to the label’s size?
- Also, when the li run to the end of the page’s width, how can I make it wrap to a new row?
For example:
- Black’s width should be smaller
- Forest Green’s width should be wider
- The first row should end at Purple, that’s the width of the page. Rose Pink, Rose Red.. and the rest, should wrap to a new row
.cable-config span {
font-size: 14px;
font-weight: 400;
color: #86939E;
display: inline-block;
}
.cable-choose {
list-style-type: none;
margin: 25px 0 0 0;
padding: 0;
}
.cable-choose {
white-space: nowrap;
}
.cable-choose li {
/* float: left; */
margin: 15px 5px 0 0;
width: 100px;
height: 40px;
display: inline-block;
position: relative;
}
.cable-choose label,
.cable-choose input {
display: block;
position: absolute;
/* top: 0; */
left: 0;
right: 0;
bottom: 0;
}
.cable-choose input[type="radio"] {
opacity: 0.01;
z-index: 100;
}
.cable-choose input[type="radio"]:checked+label,
.Checked+label {
background: yellow;
}
.cable-choose label {
border: 2px solid #E1E8EE;
border-radius: 6px;
padding: 13px 20px;
font-size: 14px;
color: #000;
background-color: #fff;
cursor: pointer;
transition: all .5s;
cursor: pointer;
z-index: 90;
}
.cable-choose .sold-out {
color: #ced3d9;
}
.radio-custom:checked ~ label {
border: 2px solid #121b21;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
<div class="cable-config clearfix">
<div><h5>Color:</h5></div>
<ul class="cable-choose">
<li>
<input
type="radio"
/><span class="radio-custom-dummy"></span
><label class="" for="black">Black</label>
</li>
<li>
<input
type="radio"
/><span class="radio-custom-dummy"></span
><label class="" for="forest-green">Forest Green</label>
</li>
<li>
<input
type="radio"
disabled=""
/><span class="radio-custom-dummy"></span
><label class="sold-out" for="light-purple">Light Purple</label>
</li>
</ul>
</div>
4
Answers
Use "flex" styles for ul and make it simple
Use
display: flex
for ul styles.Use flexbox and remove all the absolute positioning, it’s unnecessary and over-complicating what you are trying to do.
Your main problem is the fixed width and height used for the list items. Text will simply overflow if it’s larger.
There is also no need to use an unordered list for this scenario.
CSS flexbox would definitely be an easier solution as suggested by other members. I have included a working example for you and made some improvements with regard to the custom radio buttons.
Note: "max-width: 600" is only set to demonstrate how the custom radio buttons are wrapped.