Why does #id not work in CSS but element#\#id does? Additionally, why doesn't general CSS affect element#\#id when it should?

Why isn't #hex{ } working properly across different browsers (I've tested in Edge, Chrome, and Opera but it's not working)? On the other hand, img#/#hex{} (which I discovered using Chrome Developer Tools) works perfectly everywhere.

Below is my HTML code:

<img src="https://emojipedia-us.s3.amazonaws.com/thumbs/240/apple/118/bacon_1f953.png" alt="bacon-img">
<img id="#hex" src="https://emojipedia-us.s3.dualstack.us-west-amazonaws.com/thumbs/120/apple/325/broccoli_1f966.png" alt="broccoli-img">

Here is the CSS code that isn't working, even when I've tried img#hex or simply #hex:

img{
    background-color: rgba(0, 255, 0, 0.445);
    border-radius: 20px;
}

img:hover{
    background-color: transparent;
    border-radius: 25px;

 
#hex{
    background-color: rgba(255, 0, 0, 0.445) ;
    border-radius: 10px;
}

Here is the CSS code that partially works, as the hover effect doesn't work on img#\#hex. I'm unsure how img#\#hex functions or why it works:

img{
    background-color: rgba(0, 255, 0, 0.445);
    border-radius: 20px;
}

img:hover{
    background-color: transparent;
    border-radius: 25px;

 
img#\#hex {
    background-color: rgba(238, 255, 0, 0.445) ;
    border-radius: 10px;
}

Image 1 before hovering: enter image description here

Image 2 when hovering over the first image: enter image description here

Image 3 when hovering over the second image: enter image description here

Did I make a mistake somewhere in my code or is there an issue with my code? Any feedback would be appreciated. Thank you in advance!

I expected :hover to work with img#\#hex and provide a transparent background upon hovering!

Answer №1

# within css serves as a selector for ID. To resolve the issue in your code, adjust the ID of the image to hex.

Moreover, ensure to make the necessary modifications to rectify your css selectors

<img id="hex" src="https://emojipedia-us.s3.dualstack.us-west-amazonaws.com/thumbs/120/apple/325/broccoli_1f966.png" alt="broccoli-img">

Furthermore, consider eliminating the img selector since the id should be unique, rendering the img part redundant.

#hex {
    background-color: rgba(238, 255, 0, 0.445) ;
    border-radius: 10px;
}

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

Searching for an attribute with no value in an HTML tag using Selenium: A step-by-step guide

Example: Radio - selected <input id="rdo" type="radio" name="nrdo" value="1" checked> Example: Radio - not selected <input id="rdo" type="radio" name="nrdo" value="1"> Searching for attributes without values in HTML tags using Selenium. ...

implement adding a division element to the DOM using the append

I am facing an issue with this particular code. The intended functionality is to create multiple divs in a loop, but it seems to be dysfunctional at the moment. Upon clicking, only one div appears and subsequent clicks don't trigger any response. < ...

Display a jQuery loading window

Need help with displaying a loading div when making an ajax post request. I've tried the following code but it's not working. Can someone please assist in resolving this issue? $(document).ready(function() { $('a.pagerlink').click( ...

Looking to include a badge within the nebular menu

Can someone assist me with adding a badge to the Nebular menu to display the inbox count dynamically? Any help would be greatly appreciated. Thanks! import { NbMenuItem } from '@nebular/theme'; export const MENU_ITEMS: NbMenuItem[] = [ { ti ...

Css flex is not compatible with Safari, iPhone 5, and some mobile web browsers' webviews

All of the following code snippets are written in HTML: <div class="btn-toolbar text-center" role="toolbar"> <a href="#" class="btn btn-success">Link 1</a> <a href="#" class="btn btn-success">Link 11</a> < ...

When adding text to a React Material UI Box, it can sometimes extend beyond the boundaries

I'm currently working on a straightforward React page, but I've encountered an issue right from the start: my text is overflowing the box and I can't figure out why. Could someone take a look at my styles? Here's a picture showing the ...

What steps can be taken to enable automatic playback for this slider?

After downloading the slider from this site, I've been attempting to enable autoplay on this slider but have not had any success. Can anyone provide guidance on how I can achieve this? I've tried modifying the code below without achieving the des ...

Link an HTML contact form to an Express.js backend server

Hey there, I'm a newcomer to web development and I've been working on creating the backend code for a contact form, but it keeps throwing errors. Here's my HTML code for the contact form: </div> <div class="row"> ...

Having issues with customizing the design of MaterialUI Accordion header

Link to my code sandbox project I am encountering issues with the styling in my Accordion Heading. My Accordion Contents are set up like this: <Accordion heading1= { <DishItem name="Döner Teller Spezial" price="13,60€"/> ...

Having trouble with margin auto 0 not functioning properly in Safari on iPad?

I'm facing a challenge with centering a div in Safari on iPad. It works fine in Chrome, but in Safari there are margins on the left and right which require scrolling to view the whole content. Any suggestions on how to make this work seamlessly in bot ...

Navigating through the img src using JavaScript

Currently, I am working on a task that involves the following code snippet: <input type="file" id="uploadImage" name="image" /> <input type="submit" id="ImageName" name="submit" value="Submit"> My goal is to have the path of the selected imag ...

Finding attributes with spaces in their values using XPath

Is there a way to select an attribute with spaces in xpath? I am currently trying to select a checkbox attribute named "checked type". I have experimented with //[@checked type], //[@checked-type], //[@checked_type], and //[@checked\stype], but none ...

H1 and span elements experiencing abnormal size discrepancies

Check out this code snippet: <h1><a href="">Windows App Store<br /><span class="smallSubText">applications</span></a></h1> and here's the CSS styling: #windowsStoreApplications { float: right; width ...

Control the line height in DataTables

Is there a way to adjust the line height for a tr using either DataTables settings or CSS? I've attempted different methods, but nothing seems to change the line-height. https://i.sstatic.net/GwFaD.png Table CSS ...

Obtaining IDs of Divs that have been Dragged into a Drop Zone upon Clicking a Button using JavaScript

I'm currently working on a solution to extract the ids of dragged divs once they have been placed in the drop zone. Each drag component is assigned an id ranging from drag1 through drag8, while the drop zone is labeled as div drop zone. Since there a ...

How can I modify the color of a div when a validation error occurs?

I have recently completed a contact form with validation using the constraint validation api. Although the files are functioning as intended, I am curious if there is a method to make the error boxes turn red when an error occurs and white (or hidden) when ...

Is there a way to incorporate my personalized styling and HTML code into Django forms?

I've been struggling to customize form styling in Django. I want to exclusively use my own styles for the forms. How can I achieve this? <form id="contact" action="" method="post"> <div class="row"> ...

Database query results displayed in a vertical scroll block

I'm facing an issue with the placement of a DIV tag on my page. I have a large number of rows being returned from a database query, and I can't seem to get the DIV tag in the right location. When I put the DIV tag outside the while loop, it appea ...

Streamlining programming by utilizing localStorage

Is there a more efficient way to streamline this process without hard-coding the entire structure? While attempting to store user inputs into localStorage with a for loop in my JavaScript, I encountered an error message: CreateEvent.js:72 Uncaught TypeErr ...

Get tailored components from a table using Selenium in Python

Currently, I am facing an issue with my code while attempting to extract specific data from a table on a device's web server. The error seems to be related to finding a particular element in a column of the table using what appears to be an invalid ex ...