Inquiring about the efficient use of inline background SVGs

My attempts to utilize an inline SVG as a background have been unsuccessful thus far. Despite referencing some stackoverflow posts and reading the css-tricks article on how to do it, I just can't seem to make it work :(

I am specifically aiming to use a simple "X" SVG as a CSS background following the instructions laid out in the css-tricks article, but evidently there is a misstep in my implementation since nothing is showing up on my page. Below is a snippet of the code I have been working with.

body {
  background: black;
}

.container {
 background:url('data:image/svg+xml,utf-8,<svg stroke="%23fff" viewBox="0 0 40 40"><path d="M 10,10 L 30,30 M 30,10 L 10,30"></path></svg>');
  height: 100vh;
  width: 90vw;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div class="container"></div>
  <!-- <svg stroke="#fff" viewBox="0 0 40 40"><path d="M 10,10 L 30,30 M 30,10 L 10,30"></path></svg> -->
</body>
</html>

In the HTML file, I left the SVG commented out. Once uncommented, the SVG displays perfectly when used conventionally (as an HTML element). It's only when employed as a CSS background that things start to go awry. I even attempted encoding it using a base64 tool, as suggested by some users, but no luck. I'm clearly missing something here.

I understand this may be a basic issue, so I apologize for that. Appreciation in advance to anyone who offers assistance!

Answer №1

You made a mistake with one typo and forgot to include a namespace definition:

incorrect:

data:image/svg+xml,utf-8,<svg stroke="%23fff"

correct :

 data:image/svg+xml;utf-8,<svg xmlns="http://www.w3.org/2000/svg" stroke="%23fff"

body {
  background: black;
}

.container {
  background:url('data:image/svg+xml;utf-8,<svg xmlns="http://www.w3.org/2000/svg" stroke="%23fff" viewBox="0 0 40 40"><path d="M 10,10 L 30,30 M 30,10 L 10,30"></path></svg>');
  height: 100vh;
  width: 90vw;
}
  <div class="container"></div>

Answer №2

body { background-image: url("")}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What measures can be taken to stop links from disrupting the style of a Polymer site?

In Polymer, it seems that many styles are inherited from the surrounding document. For example, the code snippet below will display the icon and button with a white foreground color and adjust spacing: <paper-toolbar> <paper-icon-button icon=" ...

Create an HTML and CSS code that allows you to split paragraph text into columns within a

I am seeking a way to create dynamic paragraph column text using only the https://i.sstatic.net/ZX1AN.png Here is an example of how it could be displayed in HTML: <div> <p> Sed ut perspiciatis, unde omnis iste natus error sit voluptatem ...

Changing datetime-local format from European to American dates

<input type="datetime-local"> While this input retrieves the local time from your PC, I'm curious if there's a way to make it display as US time specifically. ...

Is there a way to position this navbar menu on the right side of the website?

Does anyone know how to align the navbar menu to the right side? I've been attempting to float it right without success. What would be the recommended solution? I have tried multiple approaches but haven't achieved any results yet. Can someone ...

Issue with applying Bootstrap button style to buttons

I have been attempting to customize these buttons using my CSS, but unfortunately, they are not reflecting the desired style. Even after creating new buttons, I encountered the same issue where they did not adhere to the CSS. Upon closer inspection, I not ...

Convert a string into HTML text using regular expressions

How can you correctly transform a string such as this: html attr = "value" attr2 = 'UnmatchInSubstrings' some \escapedTag content subtag subcontent /subtag br / /html into: <html attr = "value" attr2 = 'UnmatchInSubstrings'&g ...

Reading settings from web.config file in an HTML page

Currently working with ASP.NET 3.5 web forms. I've recently saved the path of my website in the web.config file under the key "CommonPath". I have a basic static HTML page that I would like to utilize this key on. Any suggestions on how to achieve ...

What is the reason behind the select tag requiring at least one child for it to be considered XHTML 1.0 compliant?

Why is it necessary for the select tag to have at least one child in order to be considered valid XHTML 1.0? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="ht ...

Unable to retrieve email data from this PHP form

I've been working on implementing an email form on my website, but for some reason, I'm unable to receive emails from this form. HTML Code <form method="POST" action="processor.php" id="contactform" onsubmit="return(validateForm());"> ...

Is HTML5 data-target used for loading pages?

Currently, I am working with some HTML5 code: <li class="tile google" data-target-activation="click" data-target="loading"> <div> <a href="#">Search Google</a> <h2>Google</h2> </div> </li> Upon ...

Increasing the padding at the top of the logo when scrolling down the page

When scrolling down the page, there seems to be extra padding above the logo that is throwing off the alignment with the rest of the site. I've been trying different solutions to correct this issue: //$('.navbar-brand').css({ 'padding- ...

"Struggling with getting the text color to change on hover in Tailwind

Today has been a frustrating day as I am diving into the world of tailwindcss and have been struggling with a particular issue. My code is below: <button className="bg-yellow-500 px-4 py-2 hover:text-black text-white"> Some Text Her ...

Navigation that sticks and changes upon hovering over div elements

Just delving into the world of jQuery and JS, so I appreciate your patience:) Currently, I have a sticky navigation bar positioned at the top of my webpage that links to different sections of content below. I am looking to create an effect where the corr ...

What is the best way to align an element at the bottom in order to allow it to grow vertically

I have a unique structure on one of my pages that I want to use for a tooltip style behavior. When the "container" element is clicked, the "child" element, which is initially hidden, should appear above the container. However, since the child's height ...

Create an instance of an object in order to make it accessible to several directives

My Current Project: I am currently working on developing two attribute directives. One directive is designed to move an element to the left, while the other centers an element on the page. Below is a snippet of the code showcasing how I am using ng-style t ...

What causes Next.JS to automatically strip out CSS during the production build process?

Encountering unpredictability in CSS loading while deploying my Next.JS site. Everything appears fine locally, but once deployed, the CSS rules seem to vanish entirely. The element has the attached class, yet the corresponding styling rules are nowhere to ...

How can we integrate this icon/font plugin in CSS/JavaScript?

Check out the live demonstration on Jsfiddle http://jsfiddle.net/hc046u9u/ <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materializ ...

adjusting the spacing between lines for a specific li element

I am seeking to vertically align the phone number and email address with the little icons next to them. When I try to adjust the line-height, it affects all the li's in that section. I believe this is because they are inline elements. Here is the webs ...

Updating the navigation with a dynamic CSS class using jQuery

Hey there, I'm on the lookout for a simple and discreet method to dynamically add an "active" class to a navigation menu based on the current directory. Unfortunately, I am unable to modify the body tag with a custom class, ruling out a pure CSS solut ...

Is there a way to determine the location where my website is fetching CSS and media files from?

After implementing Wagtail on my website, I encountered an issue with the CSS and images not being found when the site was put into production. I have already tried running python manage.py collectstatic and ensured that all of my CSS files are located in ...