Webpack loads SVG with white border

I have an SVG that I load through a file loader pointing to a path in my .scss file and for some odd reason got a white border. If I set the css border it is set outside of white.

I checked the file to make sure it doesn't have a white background. This was confirmed.

menubar.js

import React, { Component } from 'react';

class MenuBar extends Component {
    render() {
        return (
            <div className="menu-bar">
                <img className="logo" />

                <div className="nav">
                    <a className="selected" href="#">Sky</a>
                    <a href="#">HQ</a>
                    <a href="#">Settings</a>
                </div>
            </div>
        );
    }
}

export default MenuBar;

      

menubar.scss

:

.menu-bar .logo {
    position: relative;
    background: url('../images/glimpse-menu-bar.svg') center center no-repeat;
    top: 50%;
    transform: translateY(-50%);
    padding-left: 20px;
    cursor: pointer;
}

      

My webpack.dev.config.js

:

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

// Config directories
const SRC_DIR = path.resolve(__dirname, 'src');
const ASSETS_DIR = path.resolve(__dirname, 'assets');
const OUTPUT_DIR = path.resolve(__dirname, 'dist');

// Any directories you will be adding code/files into, need to be added to this array so webpack will pick them up
const defaultInclude = [SRC_DIR];

module.exports = {
    entry: ['babel-polyfill', SRC_DIR + '/index.js'],

    output: {
        path: OUTPUT_DIR,
        publicPath: '/',
        filename: 'bundle.js',
    },

    module: {
        rules: [
            { 
                test: /\.css$/, 
                loader: 'style-loader!css-loader',
            },
            { 
                test: /\.scss$/, 
                loader: 'style-loader!css-loader!sass-loader',
            },
            {
                test: /\.jsx?$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['react', 'es2015', 'stage-3'],
                },
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                loader: "file-loader",
            },
            { 
                test: /.(ttf|eot|otf)(\?v=[0-9].[0-9].[0-9])?$/, 
                loader: "file-loader",
            },       
        ],
    },
    target: 'electron-renderer',
    plugins: [
        new HtmlWebpackPlugin(),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
        }),
    ],
    devtool: 'cheap-source-map',
    devServer: {
        contentBase: OUTPUT_DIR,
        stats: {
            colors: true,
            chunks: false,
            children: false,
        },
    },
};

      

Any ideas here?

enter image description here

Another example:

enter image description here

Here's Code 1 from SVG. Every SVG in my project shows a border

<svg width="53" height="54" viewBox="0 0 53 54" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>&#224;&#213;&#172;</title>
<desc>Created using Figma</desc>
<g id="Canvas" transform="translate(-6960 -4611)">
<g id="Ellipse 2">
<mask id="mask0_outline_ins">
<use xlink:href="#path0_fill" fill="white" transform="translate(6960 4611)"/>
</mask>
<g mask="url(#mask0_outline_ins)">
<use xlink:href="#path1_stroke_2x" transform="translate(6960 4611)" fill="#FFFFFF"/>
</g>
</g>
</g>
<defs>
<path id="path0_fill" d="M 53 27C 53 41.9117 41.1355 54 26.5 54C 11.8645 54 0 41.9117 0 27C 0 12.0883 11.8645 0 26.5 0C 41.1355 0 53 12.0883 53 27Z"/>
<path id="path1_stroke_2x" d="M 50 27C 50 40.3079 39.4261 51 26.5 51L 26.5 57C 42.845 57 56 43.5154 56 27L 50 27ZM 26.5 51C 13.5739 51 3 40.3079 3 27L -3 27C -3 43.5154 10.155 57 26.5 57L 26.5 51ZM 3 27C 3 13.6921 13.5739 3 26.5 3L 26.5 -3C 10.155 -3 -3 10.4846 -3 27L 3 27ZM 26.5 3C 39.4261 3 50 13.6921 50 27L 56 27C 56 10.4846 42.845 -3 26.5 -3L 26.5 3Z"/>
</defs>
</svg>

      

+3


source to share


2 answers


You use the tag <img>

without giving it the actual source, relying instead on the background image. The browser displays a placeholder for your image since you didn't give it an actual render resource, which is where the weird border appears.

Now managed to highlight it http://jsbin.com/difopib/



You must either set an attribute src

on this image or use a different tag. If for some reason you are dead on the <img>

+ background svg, you can set the source to 1px transparent gif, see https://css-tricks.com/snippets/html/base64-encode-of-1x1px-transparent-gif/

+5


source


Your img tag has no origin. Add a source (maybe a transparent png) and the border disappears.
You can also set an empty source:



 <img src=""/>

      

+2


source







All Articles