How can I customize the color of the Title Component in Ant Design using Styled Components?

I am currently integrating Ant Design with styled components in my project.

One of the components in my application is a NavBar that includes an H1 Component.

H1.tsx

import React from 'react';
import { Typography } from 'antd';
import styled from 'styled-components';

const { Title } = Typography;

const TypographyWrapper = styled.div`
  font-family: 'Muli', sans-serif;
`;

interface H1Props {
  children?: String;
}

export const H1: React.SFC<H1Props> = ({ children, ...rest }) => {
  return (
    <TypographyWrapper>
      <Title style={{ margin: '0px' }} {...rest}>
        {children}
      </Title>
    </TypographyWrapper>
  );
};

export default H1;

Navbar.tsx

import React from 'react';
import { primary, monochrome } from '../colors/Colors';
import styled from 'styled-components';
import { NavbarConstants } from '../../config/stringConstants';
import H1 from '../typography/H1';

const Wrapper = styled.div`
  background-color: ${primary['Blue-400']}
  width: 100%;
  display: flex;
  flex-direction: row;
  padding: 30px;
  align-items: center;
`;

const StyledH1 = styled(H1)`
  color: ${monochrome.offWhite};
`;

interface NavbarProps {}

export const Navbar: React.SFC<NavbarProps> = () => {
  return (
    <Wrapper>
      <StyledH1>{NavbarConstants.NAVBAR_TITLE}</StyledH1>
    </Wrapper>
  );
};

I'm trying to customize the color of the text in the H1 component, specifically color: ${monochrome.offWhite}, but I'm having trouble selecting and targeting the component.

In the console, it seems to be overridden by

h1.ant-typography, .ant-typography h1
. I've experimented with different selectors to target it, but have been unsuccessful so far.

How can I successfully change the text color using styled components?

Answer №1

Your current styles are being overridden by Ant Design styles. To fix this, try adding a more specific style to override them.

const CustomH1 = styled(H1)`
  &.ant-typography {
    color: ${monochrome.offWhite};
  }
`;

Answer №2

To avoid Antd overriding the color property, remember to apply the important rule (e.g., color: red !important).

When using tailwindcss, applying the important rule is simple and can be done on the fly as shown in the example below:

<Title className="!text-red-800">Title Header</Title>

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

Creating fluid designs for my boxes with CSS to enhance responsiveness

I have designed my HTML file with 8 boxes that appear correctly on desktop, but when viewed on mobile devices, the columns are misaligned and shifted to the right side instead of being centered. How can I make it more responsive? See Preview Here is the ...

What could be the reason behind the frequent occurrence of the "ECONNRESET" error in React?

While attempting to create a React app using 'npx create-react-app my-app', I encountered an npm ECONNRESET error. Being new to ReactJS, I am unsure of how to resolve this issue and would appreciate any assistance provided. I have decided to seek ...

What causes error TS2339 to occur: The property 'classList' is not found on type 'never'

Currently, I am working on creating a basic component called "BackToTop" const BackToTop: React.FC = () => { const bttEl = useRef(null); function scrollHandler(): void { var bttHtmlEl: HTMLElement | null = bttEl.current; if (bttH ...

The feature for favoriting or unfavorite a post is currently not functioning correctly on the frontend (react) side

I have been working on a social media website project for practice, and I successfully implemented the liking and disliking posts feature. However, I encountered an issue where when I like a post and the icon changes to a filled icon, upon refreshing the p ...

Redux: Unhandled promise rejection TypeError - Unable to access property 'props' as it is undefined

Currently in the process of learning react and redux with the aim to fetch data from a server and display it on the webpage. However, encountering an error in the process. Any ideas on what might be going wrong? index.js import 'babel-polyfill& ...

Unable to place an absolutely positioned Div within relative parent containers

After applying the id "enterGame" with position: absolute, I noticed that the div disappears. Even when I tried adding position: relative to the body or html tag, the div remained invisible. The relevant code snippet is as follows: html { height: 10 ...

Is there a way to prevent the conversion of .json/.webmanifest URLs into base64 strings?

I've been grappling with an issue for quite some time now. In my VSCode vite-react-typescript project, I have a link in the index.html file pointing to a webmanifest, which is essentially a json file with a different extension. After building my app, ...

discovered a total of 63 minor vulnerabilities during the npm dependencies installation process

pm WARN [email protected] is in need of typescript@* as a peer dependency, however it has not been installed. It is necessary to manually install the required peer dependencies. ...

Resizing the Vue page to fit the viewport and using Flexbox to anchor the footer to the bottom

My Vue.js page structure consists of a navigation bar, main body content, and a footer: <template> <div class="flex_container"> <div class="container_navigation"> <nav-bar /> </div> < ...

adjust the width of an element based on the size of characters within it

I need to ensure that the width of a div is equivalent to 10 characters If one character is equal to 2px, then I want the width to be 20px, even if the text is only 4 characters long. <div class="row"> <div class="key">City</div> ...

Display a div using Jquery depending on the visibility and checked value of another div

Trying to toggle the visibility of a div based on another div's input has proven to be quite challenging in this scenario. Here is the HTML setup: <div id="main-question"> <div class="form-group"> <div class="form-radios"> ...

How a Dynamic Icon Component in NextJS/React can disrupt Jest testing

Hello there! I'm a new member of this community, and usually I can find answers to my questions by searching. However, this time around, I need some help. My Current Setup I am using NextJS solely as a framework application without utilizing its API ...

Generating divs dynamically with inline styling and utilizing ngFor, while incorporating a conditional check with ngIf in Angular

Currently, I have an Angular component where I am dynamically generating div elements using the ngFor directive. However, I also need to validate a condition before creating each div, and I'm facing challenges when trying to use both ngFor and ngIf in ...

Retrieving contextual data from a hook

Currently, I am facing an issue while trying to utilize the Context API in conjunction with a React hook. This is how I establish my Context: import React from 'react' export const DecimalSeparatorContext = React.createContext('.') ...

Properly arrange the positioning of floating HTML elements

I am currently using the float property to structure the layout. <style> div { float: left; } .pro { width : 100px; color : blue; } </style> <span> <div class="pro">Long property name : </div> <div>value&l ...

Issues with connecting static files in express.js

Hey there! I'm having an issue with the redirect not working when I click the "Submit" button on the login page. The code in my index.js file looks like this: app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static(path.join(__dir ...

Is it advisable to combine/minimize JS/CSS that have already been minimized? If the answer is yes, what is

Our app currently relies on multiple JS library dependencies that have already been minified. We are considering consolidating them into a single file to streamline the downloading process for the browser. After exploring options like Google Closure Compi ...

Floating CSS drop menu with added bottom margin

I have created a simple dropdown CSS menu positioned in the footer with an upwards direction. You can view it here: http://jsfiddle.net/RmTpc/11/ My issue is that I want the opened menu to have some bottom margin from the main list item ("Items"). However ...

Tips for increasing the size of a parent div when a child div is displayed with a set width?

Is there a way to make the parent div automatically expand when the child div with a fixed width is displayed onclick? Goal: I want the child div to show up when I click the link, and at the same time, I need the parent div to expand or scale to fit the ...

Attempting to divide the main page into quadrants

I want to split my homepage into four equal images without any scrolling. When you click on the website, I want the four images to expand and cover the entire screen with no scrollbars. I've made some progress with my code, but it's not quite the ...