简体   繁体   English

如何在 CSS 中定位这个 HTML?

[英]How to target this HTML in CSS?

I'm trying to style Vanilla Forums and I just can't seem to figure out to select this class <li class="Item Announcement"></li> so that I can style it.我正在尝试为 Vanilla 论坛设置样式,但我似乎无法弄清楚 select 这个 class <li class="Item Announcement"></li>以便我可以设置它的样式。

I don't know why it's being so difficult.我不知道为什么这么难。 Why would this not work?为什么这行不通?

.Item Announcement {
background-color: #FFF;
{

Try:尝试:

li.Item.Announcement {
    background-color: #FFF;
}

That list item has two classes applied to it (Item and Announcement).该列表项应用了两个类(Item 和 Announcement)。 So to target that with CSS, you need to prefix each class with a period and then remove the spaces.因此,要以 CSS 为目标,您需要在每个 class 前加上一个句点,然后删除空格。 Leaving the spaces in the CSS selector would apply it to a descendant element that had the class.在 CSS 选择器中保留空格会将其应用于具有 class 的后代元素。

Quick jsFiddle example .快速jsFiddle 示例

The problem is that with spaces, its is no longer one selector but two, so you need to select using multiple class selector.问题是对于空格,它不再是一个选择器而是两个,所以你需要 select 使用多个 class 选择器。

.Item.Announcement {
    background-color: #FFF;
}

I think your problem is that your class name has a space in it.我认为您的问题是您的 class 名称中有一个空格。 You probably meant for it to be a single class name - so change it to class="ItemAnnouncement" and .ItemAnnouncement {... and everything will work.您可能意味着它是一个 class 名称 - 所以将其更改为class="ItemAnnouncement".ItemAnnouncement {...一切都会起作用。

Keep in mind that <li class="Item Announcement"></li> is actually two classes.请记住<li class="Item Announcement"></li>实际上是两个类。 Classes cannot have spaces in their names.类的名称中不能有空格。 You can select this element with either li, .Item, .Announcement, or a combination of all three.您可以使用 li、.Item、.Announcement 或这三者的组合来 select 此元素。

To make sure that you are selecting that exact element, your code should look something like this:为确保您选择的是确切的元素,您的代码应如下所示:

li.Item.Announcement {
    background-color: #fff;
}

the space you have in your class name adds two distinct classes in your html element, .Item & .Announcement . class 名称中的空间在.Announcement元素中添加了两个不同的类, .Item 和.Item You should consider naming your class itemAnnouncement, or if you're not a camel case fan, item-announcement.您应该考虑将您的 class itemAnnouncement 命名为 itemAnnouncement,或者如果您不是驼峰爱好者,则命名为 item-announcement。

If on the other hand you meant to have different classes you can style the one that suits you best, ie另一方面,如果您打算拥有不同的课程,则可以设计最适合您的课程,即

.Item {background-color:#fff;}

spaces in class names and id are not allowed, so if you Put a space you are actually writing 2 class names for that item.不允许在 class 名称和 ID 中使用空格,因此如果您放置一个空格,您实际上是在为该项目编写 2 class 名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM