The props are not receiving the margin property

I am working on a styled component that needs to receive a prop to determine if it should have a margin on the left or right.

It seems that there is a syntax issue that I am struggling with.

Here is the code snippet:

const FormDiv = styled.div`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
  flex: 1;
  position: relative;
  ${({ left }) =>
    css`
     *** margin${'-'}${left ? 'right' : 'left'}: 30vw; ***
    `};

  @media ${device.mobileS} {
    height: 100%;
    flex-grow: 1;
  }
  @media ${device.tablet} {
    height: 100vh;
    width: 100%;
  }
`;

export default FormDiv;

Even after passing 'left' or 'right' as props in the component, it doesn't apply any margin. How can I correct the syntax in the bold sentence?

Answer №1

Referencing the information from https://styled-components.com/docs/faqs#can-i-nest-rules

const FormDiv = styled.div`
  ...

  ${props => props.left ? "margin-right" : "margin-left"}: 30vw;

  ...
`

Alternatively

  ${props => props.left 
      ? 'margin-right: 30vw;'
      : 'margin-left: 30vw;'}

Another option

  ${props => props.left 
      ? css`margin-right: 30vw;`
      : css`margin-left: 30vw;`}

Or you can use this approach

  margin-right: 30vw;

  ${props => props.left && css`
       margin-right: unset;
       margin-left: 30vw;`}

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

Boost Google Pagespeed score by reducing 'Total Blocking Time' in NextJs

I've recently started using NextJS and I'm looking to improve my Google Pagespeed ranking. So far, I've made some good progress in optimizing different metrics. From the screenshot provided, it's clear that the only issue remaining i ...

Warning message about dependency in ReactJS useEffect causing an infinite loop

I am facing an issue in my functional component where I have state that changes, and I want to perform some actions whenever it updates. To achieve this, I tried using the useEffect hook, but I encountered a "dependency missing" error. Even after adding al ...

How can I alter the text color on hover within a Material UI card? My goal is to have the text color change when hovering over the card itself, rather than just the text

When I apply CSS to the card hover effect, it works fine. However, I also want to change the text color along with the card color on hover. card: { maxWidth: 350, height: 300, '&:hover': { backgroundColor: '#373737 !impor ...

Pondering the importance of the "zoom" feature in creating responsive designs

Currently, I am working on creating a responsive design using HTML and CSS. However, I have encountered an issue that I need help with: When resizing the window, the layout adapts perfectly. Similarly, when zooming in or out without resizing the window, e ...

Incorporating and designing a side button using jQuery Mobile

I'm working on adding a button to the left side of the screen that is round (but not necessarily) and partially visible, with a visually appealing design. This button will allow users to open a side menu panel. Below is the HTML code for the button: ...

The error message encountered is: "npm ERROR! The script 'build' is not found."

Currently, I am facing an issue while trying to deploy my React project. Instead of using create-react-app to initialize the project, I opted for a platform called createapp.dev. I am unsure if this choice is causing the problem. Below, I have shared my ...

Hover over MUI to see the text transform with a sleek underline animation

Before dismissing this question as a duplicate of this one, I urge you to consider my situation. My goal is very similar to the linked post, but the CSS solution provided does not quite address the specific issue at hand. I am seeking a complete MUI-based ...

Building a hierarchical tree structure using arrays and objects with Lodash Js

I am attempting to create a tree-like structure using Lodash for arrays and objects. I have two arrays, one for categories and the other for products, both with a common key. The goal is to organize them into a tree structure using string indexing. let ca ...

Moving images displayed on a webpage

Is animating 5 pictures/photos a simple task? Take a look at this example: Which programming languages are recommended for achieving this effect? Thank you Tea ...

Issue - The 'defaultValue' is failing to load the state value, and the 'value' is not being updated when changed

My current setup involves an input field in my MovieInput.tsx file: <input id="inputMovieTitle" type="text" onChange={ e => titleHandleChange(e) } value={ getTitle() }> </input> This is how the titleHandleChange function ...

What is the most efficient way to pass a collection of conditional elements to a parent component in React?

I am working on developing a versatile component that can handle an unspecified number of child elements and selectively display them based on a state value. Let's kick things off with a straightforward component that displays its children conditional ...

Problems encountered with React Image Magnifiers while attempting to zoom in with Next.js

When I try to use the react-image-magnifiers plugin to make an image zoom in on hover, it works fine without next.js. However, when I integrate it with next.js, the zoom functionality does not work. Could there be an issue in my next.config.js file? This ...

Looking to implement a CSS banner image for seamless customization across multiple pages

Is it possible to change the banner image easily in the CSS without having to update every HTML page? I don't want the image source to be directly on the HTML pages. How can this be achieved? Additionally, there are already two references in the CSS: ...

How can we add a class to a radio button using jQuery when it is checked, and remove the class when it

I'm looking for help with using jQuery to toggle between two radio buttons and display different divs based on the selection. jQuery(document).ready(function($) { if ($('#user_personal').is(':checked')) { $('.user_co ...

Struggling to get my react.js environment up and running, experiencing technical difficulties

I followed a tutorial on setting up the environment for ReactJS from https://www.tutorialspoint.com/reactjs/reactjs_environment_setup.htm After entering "npm start" in the command prompt, I received the following error message. I have already installed no ...

Trouble arises when managing click events within the Material UI Menu component

I've implemented the Menu Component from Material UI as shown below - <Menu open={open} id={id} onClose={handleClose} onClick={handleClick} anchorEl={anchorEl} transformOrigin={{ horizontal: transformOriginRight, vertical: t ...

The button is positioned at the bottom right within a div surrounded by text that flows around it seamlessly

Image Is there a way to position a button in the bottom right corner of a div with a fixed height that is filled with text where the text flows around the button? I have attempted using "float" and positioning the button, but I have not had any success. ...

Struggling with the incorporation of Typescript Augmentation into my customized MUI theme

I'm struggling with my custom theme that has additional key/values causing TS errors when I try to use the design tokens in my app. I know I need to use module augmentation to resolve this issue, but I'm confused about where to implement it and h ...

"Google Maps functionality on ReactJS web app is functional when accessed on a PC's localhost, but fails

After experiencing great success with my web app built on React on my PC, I encountered a problem when trying to access the same app from localhost:3000 on my phone. The app seems to be functioning properly on the PC, with components like the navbar, sideb ...

What is the best way to transform private variables in ReactJS into TypeScript?

During my conversion of ReactJS code to TypeScript, I encountered a scenario where private variables were being declared in the React code. However, when I converted the .jsx file to .tsx, I started receiving errors like: Property '_element' d ...