Prevent oversizing borders by using the :hover pseudo-class on the child element

If I create a list using <ul> and remove the borders from the <li> elements. When a <li> is hovered over, it gains a 3px black border. If a <li> is clicked, it becomes a child <span> of the selected <li>.

The issue arises when hovering over the <span> element as an additional 3px border is added, resulting in a total of 6px border.

Is there a way to prevent this behavior?

How can I disable the hover effect on child elements of the li?

Here is the current code snippet:

.tablinks li {
    color: #8c8c8e;
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}

.tablinks span {
    display: block;
    border-top-color: #000;
    border-top-style: solid;
    border-width:3px;
    color:#000;
}

.tablinks > li:hover {
    color: #000;
    border-top-color: #000;
    border-top-style: solid;
    border-width:3px;
}

I have attempted to set the border-width of span:hover to 0px but the transition causes the border to jump from 3px to 0px and back, resulting in a poor visual effect:

.tablinks span:hover {
    border-width:0px;
}

Answer №1

If you want to enhance the code by adding a span tag, consider adding a class to the selected li element.

This approach would enable you to utilize the :not selector to exclude the selected element. Here's an example:

.tablinks > li:not(.selected):hover{
   color: #000;
   border-top-color: #000;
   border-top-style: solid;
   border-width:3px;
}

Imagine starting with this basic HTML structure:

<ul class="tablinks">
    <li>item 1</li>
    <li>item 2</li>
    <li>item 3</li>
    <li>item 4</li>
</ul>

Once an item is selected, the HTML transforms into:

<ul class="tablinks">
    <li>item 1</li>
    <li class="selected"><span>item 2</span></li>
    <li>item 2</li>
    <li>item 3</li>
</ul>

By implementing the CSS adjustments mentioned, the styling will exclude the selected li.


DEMO - Excluding selected li


Alternatively, instead of using a class, you could utilize a data attribute like this:

<ul class="tablinks">
    <li>item 1</li>
    <li data-selected><span>item 2</span></li>
    <li>item 2</li>
    <li>item 3</li>
</ul>

Then adjust the CSS in a similar manner:

.tablinks > li:not([data-selected]):hover{
    color: #000;
    border-top-color: #000;
    border-top-style: solid;
    border-width:3px;
}

Answer №2

Give this a shot:

.tablinks li span { border-width: 0px; } 

Remove the .tablinks span { border-width } class that impacts the border-width of the "li span"

By using .tablinks span, you are modifying the span child of the li element. This means that when you hover over the li, it will have a 3px border (as stated in ".tablinks > li:hover"), and ".tablinks span" sets a 3px border under the li element.

You can alternatively change ".tablinks span" to

.tablinks > span

If your span is directly under .tablinks already.

Answer №3

You may want to consider including this code snippet in place of the previous one you shared:

.tablinks li:hover span {
    border-width: 0px;
}

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 incorporate a set of buttons and dynamically update them by using next and previous buttons?

Currently, I have code for two sets of buttons. How can we add functionality for next and previous buttons? <div id="chart-section-wrapper" class="container"> <div id="chart-section"> <div class=& ...

Is there any method to determine whether a floated element has been pushed down?

Imagine a dynamic menu with floating elements, each set at a width of 150px. As the menu's width decreases, the elements progressively move to the next row. You are contemplating how to detect when an element has been moved down. One approach could b ...

How to utilize a specific option as a variable within a PHP select dropdown menu

In my PHP code, I am retrieving data from an Azure SQL database and using it to populate a drop-down menu with options. The SQL query I used returns two columns: Title and Cycle_ID. After setting the Title as the text string and Cycle_ID as the value for ...

Numerous Radio Buttons

I am currently working on creating a quiz similar to those found on Buzzfeed and Zimbio. I apologize if this question has been asked before, but despite my efforts in searching, I have not been able to find the answer I am looking for. In this quiz, partic ...

Initializing the $(this) variable prior to the function declaration

I am faced with the task of performing multiple checks based on the IDs of certain elements, all of which vary slightly. So, I created several functions to handle this, and rather than repeatedly using $(this).children ..., I wanted to create a short vari ...

Instructions for creating a distinct click listener for each <img> element inside a table cell

Within my table, each row contains multiple columns with images as data. I successfully implemented a 'common listener' that is triggered when any image within a table row is clicked. $('#mytable tbody td img').click(function () { // ...

Tips for using the arrow keys to navigate the cursor/caret within an input field

I am trying to create a function that allows the cursor/caret to move inside an input field character by character using the arrow keys (ArrowLeft, ArrowRight) on a keydown event. Current Approach: const handleKeyDown = (e: KeyboardEvent<HTMLInputEle ...

Do Scss mixins have the ability to adapt to different screen sizes

In my CSS, I set different font sizes for different devices: For example: @media only screen and (min-width: 480px) and (max-width: 599px) { t-heading { font-size:14px; } } @media only screen and (min-width: 600px) { t-heading { font-size:24px; } } I ...

Tips for choosing a drop-down menu option without an identifier using Selenium in Python

I am trying to choose an element/item from a drop-down menu in Python and Selenium without using an id element. Here is the HTML code snippet: <mbo-transaction-mode-select mode="filters.mode" class="ng-isolate-scope"> <select class="form-con ...

Issue with Vue.js - Double curly braces not rendering data on the page

I've recently delved into the world of vue.js Strangely enough, the double curly braces syntax doesn't seem to be rendering for me. However, when I utilize the v-text directive, everything falls into place. Here's a snippet of my code: HTM ...

Learn how to incorporate a consistent header and footer across multiple pages on a website with Node.js, ExpressJS, and either hbs or ejs templates

Creating a common header (navbar) and footer page to be included in multiple/several pages of a website. I want to create a dynamic website using Node.js and Express.js. The code for the navbar and footer will be placed in a common file named header.html ...

Tips for displaying the initial slide when a tab is loaded on a multi-tab webpage

I have a total of four tabs, with the first tab containing a slideshow. On page load, I am successfully triggering the first tab, but for some reason, the first slide in the slideshow within that tab is displaying a blank image. I'm struggling to find ...

Exploring an Array Based on User's Input with JavaScript

Looking to implement a search functionality for an array using AJAX. The array is pre-populated with values, and the user will input a value in a HTML text box. If the entered value is found in the array, it should display "Value found", otherwise "not f ...

Ensure that input field is validated only upon clicking the save button in Angular framework

Can anyone suggest the most efficient method to validate required fields in an input form using AngularJS? I want to display an error message only after the user clicks the save button (submit). Thank you in advance. ...

Heroku App Breaks Down - Fails to Render Static HTML Content (Express Server)

Despite my best efforts, I keep encountering this persistent error on Heroku whenever I attempt to serve a static HTML page with some basic JS, images, and CSS. I diligently followed all the advice from SO, made adjustments to index.js, restructured files, ...

Issue with $_SERVER['PHP_SELF'] not functioning properly

Apologies if this question has been asked before, but after searching extensively I couldn't find a solution. Therefore, I am posting here... The issue is that I'm trying to submit values on the same page (using jQuery Mobile UI) and I have used ...

Buttons are to be placed at the identical location

Here is the HTML code I am working with: <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33545c5446734a525b5c5c1d505c5e">[email protected]</a> wants to be your partner. Do you agree? <br><input type="but ...

Tips for stopping context menu on an iframe?

My attempt to stop the browser's context menu from appearing when right-clicking on any 'iframe' element is not successful - the context menu still appears. Here is the code I am using: $(document).ready(function () { $("iframe").e ...

Is there a way to have a fixed width div positioned in the center of the screen, with two additional divs on either side

I have come across this stunning image: https://i.stack.imgur.com/tUYMc.png It serves as a header for a website - click on it to see the full size. I am trying to replicate this using HTML, CSS, and images, but I'm struggling. It needs to be 100% w ...

Php Error 404 occurs upon attempting to redirect from the main landing page

I have been encountering an issue while trying to add links to different pages of my website. Whenever I click on the link, it displays a "404 Page Not Found" error message. Objective: My main goal is to create links from my home page, index.php, to other ...