Ask Your Question
2

How to create HTML SVG texts with dissimilar background colors?

asked 2023-06-26 16:19:21 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-26 16:25:02 +0000

huitzilopochtli gravatar image

You can create HTML SVG texts with dissimilar background colors using:

  1. The <rect> element: You can create a <rect> element with the desired background color and put the <text> element on top of it.

For example:

<svg>
  <rect x="0" y="0" width="100" height="50" fill="red"></rect>
  <text x="50" y="30" fill="white" text-anchor="middle">Hello, World!</text>
</svg>

This will create a SVG text with a red background and white text.

  1. The <foreignobject> element: You can use the <foreignobject> element to embed HTML elements inside SVG.

For example:

<svg>
  <foreignObject x="0" y="0" width="100" height="50">
    <div style="background-color: red; color: white; text-align: center;">Hello, World!</div>
  </foreignObject>
</svg>

This will create a SVG text with a red background and white text using CSS in a <foreignobject> element.

Note: Not all web browsers support the <foreignobject> element, so it may not work in all situations.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-26 16:19:21 +0000

Seen: 24 times

Last updated: Jun 26 '23