响应式的SVG(相同的字号),作为内联块。

huangapple go评论55阅读模式
英文:

Responsive SVG (same font-size) as inline-block

问题

I write a manual were I explain what different cube values (1 to 6) mean.

So I have created the cubes as SVG and use the svg as inline in my html. My goal would be that the viewbox or the SVG is repsonsive without javascript. So when i change the font-size of p.para from 0.5rem to any other size. The cube should follow the text size.

I tried it with a <span> and without it.

Here the HTML code (the cube is just a rectangle)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p.para {
  text-align: justify!important;
  font-size: 0.5rem;
}

p.para span {
  display: inline-block; 
  width: 1rem; 
  height: 1rem;
}

p.para svg {
  height: 1rem;
  display: inline-block;
  position: relative;
  vertical-align: bottom;
}

<!-- language: lang-html -->

<p class="para">
  bla bla foo bar foobar bla bla
  <span>
    <svg viewBox="0 0 20 20" >
      <rect width="20" height="20" rx="5" 
      fill="blue" />
    </svg>
  </span>
  bla bla foo bar foobar bla bla.
  More text and even more text.
</p>

<!-- end snippet -->

At the moment the SVG has always the same size, and is not relative or responsive to the p font size.

英文:

I write a manual were I explain what different cube values (1 to 6) mean.

So I have created the cubes as SVG and use the svg as inline in my html. My goal would be that the viewbox or the SVG is repsonsive without javascript. So when i change the font-size of p.para from 0.5rem to any other size. The cube should follow the text size.

I tried it with a &lt;span&gt; and without it.

Here the HTML code (the cube is just a rectangle)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p.para {
  text-align: justify!important;
  font-size: 0.5rem;
}

p.para span {
  display: inline-block; 
  width: 1rem; 
  height: 1rem;
}

p.para svg {
  height: 1rem;
  display: inline-block;
  position: relative;
  vertical-align: bottom;
}

<!-- language: lang-html -->

&lt;p class=&quot;para&quot;&gt;
  bla bla foo bar foobar bla bla
  &lt;span&gt;
    &lt;svg viewBox=&quot;0 0 20 20&quot; &gt;
      &lt;rect width=&quot;20&quot; height=&quot;20&quot; rx=&quot;5&quot; 
      fill=&quot;blue&quot; /&gt;
    &lt;/svg&gt;
  &lt;/span&gt;
  bla bla foo bar foobar bla bla.
  More text and even more text.
&lt;/p&gt;

<!-- end snippet -->

At the moment the SVG has always the same size, and is not relative or responsive to the p font size.

答案1

得分: 0

Thanks to CBroe I was able to fix it.

And even the code is now simpler. 响应式的SVG(相同的字号),作为内联块。

  • I removed the child-selector &gt;
  • I removed the styling for the &lt;svg&gt; at all

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p.para {
  text-align: justify!important;
  font-size: 3rem;
}

p.para span {
  display: inline-block; 
  width: 1em; 
  height: 1em;
}

<!-- language: lang-html -->

&lt;p class=&quot;para&quot;&gt;
  bla bla foo bar foobar bla bla
  &lt;span&gt;
    &lt;svg viewBox=&quot;0 0 20 20&quot; &gt;
      &lt;rect width=&quot;20&quot; height=&quot;20&quot; rx=&quot;5&quot; 
      fill=&quot;blue&quot; /&gt;
    &lt;/svg&gt;
  &lt;/span&gt;
  bla bla foo bar foobar bla bla.
  More text and even more text.
&lt;/p&gt;

<!-- end snippet -->

英文:

Thanks to CBroe I was able to fix it.

And even the code is now simpler. 响应式的SVG(相同的字号),作为内联块。

  • I removed the child-selector &gt;
  • I removed the styling for the &lt;svg&gt; at all

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

p.para {
  text-align: justify!important;
  font-size: 3rem;
}

p.para span {
  display: inline-block; 
  width: 1em; 
  height: 1em;
}

<!-- language: lang-html -->

&lt;p class=&quot;para&quot;&gt;
  bla bla foo bar foobar bla bla
  &lt;span&gt;
    &lt;svg viewBox=&quot;0 0 20 20&quot; &gt;
      &lt;rect width=&quot;20&quot; height=&quot;20&quot; rx=&quot;5&quot; 
      fill=&quot;blue&quot; /&gt;
    &lt;/svg&gt;
  &lt;/span&gt;
  bla bla foo bar foobar bla bla.
  More text and even more text.
&lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月10日 14:58:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76215687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定