对于带有伪元素的标签,flex box 内部无法在末尾对齐
inside flex box not able align at end for label with pseudo elements
在下面的代码片段中,我有工具名称和同一行的第一个单选按钮,但即使我已经给出 space-between
但单选按钮不在最后。
如何将单选按钮对齐到右侧的末尾,就像 space-between
正常工作一样
.container-section-column-wrapper {
display: flex;
justify-content: space-between;
align-items: center;
}
input[type='radio'] {
display: none;
}
label {
cursor: pointer;
position: relative;
font-size: 4rem;
}
label::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: transparent;
border: 2px solid grey;
border-radius: 50%;
top: 50%;
left: -5rem;
transform: translateY(-50%);
transition: border-color 400ms ease;
}
label::after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: red;
border: 2px solid red;
border-radius: 50%;
top: 50%;
left: -5rem;
transform: translateY(-50%) scale(0);
transition: transform 400ms ease;
}
input[type='radio']:checked+label::before {
border-color: red;
}
input[type='radio']:checked+label::after {
transform: translateY(-50%) scale(0.55);
}
<div class='container-section'>
<div class='container-section-column-wrapper'>
<div class='column-one'>
<div class='tool-name'>Tool one</div>
</div>
<div class='column-two'>
<input type='radio' id='tool-one' name='tool-pick-group' />
<label for='tool-one' />
</div>
</div>
是因为你的pseudo-elements:before
和:after
绝对定位在一个相对定位的父类(label
)中。您正在指定 left: -5rem;
以尝试向左对齐,这会导致您看到间距。相反,只需使用 right: 0;
.
.container-section-column-wrapper {
display: flex;
justify-content: space-between;
align-items: center;
}
input[type='radio'] {
display: none;
}
label {
cursor: pointer;
position: relative;
font-size: 4rem;
}
label::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: transparent;
border: 2px solid grey;
border-radius: 50%;
top: 50%;
right: 0;
transform: translateY(-50%);
transition: border-color 400ms ease;
}
label::after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: red;
border: 2px solid red;
border-radius: 50%;
top: 50%;
right: 0;
transform: translateY(-50%) scale(0);
transition: transform 400ms ease;
}
input[type='radio']:checked+label::before {
border-color: red;
}
input[type='radio']:checked+label::after {
transform: translateY(-50%) scale(0.55);
}
<div class='container-section'>
<div class='container-section-column-wrapper'>
<div class='column-one'>
<div class='tool-name'>Tool one</div>
</div>
<div class='column-two'>
<input type='radio' id='tool-one' name='tool-pick-group' />
<label for='tool-one' />
</div>
</div>
在下面的代码片段中,我有工具名称和同一行的第一个单选按钮,但即使我已经给出 space-between
但单选按钮不在最后。
如何将单选按钮对齐到右侧的末尾,就像 space-between
正常工作一样
.container-section-column-wrapper {
display: flex;
justify-content: space-between;
align-items: center;
}
input[type='radio'] {
display: none;
}
label {
cursor: pointer;
position: relative;
font-size: 4rem;
}
label::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: transparent;
border: 2px solid grey;
border-radius: 50%;
top: 50%;
left: -5rem;
transform: translateY(-50%);
transition: border-color 400ms ease;
}
label::after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: red;
border: 2px solid red;
border-radius: 50%;
top: 50%;
left: -5rem;
transform: translateY(-50%) scale(0);
transition: transform 400ms ease;
}
input[type='radio']:checked+label::before {
border-color: red;
}
input[type='radio']:checked+label::after {
transform: translateY(-50%) scale(0.55);
}
<div class='container-section'>
<div class='container-section-column-wrapper'>
<div class='column-one'>
<div class='tool-name'>Tool one</div>
</div>
<div class='column-two'>
<input type='radio' id='tool-one' name='tool-pick-group' />
<label for='tool-one' />
</div>
</div>
是因为你的pseudo-elements:before
和:after
绝对定位在一个相对定位的父类(label
)中。您正在指定 left: -5rem;
以尝试向左对齐,这会导致您看到间距。相反,只需使用 right: 0;
.
.container-section-column-wrapper {
display: flex;
justify-content: space-between;
align-items: center;
}
input[type='radio'] {
display: none;
}
label {
cursor: pointer;
position: relative;
font-size: 4rem;
}
label::before {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: transparent;
border: 2px solid grey;
border-radius: 50%;
top: 50%;
right: 0;
transform: translateY(-50%);
transition: border-color 400ms ease;
}
label::after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background-color: red;
border: 2px solid red;
border-radius: 50%;
top: 50%;
right: 0;
transform: translateY(-50%) scale(0);
transition: transform 400ms ease;
}
input[type='radio']:checked+label::before {
border-color: red;
}
input[type='radio']:checked+label::after {
transform: translateY(-50%) scale(0.55);
}
<div class='container-section'>
<div class='container-section-column-wrapper'>
<div class='column-one'>
<div class='tool-name'>Tool one</div>
</div>
<div class='column-two'>
<input type='radio' id='tool-one' name='tool-pick-group' />
<label for='tool-one' />
</div>
</div>