When using the `:hover` pseudoclass, it appears that it does not apply to the `<p>` element, but it does work

While hovering over the <a>, I noticed that it changes color correctly. However, when I hover over the <p>, nothing seems to happen. It's puzzling why the <p> does not change color upon being hovered over.

If I switch the selector to p:hover, then the <p> responds as expected when hovered (but unfortunately, the <a> stops working). Is there a way to make both the <p> and the <a> change color while being hovered over with just one rule? And why isn't this functioning as anticipated?

<html>
    <head>
        <style>
            :hover {
                background-color: black;
                color: white;
            }
        </style>
    </head>
    <body>
        <p>some text</p>
        <a href="https://stackoverflow.com">stackoverflow</a>
    </body>
</html>

For potential future editors: This code will not showcase the issue in a Stack Snippet due to quirks mode in browsers, something that cannot be replicated within Stack Snippets

Answer №1

Your code is triggering quirks mode due to the absence of a doctype declaration. According to the quirks mode specifications:

4.1. The :active and :hover quirk

When in quirks mode, a compound selector must adhere to certain conditions in order to match elements that would also match the :any-link selector. [SELECTORS4]

  • The selector uses either the :active or :hover pseudo-classes.

  • The selector does not include a type, attribute, ID, class, other pseudo-class, or pseudo-element selectors.

  • The selector is not part of an argument within a functional pseudo-class or pseudo-element.

Since the :hover selector fulfills those specific criteria, it fails to match the intended p element.

Answer №2

After testing out the code myself, I noticed that the styles were applied to all tags causing a color change on the entire body. In order to specifically change the color of tags "a" and "p", you must explicitly specify these two tags for color changes.

p:hover,a:hover{
color: red;
background-color: blue;
}

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

When you click on a sublevel in the vertical CSS and JavaScript onclick menu, it disappears automatically

I'm a beginner when it comes to Javascript, and I'm still getting the hang of CSS/HTML. Currently, I am working on creating a vertical menu using CSS and javascript. My goal is to have the sublevels appear on the right side of the menu when someo ...

chart for visualizing two rows with matching IDs and dates

I have been attempting to accomplish the following two scenarios: 1) When both ID and dates are the same in the chart, but the descriptions differ, I want to display them on separate lines. 2) If there is not enough room to show the row label, I would li ...

Struggling with VueJS computed properties to automatically wrap text to the next line instead of using the <h4> tag

Currently, I have implemented a computed property to dynamically generate an interpolated string for rendering. Below is an example snippet from my code: <div class="productDetailContainer"> <nuxt-link :to="{ path: '/product/ ...

How can I remove the div container every time the submit button is clicked?

I am currently working on a form that is capturing values as shown below. <form role="form" id="calculate"> <div class="form-group"> <select class="form-control" id="paper"> < ...

Maximizing Efficiency: Implementing Loading Panels in AngularJS

I need a loading icon (spinner) to appear when I press the button and the request is sent from the controller to the server. It may take some time for a response, so I want the loading icon to be displayed during this process. Once the server responds to t ...

I'm struggling to locate the space that should be between these elements

After accidentally starting in quirks mode without declaring a doctype, I realized my mistake and corrected it by declaring a doctype. However, there is a persistent issue with a space between .car-box-img and car-box that I can't seem to locate in t ...

Tips for utilizing the Delete function in Python Flask

Need help with the Delete method in Python Flask. I'm encountering some difficulties in my code and it keeps displaying Method Not Allowed error when trying to load the URL http://localhost:8082/api/users/remove_group/5. @app.route('/api/users/re ...

Is it possible to leverage the flex features of react-native in a manner similar to the row/column system of

Is it a good idea to utilize flex in react native by creating custom components that retrieve flex values? I previously used the bootstrap grid system and now I am exploring react native. Is there a way to implement this example using react-native bootstr ...

An unusual icon with three varying sizes in a single file

I have come across an interesting favicon file. Click here to download the favicon file Unfortunately, I am unable to upload this image file as it does not meet the required format standards. Upon viewing the file in Windows 7, a fascinating discovery w ...

I'm a bit torn between using images for text or utilizing Google Fonts

I am looking to use a unique font that is not commonly used. This font is available in Google Fonts as well as in Photoshop. I am unsure about which method to choose because both options involve some loading time. Although using images for text is not idea ...

Tips for aligning an image in the center with a background attachment

I am facing an issue where only half of the image shows up when inserting the fixed effect. I need the entire image to be visible on the right side while adapting it for mobile devices. * { margin: 0; padding: 0; box-sizing: border-box; } .con ...

What could be causing the problem with my CSS background-image being referenced correctly?

Everything seems to be in order: background-image: url(./dir/img.jpg); However, I encounter a 404 error when attempting to reference an image using this approach: index.html <div style="--url: url(./dir/img.jpg);"></div> css backg ...

What is the proper method for utilizing colspan within the footerData of a jqGrid?

Are you looking to customize the footer of your jqgrid as shown in the example below? I am trying to set up a custom footer for my jqgrid similar to the one displayed above. I have already enabled the footerrow:true option and used $self.jqGrid("footerDat ...

Is it possible to override CSS classes in Ionic without using app.scss?

I'm currently working on designing a dark theme for my ionic application by watching this tutorial. It's been effective for most of the classes, however, I've encountered some CSS classes that only respond to overrides in app.scss. If I try ...

Issue with GridLayout causing rows to be misaligned

As a newcomer to the world of CSS, I found myself in need of a grid layout for a quick project. Here's the mishmash of CSS code I came up with: body { margin: 40px; } .container { display: grid; grid-template-rows: [row1start] 20% [row2sta ...

What is the best way to align inline-block elements in the center?

I'm looking for a way to center inline-block elements, but I haven't had much luck so far. When I tried using margin-top: x em;, it just moved the image down instead of centering it. The inline-block is intentionally nested inside an image block. ...

Tips for choosing the parents sibling using CSS

How can I change the background color of the .arrow-tip class when hovering over the first <li>? Can you provide me with the correct CSS rule? Here is the HTML code: <nav> <ul> <li>Item 1</li> <li>I ...

Error Encountered: TemplateSyntaxError on the User Profile Page

I encountered an error: TemplateSyntaxError at /accounts/profile/ <ExtendsNode: extends "registration/accounts/base.html"> must be the first tag in the template The content of base.html is as follows: {% load staticfiles %} <htm ...

When I click on the button, the page reloads instead of executing the essential code

On my website, there is a settings page where users can edit their profiles. After editing, they click the update button which triggers the updateProfileData function. This works smoothly in Chrome browser, but in Firefox, the page reloads as soon as the b ...

What are the best strategies to prevent text from appearing jumpy while fading in word by word?

I'm struggling to create a smooth fade-in effect word by word without causing the first word to suddenly jump to the left when the second word appears. To better explain my issue, I've provided a jsfiddle link: http://jsfiddle.net/6czap/504/ If ...