为什么“<use xlink:href...”会改变 svg 图像的行为?

Why does "<use xlink:href..." change the behavior of an svg image?

我正在研究下面的代码:

* {
  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 class),检查 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 class 并检查了标签中缺少 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>