Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 向特定类添加样式_Html_Css_Styles - Fatal编程技术网

Html 向特定类添加样式

Html 向特定类添加样式,html,css,styles,Html,Css,Styles,我有一个三个div,有相同的类分配给它。我想做的是对第一个div应用一个单独的样式,并对其他两个div应用不同的样式 <div class="news_post">...</div> <div class="news_post">...</div> <div class="news_post">...</div> 。。。 ... ... 有什么建议是正确的方法吗? 如果将来要自定义任何元素 .news_post:n

我有一个三个div,有相同的类分配给它。我想做的是对第一个div应用一个单独的样式,并对其他两个div应用不同的样式

   <div class="news_post">...</div>
<div class="news_post">...</div>
<div class="news_post">...</div>
。。。
...
...
有什么建议是正确的方法吗?

如果将来要自定义任何元素

.news_post:nth-child(2) { // 2 represents the position
  background: blue;
}

如果将来要自定义任何元素

.news_post:nth-child(2) { // 2 represents the position
  background: blue;
}

如果将来要自定义任何元素

.news_post:nth-child(2) { // 2 represents the position
  background: blue;
}

如果将来要自定义任何元素

.news_post:nth-child(2) { // 2 represents the position
  background: blue;
}
尝试伪类

.news_post:first-child
.news_post:last-child
css中的伪类

试试伪类

.news_post:first-child
.news_post:last-child
css中的伪类

试试伪类

.news_post:first-child
.news_post:last-child
css中的伪类

试试伪类

.news_post:first-child
.news_post:last-child
css中的伪类

您可以尝试以下方法:

.news_post:nth-child(1)
{
   background:#ff0000;
}

.news_post:nth-child(2)
{
   background:blue;
}

.news_post:nth-child(3)
{
   background:black;
}
您可以尝试以下方法:

.news_post:nth-child(1)
{
   background:#ff0000;
}

.news_post:nth-child(2)
{
   background:blue;
}

.news_post:nth-child(3)
{
   background:black;
}
您可以尝试以下方法:

.news_post:nth-child(1)
{
   background:#ff0000;
}

.news_post:nth-child(2)
{
   background:blue;
}

.news_post:nth-child(3)
{
   background:black;
}
您可以尝试以下方法:

.news_post:nth-child(1)
{
   background:#ff0000;
}

.news_post:nth-child(2)
{
   background:blue;
}

.news_post:nth-child(3)
{
   background:black;
}
你可以用

如果您只想在
First Div
上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}

如果要在所有三个div上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}
您可以使用

如果您只想在
First Div
上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}

如果要在所有三个div上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}
您可以使用

如果您只想在
First Div
上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}

如果要在所有三个div上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}
您可以使用

如果您只想在
First Div
上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}

如果要在所有三个div上应用不同的样式,请尝试以下操作:

.news_post{
    background-color:black;
    height:20px;
    width:200px;
}
.news_post:first-child
{
    background-color:red;
}
.news_post{
    height:20px;
    width:200px;
}
.news_post:nth-child(1)
{
   background-color:red;
}

.news_post:nth-child(2)
{
   background-color:green;
}

.news_post:nth-child(3)
{
   background-color:black;
}

第二节呢?是的,我也有同样的问题。如果第一个div和最后一个div之间有div,该怎么办?这是一个如何使用伪类的示例。我不是在为他写确切的答案。让他看一遍,它会给出一个实用的想法
我不是在为他写确切的答案
-好吧,如果你试图回答一个问题,最好完整地回答,而不是给出一部分,然后期待其他部分被搜索。第二部分呢?是的,即使我也有同样的问题。如果第一个div和最后一个div之间有div,该怎么办?这是一个如何使用伪类的示例。我不是在为他写确切的答案。让他看一遍,它会给出一个实用的想法
我不是在为他写确切的答案
-好吧,如果你试图回答一个问题,最好完整地回答,而不是给出一部分,然后期待其他部分被搜索。第二部分呢?是的,即使我也有同样的问题。如果第一个div和最后一个div之间有div,该怎么办?这是一个如何使用伪类的示例。我不是在为他写确切的答案。让他看一遍,它会给出一个实用的想法
我不是在为他写确切的答案
-好吧,如果你试图回答一个问题,最好完整地回答,而不是给出一部分,然后期待其他部分被搜索。第二部分呢?是的,即使我也有同样的问题。如果第一个div和最后一个div之间有div,该怎么办?这是一个如何使用伪类的示例。我不是在为他写确切的答案。让他看一下,这会给你一个切实可行的想法。我不是在为他写确切的答案。好吧,如果你想回答一个问题,最好用完整的方式来回答,而不是只给出一部分,然后期待其他的被搜索。