css - 如何根据文本方向移动我的伪元素复选框?

标签 css

制作自定义外观复选框元素的常用方法是隐藏浏览器的默认复选框,并在复选框标签上为自定义外观的复选框创建一个伪元素:

.myclass::before {
    <Some styles for the custom looking checkbox>
}

通常呈现的 HTML 最终看起来像这样:

::before
<label class="myclass">text</span>

不过,我无法根据文本方向定位我的复选框。对于 ltr 语言(如英语)让复选框出现::在标签文本之前是有意义的,但它在 rtl 语言(如希伯来语)中看起来很尴尬。制作一个相同样式的::after 伪元素解决了我的问题,但我不知道如何根据文本方向仅将一种 CSS 样式应用于另一种样式。似乎没有任何方法可以单独在 CSS 中检测元素的计算文本方向 ( unless you're Firefox? )

有什么建议吗?

最佳答案

div{
  margin: 10px 10px 30px;
}
label{
  position: relative;
  cursor: pointer;
  display: block;
}
span{
  content: '';
  display: block;
  background: #fff;
  width: 15px;
  height: 15px;
  border: 1px solid red;
  position: relative;
  top: 20px;
}
label strong{
  color: #fff;
  display: block;
  position: relative;
  top: -15px;
}
input:checked + strong{
  color: red;
}
<div>
<label>
  <span></span>
  <input type="checkbox"> English 
  <strong>X</strong>
</label>
</div>

<div>
<label dir="rtl">
  <span></span>
  <input type="checkbox"> العَرَبِيَّة 
  <strong>X</strong>
</label>
</div>

关于css - 如何根据文本方向移动我的伪元素复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52544403/

相关文章:

javascript - 在网页中创建连续或永无止境的滚动效果?

css - 使用 CSS 在 Flex 4.5 中设计 Accordion 标题的样式

jquery - 单击时 HTML 元素向右移动 - css 问题

css - 菜单向上滑动而不是向下滑动

html - 在CSS中选择多个 child

html - 标题和元标记装饰(样式)

html - 父 div 高度不适合子图像高度

jquery - float 和 jQuery UI 按钮

html - Wordpress Widgets <a> 链接标签在 <li> 标签之外?

css - 移动页面宽度大于导致 Chrome 模拟器缩放更小