Styling Elements in React Using Pseudo Elements with Styled Components

After researching multiple solutions, I still can't figure out why my setup isn't working as expected.

The index.css file in my document is successfully creating a gradient background and the footer is functioning properly. However, the problem arises when I try to add an SVG as a semi-transparent background to the footer.

If I include the following code snippet, the background image appears but the opacity affects the entire footer:

const Foot = styled.footer `
    position: relative;
    margin: 0px;
    padding-top: 2em;
    padding-left: 0em;
    padding-right: 0em;
    padding-bottom: 2em;
    color: white;
    background-image: url('/logo.svg');
    opacity: 0.25;
}
`

On the other hand, if I use this alternative approach to maintain the opacity only on the background image while keeping the rest of the footer opaque, the background image doesn't show at all:

const Foot = styled.footer `
    position: relative;
    margin: 0px;
    padding-top: 2em;
    padding-left: 0em;
    padding-right: 0em;
    padding-bottom: 2em;
    color: white;
    
     &::before {
    content: "";
    background-image: url('/logo.svg');
    position: absolute;
    opacity: 0.25;
    }
}
`

How should I configure my setup so that the background image is semi-transparent without affecting the opacity of the rest of the footer?

Here is the JSX code for my footer in case it provides any insights:

<Foot id="footer-page">
                    <Div>
                        <div className="container">
                            <div className="row">
                                <div className="col-lg-4 col-sm-12 text-center">
                                    <p className="lead">HUMAN RESOURCES SOLUTIONS></p>
                                    <img className="img-fluid" src="/assets/images/wbe-seal.png" alt="certified women owned business seal" />
                                </div>

                                <div className="col-lg-4 col-sm-12 text-center"><p className="lead">FOLLOW US ON <Span orange>SOCIAL MEDIA</Span></p>
                                        <A href="https://www.facebook.com" target="_blank" rel="noopener noreferrer"><i className="fab fa-facebook-square fa-3x px-2"></i></A>
                                        <A href="https://www.instagram.com" target="_blank" rel="noopener noreferrer"><i className="fab fa-instagram fa-3x px-2"></i></A>
                                        <A href="https://www.linkedin.com/" target="_blank" rel="noopener noreferrer"><i className="fab fa-linkedin fa-3x px-2"></i></A>
                                        <A href="https://twitter.com" target="_blank" rel="noopener noreferrer"><i className="fab fa-twitter-square fa-3x px-2"></i></A>
                                </div>
                            </div>
                        </div>
                    </Div>
                </Foot>

Answer №1

If you're looking to utilize a single colon (:) instead of the &::before pseudo-selector, make sure to update your code accordingly.

In case that adjustment doesn't yield the desired result, experimenting with the after pseudo-selector could be worth considering as well.

const Foot = styled.footer `
    position: relative;
    margin: 0px;
    padding-top: 2em;
    padding-left: 0em;
    padding-right: 0em;
    padding-bottom: 2em;
    color: white;
    
    &:before {
      content: "";
      background-image: url('/logo.svg');
      position: absolute;
      opacity: 0.25;
    }
}

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

Resolving "SyntaxError: Unexpected identifier" when using Enzyme with configurations in jest.setup.js

I'm currently facing an issue while trying to create tests in Typescript using Jest and Enzyme. The problem arises with a SyntaxError being thrown: FAIL src/_components/Button/__tests__/Button.spec.tsx ● Test suite failed to run /Users/mika ...

CSS / SCSS: altering child element styles when parent is focused

Is there a way to change the style of child elements when focusing on a parent element without using javascript? For example, let's say we have: <li class="parent"> <div class="child1"></div> <div class="child2"></d ...

Preserve the active tab's state upon page refresh using React/Redux

Looking for advice on maintaining tab state in React/Redux! I created a page with a tab component containing 5 tabs. How can I ensure that the selected tab remains active after a page reload or refresh? Currently, I am considering two options: Utilizin ...

Is it possible to modify the cursor for disabled <button> or <a> elements in Bootstrap 4?

Is there a way to apply the cursor: not-allowed style to a button or a element? I've attempted the following: .not-allowed { pointer-events: auto! important; cursor: not-allowed! important; } Here is an example of my button: <a class=" ...

Kendo popup event binding issue arises after upgrading React to version 17.0.1

After upgrading my application from react version 16.4.1 to 17.0.1, I encountered an issue where a function is not being called when clicking a button. Strangely, the function works fine if the component is not transformed into a Kendo window using jQuery. ...

Design a slider that mimics infinite scrolling using CSS

I am attempting to design a CSS slider with infinite scroll functionality, but I want to achieve this without the need to dynamically add or manipulate DOM elements. The goal is for the slider to loop back to the first slide when the last one is reached. ...

What is the best way to extract the user's name from the userId and showcase it in a post

Greetings everyone! I am currently attempting to include the user's name in the post that they have created, but I seem to be experiencing some difficulties. Below is where the user's name should be displayed within the post: <Link style={{ ...

Special Bootstrap's hamburger menu

My website has a vertical navigation bar with a breakpoint set for medium-sized screens to display a hamburger menu. When the hamburger menu appears, I want to hide the text following the icons to save space and only show the icons. Clicking on the hamburg ...

The background on hover in Tailwind is stuck and won't transition

I am working with an array of objects that contain information and a HEX color property. I am mapping through this array and passing each object to a React component where I destructure it. My goal is to change the background color of the object when hover ...

Creating a React component that leverages the power of Vega data

Trying to incorporate vega into a React component has been challenging, possibly due to installation issues. A component was created with the following structure: import vega from 'vega'; class Chart extends React.PureComponent { ... compone ...

Utilize JavaScript to automatically scroll to the element id of the current view

Is it possible to retrieve the name of an anchor tag in view as a user scrolls through an HTML page with multiple paragraphs and associated anchor tags? <p><a name="para1"></a> some long text </p> <p><a name="para2"> ...

What is the best way to navigate back to the top of the page once a link has been clicked?

One issue I'm facing is that whenever I click on a link in NextJS, it directs me to the middle of the page: <Link href={`/products/${id}`} key={id}> <a> {/* other components */} </a> </Link> I believe the problem l ...

The functionality of the "#Next" and "#Prev" features in the Tiny carousel are currently not functioning properly within the MVC project

Looking to implement a simple content carousel using jQuery 2.1.3 in my project. Previously used a non-responsive jQuery content carousel and switched to find a responsive option:Tiny Carousel Added Tiny Carousel's .js and .css files to Bundles.confi ...

Troubleshooting the Inline-Block Problem with Jquery Image Bubble Display

Recently, I've been exploring ways to eliminate the padding that is creating space between images before they align correctly. The culprit causing the padding seems to be the display: inline-block property. While it was suggested to have all the li t ...

Diving into the world of React and Typescript: Leveraging the power of Typescript in defining constants

Can you help me understand how to incorporate TypeScript into the code snippet below? I've been struggling for an hour and would really appreciate your guidance! In the code snippet, the map function utilizes sortOptions, which I attempted to define ...

Using HTML and CSS to create interactive icons that change color when clicked, similar to how a link behaves

Have you ever wondered if there's a way to make an icon act like a link when clicked, just like regular text links turning purple? And not just the last one clicked, but every single icon that gets clicked. Trying to use the :visited pseudo was unsucc ...

CSS - Desiring a border with three layers

Here is the CSS code I am using: border: 2px solid #00ff60; outline: 1px solid #000; outline-offset: 0px; This is how it looks: https://i.sstatic.net/SuvU1.png Is there a way to modify it so that it includes an inner black border, similar to the one at ...

Tips for returning the slider to its default position when a tab is deselected

A fantastic code snippet can be found at this link for creating an animated navigation bar. The only thing left on my wishlist is for the slider to reset to its original position if a tab is not selected. Currently, it remains static regardless of selectio ...

What could be causing the transparency of my buttons when viewed on my device?

Recently, I customized the appearance of buttons in my App by adding colors. Surprisingly, everything looks perfect when I test it on a local server on my PC. However, once I deploy the App to my Android device, all the buttons become transparent. In my v ...

Extract data from a Json file and use it to populate options for a Picker

As a newcomer to react native, I am facing an issue while trying to fetch a webservice and extract a specific element from the response to populate a Picker component (using native-base). The element I am looking for is labeled as LIBELLE in my JSON data. ...