为什么"<使用 xlink:href..."更改 SVG 图像的行为?



我正在学习下面的代码:

* {
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
text-rendering: optimizelegibility;
color: #223254;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.cbx {
-webkit-user-select: none;
user-select: none;
cursor: pointer;
padding: 6px 8px;
border-radius: 6px;
overflow: hidden;
transition: all 0.2s ease;
}
.cbx:not(:last-child) {
margin-right: 6px;
}
.cbx:hover {
background: rgba(0, 119, 255, 0.06);
}
.cbx span {
float: left;
vertical-align: middle;
transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
position: relative;
width: 18px;
height: 18px;
border-radius: 4px;
transform: scale(1);
border: 1px solid #cccfdb;
transition: all 0.2s ease;
box-shadow: 0 1px 1px rgba(0, 16, 75, 0.05);
}
.cbx span:first-child svg {
position: absolute;
top: 3px;
left: 2px;
fill: none;
stroke: #fff;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 16px;
stroke-dashoffset: 16px;
transition: all 0.3s ease;
transition-delay: 0.1s;
transform: translate3d(0, 0, 0);
}
.cbx span:last-child {
padding-left: 8px;
line-height: 18px;
}
.cbx:hover span:first-child {
border-color: #07f;
}
.inp-cbx {
position: absolute;
visibility: hidden;
}
.inp-cbx:checked+.cbx span:first-child {
background: #07f;
border-color: #07f;
animation: wave 0.4s ease;
}
.inp-cbx:checked+.cbx span:first-child svg {
stroke-dashoffset: 0;
}
.inline-svg {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
user-select: none;
}
@-moz-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-webkit-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-o-keyframes wave {
50% {
transform: scale(0.9);
}
}
@keyframes wave {
50% {
transform: scale(0.9);
}
}
<input class="inp-cbx" id="morning" type="checkbox" />
<label class="cbx" for="morning">
<span>
<svg width="12px" height="10px">
<use xlink:href="#check"></use>
</svg>
</span>
<span>Label</span>
</label>
<!--SVG Sprite-->
<svg class="inline-svg">
<symbol id="check" viewbox="0 0 12 10">
<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</symbol>
</svg>

当我提取单独的svg并将其直接放在svg标记中(但删除了内联svg类)时,检查svg不再显示。如下所示:

* {
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
text-rendering: optimizelegibility;
color: #223254;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.cbx {
-webkit-user-select: none;
user-select: none;
cursor: pointer;
padding: 6px 8px;
border-radius: 6px;
overflow: hidden;
transition: all 0.2s ease;
}
.cbx:not(:last-child) {
margin-right: 6px;
}
.cbx:hover {
background: rgba(0, 119, 255, 0.06);
}
.cbx span {
float: left;
vertical-align: middle;
transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
position: relative;
width: 18px;
height: 18px;
border-radius: 4px;
transform: scale(1);
border: 1px solid #cccfdb;
transition: all 0.2s ease;
box-shadow: 0 1px 1px rgba(0, 16, 75, 0.05);
}
.cbx span:first-child svg {
position: absolute;
top: 3px;
left: 2px;
fill: none;
stroke: #fff;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 16px;
stroke-dashoffset: 16px;
transition: all 0.3s ease;
transition-delay: 0.1s;
transform: translate3d(0, 0, 0);
}
.cbx span:last-child {
padding-left: 8px;
line-height: 18px;
}
.cbx:hover span:first-child {
border-color: #07f;
}
.inp-cbx {
position: absolute;
visibility: hidden;
}
.inp-cbx:checked+.cbx span:first-child {
background: #07f;
border-color: #07f;
animation: wave 0.4s ease;
}
.inp-cbx:checked+.cbx span:first-child svg {
stroke-dashoffset: 0;
}
.inline-svg {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
user-select: none;
}
@-moz-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-webkit-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-o-keyframes wave {
50% {
transform: scale(0.9);
}
}
@keyframes wave {
50% {
transform: scale(0.9);
}
}
<input class="inp-cbx" id="morning" type="checkbox" />
<label class="cbx" for="morning">
<span>
<svg width="12px" height="10px">
<symbol id="check" viewbox="0 0 12 10">
<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</symbol>
</svg>
</span>
<span>Label</span>
</label>

这种行为的原因是什么,我如何使它以同样的方式表现?

我已经尝试使用内联svg类和标签检查标签什么css属性缺失,但我看不到不同的东西,这让我相信这是svg标签的一些特殊行为。

在@enxaneta的帮助下,我了解到<symbol>是不可见的,除非与<use>一起使用,所以我只是删除了<symbol>并用<svg>代替它,它可以完美地工作:

* {
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
font-size: 16px;
-webkit-font-smoothing: antialiased;
text-rendering: optimizelegibility;
color: #223254;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.cbx {
-webkit-user-select: none;
user-select: none;
cursor: pointer;
padding: 6px 8px;
border-radius: 6px;
overflow: hidden;
transition: all 0.2s ease;
}
.cbx:not(:last-child) {
margin-right: 6px;
}
.cbx:hover {
background: rgba(0, 119, 255, 0.06);
}
.cbx span {
float: left;
vertical-align: middle;
transform: translate3d(0, 0, 0);
}
.cbx span:first-child {
position: relative;
width: 18px;
height: 18px;
border-radius: 4px;
transform: scale(1);
border: 1px solid #cccfdb;
transition: all 0.2s ease;
box-shadow: 0 1px 1px rgba(0, 16, 75, 0.05);
}
.cbx span:first-child svg {
position: absolute;
top: 3px;
left: 2px;
fill: none;
stroke: #fff;
stroke-width: 2;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 16px;
stroke-dashoffset: 16px;
transition: all 0.3s ease;
transition-delay: 0.1s;
transform: translate3d(0, 0, 0);
}
.cbx span:last-child {
padding-left: 8px;
line-height: 18px;
}
.cbx:hover span:first-child {
border-color: #07f;
}
.inp-cbx {
position: absolute;
visibility: hidden;
}
.inp-cbx:checked+.cbx span:first-child {
background: #07f;
border-color: #07f;
animation: wave 0.4s ease;
}
.inp-cbx:checked+.cbx span:first-child svg {
stroke-dashoffset: 0;
}
.inline-svg {
position: absolute;
width: 0;
height: 0;
pointer-events: none;
user-select: none;
}
@-moz-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-webkit-keyframes wave {
50% {
transform: scale(0.9);
}
}
@-o-keyframes wave {
50% {
transform: scale(0.9);
}
}
@keyframes wave {
50% {
transform: scale(0.9);
}
}
<input class="inp-cbx" id="morning" type="checkbox" />
<label class="cbx" for="morning">
<span>
<svg width="12px" height="10px" viewbox="0 0 12 10">
<polyline points="1.5 6 4.5 9 10.5 1"></polyline>
</svg>
</span>
<span>Label</span>
</label>

最新更新