React Material-UI eliminates the 'content' property from the CSS pseudo-element

Creating a React MUI Box and styling it like this:

<Box
  id='my-box'
  sx={{
    '&::before': {
      content: 'Yay! ',
      backgroundColor: 'red'
    }
  }}
>Life is beautiful</Box>

However, during runtime, the CSS property content disappears, leaving only background-color. Manually adding content: 'Yay! ' through browser dev tools displays it correctly.

Detailed breakdown:

  1. Screenshot of code:

https://i.stack.imgur.com/DnikJ.png

  1. Incorrect display in the browser:

https://i.stack.imgur.com/YQbJT.png

  1. Inspecting element via dev tools - missing content:

https://i.stack.imgur.com/Xgz5s.png

  1. Manually adding content through dev tools:

https://i.stack.imgur.com/lX0qo.png

  1. Result after manually adding content:

https://i.stack.imgur.com/Basxy.png

How can I prevent the removal of the CSS property content at runtime? And why does this occur initially?

Appreciate any insights. Thank you.

Answer №1

To properly style the content prop within the sx attribute, you should use both double and single quotes. Here is an example:

<Box
  id="my-box"
  sx={{
    "&::before": {
      content: "'Yay! '",
      backgroundColor: "red"
    }
  }}
>
  Life is beautiful
</Box>

The expected result will look like this:

https://i.stack.imgur.com/ATtVZ.png

You can view the working code on CodeSandbox: https://codesandbox.io/s/friendly-golick-zl6drq?file=/demo.tsx:119-299

(Note that if you don't use quotes as instructed, you may encounter an error message suggesting to include quotes in the 'content' property value.)

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

Struggling to retrieve a single value from my React app using sequelize findbyPk

Greetings, I am new to this and have a question that may seem silly. Despite my efforts to resolve it on my own, I have been unsuccessful. When using Postman, the data returned to "localhost:8000/playlists/1" is correct, but when I try to access it through ...

Repairing the Performance of the 'Save' Feature

Can the 'Save' button in my code save team assignments for players selected using drag and drop? I'm considering using localStorage, but unsure about implementation. Note: To run the code properly, copy it as an HTML file on your computer. ...

Attempting to position an image in the middle-right of a column using bootstrap

After spending all day researching flexbox and vertical align, I still can't figure out how to align an image to the right in my bootstrap column with text wrapping around it on the left. Check out what I've tried so far here: https://codepen.io/ ...

Transferring information between Express and React through the Contentful API

I have embarked on a journey to explore Contentful's headless CMS, but I am encountering a challenge with their API client. My goal is to combine Express with React for server-side rendering, and I am utilizing this repository as my starting point. S ...

Navigating through objects to retrieve internal object values

I currently have a collection of objects stored in my program. const objs = { "1":{ "name":"Candice", "Classes": [00029,00023,00032,000222], "id":0002918 }, "2":{ "name":"Clark", "classes" ...

I am attempting to incorporate a data layer into kepler.gl using react, however, the data layer is not appearing

I'm facing an issue while attempting to programmatically add a cluster layer in kepler.gl map. Even after dispatching the data on the map, I am unable to view any layers. Any assistance with this problem would be greatly appreciated. dispatch( ...

Troubleshooting Material-UI: The Mystery of the Missing Dialog

I have been struggling with getting a pop-up dialog to appear when a form is incorrectly filled out. Despite my efforts, the code that should trigger the dialog upon submission does not seem to be working as expected. The function renderError(), responsib ...

Are there any extensions in VS Code that can identify all files that are importing the current file?

class ButtonNoclickTag is exported default {...} I am curious to find out which files have imported this ButtonNoClickTag component through vscode ...

Resolving type error issues related to using refs in a React hook

I have implemented a custom hook called useFadeIn import { useRef, useEffect } from 'react'; export const useFadeIn = (delay = 0) => { const elementRef = useRef<HTMLElement>(null); useEffect(() => { if (!elementRef.current) ...

Exploring the effectiveness of React Hook Form using React Testing Library

My Component includes a form that, upon submission, utilizes Apollo's useLazyQuery to fetch data based on the form values. The form in the component is managed by React Hook Forms, with the handleSubmit controlled by RHF. <FormContainer onSubmit= ...

Unable to display complete content of Ionic 3 webpage

Trying to print a specific part of an Ionic page which contains a long list of items. However, encountering an issue where the entire list doesn't fit on one page and requires scrolling down to view all items. Expecting the print dialog, triggered by ...

Every time I attempt to set up Material-UI, a barrage of errors is thrown my way, making the installation process frustrating

After successfully installing React using npx, I encountered issues when trying to install Material-UI. Below is the code snippet that shows the errors: npm install @material-ui/core npm WARN saveError ENOENT: no such file or directory, open '/Users ...

What is the reason for the index.html file not connecting to the local .css files and .js file?

I'm currently using node.js to send an .html file that includes the following tags: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="description" content="This is my personal profile website" /> <link r ...

Show the table within the flex container

I have a question about CSS. Is it possible to apply display: table to a flex item? Whenever I try to do this, the layout doesn't behave as expected - specifically, the second flex item does not fill the available vertical/horizontal space. .conta ...

When attempting to shift the label above the input field upon focusing, it unexpectedly becomes concealed

I am attempting to reposition the label above the input field when it is focused, but instead it becomes hidden. label > span::focus { top: -20px; font-size: 15px; color: blue; } Check out the complete Html & CSS code. Update: ...

The copying process of the React App Docker image seems to be dragging on for quite

My Dockerfile is shown below: FROM node:16 as build-stage WORKDIR /app COPY package*.json /app/ ARG PROJECT_NAME=react-ui RUN npm install --force COPY ./ /app/ RUN npm run build FROM nginx:alpine WORKDIR /usr/share/nginx/html RUN rm -rf ./* COPY - ...

What's the best way to update the fill color of an SVG dynamically?

Is it possible to change the color of an SVG image dynamically without using inline SVG tags? I want to create a code that allows users to specify the source for the SVG tag and a hexadecimal color value, enabling them to customize any SVG image with their ...

The bottom navigation component from material-ui for React JS is experiencing display issues on certain screen sizes

Added a bottom navigation bar from material ui to a react application. Encountering an issue where on some screens (such as Iphone 5, Galaxy S5), the drawer is not fully displayed and the edges are cut off. However, on other mobile browsers (like Pixel 2), ...

The stature of Bootstrap's columns

Exploring Bootstrap has been an exciting journey for me, as I believe it will simplify the process of creating visually appealing web pages. I'm currently facing a challenge in understanding these two examples: Bootstrap 3: http://codepen.io/rhutchi ...

Tips for transferring information from Django to React without relying on a database

Currently, I am in the process of developing a dashboard application using Django and React. The data for the user is being pulled from the Dynamics CRM API. To accomplish this, I have implemented a Python function that retrieves all necessary informatio ...