Tweaking react-lottie and SVG dimensions with CSS media queries: A step-by-step guide

I am currently working on a React project that involves displaying Lottie-react animations and SVG's. My main concern is making sure that these illustrations are responsive on my website.

The components are stored in a .js file, which I import into my project as shown below:

import React from 'react'
import MobileAnimation from '../../images/illustrations/mobileanimation.js';
import Icon from '../../images/illustrations/Icon.js';

const Section = () => {
return (
  <section className="section-four">
    <MobileAnimation fill="#49c" width={100} name="phone" className="pagefouranimation" />

     // No effect to size
     <Icon fill="#49c" name="phone" width="800" height="800"/>
  </section>

)
}

export default Section

I have tried giving the 'MobileAnimation' a className and changing the width and height like this:

.pagefouranimation {
padding: 0;
width: 100px;
height: 50px;
margin-left: -124px;
}

.illustration-comp {

width: 200px;
height: 200px;

 }

SVG

  import React from "react";
  import styled from "styled-components";

  export default class Icon extends React.Component {
  render() {
  return (
  <svg className="illustration-comp" class="illustration" viewBox="0 0 1155 700" fill="none" xmlns="http://www.w3.org/2000/svg">
  
  // SVG paths and shapes go here...

  </svg>  
  }
  }

However, directly modifying the size in the .js file doesn't seem to work. How can I make the Lottie illustrations and animations responsive across different devices? Currently, I'm looking for alternative solutions to address this issue.

Answer №1

In my view, there are 2 options for you

Within your component

<Icon fill="#49c" name="phone" width="800" height='800'/> 

The first option is to include the following code in your Icon.js file

export default class Icon extends React.Component {
    render() {
        return (
            <svg className='illustration-comp'  viewBox="0 0 1155 700" >
                <rect x="359.562" y="82" width="254.804" height="537.533" rx="24.4333" fill="#F6F0F9"/>
                // More SVG elements here
            </svg>
        )
    }
}

Alternatively, the second option is to apply inline styles like this

<svg style={{width:this.props.width,height:this.props.height}} viewBox="0 0 1155 700" >

Answer №2

If you adjust the width attribute of the svg, it will work efficiently.

  • You can utilize CSS media queries to achieve this:

@media only screen and (max-width: 600px) {
.illustration-comp {width: 150px(for example);}
}

  • Alternatively, customize the size as needed by modifying the width attribute using React state and props:

<svg width={{this.props.width}} viewBox="0 0 1155 700" >

To delve deeper into media queries, refer to this article css-media-query-breakpoints

.illustration{ width:150px; }
<svg className="illustration-comp" class="illustration" viewBox="0 0 1155 700" fill="none" xmlns="http://www.w3.org/2000/svg">
  // SVG content goes here
</svg>

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

Encountering 503 errors with Heroku while making asynchronous API calls in my React application

Almost at the finish line with my project! The coding part was a breeze, but deployment is proving to be quite the nightmare. I've managed to get most of it deployed, but now the app is experiencing timeouts on API calls, even though they work perfect ...

Style prop in React components

I am currently utilizing the text-fields component from UI Materials found at this link. I'm looking to customize the style of the helper text associated with this component. https://i.stack.imgur.com/eDwXD.png My attempt so far: pstyle = { "& ...

Utilizing an observer to encapsulate a custom React hook- a comprehensive guide

As part of my project, I have developed a unique custom react hook that relies on observable state from the store for its dependencies within useEffect: Here is an example of my custom hook: const useFoo = (() => { const { count } = store; useEff ...

What could be the reason behind CSS styles applying only when inserted within a style tag in my form, and not in the linked CSS file?

I want to express my gratitude for finding a solution to positioning Validator CallOuts on a web form through this question regarding repositioning the AJAX toolkit's CalloutExtender control. Does anyone know why applying CSS classes directly within ...

Create objects in the gallery

I recently developed a React Material-UI component using Typescript: <Grid container direction="row" justifyContent="flex-start" alignItems="flex-start"> <Grid item xs={5}> <B ...

UI experiencing issues with selecting radio buttons

When trying to select a radio button, I am facing an issue where they are not appearing on the UI. Although the buttons can be clicked, triggering a function on click, the selected option is not displayed visually. <div data-ng-repeat="flagInfo in avai ...

What are the steps to create a "gooey glide" animation using React and MUI?

I am looking to create a unique animation for my list of items on a web page. My goal is to have the items slide in one by one with rapid succession and then slightly shrink once they reach their final position, similar to how pillows might fall or like a ...

Is there a way to remove the numeric arrow from the text field?

Is there a way to remove the arrows from a number textField? I have tried the following code without success: const useStyles = makeStyles(theme => ({ const theme = createMuiTheme({ MuiInput: { root: { "&::-webkit-outer-s ...

Currently working on enhancing the responsiveness of the login form

I am encountering difficulties while trying to make the page responsive. I've checked my basic code, but something seems off. Could you please take a look at the plnkr link below and give me some feedback on where I might have gone wrong? Here's ...

Guide to updating information in the MERN stack

I'm facing an issue while trying to set up an Inventory update route in a MERN stack. The "Add new item" route is working perfectly, but I'm having trouble with the "update route". Can someone please help me identify what I might be doing wrong? ...

Exploring the integration of mixins from .scss files within node_modules into our .tsx or .jsx files for enhanced styling

I am currently facing a challenge with importing a .scss file from one of the installed node_modules into my .tsx file. The scss file contains a mixin named @mixin make-embedded-control($className: embedded-control){.....} which has all the necessary css ...

What could be causing my Next.js layout to re-render?

I currently have a basic root layout set up in Nextjs (app router) that displays a navigation bar and the child components underneath it. ROOT LAYOUT import "./globals.css"; import type { Metadata } from "next"; import { Inter } from & ...

Bootstrap does not support horizontal alignment of columns

I can't seem to get my columns to align horizontally in bootstrap. I want the image on the left and the text on the right, but they keep stacking vertically. Can someone help me with this? As far as I know, col-md-6 should divide the page into 2 colu ...

What is the best way to make my text scroll horizontally if the container is not wide enough?

Instead of overwhelming you with code, I'll just share a link to the types of animations I've come across so far. Although these options are close to what I'm looking for, none of them are exactly right. The one that comes closest to my vis ...

Is there a built-in binding for input fields in ReactJS?

I've created a somewhat intricate form in React that, when simplified, resembles the following: var MyForm = React.createClass({ init: { data: { "MyValue" : "initial value" } }, getInitialState: function() { return this.init; }, ...

Having trouble incorporating a title in the header section

I encountered an issue while attempting to include the Fragment, title, and Head. When these are added, an error occurs but when removed, the page displays correctly. I have searched for a solution to this problem but couldn't find anything. import ...

React: Implementing Material-UI Typography with custom inline spacing

Take a look at this code snippet: <Typography className={classes.welcomeMessage} variant="h1"> A <span className={classes.redText}>smart nation </span> approach to <span className={classes.redText} ...

Is it possible to change the style of the current page in PHP using the ?page href method?

Currently, I am facing an issue with styling in my code. Specifically, I want to style the page numbers that are currently open or active. For example, if a page is open, I would like its corresponding number to be displayed in a different color or style. ...

Steps to implement a text border in Raphael.js

I have a website dedicated to creating customized football jersey designs. I'm utilizing Raphael.js for customization, and I am looking for a way to add an outline to text using Raphael. My research led me to the suggestion of using the stroke proper ...

Tips for choosing specific sections of an element to display on the web page using React and Material UI

https://i.sstatic.net/JPbD9.png Above is the object I am working with. It's actually an array of objects, and I have used mapping over the array like this: <TableBody> {union && unionArray.map((row) => ...