简体   繁体   English

如何将css属性应用于我的第一个div类

[英]how to apply css property to my first div class only

在此输入图像描述 I am having two dhx_scroll_cont div class, when i write css code as bellow it working for two classes. 我有两个dhx_scroll_cont div类,当我编写css代码时,它正在为两个类工作。 Now i want to write a css code that apply for first div call 现在我想编写一个适用于第一个div调用的css代码

.dhx_scroll_cont:before{
    //some code here
    }

Demo Fiddle 演示小提琴

Simply use: 只需使用:

.dhx_scroll_cont:first-of-type:before{
    //some code here
}

more on first-of-type 更多关于first-of-type

The :first-of-type CSS pseudo-class represents the first sibling of its type in the list of children of its parent element. :first-of-type CSS伪类在其父元素的子元素列表中表示其类型的第一个兄弟。


Update 更新

According to the screenshot the OP posted the below should work: 根据截图OP发布的下面应该有效:

.dhx_view_day_events .dhx_scroll_cont:first-of-type:before{
    //some code here
}

Depending on the structure of your HTML the solution you require will change. 根据HTML的结构,您需要的解决方案将会发生变化。

Would you be able to provide the HTML structure for us to work from? 您能为我们提供HTML结构吗?

Otherwise, you could: 否则,您可以:

Add another class to the div you want to change 将另一个类添加到要更改的div中

By having <div class="dhx_scroll_cont"> you are only giving one targetable class. 通过<div class="dhx_scroll_cont">您只提供一个可定位的类。 One way around this is having 2 classes separated by a space, such as: 解决此问题的一种方法是使用空格分隔2个类,例如:

<div class="dhx_scroll_cont OTHER_CLASS">

This will allow you to target the class OTHER_CLASS with the certain CSS values that you want effecting the first div. 这将允许您使用您想要影响第一个div的某些CSS值来​​定位OTHER_CLASS类。

Using :first-child or :first 使用:first-child:first

:first-child and :first allow you to target the div that is the first child element of it's parent. :first-child:first允许你定位div,它是父元素的第一个子元素。 For example: 例如:

<div class="surround">
    <div class="dhx_scroll_cont">

    </div>
    <div class="dhx_scroll_cont">

    </div>
</div>

dhx_scroll_cont:first-child {
    CSS HERE
}

This will effect the first dhx_scroll_cont div. 这将影响第一个dhx_scroll_cont div。

As I said previously, if you can give some more information on the structure of your HTMl it would help us with your solution. 正如我之前所说,如果您可以提供有关HTMl结构的更多信息,它将帮助我们解决您的问题。

EDIT 编辑


Thanks for showing the HTML structure. 感谢您显示HTML结构。

With that structure out of the methods that I have shown, adding another class to the first of the dhx_scroll_cont will allow you to specifically target that div, and not the other one. 使用我已经显示的方法中的结构,在dhx_scroll_cont的第一个中添加另一个类将允许您专门针对该div,而不是另一个。

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

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