gpt4 book ai didi

html - 为什么元素没有在复选框中居中?

转载 作者:行者123 更新时间:2023-11-27 23:36:31 26 4
gpt4 key购买 nike

为什么元素没有在复选框中居中?

必须正好位于复选框的中间。

点击后,它应该是这样的:

enter image description here

尝试过:

left: 50%; top: 50%; - does not seem to work.

我不明白为什么单击复选框时会出现水平滚动。

overflow-x: hidden; - not a solution

代码:

* {
box-sizing: border-box;
user-select: none;
}

section {
display: flex;
flex-direction: column;
padding: 0.5em;
}

input[type='checkbox'] {
height: 0;
width: 0;
}

input[type='checkbox']+label {
position: relative;
display: flex;
margin: .6em 0;
align-items: center;
color: #9e9e9e;
}

input[type='checkbox']+label>ins {
position: absolute;
display: block;
bottom: 0;
left: 2em;
height: 0;
width: 100%;
overflow: hidden;
text-decoration: none;
}

input[type='checkbox']+label>ins>i {
position: absolute;
bottom: 0;
font-style: normal;
color: #000;
}

input[type='checkbox']+label>span {
display: flex;
justify-content: center;
align-items: center;
margin-right: 0.8em;
width: 1.2em;
height: 1.2em;
background: transparent;
border: 2px solid #E6ECF0;
border-radius: 3px;
cursor: pointer;
transition: all 250ms cubic-bezier(.4, .0, .23, 1);
}

input[type='checkbox']:checked+label>ins {
height: 100%;
}

input[type='checkbox']:checked+label>span {
border: .5em solid #7726E5;
}

input[type='checkbox']:checked+label>span:before {
content: "";
position: absolute;
box-shadow: inset 0px 0px 0px 5px rgba(0, 0, 0, 0.75);
border-radius: 2px;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Checkbox</title>
<meta name="viewport" content="width=device-width">
</head>

<body>

<section>

<input id='one' type='checkbox' />
<label for='one'>
<span></span>
Off with your head
<ins><i>Off with your head</i></ins>
</label>

<input id='two' type='checkbox' />
<label for='two'>
<span></span>
Dance ’til you’re dead
<ins><i>Dance ’til you’re dead</i></ins>
</label>

<input id='three' type='checkbox' />
<label for='three'>
<span></span>
Heads will roll
<ins><i>Heads will roll</i></ins>
</label>

<input id='four' type='checkbox' />
<label for='four'>
<span></span>
On the floor
<ins><i>On the floor</i></ins>
</label>

</section>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>



</body>

</html>

最佳答案

老实说,它看起来非常适合我。出现水平滚动是因为您的黑色文本(显示在灰色文本之上)位于宽度为 100% 的元素内,但其父元素也包含复选框。我建议只是让原始标签的颜色更深,而不是使用第二层文本。

这里是一个编辑:

* { box-sizing: border-box; user-select: none; }

section{
display: flex;
flex-direction: column;
padding: 0.5em;
}

input[type='checkbox']{ height: 0; width: 0; }

input[type='checkbox'] + label{
position: relative;
display: flex;
margin: .6em 0;
align-items: center;
color: #9e9e9e;
}

input[type='checkbox'] + label > span{
display: flex;
justify-content: center;
align-items: center;
margin-right: 0.8em;
width: 1.2em;
height: 1.2em;
background: transparent;
border: 2px solid #E6ECF0;
border-radius: 3px;
cursor: pointer;
transition: all 250ms cubic-bezier(.4,.0,.23,1);
}

input[type='checkbox']:checked + label > span{
border: .5em solid #7726E5;
}

input[type='checkbox']:checked + label > span:before{
content: "";
position: absolute;
box-shadow: inset 0px 0px 0px 5px rgba(0,0,0,0.75);
border-radius: 2px;
}

input[type='checkbox']:checked + label {
color: unset;
}
<!DOCTYPE html>
<html lang="en" >

<head>
<meta charset="UTF-8">
<title>Checkbox</title>
<meta name="viewport" content="width=device-width">
</head>

<body>

<section>

<input id='one' type='checkbox' />
<label for='one'>
<span></span>
Off with your head
</label>

<input id='two' type='checkbox' />
<label for='two'>
<span></span>
Dance ’til you’re dead
</label>

<input id='three' type='checkbox' />
<label for='three'>
<span></span>
Heads will roll
</label>

<input id='four' type='checkbox' />
<label for='four'>
<span></span>
On the floor
</label>

</section>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>



</body>

</html>

如果你想让复选框中间的白色方 block 更宽(就像你的 png 中的那样),你应该在此处缩小边框:

input[type='checkbox']:checked + label > span{
border: .5em solid #7726E5;
}

关于html - 为什么元素没有在复选框中居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223438/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com