It is not possible to adjust the font size of <h1> element within a component in React

class App extends Component {

    constructor() {
        super()
        this.state = {   //state is what decribes our app
            robot: robot,
            searchfield: ''
        }
    } 

    onSearchChange = (event) =>  {
        this.setState({ searchfield: event.target.value })       
        console.log(this.state.robot);
    }  

    render() {
        const filteredRobots = this.state.robot.filter( robot => {
            return robot.name.toLowerCase().includes(this.state.searchfield.toLowerCase()); 
            })
        return(
        <div className='tc'>
            <h1>ROBOFRIENDS</h1>
            <SearchBox searchChange={ this.onSearchChange } />
            <CardList robot = { filteredRobots }/>  
        </div>
        );
    }
}

I'm struggling to increase the font size of the title ROBOFRIENDS. I have attempted creating a separate CSS file for modifying h1 and also tried

<h1 className="style:{fontSize=3em}">ROBOFRIENDS</h1>

However, these methods have not yielded the desired result. Strangely, changing the font color and background color using the same method has been successful! If anyone could assist me with solving this issue, it would be greatly appreciated.

Answer №1

When it comes to styling elements, the className prop cannot be used for adding styles directly. There are two alternative approaches:

  • Assign a specific className to the element and define the corresponding style in a CSS file.
  • Alternatively, you can use the style property inline like so: style={{ color: 'red' }}

Answer №2

Personally, I have a preference for the second option:

style={{ fontSize: '3em' }}

This allows you to easily pass variables to the styling.

However, my top choice is utilizing styled-components. https://styled-components.com/

You should definitely check it out as it offers a clean and efficient way to style in react.js while promoting reusability of styles.

Answer №3

There are various methods available. One effective approach is to integrate 'styled-components' into your project. Below is a sample showcasing how you can implement it:-

import styled from 'styled-components';

export const StyledHeading = styled.h1`
    font-size: 3em;
`;


class App extends Component {

    constructor() {
        super()
        this.state = {
            robot: robot,
            searchfield: ''
        }
    } 

    onSearchChange = (event) =>  {
        this.setState({ searchfield: event.target.value })       
        console.log(this.state.robot);
    }  

    render() {
        const filteredRobots = this.state.robot.filter( robot => {
            return robot.name.toLowerCase().includes(this.state.searchfield.toLowerCase()); 
            })
        return(
        <div className='tc'>
            <StyledHeading>ROBOFRIENDS</StyledHeading>
            <SearchBox searchChange={ this.onSearchChange } />
            <CardList robot = { filteredRobots }/>  
        </div>
        );
    }
}

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

Warning: MaxListenersExceededNotification may occur during the installation or creation of Node.js projects on macOS

Encountering a warning message while attempting to set up or generate Node.js projects on macOS (darwin): (node:80101) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 close listeners added to [TLSSocket]. Use emitter.setMaxList ...

Transform a log file into a JSON structure

In my log file titled request.log, the following entries are present: [2022-06-30T09:56:40.146Z] ### POST https://test.csdf/auth/send_otp { "method": "POST", "headers": { "User-Agent": "testing&q ...

Is there a way to effectively utilize meta tags with dynamic values in react js?

Does anyone have knowledge on using dynamic values in meta tags with react js? I need help visualizing my requirements, please refer to the accompanying image. In order to meet React's requirement of wrapping complete HTML inside a single tag, I am ...

Creating dynamic web applications using Node JS and Angular

After setting up Node JS and importing an existing project into Visual Studio Code, I proceeded to execute npm install in the project directory followed by npm start, which successfully launched the application. However, when attempting to run an ng comman ...

How to vertically align and center multiple divs with Flexbox programming

I have been grappling with the challenge of aligning two sets of rows vertically, each consisting of an image on one side and text on the other. While I usually use flex to achieve vertical alignment, it seems that a different approach is needed in this pa ...

Is there a way to dynamically compute the height of rows in a VariableSizeList based on their index?

Is there a method to dynamically calculate the height of rows in React using the react-window library? Since it's uncertain whether all rows will have the same size, I find myself needing to utilize VariableSizeList. However, I'm wondering if the ...

The benefits of NEXTJS Server Side Rendering include providing clear and defined content

Even though I've followed the solution provided above for fetching data, I still end up receiving 'undefined' instead of the actual data. Interestingly, when I make the same request using Postman or Thunderclient, it returns a success with s ...

The setter function for a boolean value in React's useState hook is malfunctioning

I encountered an issue while trying to update a state value using its setter from the useState hook. Surprisingly, the same setter worked perfectly in a different function where it set the value to true. To confirm that the function call was correct, I te ...

What is the mechanism by which the useState hook in React determines the calling context?

After transitioning from using class components to functional components in React, I delved into the documentation with keen interest to understand how the useState hook functions. Upon consulting the FAQ page, it was explained that each component has an ...

Executing the Docusaurus command to publish to Github Pages

Seeking assistance for a Windows user! I've tried various tutorials, but none have resolved my issue. Whenever I attempt to use the publish command in Git Bash, I encounter the following error: I am running Windows 10 and have tried both Github Desk ...

Best practices for managing backend errors with Next.js 14

Currently, I am developing a project in Next.js 14 and I have set up my API requests using fetch within a handler.tsx file as shown below: async function getPositions() { const response = await fetch( process.env.BASE_API_URL + "/positions?enabl ...

Node.js encountered an error: Module "express" not found

I just created my first node.js application, but I'm having trouble finding the express library: C:\ChatServer\Server>node server.js module.js:340 throw err; ^ Error: Cannot find module 'express' at Function. ...

The definition of Sequelize.op is not found

Hey there! I'm currently utilizing Sequelize in conjunction with Node.js, and I've encountered an issue while attempting to utilize the Sequelize.op request. Below is a snippet of my code: var Sequelize = require('sequelize'); const Op ...

Guide to dividing a URL in reactjs/nextjs

Here is the complete URL: /search-results?query=home+floor&categories=All+Categories. I am looking to separate it into two sections - /search-results and query=home+floor&categories=All+Categories. My objective is to extract the second part of t ...

What could be causing a syntax error when I use `npm run start` with npm react-scripts?

After months of working on a full stack React app, I encountered an unexpected error when trying to run npm run start from the command line. The error message was as follows: // npm run start > [email protected] start /Users/eden/Documents/GitH ...

The comparison between importing TypeScript and ES2015 modules

I am currently facing an issue with TypeScript not recognizing the "default export" of react. Previously, in my JavaScript files, I used: import React from 'react'; import ReactDOM from 'react-dom'; However, in TypeScript, I found tha ...

When exporting a custom ES6 module and importing it into a different local project, you may encounter unexpected outputs such as being undefined or

Currently, I am using TypeScript 3.4.5 and Webpack 4.32.2 on Windows 10 via WSL. My goal is to create a local package of tools that consolidates basic classes into an index file for exporting. However, when I try to import these classes into other project ...

Attempting to display four videos in my application by making an API request to YouTube

import React, { useState, useEffect } from 'react'; Grabbing my custom hooks function useFetch(url, defaultResponse) { const [data, setData] = useState(defaultResponse); async function getDataFromAPI(url) { try { ...

What is the best way to implement Media Queries in the Next.js application Router?

I am currently working with Next.js 13 and the App Router. Within my client component, I have implemented media queries in JavaScript to customize sidebar display for small and large screens. "use client"; export default function Feed() { co ...

Trigger a function post-rendering in a React component

Hey everyone, hope you're having a great day! I've been diving into React for a few months now. I'm making an effort to steer clear of using the traditional React Components, opting instead for React Hooks. However, there are instances wher ...