使用 css 旋转 svg 会导致 svg 上出现灰色背景
Rotating svg with css causes a gray background to appear on the svg
我有一个带有 :hov 动画的按钮:
Button without pressing it
这是按下按钮后的按钮,由于某种原因背景出现在 svg 元素上:
Button after pressing it
css 按钮空闲和 svg 空闲:
.input-accordion{
display: flex;
flex-direction: row;
align-items: center;
background-color:rgb(32, 32, 109) ;
color: white;
width: 300px;
text-align: left;
padding-left: 30px;
outline: none;
border-style: none;
border: 2px solid white;
transition: 0.3s ease;
width:400px;
}
.input-accordion img{
margin-left: 5px;
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(118deg) brightness(101%) contrast(102%);
transform: scale(90%);
transition: 0.3s ease;
}
我通过添加“打开”class 名称来激活按钮,这里是激活按钮(和 svg)的 css:
.input-accordion.open{
background-color: rgb(136, 136, 202);
}
.input-accordion.open img{
background-color: rgb(136, 136, 202);
transform: rotate(180deg);
transition: 0.3s ease;
}
.input-accordion{
display: flex;
flex-direction: row;
align-items: center;
background-color:rgb(32, 32, 109) ;
color: white;
width: 300px;
text-align: left;
padding-left: 30px;
outline: none;
border-style: none;
border: 2px solid white;
transition: 0.3s ease;
width:400px;
}
.input-accordion.open{
background-color: rgb(136, 136, 202);
}
.input-accordion.open svg{
background-color: rgb(136, 136, 202);
transform: rotate(180deg);
transition: 0.3s ease;
}
.input-accordion svg{
margin-left: 5px;
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(118deg) brightness(101%) contrast(102%);
transform: scale(90%);
transition: 0.3s ease;
}
.input-accordion:hover{
cursor: pointer;
background-color: rgb(136, 136, 202);
transition: 0.3s ease;
}
<button class='input-accordion'>
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z"/></svg>
</button>
<br/>
<br/>
<button class='input-accordion open'>
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z"/></svg>
</button>
我认为您需要从 .input-accordion.open svg
规则中删除背景颜色:
<!DOCTYPE html>
<html>
<head>
<style>
.input-accordion {
display: flex;
flex-direction: row;
align-items: center;
background-color: rgb(32, 32, 109);
color: white;
width: 300px;
text-align: left;
padding-left: 30px;
outline: none;
border-style: none;
border: 2px solid white;
transition: 0.3s ease;
width: 400px;
}
.input-accordion.open {
background-color: rgb(136, 136, 202);
}
.input-accordion.open svg {
/* background-color: rgb(136, 136, 202); <-- don't add background to svg*/
transform: rotate(180deg);
transition: 0.3s ease;
}
.input-accordion svg {
margin-left: 5px;
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(118deg) brightness(101%) contrast(102%);
transform: scale(90%);
transition: 0.3s ease;
}
.input-accordion:hover {
cursor: pointer;
background-color: rgb(136, 136, 202);
transition: 0.3s ease;
}
</style>
</head>
<body>
<button class="input-accordion">
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z" />
</svg>
</button>
<br />
<button class="input-accordion open">
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z" />
</svg>
</button>
</body>
</html>
我有一个带有 :hov 动画的按钮: Button without pressing it
这是按下按钮后的按钮,由于某种原因背景出现在 svg 元素上: Button after pressing it
css 按钮空闲和 svg 空闲:
.input-accordion{
display: flex;
flex-direction: row;
align-items: center;
background-color:rgb(32, 32, 109) ;
color: white;
width: 300px;
text-align: left;
padding-left: 30px;
outline: none;
border-style: none;
border: 2px solid white;
transition: 0.3s ease;
width:400px;
}
.input-accordion img{
margin-left: 5px;
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(118deg) brightness(101%) contrast(102%);
transform: scale(90%);
transition: 0.3s ease;
}
我通过添加“打开”class 名称来激活按钮,这里是激活按钮(和 svg)的 css:
.input-accordion.open{
background-color: rgb(136, 136, 202);
}
.input-accordion.open img{
background-color: rgb(136, 136, 202);
transform: rotate(180deg);
transition: 0.3s ease;
}
.input-accordion{
display: flex;
flex-direction: row;
align-items: center;
background-color:rgb(32, 32, 109) ;
color: white;
width: 300px;
text-align: left;
padding-left: 30px;
outline: none;
border-style: none;
border: 2px solid white;
transition: 0.3s ease;
width:400px;
}
.input-accordion.open{
background-color: rgb(136, 136, 202);
}
.input-accordion.open svg{
background-color: rgb(136, 136, 202);
transform: rotate(180deg);
transition: 0.3s ease;
}
.input-accordion svg{
margin-left: 5px;
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(118deg) brightness(101%) contrast(102%);
transform: scale(90%);
transition: 0.3s ease;
}
.input-accordion:hover{
cursor: pointer;
background-color: rgb(136, 136, 202);
transition: 0.3s ease;
}
<button class='input-accordion'>
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z"/></svg>
</button>
<br/>
<br/>
<button class='input-accordion open'>
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z"/></svg>
</button>
我认为您需要从 .input-accordion.open svg
规则中删除背景颜色:
<!DOCTYPE html>
<html>
<head>
<style>
.input-accordion {
display: flex;
flex-direction: row;
align-items: center;
background-color: rgb(32, 32, 109);
color: white;
width: 300px;
text-align: left;
padding-left: 30px;
outline: none;
border-style: none;
border: 2px solid white;
transition: 0.3s ease;
width: 400px;
}
.input-accordion.open {
background-color: rgb(136, 136, 202);
}
.input-accordion.open svg {
/* background-color: rgb(136, 136, 202); <-- don't add background to svg*/
transform: rotate(180deg);
transition: 0.3s ease;
}
.input-accordion svg {
margin-left: 5px;
filter: invert(100%) sepia(0%) saturate(0%) hue-rotate(118deg) brightness(101%) contrast(102%);
transform: scale(90%);
transition: 0.3s ease;
}
.input-accordion:hover {
cursor: pointer;
background-color: rgb(136, 136, 202);
transition: 0.3s ease;
}
</style>
</head>
<body>
<button class="input-accordion">
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z" />
</svg>
</button>
<br />
<button class="input-accordion open">
<h3>Bleach</h3>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
<path d="M0 7.33l2.829-2.83 9.175 9.339 9.167-9.339 2.829 2.83-11.996 12.17z" />
</svg>
</button>
</body>
</html>