How can the hover effect be disabled when the pseudo-element is being hovered over?

I need the hover effect (the background of the pseudo-element changing to green) to only show up when the button is being hovered over. Right now, it also triggers when hovering over the pseudo-element itself (green box).

button {
    box-sizing: border-box;
    background-color: blue;
    padding: 10px 20px;
    color: white;
    position: relative;
}

button::before {
    content: '';
    position: absolute;
    background-color: transparent;
    height: 20px;
    width: 100%;
    left: 0;
    bottom: -30px;
}

button:hover::before {
    background-color: green;
}
<button>
I am a button
</button>

Answer №1

Prevent all click and cursor events by adding pointer-events: none;.

button {
    box-sizing: border-box;

  background-color: blue;
  padding: 10px 20px;
  color: white;
  position: relative;
}

button::before {
  content: '';
  position: absolute;
  background-color: transparent;
  height: 20px;
  width: 100%;
  left: 0;
  bottom: -30px;
  pointer-events: none;
}

button:hover::before {
  background-color: green;
}
<button>
I am a button
</button>

Answer №2

To hide and show a pseudo-element, you can start by setting its default state to hidden using the display property with display: none;. When the user hovers over the button, you can make the pseudo-element visible by changing the display property to display: block;. However, if they move the mouse away from the button, it will be hidden again.
It's important to note that this approach may not be suitable for creating a drop-down menu.

button {
    box-sizing: border-box;
    background-color: blue;
    padding: 10px 20px;
    color: white;
    position: relative;
}

button::before {
    content: '';
    position: absolute;
    background-color: transparent;
    height: 20px;
    width: 100%;
    left: 0;
    bottom: -30px;
    display: none;
}

button:hover::before {
    background-color: green;
    display: block;
}
<button>
I am a button
</button>

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

What is the best way to style divs in this manner?

Imagine you have a div with specific height and width (as shown in the illustration as the innermost one), and you wish for the outer divs to encompass it, with an outer margin of 1 em. How could this be achieved? +--------+ | +-----+| | |+---+|| | || ...

The property cannot be set for an undefined element in Jquery UI slider tabs

There seems to be some extra space at the bottom of the slider tabs between the navigators and content. Additionally, I am facing a challenge in making the page responsive as the content size does not adjust when I resize the window. To see the changes, I ...

Function for masking phone numbers is adding additional "x's" to the input field

My phone number formatting script is supposed to add dashes after the third and sixth characters, as well as insert an "x" after the 10th character for extensions. However, it is adding extra "x's" after the 12th character, resulting in the incorrect ...

Is there a way to add a clickable icon to a form-floating label in Bootstrap 5?

I'm having trouble getting a clickable icon to work properly in a form-floating label. It seems that the label is being rendered behind the input field. I've tried adjusting the z-index with no success, and I'm not sure what else to try to ...

Real-time console input/output feature for a gaming server utilizing either JavaScript or PHP

About My Minecraft Server: After running a Minecraft server in my basement for a few months, I realized that using TeamViewer to input commands and monitor the console was not ideal. The console of a Minecraft server provides a log of events with timestamp ...

Different width, same height - images arranged side by side in a responsive layout

The problem lies in the images: https://i.sstatic.net/rydNO.png Here is an example of desktop resolution: https://i.sstatic.net/uv6KT.png I need to use default size pictures (800x450px) on the server. This means I have to resize and crop them with CSS to ...

I'm curious, are there any html rendering engines that can display text-based content using curl-php?

When utilizing PHP cURL to interact with webpages, I often find myself needing to use regular expressions if the page contains AJAX and JavaScript elements. Does anyone have any recommendations for rendering HTML pages and extracting the text-based render ...

Tips on how to prevent a video from playing in the background of a popup even after it has been closed

After creating a video popup that plays upon clicking a button using jQuery, I encountered an issue where the video continues to play in the background even after closing the popup by clicking the close(X) button. Below is my code, can someone assist me in ...

Margin of Contents in a Table

I have created a table within a box on my webpage. The default setting centers the table inside the box, but I want it to align with the bottom border of the box instead. Each row in the table contains two cells, each cell displaying a picture that I would ...

What is the best way to generate a unique bootstrap card for every piece of submitted data?

I am trying to figure out how I can create a dynamic bootstrap card for each submitted data. When the form is submitted, a new card will be created and the data will be stored in that card. Each piece of data will be displayed in a column format similar to ...

Steps for turning off image maps on a slider for mobile users

Hello, I'm currently working on a website that has an image slider with image maps embedded in each image. Everything works perfectly on desktop, but when it comes to mobile versions, things start to get messy. I was wondering if there is any way to d ...

JavaScript is currently only being applied to sort the first table

I am having an issue with sorting three tables on my page based on a value (year in this case). <table id="table1"> <tr> <th>Name</th> <th>Surname</th> <th>Year</th> </tr> <tr> ...

Bootstrap 3 offset doesn't respond to dimensions

Even though I have set an offset for a medium-sized screen, the offset still appears in large screens. Here is a snippet of my HTML code. How can I resolve this issue? <div class="col-lg-6 col-sm-12"> <div class="row"> <div class="co ...

Ensuring Consistent Headings While Scrolling

Imagine having headings structured like this: <h1 class="fixontop">heading 1</h1> content goes here. . . . long paragraph. <h1 class="fixontop">heading 2</h1> 2nd content goes here. . . . long paragraph. <h1 class="fixontop"> ...

I am looking for a way to display or conceal text depending on the presence of an image

Could you please help me with this? I am looking for a solution where text is displayed only when there is content in the src attribute of an img tag. Essentially, I need JavaScript code that can identify if an attribute has content and then make a paragra ...

Vuetify's offset feature seems to be malfunctioning as it does not

I'm facing an issue with the <v-flex> element in my code, specifically with the offset property not responding as expected. Despite following the recommended layout from the vuetify docs, I can't seem to get it right. Below you can see the ...

Issue with VideoJS: MP4 file does not play after dynamically updating video URL

I have been using videoJs to display videos on my website. Below is the HTML code: <video id="player-vjs_html5_api" class="vjs-tech" crossorigin="anonymous" preload="auto" src="http://path-to-video/small.mp4"> <p class="vjs-no-vjs">Your bro ...

What you see is what you get when it comes to effortlessly copying and pasting

As a web developer, I am aware that images cannot simply be copied from a local machine and pasted into a website - they must be uploaded. I have experience with wysiwyg tools like TinyMCE and FCKEditor, but my client is frustrated about not being able to ...

Can the vacant space on an HTML page be determined through calculation?

Could someone please help me figure out the amount of empty space on a html page? https://i.sstatic.net/VXzbz.png Is it possible to determine the area highlighted in red using javascript? Apologies if this question seems trivial. ...

Enhancing HTML "range" element with mouse scroll functionality for incrementing values in step increments

I'm working on developing a scroll feature that operates independently from the main window's scrolling function. I aim to trigger specific events in the primary window based on interactions with this separate scrollbar. The only solution I coul ...