SVG 100% width - auto height


Topic: SVG 100% width - auto height

Nates pro premium asked 5 years ago

Expected behavior I have many SVGs in a each. I want to ensure each SVG is 100% wide and height cna be auto. How do I achieve this? I tried img-fluid and embed-responsive...

Note, I am and need to import the SVGs as ReactComponent because I have 1000 SVGs and by doing it like this it gets in-lined i..e not singular requests...

Actual behavior It always displays the full width or some other odd behavior having tried a few things.. I have left commented out code to show some things I have tried.

Resources (screenshots, code snippets etc.)

            <Image
                alt={ "Block " + index }

                // viewBox={ "0 0 " + this.state.width.toString() + "300" }
            // preserveAspectRatio="xMinYMin meet"
            // width={ this.state.width - 50 }
            // preserveAspectRatio="xMinYMin slice"
            //  className="embed-responsive"
            // preserveAspectRatio="none"
            //viewBox={ "0 0 50 50" }
            // height={ imageHeight }
            /*
            width={ imageWidth }
            */
            />

Jakub Chmura staff premium answered 5 years ago

Hi @Nates

Maby try this -> ('stackoverflow answer'). I think it could help you with this problem. But if the solution from this thread will not work in your case please tell me I'll try to find better answer for you.

If there is anything else I could do for you do not hesitate to ask me. I'll be happy to help you.

Best Regards,

Kuba


Nates pro premium answered 5 years ago

import { ReactComponent as Image} from '../../book/images/lesson01/L1-01.svg';


Nates pro premium answered 5 years ago

The issue was my SVG never had a viewBox values, so once I used the original SVGs which had (unoptimized), I could set the width on the SVG component and height comes "auto" it seems.

Thanks though


Jakub Chmura staff premium answered 5 years ago

Good to hear that you solved your problem. If there is anything else I could do for you do not hesitate to ask me. I'll be happy to help you.

Best Regards,

Kuba


FREE CONSULTATION

Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.

Status

Closed

Specification of the issue
  • User: Pro
  • Premium support: Yes
  • Technology: MDB React
  • MDB Version: 4.21.1
  • Device: All
  • Browser: All
  • OS: Windows
  • Provided sample code: No
  • Provided link: No
Tags