Tips for dynamically styling a Styled Component with all the CSS housed in an external file

My goal is to dynamically render a Styled Component. In the past, it was simple because all styling was contained within the component itself. However, I now strive to maintain a separation of concerns by storing the CSS in an external file. While this approach works well for most cases, I am facing a challenge when trying to dynamically change the font color based on props. Despite my efforts to find a solution, I have not been successful.

Below is the component that requires dynamic font color:

<Styled.HeroHeadline as="h4">
  {parse(el.header)}
</Styled.HeroHeadline>

And here is the styles file where I define the CSS:

export const HeroHeadline = styled(Heading)`
  p,
  div,
  span,
  h4,
  h3 {
    display: block;
    color: {dynamicProp}

    @media (min-width: 992px) {
      font-size: var(--font-size-h1);
      letter-spacing: var(--letter-spacing-100);
      line-height: var(--line-height-h1);
      margin: 0;
      text-align: center;
    }
  }
`

If anyone has any insights or suggestions, I would greatly appreciate it. Thank you!

I attempted to find a way to store the prop value in the external CSS file but encountered difficulties in doing so.

I also conducted a search to see if others had faced similar challenges but came up empty-handed.

Answer №1

Take a look at the CSSStyleDeclaration interface.

You can achieve things like this:

element.style.backgroundColor = "red"

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

Utilizing JavaScript in AJAX Responses

Can I include JavaScript in an AJAX response and run it, or should I only use JSON or plain HTML for a more elegant solution? I'm trying to figure out the best way to handle AJAX requests that involve inserting HTML or running JavaScript based on user ...

Tips for organizing and distributing a Pusher instance efficiently

After researching, I discovered that it's not advisable to use multiple instances of Pusher. To address this issue, I've implemented React's context feature. import React, { useContext, useEffect, useRef } from "react"; import Push ...

An uncaught runtime error has occurred: TypeError - subSector.map is not a valid function

I'm encountering a challenge when attempting to map through JSON data retrieved from a fictitious API. The process works smoothly when there is more than one data item, but I encounter an error when there is only a single object. Below is the code sn ...

I am in need of a JavaScript event that will take precedence over the CSS pointer

There is an image causing issues with scrolling on a mobile device because it's located in the finger-friendly area where people tend to scroll. I attempted to use pointer-events: null; to allow scrolling, but this also prevented the click event from ...

Real-time data feeds straight from JSON

Currently, I have a JSON file that is generated dynamically and it contains match information along with a unique id. This JSON data is categorized into live, upcoming, and recent arrays. Being new to Javascript, I am unsure about the best approach to crea ...

In Internet Explorer 9, the cursor unexpectedly moves up above the element

Within my MVC3 application, I have implemented a feature to change the cursor when hovering over an element. Below is the JavaScript code that accomplishes this: $('#print').hover(function () { var cursorUrl = 'url(@Url.Content("~/Cont ...

The proper way to eliminate padding from Components

Trying to nest a List inside either a Card or an Expansion Panel has proven challenging due to the unnecessary padding added by both components. The end result looks quite awkward... <Card> <CardHeader title="Title" subheader="Subheader"/> ...

Embedding a picture logo within a CSS-designed content box

I am trying to create a unique layout for my website that reveals content using labelled radio buttons, without the use of scripts. Instead of using <a href='#'>, I want to achieve a specific layout like the one shown in this example. When ...

What is the process for bringing in a reducer from createSlice into store.js using @reduxjs/toolkit?

Can someone please provide guidance on how to correctly export the todos reducer from todosSlice using createSlice @reduxjs/toolkit, and then import it into the store using configureStore? Below is my todosSlice file : //todosSlice file import {cr ...

What is the CSS technique to make text wrap around an image, similar to the layout seen in certain newspapers?

I am looking for a way to have my images wrap with text align in this specific layout: <html> text text text text text <br> text text text text text <br> ______________ text text text <br> image | text text text <br> i ...

Creating a sleek and functional dashboard using HTML and leveraging the power of Bootstrap 4

I'm working on styling my HTML dashboard with Bootstrap 4. I want the text to be big and have minimal white space, but not too cluttered. <html> <head> <meta charset="utf-8"> <meta name="viewport" conte ...

Using Promise.map inside another Promise.map

Attempting to return a JSON object via Bluebird's Promise.mapSeries/Promise.map nested within another Promise.mapSeries/Promise.map is proving difficult. Below is the code snippet for the function: function getMovieDetails(link){ return new Promise(f ...

What is the best way to display a loader when utilizing AJAX with jQuery?

I am having trouble implementing a loader in my ajax jQuery call. My goal is to display a loader while the ajax request is fetching data from an API and hide it once the request is completed. I have split this functionality into 2 separate JavaScript fil ...

When the span element's height is set to 100%, it does not automatically match the height of its parent div

I have a scenario where I am working with two div elements and a span element in between. The parent div element does not have a defined height, it is calculated based on the content within the div. However, the span element's height is set to 100% ev ...

Issue with CSS Media Queries: Preventing an Element From Being Affected

As a complete beginner in HTML and CSS, I have a question that may seem silly. Recently, I created this HTML code: * { margin: 0px; padding: 0; box-sizing: border-box; } p { font-size: 14px; font-family: 'Work Sans', 'sans seri ...

Struggling to accurately differentiate between a CSS and Bootstrap code

I'm attempting to create a visually appealing underline under the active tab, but I am facing challenges in targeting the specific class. Since I am utilizing bootstrap and unable to adjust the width and margin of text-decoration or recognize an eleme ...

What is the best way to position a component below another component in terms of styling?

Struggling with setting up my React web app using Material-UI. The drawer component is not fitting nicely under the app bar as expected. Despite applying classNames and checking in the console, the styles are not being applied correctly. Currently, the dra ...

Encountering a syntax error while attempting to import modules from amCharts

Recently, I have been attempting to incorporate amcharts into my project using npm. After running the command npm install@amcharts/amcharts4, I noticed that all the necessary modules were now present in my node_modules folder and package.json file. Specifi ...

Adding style tags dynamically to a component and then encapsulating the styles again (using Angular)

Currently, I am in the process of upgrading from AngularJS to Angular. Our app is currently a hybrid one being bundled up with webpack instead of just angular cli. Due to this setup, we have encountered issues where our css or scss files are not correctly ...

Eliminating Tailwind typography with PurgeCSS in a next.js project

Building a next.js site and utilizing specific text like the code snippet below: const defaultTheme = require('tailwindcss/defaultTheme') module.exports = { theme: { extend: { fontFamily: { sans: ['SFMono-Regular', ...