Utilizing CSS inheritance in React app to style multiple classes simultaneously

My app features two buttons that serve similar functions on different pages. One button directs users to the search page, while the other takes them back to the home page.

The challenge I'm facing is that I need the background image for each button to be unique without duplicating the rest of the styling. How can I either inherit or override the background-image property?

I attempted the following code, but it doesn't seem to work as the button for ".return-home" is not rendered:

Link:

<Link
  to='/'
  className="return-home">
</Link>

App.css

.open-search, .return-home a {
  display: block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #2e7d32;
  background-image: url('./icons/add.svg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: 28px;
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
  font-size: 0;
}

.return-home a {
  background-image: url('./icons/arrow-back.svg');
}

Answer №1

You have the option to insert the image using various css selectors.

.open-search, .return-home{
  display: block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #2e7d32;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 28px;
  box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
  font-size: 0;
}

.open-search{
  background-image: url('./icons/add.svg');
}
.return-home{
  background-image: url('./icons/arrow-back.svg');
}

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

Having Trouble with Axios PUT Request in React and Redux

I'm having trouble making a PUT request to the server. I understand that for a PUT request, you need an identifier (e.g id) for the resource and the payload to update with. This is where I'm running into difficulties. Within my form, I have thes ...

Error Encountered in Reactjs: TypeError When Looping Through State Array to Render JSX Component

Currently in the process of constructing a gallery view showcasing cards that are populated with images fetched through an axios API call. The API call is made within a useEffect hook and the resulting object is then stored using useState. However, when ...

Arrange images/divs horizontally beside each other without specifying the width of the container div

Check out my code snippet <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <style type="text/css"> #image_container { position:absolute; top:100px; left:0px; he ...

A guide on dynamically adjusting text size based on viewport width and height in React with TailwindCSS

Utilizing React and Tailwind, I am working towards creating two overlapping rectangles with text. The top rectangle's text should be aligned to the left, while the bottom rectangle's text should be aligned to the right. Regardless of window size ...

Adding a drop shadow effect to SVG elements when hovered over

I am curious if it is feasible to have a drop shadow on my SVG element, but only make it visible when hovered over (:hover). I utilized the filter tag for the SVG and this is somewhat the effect I am aiming for. From what I have read, inline :hover is no ...

The one-click button is functional on larger screens, while on mobile devices, only a double click will register

I am facing an issue in angular where a button works perfectly fine with one click on larger screens, such as Macbook. However, on iPhone, it requires a double click to function properly (it works fine on Android too). The alert triggers with a single cl ...

Masking text in React Fiber/Drei can be achieved through a variety

Can you use a <Text>/<Text3D> to mask another object, like shown in this image where text is masking a cube? I have looked at examples on pmndrs/drei and tried various methods with replacing other objects, but I am unable to achieve the desire ...

Hide/Show overflow causing a disruption in my perspective

I am puzzled as to why my paragraph is not starting on a new line despite the overflow property set on the previous element. Take a look at my plunker, adjust the overflow property in line 11 to hidden and it will display correctly. When set to visible, i ...

Material-UI Grid Layout for React Applications

Is there a way to create a responsive grid system for building material design apps using react and material-ui? Although React-Bootstrap provides a solution like this, I haven't come across anything similar with Material-UI. The GridList component is ...

Error: In React js, attempting to access the 'value' property of an undefined object is causing a TypeError

Every time I try to search using the Search bar, I keep getting an error saying "cannot read property value". This is really strange because I have created many forms before without encountering this issue. Can someone please explain why this error is occu ...

Error encountered while rendering a functional component in ReactJS

Recently, I've been utilizing functional components in my project, like this: const Component = (props) => <div>asdasdasd</div>. However, I'm encountering a cryptic error when trying to render my application. The console displays a ...

Why is it that React has varying behavior in when it decides to rerender a child component?

Currently, I am developing a React Quiz application for an online course. The application consists of two main components. The first component is the Quiz component: export default function Quiz() { const [userAnswers, setUserAnswers] = useState([]); c ...

Bootstrap: customizing content for extra-small screens

Currently, I am in the process of developing a website using PHP and MYSQL. While the responsive design with Bootstrap works well for most pages, there are a few pages where not only do the columns need to rearrange for xs screen sizes, but the content wit ...

Error message: Unable to assign type (Combining React, Typescript, and Firebase)

Just started using TypeScript and in the process of migrating my React app to incorporate it. Encountering some type issues with Firebase auth service that I can't seem to find a solution for. Any suggestions? import React, { useEffect, useState } f ...

Tips for customizing compound components in Mui using 'classes'

Is there a way to customize Mui styles using the classes prop? One example is overriding the color of the InputLabel in the TextField component. I want to define makeStyles that will apply css rules starting at the root and override specific elements, li ...

Employing an odd/even toggle for assigning class names

I need each this.state.title to be aligned based on a different classname. I attempted using css flex boxes/nth-of-type/nth-child, but it didn't work well with React. I'm utilizing this.state to access my objects. My failed strategy render: f ...

Is it possible to implement a while loop in React?

Issue: I am facing a challenge while attempting to generate an array of 4 elements from a list using a while loop, but it seems to result in an infinite loop. const [options, setOptions] = useState([]); const fetchElements = () => { while(options. ...

What is the proper method to restrict a function to execute exclusively on the frontend following authentication on the backend?

My React frontend currently features two buttons—one for authentication and the other for displaying data once authenticated. frontend: const auth = async () => { window.open("callbackApiRouteOnBackend") } const displayData = async () ...

Tips for utilizing Redux toolkit dispatch within Next.js when using SSG or SSR

I've been struggling for a while to resolve my problems with using Hook like useDispatch in either getStaticProps or getServerSideProps. I'm utilizing redux-toolkit for state management and also working with Next.js, but encountered an error when ...

Ways to conditionally display a component in Next.js without the issue of caching CSS styles

I'm a newcomer to Next.js and I'm still trying to wrap my head around how the caching works. Let's take a look at this simplified example: An index page that displays either Test1 or Test2 components, based on whether the current minute is ...