Conditionally load styles in React using css-loader and style-loader

I am looking to create an if statement that will change the header color to grey if there is no icon present. Otherwise, the color should remain unchanged. Here is what I have in mind:

<h3 className={ icon ? "" : {style.grey} }  >Lorem ipsum dolor</h3>

Can anyone guide me on how to implement this using style and css loader?

This is my code:

import style from "./Article.css";

const Article = ( { image, icon } ) => {

    return (
       <div className= { style.article }>
            <article>
               {image ? <img className={ style.imgArticle } src= { image }  /> : ""}
               <h3 className={ icon ? "" : style.grey }  >Lorem ipsum dolor</h3>
               {icon ? <img className={ style.imgArticleTiny } src={ icon } /> : ""}
               <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
            </article>
       </div>
    )
}

export default Article;

CSS :

.article h3 .grey {
    color: #666666;
}

Answer №1

ISSUE RESOLVED

<h3 className={ icon ? "" : style.grey }  >Lorem ipsum dolor</h3>

All good now. The problem was with an incorrect CSS selector. The correct one should be:

.article h3.grey {
    color: #666666;
}

Mistake acknowledged

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

In what way does a child's width vary when set to "100%" or "inherit" from its parent element?

Can a Child element, nested within a Parent, have a width that is different from the parent's width when: 1) the Child's Width is set to 100% 2) the Child's Width is set to 'inherit'? I am experiencing both scenarios. It is challe ...

What is the best way to send props values to an input field in React without encountering any errors?

When I try to pass values from props to the current employee data in my update modal, I encounter an error. Most solutions online suggest using onChange in input fields, but that only shows the effect when a user starts typing. I need it to be visible when ...

Is there a way to disable auto rotation on a website when accessed from a mobile phone?

My current solution involves the following code: @media (max-height: 480px) and (min-width: 480px) and (max-width: 600px) { html{ -webkit-transform: rotate(-90deg); -moz-transform: rotate(-90deg); -ms-transform: rotate(- ...

How can I position a Font Awesome icon in the center of an image with Bootstrap 3?

I'm attempting to center a font-awesome icon on top of an image within bootstrap 3. Below is the image code enclosed in a div: <div id="quickVideo"> <a data-fancybox href="https://player.vimeo.com/video/153113362?autoplay=1&color=54 ...

What is the best way to update the state of my components using React?

On my landing page, there is a menu div and a main div that render out. The page has two states: constructor(props) { super(props); this.state = { file: 'Main', menuFile: '', } } When I mount my component, I s ...

Toggle Reactstrap Collapse based on e.target

I'm currently working on implementing a conditional collapse feature to prevent specific elements within the header from triggering the collapse. One example is having a delete button inside the toggle element, which I don't want to activate the ...

Error message saying "Host header not allowed" encountered in a React application hosted on IBM Cloud

I recently created a basic Todo List app (Express + React) following an instructional video by Brad Traversy on YouTube. The app was successfully deployed to Heroku and is now live. However, when attempting to deploy the same code to IBM Cloud, I encounter ...

Two pages with contrasting fonts but identical CSS styling

Within my website, I have utilized the code font-family: 'Caesar Dressing',cursive,"brushsci"; However, it appears that two different fonts are being displayed on two separate pages. Please take a look at the font styles in the header menu of t ...

Having trouble getting CSS media query to work in my Rails mailer template

Is it true that media queries do not work in email templates? I have a simple test template and a query BUT can't seem to get it to function properly. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DT ...

Words with emphasized border and drop shadow effect

I am trying to achieve a specific font style. If it's not possible, I would like to get as close as possible to the desired style. However, due to IE support requirements, I am unable to use text-stroke. https://i.sstatic.net/JwQyb.png The closest a ...

Spacing between division elements and a footer that is positioned with float property

I have searched extensively, but none of the solutions I've found address my specific issue. There is a persistent 14px gap between vertical DIV elements on my page. Even when inspecting the code, there seems to be no margin or padding causing this g ...

What would be the most optimal method for displaying this text using internationalization (i18n)?

Essentially, I want to showcase the string I am Python, Javascript and Flutter developer.. However, it appears like this: https://i.sstatic.net/PqBKq.png Using the following react component: <Typography gutterBottom variant="h6"> I am & ...

Creating a dynamic dropdown menu where the available options in one select box change based on the selection made in another

Looking at the Material-UI Stepper code, I have a requirement to create a select element with dynamic options based on the selected value in another select within the same React component. To achieve this, I wrote a switch function: function getGradeConte ...

React remains unaffected by the state

I am currently facing an issue with a form that contains radio buttons and how it is being displayed in the browser using React add-in. The main concern here is why the value attribute is empty and why the onChange function, responsible for changing the s ...

What is the method for displaying Facebook comments alongside the WordPress comments count?

I am currently using a template theme that initially had WordPress comments enabled on the website. By implementing the Facebook Comments plugin, I have successfully replaced Wordpress comments with Facebook Comments. **However, there seems to be an issue ...

The array is arranged properly, yet React is failing to render it in the correct order

Here's the code snippet I am working with: { this.state.rows.map((qc) => qc.BinsByDayByOrchardsQCs.map((qc2) => qc2.BinsByDayByOrchardsQCsDefects.map((qc3) => !defectsArray.includes(qc3.Defect) &am ...

The Role of Polling in Redux Actions

I've been working on implementing polling in one of my Redux actions. Below is the action function I've written. It seems to be functioning correctly, but I'm facing an issue where even after the status changes from "updating" to the data be ...

Unable to adjust dimensions with CSS width and height properties

I'm currently working on developing an online game with a login screen inspired by Paper.io. I have created the username input and play button, but there seems to be an issue with the width and height specified in the CSS file for the input field. Eve ...

MUI: The fontFamily property is not able to be overridden by nesting within the

My goal is to have different fonts for different parts of my application using theme nesting. Unfortunately, I discovered that theme nesting doesn't work when it comes to overriding fonts. In my App.js file: import React from "react"; impor ...

Issue with Font Awesome (6.2) Duotone not displaying correctly in Bootstrap 5 breadcrumb divider

I attempted to modify the Bootstrap 5.2 breadcrumb divider and include a Font Awesome Duotone icon, but it is not displaying. I am unable to figure out what the issue might be. Any suggestions would be greatly appreciated. Thank you. One interesting disco ...