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

Time:05-06

I am studying the following code:

* {
  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  id="morning" type="checkbox" />
<label  for="morning">
  <span>
    <svg width="12px" height="10px">
      <use xlink:href="#check"></use>
    </svg>
  </span>
  <span>Label</span>
</label>

<!--SVG Sprite-->
<svg >
  <symbol id="check" viewbox="0 0 12 10">
    <polyline points="1.5 6 4.5 9 10.5 1"></polyline>
  </symbol>
</svg>

And when I extract the separate svg and put it directly in the svg tag (but removing the inline-svg class), the check svg no longer shows up. As shown here:

* {
  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  id="morning" type="checkbox" />
<label  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>

What is the cause for this behavior and how do I make it behave the same way?

I have tried using the inline-svg class and checked tag for tag what css properties are missing but I can't see something that differs which makes me believe it is some special behavior from the svg tag.

CodePudding user response:

With the help from @enxaneta I learned that <symbol> is not visible unless used with <use> so I simply removed the <symbol> and replaced it with a <svg> and it works perfectly:

* {
  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  id="morning" type="checkbox" />
<label  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>

  • Related