删除 ::before 或 ::after 伪元素 CSS 定义

发布于 2024-10-16 15:56:58 字数 1849 浏览 7 评论 0原文

一些背景

我在应用程序中显示表单,并将每个字段(或一组相关的字段)包含在 UL/LI 元素中。这是一个示例:

<ul class="form">
    <li class="required">
        <label for="...">Field label</label>
        <div class="field">
            <input type="text" ... />
        </div>
    </li>
    ...
</ul>

正如您所看到的,我在必填字段的 LI 元素上设置了一个类 required ,因此当显示它们的 label 时,我定义了一个::after 伪元素,在其后添加一个红色星号:

ul.form li.required label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}

问题

我遇到的问题是当我的

容器。要让单选按钮或复选框旁边的标签可单击,它们必须包含在 label 中。

<ul class="form">
    <li class="required">
        <label for="...">Radio button set</label>
        <div class="field">
            <input type="radio" name="RBSet" value="1" id="RBSet1" /><label for="RBSet1">First</label><br/>
            <input type="radio" name="RBSet" value="2" id="RBSet2" /><label for="RBSet2">Second</label><br/>
            <input type="radio" name="RBSet" value="3" id="RBSet3" /><label for="RBSet3">Third</label><br/>
            ...
        </div>
    </li>
    ...
</ul>

问题当然是我的单选按钮旁边的每个标签都显示一个星号,因为它也包含在 li.required 元素内。

问题

如何从 div.field 中包含的所有 label 元素中删除 ::after 伪元素?我是否必须实际定义它,但将其内容设置为空字符串,或者是否可以将其全部删除?

我可以将 ::after 的样式定义更改为,

ul.form li.required > label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}

但出于兼容性原因,我想避免使用这些特殊的 CSS 选择器。

Some background

I'm displaying forms in my application and I enclose each field (or a related set of them) in an UL/LI element. This is an example:

<ul class="form">
    <li class="required">
        <label for="...">Field label</label>
        <div class="field">
            <input type="text" ... />
        </div>
    </li>
    ...
</ul>

As you can see I set a class required on required field's LI element, so when their label is displayed I define an ::after pseudo element, that adds a red asterisk after it:

ul.form li.required label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}

The problem

The problem I'm having is when I have a list of checkboxes or radio buttons in my <div class="field"> container. To have labels beside radio buttons or checkboxes clickable, they must be contained in a label.

<ul class="form">
    <li class="required">
        <label for="...">Radio button set</label>
        <div class="field">
            <input type="radio" name="RBSet" value="1" id="RBSet1" /><label for="RBSet1">First</label><br/>
            <input type="radio" name="RBSet" value="2" id="RBSet2" /><label for="RBSet2">Second</label><br/>
            <input type="radio" name="RBSet" value="3" id="RBSet3" /><label for="RBSet3">Third</label><br/>
            ...
        </div>
    </li>
    ...
</ul>

The problem is of course that each label beside my radio button displays an asterisk because it's also contained inside the li.required element.

Question

How do I remove ::after pseudo element from all label elements that are contained in a div.field? Do I have to actually define it but set its content to an empty string or is it possible to remove it all together?

I could change my style definition for ::after to

ul.form li.required > label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}

but I want to avoid these special CSS selectors for compatibility reasons.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

像你 2024-10-23 15:56:58

您应该使用 子组合器 >

ul.form li.required > label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}

> 组合器具有更好的 IE 兼容性(IE7及以上)比 ::after 伪元素(IE9 及以上)要好。如果您能够使用 ::after,则没有理由不使用 >。事实上,IE 对伪元素的支持非常不一致,以至于 IE8 只能识别 CSS2 :after,但不能识别 CSS3 ::after。因此,除非您使用 :after,否则您的代码将无法在 IE8 上运行,并且为了支持 IE7 及更早版本,您需要修复 JavaScript。

You should just apply the pseudo-element to labels that are children of .required using the child combinator >:

ul.form li.required > label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}

The > combinator has better IE compatibility (IE7 and up) than the ::after pseudo-element (IE9 and up). If you're able to use ::after, there is no reason not to use >. In fact, IE support for pseudo-elements is so inconsistent that IE8 recognizes CSS2 :after but not CSS3 ::after. Your code would thus not work on IE8 unless you use :after, and to support IE7 and older you need a JavaScript fix.

小…红帽 2024-10-23 15:56:58

只需继续使用子选择器即可。如果您查看此兼容性表,您将看到所有支持 的浏览器: 还支持子选择器>

如果您需要最大程度的兼容性(与 IE7),请考虑使用背景图像而不是 :after 标记。例子:

ul.form li.required label
{
  background: url(/images/star.png) top right no-repeat;
  padding-right: 10px;
}


ul.form li.required div.field label
{
  background: none;
  padding-right: 0px;
}

Just go ahead with the child-of selector. If you look at this compatibility table you will see that all browsers that support :after also support the child-of selector >.

If you need maximum compatibility (with IE7), consider using a backround image rather than the :after tag. Example:

ul.form li.required label
{
  background: url(/images/star.png) top right no-repeat;
  padding-right: 10px;
}


ul.form li.required div.field label
{
  background: none;
  padding-right: 0px;
}
凹づ凸ル 2024-10-23 15:56:58

您还可以清除所有 field 标签的 ::after 内容:

ul.form li.required label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}


ul.form li.required div.field label::after
{
    content: " ";
}

You could also clear the ::after content for all field labels:

ul.form li.required label::after
{
    content: " *";
    font-weight: bold;
    color: #f66;
}


ul.form li.required div.field label::after
{
    content: " ";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文