Create an inline svg using only css, avoiding the need to define it within the html file

I have an HTML file that utilizes inline SVG. This allows me to assign classes to my SVG elements and style them using CSS. The code snippet below demonstrates how it is currently set up:

<div>
    <svg class="icon" viewbox="0 0 512 512">
        <path d=" ... "/>
    </svg>
 </div>

However, the <path> tag can become quite lengthy if the SVG image is complex. I am using this SVG in three different locations, which means I need to repeatedly copy and paste the entire path every time. It would be more efficient if I could define the path only once, preferably within a CSS class like so:

<div>
    <svg class="icon" viewbox="0 0 512 512">
        <path class="compleximage"/>
    </svg>
 </div>

 .compleximage
 {
      d: ... ;
 }

Unfortunately, this method does not seem to work as intended. It's possible that I may be misunderstanding the syntax or that this approach simply isn't feasible. If that is the case, are there any alternative ways to avoid repetitive copying and pasting of the SVG code in my HTML files? I aim to adhere to the "0, 1, or infinite" design principle, and duplicating code three times goes against that concept.

Answer №1

If you want to display the path in multiple locations, you can utilize the use tag. This involves assigning an id attribute to the path and referencing it using xlink:href within the <use> element.

For example:

<defs>
    <path id="image1" d="..." />
</defs>
<use x="20" y="10" xlink:href="#image1" />
<use x="50" y="50" xlink:href="#image1" />

and so on.

Answer №2

If you're using HTML5, there's a way to accomplish this task.

Give this a shot:

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>

Alternatively, you can design your own SVG and retrieve it using <img src="" />. Check out the example below:

<img class="mama" src="http://s.cdpn.io/3/kiwi.svg">

For further information, refer to:

(1) http://css-tricks.com/using-svg/

Another useful resource explaining how to customize it with CSS:

(2) http://codepen.io/chriscoyier/pen/evcBu

Answer №3

SVG attributes cannot be directly controlled with CSS. Only styles like stroke and fill can be manipulated. To dynamically adjust these attributes, JavaScript must be utilized. I suggest incorporating jQuery for this task.

For guidance on utilizing jQuery to edit SVG files, refer to the following helpful link: Modify a svg file using jQuery

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

Tips for displaying and hiding content in Angular2

I have a component that toggles the visibility of elements by clicking a button. This is the snippet of my HTML code: <div *ngFor="let history of histories | sortdate: '-dateModified'"> <p><b>{{ history.remarks }}</b& ...

The functionality of CSS columns is not supported in Firefox browser

Currently, I am working on a web page design inspired by Pinterest. However, I have encountered an issue where the columns property is not functioning properly in Firefox. You can find the example I am trying to replicate here. Feel free to test it in Fir ...

What is the best way to set up a side-opening feature in the UI design?

I am having trouble with the Amoclan shape transitioning to the next side. I need the transition to occur exactly as specified in the documentation, but for some reason it is still moving on to the next side. <html> <head> <script src=" ...

"Is there a way to extract text enclosed within HTML tags, such as <div>text</div>, with Selenium using C#

How can I extract the text 'True' from within the HTML tags <div>text</div> using Selenium and C#? <div type='data' id='OfferInCart' style='display:none;'>True</div> I have attempted to retr ...

Setting the width to auto, height to 100, and using overflow hidden does not produce the desired result

In my design, I have two sets of images displayed below: https://i.sstatic.net/pQytw.jpg Both sets of images have the exact same code. The only difference between the images in the first and second set is their dimensions. I aim to make all image blocks ...

I am facing an issue where my Popover functions correctly when elements are added using HTML, but not when they are dynamically created

I'm currently working on a method to incorporate multiple popovers that have a similar appearance to the following: The screenshot of my first JsFiddle project (click to view) The initial progress I've made can be seen in my first JsFiddle, acc ...

Customize the text that appears when there are no options available in an Autocomplete component using React

Recently, I came across this Autocomplete example from MaterialUI that caught my attention. https://codesandbox.io/s/81qc1 One thing that got me thinking was how to show a "No options found" message when there are no search results. ...

Steps for using selenium to select a radio button

Looking to leverage Selenium in Python 3.9 for button clicking automation. Link to the website https://i.stack.imgur.com/KWsKG.png The goal is to click on "Effective Date", then select "July 2021", and finally click "OK". Initially, the first two steps ...

Padding-left may cause text to not wrap correctly

Every link in the left menu has a padding-left: 15px;. This is done to accommodate a background image (the blue arrow). However, when text wraps (like in "Paintings, prints, and watercolours"), it seems to ignore the padding. I've searched extensive ...

Building interactive web pages with PHP and jQuery by incorporating dynamic classes

I'm looking to create a unique PHP snippet, possibly in the form of a WordPress code snippet, to generate a dynamic CSS class every time a module is loaded on the front end. This dynamically generated class will then be used by jQuery to style the fro ...

When dealing with errors in multiline textarea content, an error may occur with the error handling

Currently, I have created an HTML/PHP form with a 'textarea' element. As I work on implementing error handling, I want to ensure that if a mistake is made in the form, the content entered by the user is retained. Specifically, when a user makes ...

Guide to using Ajax to send a form and receive text in response

Check out my code on Fiddle: $("form.signupform").submit(function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr("action"); var form = $(this); // Added this line for reference $.post(url, data, function(data) { $(for ...

Importing styles from an external URL using Angular-cli

How can I load CSS styles from an external URL? For instance, my domain is domain.eu but my site is located at sub.domain.eu. I want to use styles that are stored on the main domain (common for all sites). The example below does not work: "styles&qu ...

Make sure the top edge of your Bootstrap 4 dropdown menu is perfectly aligned with the bottom edge of your

Trying to make a Bootstrap 4 navbar dropdown display right below the navigation bar, but it consistently shows slightly above the bottom edge of the navbar. Is there a way to make this happen without fixing the height of the navbar and using margin-top fo ...

Achieve seamless alignment of radio group labels alongside radio buttons through the use of only CSS

After using a table to display radio buttons with labels in the past, I am now attempting to achieve the same result using only CSS. Is there a CSS technique that will allow me to position the label text neatly on top of the corresponding radio button? He ...

Altering Image Order Across Various Slides

I have customized a parallax website template that is divided into various sections and slides. I want to incorporate a fixed image sequence on each slide that animates based on the scroll position. With 91 images in the animation sequence, it moves quickl ...

What is the best way to implement CSS properties dynamically in real-time?

Hey there, I’m working with some HTML code here <div id="div1"></div> I’m dynamically loading content onto this div and adjusting its width in the "success" function based on the contents Here’s the jQuery code snippet I’m using Th ...

Python Automation with Selenium (Locating elements using CSS selectors)

Is it possible to select and access the first element of an array using a CSS selector in Python? For instance: css=('.od-FieldEditor-fieldTitle.ms-Label.is-required') generates an array of 6 labels. I am interested in accessing the innerText a ...

Tips for utilizing CSS flexbox to create a row of squares that can scale while maintaining their aspect ratios

Struggling with this issue has me on the verge of pulling my hair out. It seems like a simple task, yet I can't seem to achieve perfection. My goal is to create a row of squares that maintain their aspect ratio but scale down in size as their containe ...

What is the reason for the columns being divided on the xs-viewport in Bootstrap?

Attempting to grasp the concepts of Bootstrap has left me puzzled, as I cannot comprehend why the col-xs-11 is not aligning correctly with the col-xs-1. Despite having a total of 12 columns, the layout seems off. CSS: [class^="col-"] { height: 20px; ...