简体   繁体   English

CSS-如何为悬停时的特殊形状着色?

[英]CSS - How to color special shape on hover?

I've created a "tag" shape using CSS (the rectangular base + triangle). 我使用CSS(矩形底+三角形)创建了“标签”形状。 Since I have more than one tag shape I wanted to add the hover property to the class which defines that shape and that way automatically attach hover to all tags. 由于我具有多个标签形状,因此我想将hover属性添加到定义该形状的类中,并以这种方式将hover自动附加到所有标签上。 However, it appears its not working and the only way to apply hover is by id . 但是,它似乎不起作用,并且应用hover的唯一方法是通过id Why is that? 这是为什么? There surely must be an easier way to apply hover to several elements at once. 当然,一定有一种更容易的方法可以将hover一次应用于多个元素。

Second question, since tag shape is built using two shapes, how should the hover color transition should be made? 第二个问题,由于标记形状是使用两种形状构建的,应该如何进行hover颜色转换?

JSfiddle 的jsfiddle

#q{
    position:relative;
    margin:0 5px 0 10px;
    displaY:inline-block;
    height:66px;
    padding: 0 35px 0 20px;


    font-size: 25px;
    line-height:65px;

    cursor: pointer;
    font-weight: 100;
    margin: 20px 25px;

    background:#f3f3f3;
    transition: background 0.3s;


}

#q:after{
    position:absolute;
    content:"";
    right:-19px;
    width: 1px;
    height:0px;
    border-left:18px solid #f3f3f3;
    border-top:  33px solid transparent;
    border-bottom: 33px solid transparent;
    transition: background 0.3s;
}

#q:hover{
    background: green;
    border-left:18px solid lightblue;

}

HTML: HTML:

<span class="pricetag-right" id="q">tag is here!</span>

DEMO PAGE 演示页

#q{
    position:relative;
    margin:0 5px 0 10px;
    displaY:inline-block;
    height:66px;
    padding: 0 35px 0 20px;


    font-size: 25px;
    line-height:65px;

    cursor: pointer;
    font-weight: 100;
    margin: 20px 25px;

    background:#f3f3f3;
    transition: background 0.3s;


}

#q:after{
    position:absolute;
    content:"";
    right:-19px;
    width: 1px;
    height:0px;
    border-left:18px solid #f3f3f3;
    border-top:  33px solid transparent;
    border-bottom: 33px solid transparent;
    transition: border 0.3s;
}

#q:hover{
    background: green;
}

#q:hover:after{
    border-left-color:green;
}

You needed to set the transition of the :after to border and not background , since it's the border property being transitioned. 您需要将:aftertransition设置为border而不是background ,因为这是border属性的过渡。

Here's a fiddle based on vsync's with class selectors: 这是基于带有类选择器的vsync的小提琴:

https://jsfiddle.net/ajanini/9z3Lvp90/ https://jsfiddle.net/ajanini/9z3Lvp90/

.pricetag-right{
    position:relative;
    margin:0 5px 0 10px;
    displaY:inline-block;
    height:66px;
    padding: 0 35px 0 20px;


    font-size: 25px;
    line-height:65px;

    cursor: pointer;
    font-weight: 100;
    margin: 20px 25px;

    background:#f3f3f3;
    transition: background 0.3s;


}

.pricetag-right:after{
    position:absolute;
    content:"";
    right:-19px;
    width: 1px;
    height:0px;
    border-left:18px solid #f3f3f3;
    border-top:  33px solid transparent;
    border-bottom: 33px solid transparent;
    transition: border 0.3s;
}

.pricetag-right:hover{
    background: green;
}

.pricetag-right:hover:after{
    border-left-color:green;
}

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

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