I have the following code for a label and checkbox, which when the label content is short, looks perfect like so:
However, when the label becomes longer, the checkbox is "squashed" and the tick no longer aligns inside it, how can I use flex to create a more versatile solution?
.outter-wrapper {
width: 200px;
border: 1px solid black;
}
.wrapper {
display: inline-flex;
min-height: 1.5rem;
padding: 8px 24px 8px 0px;
width: 100%;
}
.input {
position: absolute;
z-index: -1;
visibility: hidden;
}
.label-wrapper {
line-height: 1.5rem;
}
.label {
position: relative;
margin-bottom: 0;
display: flex;
margin-left: 0;
margin-right: 24px;
flex-direction: row-reverse;
justify-content: space-between;
flex: 1;
align-items: center;
}
.label:before {
display: block;
width: 20px;
height: 20px;
content: "";
background-color: #fff;
border: 1px solid black;
border-radius: 4px;
flex: none;
}
.label:after {
position: absolute;
display: block;
height: 20px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 80%;
}
input[type="checkbox"]:checked~.label::after {
background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
width: 20px;
height: 20px;
top: 0;
}
<div class="outter-wrapper">
<div class="wrapper">
<input class="input" name="checkbox2" id="checkbox2" type="checkbox">
<label class="label" for="checkbox2">
<div class="label-wrapper">
Option 1Option 1Option 1Option 1Option 1
</div>
</label>
</div>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
I know possibly flex-wrap
may be the correct approach but not sure if it's possible considering the "checkbox" is created using :before
?
CodePudding user response:
Try this code:
.outter-wrapper {
width: 200px;
border: 1px solid black;
}
.wrapper {
display: inline-flex;
min-height: 1.5rem;
padding: 8px 24px 8px 0px;
width: 100%;
}
.input {
position: absolute;
z-index: -1;
visibility: hidden;
}
.label-wrapper {
line-height: 1.5rem;
position: absolute;
left: 10px;
}
.label {
position: relative;
margin-bottom: 0;
display: flex;
margin-left: 0;
margin-right: 24px;
flex-direction: row-reverse;
justify-content: space-between;
flex: 1;
align-items: center;
}
.label:before {
display: block;
min-width: 20px;
height: 20px;
content: "";
background-color: #fff;
border: 1px solid black;
border-radius: 4px;
position:absolute;
top:0;
}
.label:after {
position: absolute;
display: block;
height: 20px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 80%;
}
input[type="checkbox"]:checked~.label::after {
background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
width: 20px;
height: 20px;
top: 0;
}
<div class="outter-wrapper">
<div class="wrapper">
<input class="input" name="checkbox2" id="checkbox2" type="checkbox">
<label class="label" for="checkbox2">
<div class="label-wrapper">
THis world is full of creatures
</div>
</label>
</div>
</div>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
Codepen link: https://codepen.io/emmeiWhite/pen/LYjKWEw
Note: May require to use min-height
on any parent(if required), so that text remains within input box
CodePudding user response:
You need a flex : none
if you want a fix width for an element that is inside a flexed container. Hope it resolve your problem.
.outter-wrapper {
width: 200px;
border: 1px solid black;
}
.wrapper {
display: inline-flex;
min-height: 1.5rem;
padding: 8px 24px 8px 0px;
width: 100%;
}
.input {
position: absolute;
z-index: -1;
visibility: hidden;
}
.label-wrapper {
line-height: 1.5rem;
}
.label {
position: relative;
margin-bottom: 0;
display: flex;
margin-left: 0;
margin-right: 24px;
flex-direction: row-reverse;
justify-content: space-between;
flex: 1;
align-items: center;
}
.label:before {
display: block;
width: 20px;
height: 20px;
content: "";
background-color: #fff;
border: 1px solid black;
border-radius: 4px;
/* a line I added*/
flex:none;
}
.label:after {
position: absolute;
display: block;
height: 20px;
content: "";
background-repeat: no-repeat;
background-position: center center;
background-size: 80%;
}
input[type="checkbox"]:checked~.label::after {
background-image: url("https://cdn-icons-png.flaticon.com/512/447/447147.png");
width: 20px;
height: 20px;
top: 0;
}
<div class="outter-wrapper">
<div class="wrapper">
<input class="input" name="checkbox2" id="checkbox2" type="checkbox">
<label class="label" for="checkbox2">
<div class="label-wrapper">
Option 1Option 1Option 1Option 1Option 1
</div>
</label>
</div>
</div>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>