html - 调整窗口大小时保持复选框在表单中的位置

标签 html forms css checkbox position

.left-col {
    width:25%; }

    label {
    width:100%; 
    position:relative; }

    input[type="checkbox"] + div::before {
     right:-35px !important;
      left: auto !important; }

     input[type="checkbox"] + div::after {
     left: auto !important;
      right: -33px !important;
       width: auto; }
<div class="left-col"> <label>
    <span>text</span>
    <input type="checkbox">
    <div></div>
    </label> </div>

我有两列表格。我在标签内有带有 div 伪类的复选框。我需要将复选框放置在右栏中并且仍然在标签内。 问题是,我无法以这种方式更改它,该复选框将位于标签内,并且在调整窗口大小时将保持在同一位置。

我可以保留带有 negative right 属性的复选框,这样它在调整大小时不会改变位置,但它在标签外面并且不可点击。

我可以更改标签的宽度 - 但在调整大小时复选框会跳转。

有没有办法用 CSS 解决这个问题?

调整大小时没有负右的复选框位置:

enter image description here

我需要的复选框位置:

enter image description here

最佳答案

正如评论中所说,您可以将复选框放在标签之外,并使用 for 属性来更改其状态。至于对齐,可以这样做。

.left-col {
   display: block;
   width: 25%;
   position: relative;
}

label {
   display: inline;
}

input[type="checkbox"] {
   position: absolute;
   display: inine;
   right: 0;
}
<div class="left-col">
   <label for="idCheckbox">text</label>
   <input type="checkbox" id="idCheckbox">
</div>

关于html - 调整窗口大小时保持复选框在表单中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667847/

相关文章:

javascript - 如果未选中复选框,则隐藏标签和 img

javascript - <form> 标签外的提交按钮

css - Angular Material 中心布局

html - css菜单,使图片随当前页面移动

html - 按钮和一些用户交互对 iOS14 应用程序没有响应

php - 将HTML表格导出为PDF

javascript - jQuery 表单超时不提交 - e[h] 不是函数

javascript - 动态地给 HTML 元素一个自定义属性

php - 刷新页面后将所选值保留在下拉列表中

java - ActionEvent 文本字段检查