Embed the svg element within a specified class

I am seeking guidance on how to insert an SVG that I created into a CSS or SCSS class, so that only the class is assigned to the icon, similar to this example from W3Schools:

<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<i class="fa fa-home"></i>

Answer №1

To incorporate a custom SVG using CSS, follow these two steps:

  1. Convert the SVG into a Data Image
  2. Utilize the Data Image SVG as a CSS Background

Example in Action:

div {
display: inline-block;
width: 40px;
height: 40px;
margin-right: 1px;
background-color: rgb(255, 0, 0);
}

.svg-background {
background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect x="2" y="3" width="36" height="5" fill="rgb(255, 255, 255)" /><rect x="2" y="11" width="36" height="5" fill="rgb(255, 255, 255)" /><rect x="2" y="19" width="36" height="5" fill="rgb(255, 255, 255)" /></svg>');
}
<div></div>
<div class="svg-background"></div>

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 is the best way to show and hide text by toggling a link instead of a button?

I need help with toggling between two different texts when a link is clicked. I know how to achieve this with a button in JQuery, but I'm not sure how to do it with a link. <h1>Queries and Responses</h1> <p>Query: What is the larges ...

Utilizing effective CSS media queries within an Angular application

According to what I've read, it is not recommended in Angular to use the CSS hidden element to hide an element like this: .container{ background-color : powderblue; height : 50px; width : 100% } @media (max-width: 400px){ .container{ ...

What are the steps to configure or reach a page located in a subdirectory of a different project within a Laravel/OctoberCMS website

I have developed a website using an OctoberCMS theme, hosted on a DigitalOcean server. I am looking to integrate a separate project (specifically the code from Matomo analytics) onto the same server and create a public page that can be accessed at my_site. ...

Can someone assist me in locating a specific web element using Selenium in Java?

Here is the original HTML code snippet: <input id="ember354" class="ember-view ember-text-field search" placeholder="Ask me anything!" type="text"> This element can be reached through body-div-div-input. Attempts to find the element by tag name f ...

Transforming the Flask site URL into a user-friendly format to showcase the search query

Is there a way to display the search from a form in the URL format like https://website.com/search?q=query, specifically in Flask? forms.py from flask_wtf import FlaskForm from wtforms import StringField from wtforms.validators import DataRequired class ...

What is causing the HTML sub menu to appear in the incorrect position?

I'm working on an HTML and CSS menu. I'm trying to center the sub-menu relative to the main menu (ul.menu). Why is there extra space appearing on the left side? The styling for ul.menu li:hover ul already sets left: 0, and its closest parent i ...

How come the nth-child selector remains unaffected by other selectors?

Here is an example that I have created for this issue: http://jsfiddle.net/SXEty/ <style> table td, th { padding: 8px; text-align: left; } table td:nth-child(1) { color: red; } table td { color: blue } </style> ... <table> <tr> ...

Trigger the OnAppend event for a jQuery element upon its insertion into the DOM

After writing a code snippet that generates custom controls, I encountered an issue where the custom scrollbar was not being applied because the element had not yet been appended to the DOM. The code returns a jQuery element which is then appended by the c ...

Creating a unique CSS selector to locate the pages tab on Facebook using Selenium

My attempt to direct my chrome driver to the correct CSS locator in order to refine the search only to Facebook pages is proving to be a challenge. When inspecting the element, the HTML code is as follows: https://i.sstatic.net/8XVPs.png I decided to manu ...

Implementing Ajax to display autocomplete suggestions in an HTML table

I've been working on implementing an ajax auto complete function into my HTML code. My goal is to display the results from the auto complete function in a table on the html page. Although the auto complete function is working and I can see the drop do ...

Generating Speech from Text using jQuery API in HTML

Can a website be created to detect textbox text upon longClick by the user, and function across various browsers? The site should also have mobile compatibility. Appreciate any help! ...

What is the reason for the discrepancy in functionality between @use and @include in global SCSS files compared to component SCSS files

Background Information Currently, I am utilizing NextJS v12.1.0, React v17.0.2, and sass v1.49.9. My objective is to develop a theme switcher inspired by the content of this particular article. The structure of my folders looks like this : myProject └ ...

The CSS animation defined in the keyframes for the image fade-in and fade-out

I'm struggling to create a smooth fading effect between two images on my website. Although the images load in the browser, the animation doesn't seem to be working as expected. After reviewing my code, I suspect that the issue lies within the @ ...

Updating the content of a bootstrap alert in real-time by utilizing a form

I am looking to incorporate the following code snippet into my project: <!DOCTYPE html> <html lang="en"> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-wid ...

Using JQuery to send a post request to an iframe and receiving a

Currently, I am utilizing a form on a webpage to send data to an iFrame. This process can occur multiple times based on user requests. You can see an example of this process here: Target an iframe with a HTML Post with jQuery My goal is to implement speci ...

Difficulty loading CSS in PugJS

I can't seem to get my dashboard.pug file to load both dashboard.css and dashboard.js, even though they are all in the same folder. I have already set up express.static correctly for the public folder. const publicpath=path.join(__dirname,'../p ...

Is it possible to sanitize user input without relying on !important?

Utilizing wp_editor, I have implemented a front-end form on my WordPress website for users to post without accessing the admin section. Although it is functional, there is a concern that users may copy and paste content with inline styles that could disrup ...

What is the process for accessing the data attributes of div elements and then transferring them to a cookie?

Although this question may have been answered before, I couldn't find a specific solution. Imagine I have the following div elements... <div class="listings-area"> <div itemtype="http://schema.org/Product" itemscope=""> <a class="lis ...

Struggling to align divs in HTML using CSS

Struggling with aligning my divs in the center of the parent div and keeping them inline. I have a parent "page" containing 6 other divs - "bg_01", "bg_02", "bg_03", "bg_04", "bg_05", "bg_06". They sit inline when the window is small, but when it's re ...

What is the method for attaching a keypress event to an HTML document?

Looking to add an interactive touch to my website by creating a "press any key" page. When a key is pressed, I want it to kick off animations that bring the page to life - like sliding elements in from different directions. Open to using jQuery or plain ...