2个单独的css类应用于1个元素

时间:2016-01-08 22:18:12

标签: html css wordpress

我有一个wordpress页面,我想根据帖子类别为帖子添加一个底部边框。 如果post只有1个类别,那么我使用:

.category-daily {
  border-bottom: red solid 3px;
}

但是有些帖子有2个类别,因此有2个类别,例如:category-weekly和category-daily 如何为每日类别添加红色底部边框,然后为每周类别添加黄色底部边框

4 个答案:

答案 0 :(得分:1)

元素不能有两个边框..但你可以用伪元素伪造它。



body {
  text-align: center;
}
div {
  height: 100px;
  width: 150px;
  display: inline-block;
  box-shadow: inset 0 0 1px 0 black;
  /* for demo purposes only */
  padding-bottom: 6px;
  position: relative;
}
.category-daily:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 0;
  border-bottom: red solid 3px;
}
.category-weekly:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 0;
  border-bottom: yellow solid 3px;
}
.category-daily.category-weekly:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 0;
  border-top: red solid 3px;
  border-bottom: yellow solid 3px;
}

<div class="category-daily"></div>
<div class="category-weekly"></div>
<div class="category-daily category-weekly"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

无法定义多个边框底部。

如果您希望获得此类视觉效果,则必须为您正在应用的每个类别添加额外元素。

例如div element for each category

<强> HTML

scons variants=all

<强> CSS

<div class="post">
<div class="daily"></div>
<div class="weekly"></div>
</div>

甚至更好:a hr for each category

<强> HTML

.post .daily{
background:red;
height:1px;
width:100%;
}
.post .weekly{
background:yellow;
height:1px;
width:100%;
}

<强> CSS

<div class="post">
<hr class="daily"/>
<hr class="weekly" />
</div>

答案 2 :(得分:0)

一种选择是使用box-shadow作为另一个边框,如下所示:

.daily {
  border-bottom: 3px solid red;
}

.weekly {
  box-shadow: 0 3px 0 yellow;
}
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
  </head>

  <body>
    <div class="daily">This is daily</div>
    <div class="daily">This is daily</div>
    <div class="weekly">This is weekly</div>
    <div class="daily">This is daily</div>
    <div class="daily weekly">This is daily and weekly</div>
    <div class="weekly">This is weekly</div>
    <div class="daily">This is daily</div>
  </body>

</html>

答案 3 :(得分:0)

你不能有两个边框,但如果你对jquery感到满意,这可能适合你:

$('.category-daily.category-weekly').wrap('<div class="bbottom"></div>');
div{
  width: 60px;
  height: 60px;
}
.category-daily.category-weekly {
border: 1px solid black;
border-bottom: 9px solid red;
}
.category-daily{
border: 1px solid black;
border-bottom: 9px solid red;
}
.category-weekly {
border: 1px solid black;
border-bottom: 9px solid yellow;
}
.bbottom{
padding-bottom: 9px;
border-bottom: 9px solid yellow;  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div class='category-daily category-weekly'>
</div>
<br>
<div class='category-daily'>
</div>
<br>
<div class='category-weekly'>
</div>

这将包含两个类的div与另一个包含辅助border-bottom的div。