What could be causing the image's width not to adjust when I apply "width: 8%;" in CSS?

Recently delving into the world of react, I set out to create a basic website featuring music notes (represented by simple images) that would change color upon hovering over them. While aware of the traditional :hover method, I decided to challenge myself and practice using useState instead. After much trial and error, I was able to implement the toggle effect successfully for changing colors on hover. However, during this process, I encountered an unexpected issue with the width being disrupted. Although all other CSS properties like position and color are functioning as intended, the width remains unchanged from its original state. Currently, only note3 has the toggle feature applied since it was the note I focused on testing.

The following code snippet is a segment from my index.js file showcasing the specific music note in question.

const Body = () => {

    const [isNote3, setNote3] = useState("true");
    const ToggleNote3 = () =>{
        setNote3(!isNote3);
    }

    const [isB1, setB1] = useState("true");
    const ToggleB1 = () =>{
        setB1(!isB1);
    }

    return (
        <div>
            
                <div className="sheetmusic">
                <img className="sheet" src={sheetmusic} />
                </div>
                

            <div className="notes">
               
                <div className={isNote3 ? "note3" : "n3"}>
                <img onMouseEnter={ToggleNote3 } src={note1} /> 
                </div>
        
            </div>
           
        </div>
    )
}

The subsequent snippet presents the pertinent CSS styles corresponding to note3.

.n3{
    filter: invert(100%) sepia(26%) saturate(5791%) hue-rotate(58deg) brightness(116%) contrast(101%); 
    left: 25%;
    position: absolute;
    max-width: 8%;
    max-height: auto;
    top: 30%;
}

.note3 {
    position: absolute;
    left: 25%;
    max-width: 8%;
    max-height: auto;
    top: 30%;
 }

Moreover, a visual representation of the current status on my webpage can be viewed here (the larger note currently toggles). 3

I've spent time experimenting with different approaches but have not been able to pinpoint the root cause of the problem. Any assistance or insight would be immensely appreciated. Thank you in advance.

Answer №1

Based on the CSS code you provided, both classes note3 and n3 share the same value for max-width, so there should not be any change in width. Consider assigning different values to see a difference.

Additionally, keep in mind that in CSS, image width and height are typically set to auto by default. To ensure all images stay within the parent container's width, include img { max-width: 100%; } in your styles. You can refer to this example for more clarity: https://codesandbox.io/s/relaxed-mcnulty-p72by?file=/src/styles.css

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

Issues with utilizing the Framer Motion Library hooks in conjunction with Reagent

One interesting feature of framer motion is the ability to utilize the path tag in conjunction with useViewportScroll to create a scroll info path. const { scrollYProgress } = useViewportScroll() return ( <motion.path style={{ pathLength: scrollYProg ...

What is the best way to include keys in my JSON data, rather than just values?

I've encountered an issue with the PrimeVue datatable I created, as it is only showing empty rows. After investigating, I believe the problem lies in my JSON data structure, where the fields do not have keys. What modifications should be made to stan ...

Leveraging templates for many pages in a Meteor project

Before discovering Meteor, I was developing a web app that required multiple pages. To create buttons for tasks such as signing up, I used the following code with Bootstrap: <a href="../signUp.html" class="btn btn-lg btn-default">signup</a> N ...

Parsley JS: A Solution for Distinct IDs

I have a form that contains multiple select boxes, and I need to ensure that no two select boxes have the same value selected. In simpler terms, if select box 1 is set to value 2 and select box 4 is also set to value 2, an error should be triggered. While ...

The toggling feature seems to be malfunctioning as the div section fails to display

I'm facing an issue with my Django project while working on a template. I want to toggle the visibility of a div element between hiding and showing, but the function I used isn't working for some reason. I borrowed the function from a different t ...

Having trouble getting the height to work properly on your Blogger Blogspot template?

Issue with embed height=100% not working <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> <head> & ...

Modify a particular entry that is being looped through in PHP

Currently, I have coded the following: <div class="row"> <div class="col-lg-12"> <table id="usertable" class="table table-bordered table-hover text-center"> <thead> <th class="col-lg-1 text-center">User ID</th> ...

Create a new URL using `window.URL.createObjectURL` and specify the filename to open the PDF

Is there a way to customize the filename in createObjectURL of the blob? Currently, it generates a URL like this: <URL>/bfefe410-8d9c-4883-86c5-d76c50a24a1d const data = window.URL.createObjectURL(newBlob); const pdfWindow = window.open(); pdfWindo ...

Incorporating a popover onto a button within an Angular-centric project

Having recently delved into Angular, I decided to create a basic page showcasing details of a legal case. To enhance the visual appeal, I incorporated Angular Material library for its attractive icons, which I intended to display alongside plaintiff and se ...

Text woven seamlessly with a series of different scenes

After contemplating the use of three.js for incorporating 3D visualization into my Sphinx extension for model visualization, I realized that the current tool I am using is outdated and not being maintained. The three.js library offers a variety of example ...

Arrange Image and text to adapt to different screen sizes

As a beginner in CSS, I am overwhelmed by the numerous ways to position elements on a webpage. I am currently trying to achieve two different layouts: Scenario 1 : Plenty of space available [Image] this is the text that I want Scenario 2 : Limited spac ...

Accessing Stencil through a corporate proxy network

As I embark on my inaugural Stencil project, I've encountered a puzzling error message: Cannot download "https://github.com/ionic-team/stencil- component-starter/archive/master .zip" Check your internet connection Error: connect ETIMEDOUT" De ...

What's the issue with my virtual array not functioning?

Check out this example I created: http://jsfiddle.net/stephane_klein/Cgn2c/1/ I am attempting to create a "virtual" array in the "App.my_list2". Unfortunately, my code is not working as expected. Can you help me troubleshoot and fix the issue? Thank you ...

Checking to see if an object is null using Javascript/AngularJS

Below is the code snippet: function getBestBlock(size){ var bestBlock = {}; var blockEnteredTest = true; for(var i = 0; i < $scope.blocks.length; i++) { if($scope.blocks[i].state == "Free") { i ...

Showing MySQL query output in HTML using EJS

I am currently working on an application using the EJS view engine and Node.js, where I have created a MySQL database locally. Although I am able to receive query results in JSON format successfully, I am encountering an error when trying to display this i ...

Pattern matching for a string with numerous repetitions using Regular Expressions

There's a [tree] and a cat and a [dog] and a [car] too. I am looking to find the words inside each set of square brackets. The resulting array will be tree, dog, car My attempt at using match(/\[(.*)\]/g) didn't work as expected. It ...

Guide to displaying list-group elements in container 2 after being selected from container 1 in Bootstrap 4

Using bootstrap 4, I have two list-groups in separate containers placed side by side. My goal is to display items in the second container when a list item from the first container is selected. How can I achieve this? The number of rows in the right contai ...

Insert a parameter or substitute the existing value if detected

Trying to create a JavaScript regex that searches for a parameter in a URL and replaces its value if found. If the parameter is not found, it should be added to the URL. Here are a couple of scenarios: http://www.domain.com/?paramZ=123456 https://www.dom ...

Storing various data in separate tables using MySQL and Node.js

Currently, I am working with three tables - user, modules, and an intermediate table called user_module. To perform an inner join between the user and module tables, I need to access the user_module table. "SELECT user.uName, module.moduleName FROM user_m ...

The Highchart Angular directive is failing to update when receiving new data from a dynamic (ajax) data table

My AngularJs application fetches data from an API and populates a table with it. I then use Highcharts to create a chart based on the table data. Everything works fine when I use static data in the table, but it doesn't update properly when I try to c ...