Tips for creating a conditional CSS pseudo-element using styled-components

I am attempting to create a styled-component with conditional pseudo-elements, but I am having trouble getting it to work properly. Could someone confirm if the syntax I am using is correct?

For the context: I want to achieve a scenario where if isSelected is true, there will be an additional red-colored border around my div element (a rectangular box that already has a black border).

Any assistance would be greatly appreciated!

const CardContainer = styled.div(
    (props) => css`
        align-self: center;
        margin: auto;
        width: 80%;
        height: 100%;
        background: rgb(222, 222, 222);
        border-style: solid;
        border-width: 0.1rem;
        box-shadow: 4px 4px 8px rgba(183, 183, 183, 0.25);

        ${props.isSelected &&
        css`
            &::before {
                border-color: red;
                border-style: solid;
                border-width: 0.1rem;
            }
        `}
    `
);

Answer №1

It appears that the functionality is not operational.

The structure itself seems fine, however, you have neglected to include the content property. The inclusion of the content property is imperative as without it, the pseudo element will remain hidden.

&::before {
    border-color: red;
    border-style: solid;
    border-width: 0.1rem;
    content: 'abc'; // this value can vary, even an empty string
}

https://codesandbox.io/s/sad-sound-j5czon?file=/src/App.js:385-506

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

Guide to displaying a loading message while performing a synchronous AJAX request in a web browser

Is there a way to display a waiting message during a synchronous AJAX call in the browser? I attempted to use the code below, but even after turning off the web server, the "Saving" message did not appear. After some time, only an error event from the AJA ...

Monitoring the development of data processing in a PHP server using AJAX for streaming purposes

I have searched for solutions to implement an ajax progress bar on this platform, but unfortunately, I couldn't find any suitable answers for my specific requirement. Essentially, what I need is for the browser to make an ajax call to a server runnin ...

Encountering issues with Next JS build process as 'Export' has reported errors on specified paths

When attempting to deploy my Next.js site to Vercel, I came across this build error: 15:02:38 > Build error occurred 15:02:38 Error: Export encountered errors on following paths: 15:02:38 /about/undefined 15:02:38 at exportApp (/verc ...

Do the two types of updater functions for setState in React hold the same value?

Recently, I came across the concept of using updater functions for setState in React. After learning about this, I noticed it being implemented in two different ways. Imagine having a state object structured like this: interface State { readonly expand ...

During a jQuery ajax call, if one of the keys has the value "??", jQuery will automatically convert the value to jQuery164042601801476224854_1371690944590

When I execute the following code snippet: var i = '{"xyz":"??"}' $.ajax({ url: '/someendpoint',type: 'post', dataType: 'json', success: null,error: null,data: i }); The post request shown in Firebug is: {"xyz": ...

Accessing a nested value in MongoDB: A step-by-step guide

I am working on a document where I have a category object containing fields for category name, cost, and date. My task is to retrieve categories specifically from the year "2022". The console is currently showing "[]" output, but I want it to display categ ...

Adjust the Height of a Div Following an Ajax Request

ISSUE: Currently, I have a dynamic form that utilizes JQuery steps to transform it into a wizard. At one of the steps, selecting an option from a drop-down menu triggers an AJAX call which adds additional form fields dynamically. The challenge lies in adj ...

Unable to stop submission as a result of ajax verification

As someone new to java and jquery, I am facing a challenge with the code below: $(document).ready(function() { //Prevent SUBMIT if Session setting = On (Ajax) $('#formID').submit(function(e) { var prevent = false; $.ajax({ type: ...

In IE9, the margin: auto property does not effectively center a div element

I'm trying to center a content div in the space leftover by a sidebar. Here's how I want it to look: After some trial and error, I was able to achieve this for most browsers by using margin: auto on the specific div, along with setting overflow: ...

Having difficulty including text in the website header

I'm struggling to add text on the right side of my header. I have placed small images on the right side of the header, slightly below, and now I want to insert some text above them but I can't seem to get it to work. This is what I'm trying ...

Moving a div horizontally while clicking and holding on a control button with a smooth animation effect

I am currently working on a new feature inspired by a previous question I found on Stack Overflow. My goal is to make the scrolling start slowly and then gradually speed up. However, I am facing an issue with using easing in the animate function as it get ...

Adding a button to the right of a text-field input in Vuetify app

I need advice on the best way to use Vuetify in order to display a button to the right of a text-field (attached). I've checked the main site for information but couldn't find anything related to this specific scenario. https://i.sstatic.net/Pp ...

Deactivate button using Javascript

Can anyone assist me with this issue I am having? I currently have a button set up as follows: <input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/> I need to disable the button using jQuery, standard javascript ...

Error encountered when including JavaScript in the app.js file within a Laravel project

I have encountered an issue with my browser sniffer code that uses the function detect.js. Interestingly, the code works perfectly fine when it is not integrated into my project. However, as soon as I compile the JavaScript and include it in app.js, I enco ...

Arranging an image and button within a column div row using Bootstrap

I'm encountering an issue with Bootstrap where I am trying to align the blue buttons neatly below the image. Link to the image: img Here is a visual representation: https://i.sstatic.net/tB5mU.png This is the code snippet: <div class="row&q ...

Unable to fetch data using getJSON method

objecten.js var objectData = [ { image: 'gallery/objecten/bear.jpg', thumb: 'gallery/objecten/bear.jpg', title: 'my first image', description: 'Lorem ipsum caption&apos ...

Creating a torn paper illusion using CSS masking techniques

My goal is to create a ripped/lined paper effect using CSS mask. It's working well and looks good. However, I'm struggling to apply the mask at the top as well. Here's what I've tried so far: mask-image: linear-gradient(white, white), u ...

I need the CSS to make sure the div extends its container's width

The structure of my current HTML includes a footer div that is positioned alone within the BODY section. <div id="footer"> <div class="container"> <div id="footer-bg"> <div class="footer1"> & ...

Tips for managing errors when using .listen() in Express with Typescript

Currently in the process of transitioning my project to use Typescript. Previously, my code for launching Express in Node looked like this: server.listen(port, (error) => { if (error) throw error; console.info(`Ready on port ${port}`); }); However ...

Discover the CSS auto height value using JavaScript

Is there a way to utilize JavaScript in determining the value set for auto height in CSS? My development stack includes Grails and jQuery. For instance, consider the following CSS: .tool-preview { height: auto; } ...