score:67
In CSS, something like:
path {
fill: none;
stroke: #646464;
stroke-width: 1px;
stroke-dasharray: 2,2;
stroke-linejoin: round;
}
score:13
For the question: How to add border/outline/stroke to SVG elements in webpages with CSS ?
You can do in CSS:
path { outline: 3px solid green; }
Please note as of 2018 it is supported by chrome and safari, but might not be available across all modern browsers. See example below:
I applied outline via CSS to a <g>
group with paths inside. In static it looks good. In dynamic (dragging) I can see periodically these artifacts (to the left)
UPDATE:
- if outline is "solid" - there are no artifacts
- safari mobile doesn't support outline on
<g>
elements...
score:22
Try adding CSS filter() dropshadow (this can be applied on any svg: <img>
, background-image
, in the dom, etc.)
or
Of course, Internet Explorer supports neither of these features.
svg {
fill: #fff;
}
#example_1 {
filter:
drop-shadow(-1px -1px 0px #3e68ff)
drop-shadow(2px -1px 0px #3e68ff)
drop-shadow(2px 2px 0px #3e68ff)
drop-shadow(-1px 2px 0px #3e68ff)
}
<svg id="example_1" width="100" height="100" viewBox="0 0 288 288">
<path class="st0" d="M144.17,77.84c-37.65,0-68.18,30.52-68.18,68.18c0,37.65,30.52,68.18,68.18,68.18
c37.65,0,68.18-30.52,68.18-68.18C212.35,108.37,181.83,77.84,144.17,77.84z M166.37,117.02c10.77,0.03,15.31,15.39,15.58,20.03
c0.15,2.47-2.95,3.15-4.21,1.21c-0.89-1.37-1.44-2.48-2.56-4.65c-1.51-2.93-4.99-6.08-8.71-6.15c-3.72-0.06-7.31,2.95-8.9,5.9
c-1.23,2.28-1.86,3.4-2.66,4.89c-0.68,1.26-4.16,1.16-4.21-1.26C150.6,132.3,155.61,116.99,166.37,117.02z M121.71,117.02
c10.81-0.05,15.63,15.43,15.73,20.03c0.05,2.33-3.05,3.44-4.4,1.11c-0.82-1.41-1.39-2.45-2.47-4.55c-1.55-3.03-5.16-6.06-8.85-6.05
c-3.7,0.01-7.18,3.08-8.76,5.9c-1.24,2.22-2.18,3.19-2.81,4.74c-0.92,2.27-3.92,1.24-3.97-1.26
C106.09,132.32,111,117.06,121.71,117.02z M184.23,169.45c-1.91,8.19-18.66,26.11-40.26,26.03c-21.44-0.07-38.37-17.77-39.87-26.03
c-0.58-3.19,2.81-5.81,5.61-4.84c11.86,4.09,18.31,4.74,34.29,4.74c15.98,0,22.02-1.48,34.32-4.84
C181.52,163.65,184.9,166.55,184.23,169.45z"></path>
</svg>
<svg id="example_2" width="100" height="100" viewBox="0 0 288 288">
<defs>
<filter id="shadow">
<feDropShadow dx="-2" dy="-2" stdDeviation="0" flood-color="#3e68ff"></feDropShadow>
<feDropShadow dx="2" dy="-2" stdDeviation="0" flood-color="#3e68ff"></feDropShadow>
<feDropShadow dx="2" dy="2" stdDeviation="0" flood-color="#3e68ff"></feDropShadow>
<feDropShadow dx="-2" dy="2" stdDeviation="0" flood-color="#3e68ff"></feDropShadow>
</filter>
</defs>
<g filter="url(#shadow)">
<path d="M144.17,77.84c-37.65,0-68.18,30.52-68.18,68.18c0,37.65,30.52,68.18,68.18,68.18
c37.65,0,68.18-30.52,68.18-68.18C212.35,108.37,181.83,77.84,144.17,77.84z M166.37,117.02c10.77,0.03,15.31,15.39,15.58,20.03
c0.15,2.47-2.95,3.15-4.21,1.21c-0.89-1.37-1.44-2.48-2.56-4.65c-1.51-2.93-4.99-6.08-8.71-6.15c-3.72-0.06-7.31,2.95-8.9,5.9
c-1.23,2.28-1.86,3.4-2.66,4.89c-0.68,1.26-4.16,1.16-4.21-1.26C150.6,132.3,155.61,116.99,166.37,117.02z M121.71,117.02
c10.81-0.05,15.63,15.43,15.73,20.03c0.05,2.33-3.05,3.44-4.4,1.11c-0.82-1.41-1.39-2.45-2.47-4.55c-1.55-3.03-5.16-6.06-8.85-6.05
c-3.7,0.01-7.18,3.08-8.76,5.9c-1.24,2.22-2.18,3.19-2.81,4.74c-0.92,2.27-3.92,1.24-3.97-1.26
C106.09,132.32,111,117.06,121.71,117.02z M184.23,169.45c-1.91,8.19-18.66,26.11-40.26,26.03c-21.44-0.07-38.37-17.77-39.87-26.03
c-0.58-3.19,2.81-5.81,5.61-4.84c11.86,4.09,18.31,4.74,34.29,4.74c15.98,0,22.02-1.48,34.32-4.84
C181.52,163.65,184.9,166.55,184.23,169.45z"></path>
</g>
</svg>
Or, create some javascript function that clones each of the elements within the svg, removing fill/stroke attributes, and wrapping them in a g
tag that has fill="none"
and stroke attributes set. Then prepend that to the svg.
Source: stackoverflow.com
Related Query
- How to add border/outline/stroke to SVG elements in CSS?
- How to add links from CSV file to SVG elements generated with D3?
- svg how to add a border on a svg symbol?
- How to add CSS on SVG file in io.js ?
- how to add and remove css class on svg element
- How can I add text to SVG elements in D3 using the example below
- How to add two elements (circle and text) to SVG g element with D3js?
- How do I add an SVG line over a css style
- How do I add an SVG line over a css style
- How to add a tooltip to an svg graphic?
- How to add an image to an svg container using D3.js
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- CSS padding property for svg elements
- How to prevent a SVG marker (arrow head) to inherit path's stroke width?
- How to update the fill color on existing svg elements with d3.js?
- How to determine nearby SVG elements on a mouse event?
- How do I associate SVG elements generated by graphviz to elements in the DOT source code
- How to get reference to SVG (child) elements nested in another SVG element (parent) using D3?
- d3: How to add border to a circle on an if-condition?
- How to add filled sections to SVG circles using d3.js
- How to move elements along with svg group
- How to add external svg file (by D3.js) to Leaflet map
- How to create SVG elements of different types based on data?
- D3.js how to transition in opposite direction with basic SVG elements
- how to add dragend event on svg element
- How do I scroll d3 elements while keeping a portion of the svg in a fixed position?
- How to add a border to an image on hover in D3
- How to use D3 force layout with existing SVG elements as nodes
- How do you translate HTML elements along with SVG elements when zooming and panning with D3
- How can I add an external group to an existing svg with d3.js?
More Query from same tag
- Transition on axis with appended text
- Looking for selectAll-like operation that will include calling object in the selection (whenever it matches the selector)
- Draw vertical line after n data points in d3
- DC.js stack in line chart not showing
- Performant ways of loading a huge image into the background of a D3 map
- D3 heatmap color range updating with slider
- How can you access data that's nested like this in a csv file?
- Filter d3 js objects based on form input
- Two way data binding in C3.js in AngularJS
- Can nice() be used to round day in d3 date
- Convert JSON tree data to nodes and links
- d3 change attribute of all elements
- Error: <path> attribute d: Expected arc flag ('0' or '1')
- Incrementing month by month in a time slider with D3.js
- StopPropagation() with SVG element and G
- require("d3") sometimes returns 3 instead of object
- How to make multiple charts with d3.chart using nested data
- replacing null data d3js with focus context chart line
- D3.js: Stop transitions interrupting on mouseover?
- Masonry algorithm for d3.js
- d3v4 line generator returns NaN
- How to wait on d3.json to finish?
- Labels on a d3 line chart
- Self invoking anonymous functions
- A more elegant way to bind new elements using data bound to multiple group elements
- .attr not defined d3js
- D3js & CSS selectors
- Controlling transparency of D3 orthographic projections
- How to get the index of the data element in a histogram on mouseover?
- D3 parse number from string