Skip to content Skip to sidebar Skip to footer

Html Inside Svg

I would like to add some HTML markup in an SVG drawing. As far as I know, this is not possible with SVG. The image gets displayed in the browser. Is there a way to paint the HTML

Solution 1:

User @enxaneta pointed me to foreignObject.

It works fine (even in webbrowser Edge).

<svgviewBox="0 0 200 200"xmlns="http://www.w3.org/2000/svg"><style>div {
  color: white;
  font: 18px serif;
  height: 100%;
  overflow: auto;
}
  </style><polygonpoints="5,5 195,10 185,185 10,195" /><!-- Common use case: embed HTML text into SVG --><foreignObjectx="20"y="20"width="160"height="160"><!--
  In the context of SVG embedded in an HTML document, the XHTML 
  namespace could be omitted, but it is mandatory in the 
  context of an SVG document
--><divxmlns="http://www.w3.org/1999/xhtml">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  Sed mollis mollis mi ut ultricies. Nullam magna ipsum,
  porta vel dui convallis, rutrum imperdiet eros. Aliquam
  erat volutpat.
</div></foreignObject></svg>

CodePen: https://codepen.io/guettli/pen/RwwBxQL

Solution 2:

There you go!

.wrapper{
    position: relative;
    width: 100px;
    height: 100px;
}
.text{
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    top: 0px;
    bottom: 0px;
    left: 0px;
    right: 0px;
    color: white;
}
<html><body><divclass="wrapper"><svgwidth="100"height="100"><circlecx="50"cy="50"r="40"stroke="red"stroke-width="4"fill="blue" /></svg><divclass="text">
TEXT
</div></div></body></html>

Solution 3:

Since I want to use the css features of HTML the svg <text> element is not enough.

You can use CSS on <text>, or really any SVG element. The only major difference is that you have to use fill instead of color if you need to change the text color. Where that CSS would live would depend on how you plan on implementing the image on the site. If the SVG is embedded, ie via an <img> tag, the styles would have to be inlined into the SVG file itself, using a <defs> tag, as seen below.

<svgwidth="400"height="110"><defs><style>.my-custom-text {
fill: #fff;
font-weight: bold;
}
.cyan {
fill: #0ff;
}
</style></defs><g><rectwidth="300"height="100"style="fill:rgb(0,0,255);stroke-width:3;stroke:rgb(0,0,0)" /><textx="15"y="25"class="my-custom-text">My custom <tspanclass="cyan">text</tspan></text></g></svg>

If you are inlining the SVG, then you can actually think of it as running as HTML -- it, and its child elements, are just normal HTML elements and can be targeted just like any other element. This means that the CSS can be put anywhere you would normally put it - as an external document or in any <style> tag.

And if all else fails, you can always just absolutely position the HTML content above the image in a separate container and style it that way.

Post a Comment for "Html Inside Svg"