Is there a way to apply styles to specific parts of an SVG without including the entire SVG code in the HTML

Presented here is an SVG with the following specifications

https://i.sstatic.net/vn8K8.jpg

<svg width="589" height="911" xmlns="http://www.w3.org/2000/svg">
   <path class="move-line" d="m459,........... />
   <path d="m277,239c0 ......./>
</svg>

In this SVG, note that class="move-line" has been added to a specific path

I proceeded by copying and pasting the entire tags above into my HTML file

Subsequently adding CSS styling

.move-line {
  stroke-dasharray: 10 10; }

The desired output was achieved as displayed below

https://i.sstatic.net/GhiyI.jpg

However, an issue arose due to the size of the complete SVG tags (resulting from the path),

Including it directly in my HTML file led to excessively long code.

Is there a workaround for this?

<img src="xxx.svg" >

To mimic inserting the entire svg tag within html??

I attempted

<object data='xxx.svg'></object>

and

<svg width=".." height=".." xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
                        <image xlink:href="method-draw-image (1).svg">
                    </svg>

Both displayed the original svg image

however, my CSS styles were ineffective

Your assistance in resolving this matter would be highly appreciated, thank you!

Answer №1

In my opinion, one way to incorporate external styles into an SVG image is by utilizing jQuery to insert the

<object data="xxx.svg"></object>
tag and then dynamically adding styles by assigning classes through jQuery. If this method proves ineffective, the alternative would be to embed the entire code of the SVG image directly into an HTML page. Unfortunately, there are no other known techniques that allow for seamless application of CSS styles with SVG elements.

Answer №2

My opinion is that CSS requires the use of a special tag to paste in an inner SVG element.

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

Is your Javascript navigation functioning properly on some submenus while encountering issues on others?

If you visit , you'll notice that it's a visually appealing site. The navigation submenus seem to be functioning properly, HOWEVER upon inspecting the element, you'll notice that under the PRICING tab, there are submenus that have the same c ...

When running the Grunt build process, the styles are not combined within the build:css(.tmp) directory

When setting up my front-end with Yeoman Angular generator and using Grunt, I noticed that only the first CSS file and the Sass files generated in main.css were included in my build. Here is a snippet from my index.html: <!-- build:css(.tmp) styles/mai ...

Implementing Sass mixin transition to specifically add transition-delay - a comprehensive guide

As I continue to enhance my front-end development skills and practice Sass to optimize my CSS code, I encountered a roadblock. After exploring resources and tutorials online, I created a global mixin in Sass named 'transition'. Here is the code: ...

Obtain a postal code based on latitude and longitude coordinates

I possess latitude and longitude coordinates and I am interested in sending them through an HTTP request to a service such as Google Maps in order to obtain a postcode for the United Kingdom. Can this be achieved? ...

Customizing the Appearance of HtmlEditorExtender

Having an ajax HtmlEditorExtender featured on my webpage has been quite a challenge. I followed the instructions provided here: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/HTMLEditorExtender/HTMLEditorExtender.aspx The issue arises from t ...

Angular: Generating components dynamically in HTML is unsuccessful

I possess the capability to dynamically generate components: import { Component, ComponentFactory, ComponentFactoryResolver, ComponentRef, OnInit, ViewChild, ViewContainerRef } from '@angular/core'; import { FilterComponent } from '../filter ...

failure in data transmission causing upload issues

Is there a way for me to prevent the character > from causing issues? I have a combo box with an option listed below. I attempted to replace > with &gt;, but it still isn't uploading correctly. <select name="Category3" id="Category3"> ...

The issue arises when applying Tailwind styling to template variables that have widget attributes in Django

Currently, I am attempting to implement daisyUI styling in my django web application project, however, I am facing some confusion regarding the styling. The specific issue arises when I try to apply daisyUI styling to a dynamically generated input text fie ...

Methods for switching the visibility of table rows

In my XSLT (html) xml style sheets, I am currently toggling the visibility of some table rows. Initially, I hide 3 out of the 6 rows, and then upon clicking, I want to hide 1 row and reveal 3 others. Here is the initial setup: on load xml first row - T ...

The HTML/CSS Progress Bar appears on the browser but does not appear when attempting to print the document

I am currently assisting a friend with creating a resume and we wanted to include a visually appealing progress bar for the skills section. After successfully implementing the progress bar and seeing it display correctly in the browser, we encountered an ...

flash animation on an HTML webpage (fancy pop-up)

Hey, I'm interested in creating a similar effect to this: Check out this example >> Simply click on "Start PicLens Lite Slideshow PicLens". Did you notice how the swf/flash loads on top of the regular page? I want to achieve that :-D If there ...

Is there a way to have a span update its color upon clicking a link?

I have 4 Spans and I'm looking to create an interactive feature where clicking a link in a span changes the color of that span. Additionally, when another link in a different span is clicked, the color of that span changes while reverting the previous ...

What's the best way to position a child fixed DIV in relation to a parent fixed div?

Here is the HTML snippet I am working with: <div class="parent"> <div class="child"></div> </div> The corresponding CSS styles are as follows: .parent { position: fixed; } .child { position: fixed; left: ...

What could be causing this table to exceed its dimensions by 2 pixels?

Why is the height of this table 102px instead of 100px? Is there another CSS attribute that needs to be set besides: table { border-spacing:0; border-collapse:collapse; } For example, check out this example link. ...

Picture not adapting correctly to various screen sizes

I'm looking to adjust the size of an image based on the user's browser window. Here is the code I have so far: .image { max-height: 15%; width: auto; } The image class is used in this section of my code: <ul> <li> <img c ...

A guide to filling an irregular shape (such as a star) based on a percentage using CSS in a React project

I am currently working on developing a React component that showcases a rating percentage using a star icon with two different shades. For example, if the input rating is 79%, I want to display a star with 79% of it in gold color and the remaining 21% in ...

Creating a variable within an Angular template: A step-by-step guide

I'm working with the following template: <div> <span>{{aVariable}}</span> </div> and I want to transform it into: <div "let a = aVariable"> <span>{{a}}</span> </div> Is there a way to ach ...

The presentation of the solution alters upon completion of a calculation

If you want to view my jsfiddle, click here The issue that arises when checking the demo on jsfiddle is related to entering a number in one of the text inputs (question 1). Although it performs the calculation correctly, the displayed answer in the column ...

What is the best way to display a particular section of a website (<td>...</td>) within a UIWebView?

I have searched high and low for a solution to my issue, but have come up empty-handed. What I am attempting to accomplish is to only load a specific td from a webpage into a UIWebview. Currently, my code looks like this: - (void)viewDidLoad { [supe ...

The word-wrap property does not function properly in the <small> element or any other HTML elements

My code has a small issue with the word-wrap property not working as expected. When I change it to a div element, it works fine but I need to use the small or another specified element. Does anyone have any ideas why the word is not breaking and what could ...