html - 如何在 css 中为每个 html 帖子设置不同的样式

标签 html css

我的 HTML 页面中有 4 个帖子,除了文本内容和图像以及文本和背景的颜色外,它们看起来都一样,在下图中您会找到我想要的结果:

enter image description here

我的问题是如何在不为每篇文章创建类 css 的情况下设置每篇文章的样式我只想改变颜色

这是我的代码

.art-content {
  position: relative;
}
.art-content img {
  width: 100%;
}
.main-content article {
  margin-top: 15px;
}
.main-content article:nth-child(2n) {
  padding-left: 0px;
}
.main-content article:nth-child(2n+1) {
  padding-left: 15px;
  padding-right: 0px;
}
.main-content article:nth-child(2n) {
  padding-left: 0px;
}
.main-content article:nth-child(2n+1) {
  padding-left: 15px;
  padding-right: 0px;
}
.rect-date-even {
  position: absolute;
  display: block;
  background-color: #c2022a;
  width: 75px;
  height: 77px;
  top: 0;
  left: 0;
  text-align: center;
  padding: 9px 6px 15px 4px;
}
.rect-date-even span {
  font-family: 'Quicksand';
  font-size: 20px;
  color: white;
  padding-bottom: -10px;
}
.rect-info-art {
  position: relative;
  width: 100%;
  height: 105px;
  background-color: #dcdcdc;
}
.rect-cat {
  position: absolute;
  width: 120px;
  height: 18px;
  background-color: #c2022a;
  color: white;
  font-family: 'Quicksand';
  font-size: 12px;
  padding-left: 3px;
  top: 10px;
  left: 25px;
}
.rect-info-art p {
  position: absolute;
  font-style: italic;
  font-size: 18px;
  max-height: 10px;
  padding-right: 50px;
  top: 30px;
  left: 25px;
}
.rect-info-art a {
  position: absolute;
  color: #c2022a;
  font-size: 14px;
  text-align: center;
  top: 70px;
  right: 20px;
}
<section class="col-md-8 col-lg-8 main-content">
  <div class="title-section-bg">
    <img src="assets/img/ombre-title-section.png">
    <div></div>
    <span class="icon-megaphone"></span>
    <h2>Prochainement a Rabat</h2> 
  </div>
  <article class="col-md-6 col-lg-6 art-content">
    <img src="assets/img/IMG-even1.png">
    <div class="rect-date-even">
      <span>03</span>
      <span>AVR</span>
    </div>
    <div class="rect-info-art">
      <div class="rect-cat">Musique du monde</div>
      <p>Rihanna en concert à Mawazine 2015</p>
      <a href="">détails <span class="icon-circle-plus"></span></a>
    </div>

  </article>
  <article class="col-md-6 col-lg-6 art-content">
    <img src="assets/img/IMG-even1.png">
    <div class="rect-date-even">
      <span>03</span>
      <span>AVR</span>
    </div>
    <div class="rect-info-art">
      <div class="rect-cat">Musique du monde</div>
      <p>Rihanna en concert à Mawazine 2015</p>
      <a href="">détails <span class="icon-circle-plus"></span></a>
    </div>

  </article>
</section>

感谢帮助

最佳答案

使用 :nth-of-type(n) 伪选择器来单独设置它们的样式。

div {
  width: 100px;
  height: 100px;
  margin: 5px;
}

div:nth-of-type(1) p {
  background-color: red;
}
div:nth-of-type(2) p {
  background-color: blue;
}
div:nth-of-type(3) p {
  background-color: yellow;
}
div:nth-of-type(4) p {
  background-color: green;
}
<div><p>hello</p></div>
<div><p>hello</p></div>
<div><p>hello</p></div>
<div><p>hello</p></div>

关于html - 如何在 css 中为每个 html 帖子设置不同的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41262753/

相关文章:

html - 动态div的对齐

javascript - 网站加载并启用文本字段然后选中复选框时隐藏文本字段

html - 如何将子元素的宽度设置为全宽而不是 'visible' 宽度(被 overflow-x : auto) 截断

html - 框宽度 100% 的 CSS3 问题

php - 在产品属性下拉菜单 woocommerce 旁边添加工具提示描述

html - <aside> 标记在主要内容之前或之后

html - 当从不同页面引用部分时 CSS 不加载

javascript - 通过数据过滤器过滤折叠 div 并突出显示当前链接

css - Ionic 3 - approxItemHeight 在 vi​​rtualScroll 中不起作用

html - 从 <ul> 导航中删除左侧空间