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

标签 html css web

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

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

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

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/

相关文章:

html - 是否可以在没有 javascript 的情况下在容器后面剪辑一个绝对表头?

html - 如何让我的 div 外包装,但里面有文字?

css - 为什么我的主链接类 (a :links & a:hover) overriding my newly created unique classes (#footer_links a:links, #footer_links a:hover)

jquery - 你如何在 CSS 中使用 mouseleave?

html - 根据属性的值是否用双引号括起来来选择

javascript - Angular : {{ }} codes show for a split second

html - 横向导航让当前的li更高

html - 在 Twitter bootstrap 3 中定位模态框

haskell - 等效于 Haskell 的 WSGI/Rack 的标准 Web 服务器接口(interface)

php - 如何让 php 自动扫描并添加按日期排序的文件到下载页面