Tips for filling in path shapes on SVG?

Just a few days back, I was handed an SVG file by some designers and my task is to integrate it with some color variations and the usual hover effects, /edit: but at this point, the most important thing is to have control over the background color /edit/.

Here's a snippet of the HTML:

<div>
  <svg class="drawelements" viewBox="50 200 550 360">
    <polygon fill="red" points="312.200 265.600, 312.200 332.300, 265.600 332.300, 265.600 265.600, 312.200 265.600, 312.700 265.100, 265.100 265.100, 265.100 332.800, 312.700 332.800, 312.700 265.100, 312.700 265.100"></polygon>
    <path class="st0" d="M369 265.6v66.7h-46.6v-66.7H369M369.5 265.1h-47.6v67.7h47.6V265.1L369.5 265.1z"></path>
    <path class="st0" d="M425.7 265.6v66.7h-46.6v-66.7H425.7M426.2 265.1h-47.6v67.7h47.6V265.1L426.2 265.1z"></path>
    <path class="st0" d="M480.6 265.6v66.7H434v-66.7H480.6M481.1 265.1h-47.6v67.7h47.6V265.1L481.1 265.1z"></path>

You can see how it appears in this fiddle: fiddle

I decided to experiment with a polygon element first to eliminate potential issues with path elements.

Despite trying traditional CSS styling methods and inline styles within each element, nothing seems to work. Could it be that I need to use something other than background-color or fill?

If you check out the fiddle, please disregard the viewBox as the actual SVG has many more components which I omitted for simplicity in this post.

Answer №1

It seems that the issue lies in how the SVG shape was constructed rather than in your code. The fill property should technically fill a shape with color, but due to the way the path elements were created, there is an empty space at the center of the shape.

The coordinates defined by the d attribute dictate how the shape is drawn, and in this case, they create more of a frame than a solid board. This results in the background color not appearing as expected when applying a fill.

To better understand the commands used in the d attribute:

  • M369 265.6 - moves the pen to (369,265.6)
  • v66.7 - draws a vertical line downward
  • h-46.6 - draws a horizontal line to the left
  • v-66.7 - draws a vertical line upward
  • H369 - draws a horizontal line

The additional commands in the d attribute create an inner box within the shape, resulting in only the outer border being filled with the specified color.

It appears that the designer's approach was unconventional, and it may be simpler to remove these inner box commands for a more traditional fill effect. After doing so, apply the fill property to achieve the desired result.

For reference, check out this MDN tutorial on SVG paths.


In conclusion, modifying the d attribute of each path element and removing unnecessary coordinates will allow the fill color to be applied correctly. You can also override the default fill color using CSS properties if needed.

Note:

  • If no fill color is specified, the default is black unless set otherwise to transparent or none.
  • CSS-defined fill colors take precedence over those specified in SVG attributes, per specifications.

Answer №2

When working with SVG, you may notice that CSS can sometimes override the path's fill attribute.

To apply CSS to SVG elements, you'll need to target the specific element just like you would when styling HTML. For example, if you want to apply a style to all SVG paths, you could use the following code:

​path {
    fill: blue;
}​

Refreshing Your Css Styles

div {
  fill: red;
}

svg {
  fill: blue;
}

// The following will not work
.st0 {
  fill: red;
}

Snippet Example Provided Below

div {
  fill: red;
}

svg {
  fill: blue;
}
<div>
<svg class="drawelements" viewBox="50 200 550 360">
<polygon fill="red" points="312.200 265.600, 312.200 332.300, 265.600 332.300, 265.600 265.600, 312.200 265.600, 312.700 265.100, 265.100 265.100, 265.100 332.800, 312.700 332.800, 312.700 265.100, 312.700 265.100"></polygon>
<path class="st0" d="M369 265.6v66.7h-46.6v-66.7H369M369.5 265.1h-47.6v67.7h47.6V265.1L369.5 265.1z"></path>
<path class="st0" d="M425.7 265.6v66.7h-46.6v-66.7H425.7M426.2 265.1h-47.6v67.7h47.6V265.1L426.2 265.1z"></path>
<path class="st0" d="M480.6 265.6v66.7H434v-66.7H480.6M481.1 265.1h-47.6v67.7h47.6V265.1L481.1 265.1z"></path>
<path class="st0" d="M537.3 265.6v66.7h-46.6v-66.7H537.3M537.8 265.1h-47.6v67.7h47.6V265.1L537.8 265.1z"></path>
<path class="st0" d="M255.5 265.6v66.7H209v-66.7H255.5M256 265.1h-47.6v67.7H256V265.1L256 265.1z"></path>
<path class="st0" d="M537.3 207.3v49.4H209v-49.4H537.3M537.8 206.8H208.5v50.4h329.3V206.8L537.8 206.8z"></path>
<path class="st0" d="M537.3 341.1v42.3H209v-42.3H537.3M537.8 340.6H208.5v43.3h329.3V340.6L537.8 340.6z"></path>

</svg>
</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

Using AngularJS to link ng-include in an ng-view HTML file

Currently, I am diving into AngularJs while also investigating the creation of a viral movie website for the new x-men film. The site in question is Trask Industries (http://www.trask-industries.com/#/innovation), where the navigation system seems to dynam ...

jQuery Summation: A Step-by-Step Guide

Hello everyone, I am a new member of this forum and I am looking forward to learning and contributing with all of you. I have encountered a problem that I tried to solve on my own but couldn't figure it out. Would anyone be able to lend me a hand? H ...

Eliminating blank spaces below or above images when displayed horizontally

Displayed below is the image depicting my problem: I have discovered a cumbersome method to eliminate this unwanted whitespace by treating it as four separate lists and utilizing a complex for-loop to add elements. However, this approach limits the number ...

The intricacy of the jQuery each function and its interaction with $(this

EXPLANATION: I am working with two elements, each containing a set of options. The first element has options with db-ids as values, acting as parent categories. The second element is filled with options where the parent db-ids are their values and their o ...

Out of the blue, margin-top and marin-left suddenly appeared

Within my HTML file, I have a header containing an unordered list (ul) with list items (li) and anchor (a) elements for the menu. /* ****** */ :root { --primary: #32a852; --white: #fafafa; --black: #000000; --lightgrey: #c7c7c7; - ...

How can I view a comprehensive list of updates made to a webpage?

Is it possible to track and display all the modifications made to a web page? This includes new HTML elements added, old elements removed, and any changes in the location of existing elements. Currently, I am utilizing Selenium WebDriver with Java. Below ...

What are the essential files needed in Kendo UI Core for a mobile app?

Several months ago, I created a trial Kendo mobile app but now I want to verify it using the most recent version of Kendo UI Core. In my previous project, I referenced the following files: <link href="../styles/kendo.common.min.css" rel="stylesheet" / ...

What is the best way to conceal the jqgrid entirely while it is still loading?

Having an issue with my jqgrid. As I load the table and fetch a large amount of data from the database, the loading process naturally takes a few seconds. However, during this time, I can see the column headers without any rows or styles applied to the jqg ...

Activate the jQuery click event by specifying the URL

On my webpage, I have a jQuery click function that loads multiple HTML pages into a specified div. I am looking to set specific URLs that will trigger this event, like test.com#about <script type="text/javascript"> $(document). ...

Is there a way to void a delayed input event?

After reading this How to delay the .keyup() handler until the user stops typing? post, we have grasped how to implement delays. However, is there any suggestion on how to cancel a delayed event? Take a look at this In the scenario given, I want nothing t ...

Creating square elements using only HTML and CSS

This is just a longer text to demonstrate multiple lines being centered. ...

After running JavaScript, retrieve the canvas data using the toDataUrl() method

After working on a Javascript function that creates a canvas and writes an image and text on it, I was expecting to get the base64 dataUrl. However, all I end up with is a blank canvas. Check out the code snippet below: function createBreadCrumb(label) ...

Do not trigger popup if mandatory field is left blank

I am currently working on a contact form where, upon clicking the send button, a popup appears to confirm that the privacy policy has been read. The form does not submit if there are any empty required fields, but the popup still appears. I need to modify ...

Create a scrollbar that allows for both vertical and horizontal movement within the canvas

I am looking to create a canvas with scroll bars. The canvas is sized at 600 x 800, and the parent div is sized at 200 x 200. My goal is to have both horizontal and vertical scroll bars on the canvas. I've tried using the script provided, but unfor ...

Prevent access to website during execution of background tasks

Whenever a user visits the homepage, I need to verify that default values on the database are in place (if this is not the correct location for the check, please advise). Here's what needs to happen: Determine if the database is empty (if not, skip ...

Executing code upon the completion of jquery's slideUp() function

Is there a method to trigger the execution of html() only after the completion of slideUp()? <p class="test">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tortor elit</p> $(".test").slideUp().html(""); I attempted using ...

"Adjusting the background color based on the current day using PHP and CSS

Check out the screenshot of my result here. I'm currently experiencing an issue with the code while trying to modify a calendar. The error seems to be originating from tablerow.php, as I've indicated in the comments. I am attempting to change the ...

Struggling to get my DIV container perfectly centered

I am having trouble centering my Div container with the images. I attempted using margin 0 auto; and adding a specified width, but it's not working as expected. body{ margin: 0px; } .main{ height: 950px; background: url(../Images/Mountain.jpg) ce ...

Bootstrap Dropdown Functionality Malfunctioning

Here is a simple piece of HTML code that I have created: <!doctype html> <html> <head> <meta charset="utf-8"> <title>.:Home Page:. The Indian Sentinel</title> <link rel="stylesheet" href=" ...

Easily switch between visible and hidden content by clicking on an image that functions as a swap or toggle

Hey there, I'm new to this and could really use your assistance, I'm looking for a simple show/hide toggle feature when clicking on an image, where it switches between a 'side arrow' and a 'down arrow' (similar to a dropdown) ...