Why is my SVG being trimmed instead of resized properly?

Behold my amazing SVG tag:

<svg 
baseprofile="tiny" 
fill="#ececec" 
height="857" 
stroke="black" 
stroke-linecap="round" 
stroke-linejoin="round" 
stroke-width=".2" 
version="1.2" 
viewbox="0 0 2000 857" 
width="2000" 
xmlns="http://www.w3.org/2000/svg">

The issue arises when I attempt to adjust the width of this element using CSS, as it results in cropping rather than scaling. Even changing the height and width to 100% did not yield the desired outcome.

Previously working flawlessly in standard HTML and CSS, this component faced challenges after being ported into a React environment. Despite hours of troubleshooting, only 'scale()' in CSS showed some effectiveness, albeit failing to dynamically scale with the window size.

For reference, here is the corresponding React code snippet:

import React from 'react'

function map() {
    return (
        <div>
          <SVG>...</SVG>
        </div>
      )
}

export default map

Additionally, this tag is utilized within the following context:

function book() {
    return (
        <body>
            <div id="map-container">
                <Map/>
            </div>
        </body>
    )
}

Answer №1

To truly grasp the situation at hand, one must focus on the viewbox.

Your viewbox was accurate, but in HTML, you need to utilize view-box instead of viewbox as demonstrated in your code example.

When working with react.js, remember to format it like this: viewBox because within react.js, you are required to switch from kebab-case to camelCase for markup variables.

To see it resolved, visit this codesandbox link:

https://codesandbox.io/s/suspicious-dhawan-pwmmq?file=/src/App.js

You have the flexibility to adjust both width and height to any value, and the SVG will remain responsive!

For more details, refer to: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox

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

MUI Typography - Text turns white when hovered over

I've created a customized component that turns green on hover. Here's the code for it: const ProjectAdd= styled.div` background-color: white; list-style-type: none; list-style: none; height: 100%; width: 10%; height: 100%; border-ra ...

What kind of Typescript type should be assigned to setState when passed to the component?

In my setup, I have a variety of Parent components: const ParentOne = () => { const [error, setError] = useState<{ one: boolean }>({ one: false }); ...omit return ( <> <Child setErr={setError} name={"one"} /> </> ...

Updating state using the react `setState()` function will automatically trigger a re-render

I am currently working on a large form using React and material-ui. The form implements two-way binding to update the state when input changes occur. Interestingly, changing any input field triggers updates in all components (as observed through TraceRea ...

Insert JavaScript code into the head of the document (including the complete script code)

I am looking to insert a script in the head section of a website so that the target HTML appears as follows: <head><script type="text/javascript">*some code...*</script></head>. This particular script works perfectly: var head = d ...

Insert a triangle shape at the bottom of the material design tab with the class mat-ink-bar

I'm trying to update the appearance of the mat-ink-bar in the Angular material tab. My goal is to achieve a design similar to this: Jsfiddle example The issue I'm facing is that the mat-ink-bar is already positioned as absolute, making it diffi ...

Making a page jump with Javascript

Welcome! Let's dive into our question. Below you'll find my script and HTML code: My Script: const resultEl = document.querySelector('.allquotes'); const pageSize = document.querySelector('select[name="page-size"]') ...

Retrieving the most recent state data in React by utilizing the useEffect hook in conjunction with a route protection

Struggling with obtaining the accurate state within the history.block callback. It seems like the history.block triggers prematurely, before the useEffect can update it to reflect the current state. Is there a recommended method for setting up a route guar ...

Creating a single row with five columns using HTML and CSS

I'm currently working on setting up a layout consisting of 1 row and 5 columns placed side by side. These columns are going to showcase the projects that I intend to feature on my online Portfolio. To achieve this layout, I am utilizing the Bootstrap ...

The Drop-down menu with carousel in Bootstrap is experiencing difficulties displaying its sub-menu items

I am currently facing an issue where the sub-menu items are hidden behind the content in a bootstrap carousel. Despite everything else working fine, this particular problem is causing a headache. JSFiddle: https://jsfiddle.net/Saneesh/pwmyvsw6/65/ Is th ...

Display the username on a Meteor React component

I'm facing an issue with one of my React components, which serves as the main layout for my application. It includes a navigation bar that displays the username of the currently logged-in user. The problem arises when Meteor.user() returns undefined d ...

Is it possible to incorporate a similar box on a webpage?

I am trying to find a way to add a similar box to a webpage like the one shown in this image. The design I am referring to is from Reddit's theme, but I would like to know how to create a box with a different color and then have another box next to i ...

Why does the "error loading page" message appear on the server when there is a PHP function inside jQuery Mobile?

I am facing an issue with a function I created in my jQuery Mobile code. It works perfectly fine on my local machine, but when I host it on the server, it does not work properly. Can someone please assist me? Here is a glimpse of the code: <!DOCTYPE ht ...

Retrieve all colors from the style attribute

I'm on the hunt for a method to extract all CSS colors from a website. Currently, I have been able to manage internal and external stylesheets by utilizing document.styleSheets. However, my issue lies in the fact that any styles directly assigned to a ...

Setting the state for an array of objects in React: a comprehensive guide

Suppose I have an array of objects representing people and their powers like so: const [peoples, setPeople] = useState[ {name: 'Sam', power: 0}, {name: 'Tam', power: 0}, {name: 'Lam', power: 0}, {name: ...

Angular Material Dropdown with Multi-Column Support

I'm working on customizing the selection panel layout to display items in multiple columns. However, I'm facing an issue where the last item in each column appears off-center and split, causing overflow into the next column instead of a vertical ...

Setting up Webpack and Babel for ReactJS development

Recently, I started delving into the world of ReactJS and stumbled upon a tool called webpack which acts as a module bundler. However, I've hit a roadblock while configuring it and keep encountering the following error message: ERROR in ./src/index. ...

What is the best way to create a dynamic menu icon using CSS?

I am currently developing a CSS animation that is inspired by this. I have made progress and almost reached my goal, but I am facing some difficulties in getting it to work perfectly. For those unable or unwilling to visit the links: I aim to create an ani ...

What is the best way to modify the attributes of an object within the state?

Although I've been working with the React framework for a few weeks now, I still find myself surprised by new challenges on a regular basis. Currently, I am focused on developing the login/game lobby/server lobby for a group project game. The concept ...

Load and Activate Element

I am utilizing Bootstrap 4 features and some custom settings in my elements. <div class="btn-group btn-group-toggle" data-toggle="buttons"> <label class="btn btn-outline-selector make-active"> <input ...

What could be the reason for encountering a Typescript ts(2345) error while trying to pass a mocked constant to .mockResolvedValue()?

Within my test.tsx file, I have the following code snippet: test('Photos will load', async () => { const mockCuratedPhotos = jest.spyOn(endpoints, 'getCuratedPhotos'); mockCuratedPhotos.mockResolvedValue(mockPhotos); awa ...