Skip to content
TheChatArchiveThe Chat Archive
gpt-4.1-mini-2025-04-14· OpenAIHugging Face LMArena Human Preference 140K

Simplest way to have background for SVG text.

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.5-pro-preview-05-06’s answer.
  1. User

    Simplest way to have background for SVG text.

  2. Assistant

    The simplest way to have a background for SVG text is to place a <rect> (rectangle) behind the <text> element. You just need to position and size the rectangle to cover the text area.

    Here’s a minimal example:

    <svg xmlns="http://www.w3.org/2000/svg" width="200" height="50">
      <!-- Background rectangle -->
      <rect x="0" y="0" width="200" height="50" fill="yellow" />
      
      <!-- Text on top -->
      <text x="10" y="35" font-family="Arial" font-size="30" fill="black">Hello</text>
    </svg>
    

    Explanation:

    • The <rect> creates a yellow background.
    • The <text> is placed on top with some padding (x=10, y=35).
    • Adjust the rectangle size and position to fit your text.

    If you want the background to fit the text exactly, you can use the <text> element’s bounding box with JavaScript or manually size the rectangle based on font size and text length.


    Bonus: For a quick background just behind the text (like a highlight), you can use the <text> element with a paint-order and stroke:

    <svg xmlns="http://www.w3.org/2000/svg" width="200" height="50">
      <text x="10" y="35" font-family="Arial" font-size="30" fill="black" stroke="yellow" stroke-width="10" paint-order="stroke">
        Hello
      </text>
    </svg>
    

    This creates a thick yellow stroke behind the black text, simulating a background highlight.


    Let me know if you want a solution that automatically sizes the background to the text!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.