How come the styles in my CSS file aren't being applied to my images based on their class or ID?

When I apply a className or id to my img tag in my JavaScript (using React.js) and then add a style that references that id or class, the style does not get applied. However, when I do the same for a div, everything works perfectly fine.

<--JavaScript-->

import React from "react";
import TitleImage from './Images/TitleBG.png'
import TitleImage2 from './Images/TitleMG.png'
import TitleImage3 from './Images/TitleCG.png'
import './header.css'; 

const TopHeading = () => {

    return(

        //This is a div with the ID "TopHeading", and the style is applied correctly.

        <div id="TopHeading">

            {/*The image with class "image1" does not have its style applied.*/}

            <img className="image1" alt="Dofus" src={TitleImage}/>
            <img width='auto' alt = "Memory Game" height='50px' src={TitleImage2}/>
            <img width='auto' alt = "Character" height='50px' src={TitleImage3}/>
            <div className="aDiv"></div>
        </div>
    );
};

<--CSS-->

#TopHeading{
    display: grid;
    align-items: center;
    justify-items: center;
  
    background-color: rgb(131, 192, 131);
    height: 300px;
};

.image1{
    border-style: solid;
    width: auto; 
    height:250px;
}

.aDiv{
    size: 50px;
    border-style: solid;

The style of the ID gets properly applied to the div.

https://i.stack.imgur.com/prMWX.png

However, the style of the image with the class is somehow ignored:

https://i.stack.imgur.com/jcFyF.png

Answer №1

The mistake in your CSS code is the presence of a semicolon before the class rule, which renders the selector invalid. Remember to only use semicolons inside curly brackets { }.

This incorrect code snippet:

;
div
{
  height: 50px;
  width: 50px;
  background: red;
}
<div></div>

Should be corrected to:

div
{
  height: 50px;
  width: 50px;
  background: red;
}
<div></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

Experiencing issues with running Jest tests and encountering a failed status?

As I attempt to write my first test in the app, I encounter an error message: ● Login component tests › should have 3 input fields ! expect(received).toHaveLength(expected) Expected length: 3 Received length: 0 Received object: {} 13 | 14 | ...

What is the procedure for making the menu background color transparent?

I need help changing the background color to transparent on my menu in desktop view. ul.dropdown-menu li { background-color: transparent !important; } <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul id="menu" class="menu"> ...

Cannot transmit messages using Dotnet WebSocket, only able to receive them

I'm working on a dotnet core application with web sockets. Here is the code I have written for the web socket functionality: var builder = WebApplication.CreateBuilder(args); IConfiguration configuration = builder.Configuration; builder.Services.AddCo ...

Using React's useEffect and useContext can cause issues with certain components, particularly when dealing with dynamic routes

Currently, I am developing a React blog application where posts are stored in markdown files along with metadata in Firestore. The content .md files are saved in Cloud Storage. In the App component, I utilize useEffect to retrieve the metadata for each pos ...

The onValue event in Firebase is not triggering, and I am unable to determine the cause

I am currently working on a web3 application using Next/Js and Firebase. The concept is for users to connect with their Solana wallet, list their NFTs, and choose one to connect in a game-container. My challenge lies in retrieving information from all con ...

How to interrupt a JQuery animation and restart it from the middle?

Currently, I am tackling my first JQuery project and facing a challenge. As the user mouseleaves the .container, the animation does not reset but continues as if they are still within it. My goal is to have the animation revert in reverse if the user decid ...

Zod: ensure at least one field meets the necessary criteria

Currently, I am in the process of developing a form that allows users to input either their email address or phone number. While they have the option to provide both, they are required to enter both before proceeding. For this project, I am utilizing Zod a ...

Create a collection of boxes using THREE.js and save them in a 3D array

My latest project involves rendering a 16x16 grid of boxes in THREE.js using custom code. const drawGroup = () => { const blockSize = 16 // Positioning for (let x = 0; x < blockSize; x++) { for (let y = 0; y < blockSize; y++) ...

Using an arbitrary object as an argument in a CoffeeScript anonymous function

When I execute the below Coffeescript code: @total = (a, b) -> a + b The resulting compiled Javascript is: (function() { this.total = function(a, b) { return a + b; }; }).call(this); Is there a method in Coffeescript to substitute ...

Why is my snapshot returning null, even though there are values in the Firebase Database?

I am currently facing an issue in my code related to the snapshot. Specifically, I am trying to retrieve the value of quantity from my Firebase Database. Here's a snapshot of my database: and firebase.database().ref("mycart/"+uid+"/"+imguid).once ...

When links in React are placed in the center of a Material-ui Grid, they become unresponsive

Struggling with material-ui's grid system and react-router-dom's Link component inside the grid, especially when trying to center the link horizontally. It's frustrating: Importing Grid and Link: import { Link } from 'react-router-dom& ...

Is there a way to prevent a web page from refreshing automatically for a defined duration using programming techniques?

I am currently working on a specific mobile wireframe web page that includes a timer for users to answer a question after the web content body has loaded. There are two possible outcomes when answering the question: 1) If the user fails to answer in time, ...

Facing difficulties in Angular 8 while trying to import firestore and firebase for an authentication system

While attempting to implement Firestore/Firebase functionalities for Google OAuth signin, I encountered an error indicating that Firebase is not imported: https://i.sstatic.net/oL4rY.png CODE: ERROR in node_modules/@angular/fire/auth/auth.d.ts:4:28 - er ...

The package is warning that it needs react version 16.4.0 or higher to be installed as a peer dependency, but

Upon trying to execute a project downloaded from github, I encountered some warnings while running npm install. One of the warnings displayed was: npm WARN <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="74150c011d59101500151306 ...

Leveraging react-query with next-mdx-remote MDX Components?

I have been using next-mdx-remote in my next.js project. One of the components I've passed to it makes API calls using axios, which has been working well. However, I now want to switch to using react-query. When implementing this change, I encountered ...

Using a class variable to access canvas methods in Javascript

As someone new to Javascript, I am facing a bit of confusion when it comes to classes and objects. Recently, I refactored some code into a working class but the process did not go as smoothly as I had hoped. Despite searching through Stackoverflow, Google ...

I am unable to retrieve the local variable beyond the function

I'm having trouble accessing a local variable outside of a function even though I have initialized it before the function is called. <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jq ...

Having trouble accessing an AngularJS $scope variable because it is coming up as undefined

Currently, I am developing an Electron application using AngularJS for the frontend. Since node.js operates at the OS level while Angular runs at the client level, communication between the two is achieved through IPC (Inter-Process Communication) implemen ...

Building a WordPress calculator form that retains user input without requiring a resubmit and incorporates custom field values

Currently tackling a challenge on my Wordpress website. Without any code yet (after numerous attempts at rewriting 4 different forms), I'll simply outline what I aim to accomplish, confident it's a straightforward task with something crucial elud ...

"Learn the process of customizing the border color of a drop-down menu using

Is there a way to add validation to a drop-down menu so that the border color turns red if an option is not selected? $("#MainContent_ddlSuppliers option:selected'").css("border", "1px solid #F78181"); ...