Pseudo-element fails to display in React when applied to a paragraph tag, even with display block property set

I am experiencing an issue where the pseudo element ::after is not appearing in my browser. I am currently working with React.js and Material UI's makeStyles.

Here is the code snippet causing the problem:

modalTitle: {
    borderBottom: '2px solid red',
    display: 'block',
    margin: '10px 15px',
    padding: '0',
    width: '100px',

    '&::after': {
        background: 'green',
        content: '',
        display: 'block',
        height: '2px',
        width: '2px',
    },
},

Interestingly, the styles defined under modalTitle are rendering correctly, but the pseudo element is not displaying. Any suggestions on how to resolve this issue?

Answer №1

This particular issue can be a bit tricky to navigate. When utilizing makeStyles, it expects a styles object (or a function that returns an object). This object uses class names as keys and objects containing attribute-value pairs as values. These values are strings, such as "block", "green", or "10px 15px".

In CSS, the ::before and ::after pseudo-elements allow for the addition of content onto the page. Every pseudo-element must include a CSS content property, as without it, the default value is set to normal/none, resulting in no content being displayed.

The problem lies within your code where you have included content: '' in your object. What you actually need to do is pass an empty string as the CSS value, like this: content: '""'.

Similarly, if you wish to insert text using the content property, you cannot simply add the string. Instead, ensure to encapsulate the text within quotes and pass it as a string:

// ❌ this approach won't work
{
  content: 'some text',
}

// ✅ this approach will work
{
  content: '"some text"',
}

To resolve this issue and successfully add the element, you can implement the following:

const useStyles = makeStyles({
  modalTitle: {
    borderBottom: '2px solid red',
    display: 'block',
    margin: '10px 15px',
    padding: '0',
    width: '100px',

    '&::after': {
      background: 'green',
      content: '""',
      display: 'block',
      height: '2px',
      width: '2px',
    },
  },
});

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

Click once to expand all rows in the table with ease

I have successfully created a basic table using HTML. The table includes nested elements that are designed to open individually when clicked on the assigned toggle. Essentially, clicking on a '+' icon should reveal a sub-menu, and clicking on a & ...

cors library returns success messages, not errors

Currently, I am working on implementing the cors package within a node and express environment to validate whether the requesting domain can access my resources. Setting up the validation part following the official documentation was not an issue. However, ...

Choose all div elements except for those contained within a certain div

Having trouble with an IE7 z-index fixer that's affecting specific elements. I'm attempting to select all divs, excluding those nested within a particular div. Below is the jQuery code I'm using, but it's not functioning correctly: & ...

Angular JS has blocked the cross-origin request from $http

I have been working on implementing search suggestion functionality using a Suggestions API "". The implementation works fine with Ajax GET requests, but when I try to use it with Angular's $http service, I encounter an error in the console: Cross-Or ...

Tips for sending routeparams value to model window in AngularJS

In the current project, I am working on implementing a modal window for the edit screen functionality. The process involves displaying a list of items on the screen, from which users can select one row and then click on the modify button to open the edit s ...

Retrieving a specific time using a JavaScript interface

I am currently implementing a JavaScript control that can be found on this website: My question is, how can I retrieve the selected date from the control in order to pass it to a postback page? I attempted to figure it out myself, but my JavaScript skills ...

Ways to store Token in Browser Cache?

I am currently developing a login system for an application at my school. I have successfully implemented user registration, which is saved to my Azure DocumentDB. However, when trying to log in with the user, the token does not get saved so that I can acc ...

React Images: Carousel display issue with images not centered

Is there a way to center the selected image instead of it appearing on the left-hand side? Here is the current behavior: https://i.stack.imgur.com/SUWOK.jpg I am using the following packages in a sandbox environment with Next.js 11 and TailwindCSS 2.2.4: ...

Having trouble setting up react-i18n with hooks and encountering a TypeError: Cannot read property '0' of undefined?

Encountering an error while setting up the react-i18n with hooks: TypeError: Cannot read property '0' of undefined Here's the content of i18n.js: import i18n from 'i18next'; import { initReactI18next } from 'react-i18next/h ...

Is there a way to completely remove all styling from a specific child element?

Struggling with a drop-down menu issue - the parent element has a border, but I don't want that style to be inherited by the child sub-menu. Despite my attempts to remove the border using border: 0px, it's not working as expected. Is there any wa ...

The file upload issue with FormData append in CodeIgniter is causing errors

My current challenge involves uploading files using AJAX to my CodeIgniter based website. Unfortunately, I am encountering an issue where I cannot retrieve the file field value in the controller. This results in an error message stating "Undefined index: & ...

Utilizing Bootstrap classes alongside custom CSS within Next.JS

Recently, I've been diving into the world of Next.JS and attempting to integrate it with Bootstrap. However, I've encountered a roadblock - the inability to combine Bootstrap classes with my own custom CSS classes within the className attribute. ...

Top method for waiting for material-ui ripples to finish before capturing snapshots

Seeking expert advice from the React Testing Library community on ensuring completion of a Material UI ripple animation before taking a snapshot. We've been experiencing inconsistent test results with our CI servers due to the animation finishing fas ...

Tips for addressing dependency issues in react native applications

Starting a new ReactNative project using "react-native init newProject" is giving me some warnings. How can I resolve these issues? npm WARN deprecated [email protected]: core-js@<2.6.5 is no longer maintained. Please, upgrade to core-js@3 o ...

404 Error: JSON POST and GET Request Not Located

I need assistance with setting up an API in Django as I am encountering errors in the JavaScript console. The error messages are: GET http://127.0.0.1:8000/edit/undefined 404 (Not Found) POST http://127.0.0.1:8000/edit/undefined 404 (Not Found) Is there a ...

Placing a MongoDB query results in an increase of roughly 120MB in the total JS heap size

I'm puzzled by the fact that the heap size increases when I include a MongoDB database query in a function within my controller. Here is the code for my router: import { Router } from "express"; import profileController from '../contro ...

What is the correct way to establish the value of an editable div element?

When working with input and other elements that have a value, I usually use Object.getOwnPropertyDescriptor(input, 'value').set; followed by element.dispatchEvent(new Event('input', {bubbles: true})). However, this approach does not wor ...

Experience seamless one-to-many broadcasting with WebRTC/Kurento, featuring server-side recording capabilities

I am currently exploring Kurento to determine if it fits my needs. I am interested in developing a mobile application that can record and stream video to a server in real-time, with the server saving the video on its file system as it is being transmitted. ...

Prevent users from navigating back in their browser with jQuery

As I work on developing an online testing app, one of the requirements is to prevent users from refreshing the page or going back during the test until it has ended. While I have successfully disabled the refresh action in jQuery using various methods with ...

When a user clicks a button, Javascript will generate a fresh upload image form

I am interested in creating a button that, when clicked, generates a new image upload button within a form I have already created in HTML. My goal is to figure out the best approach for this functionality, especially considering that there is a remove butt ...