Avoid activating the hover effect on an element situated below another element without the use of JavaScript

After reviewing various posts, it seems like the only solution involves using javascript. Is there a way to hover over one element without affecting the style of another element below it, all without relying on javascript? I'm looking for a solution using vanilla HTML and CSS only. Specifically, I want to be able to hover over "blabla" or "blablabla" without adding a border to the navigation bar.
HTML

    <div class="navBar">
        <div>
            <h1 id="Title">A Random Website</h1>
        </div>
        
        <div class="navBarChild">
                    <a href="blabla.html" id="linkBla">Notepad</a>
              
                    <a href="blablabla.html" id="linkBlaBla">Help</a>
        </div>
    </div>

CSS

.navBar{
    display: flex;
    position: sticky;
    top:0;
    padding: 20px;
    border: 2px solid gainsboro;
    width: 100%;
    justify-content: center;
    background-color: gainsboro;
    z-index: 2;
    
 }
 #Title{
    color: black;
    font-family: monospace;
 }
 .navBar:hover{
    border: 2px solid black;
 }
 h3{
   z-index: 2;
 }

body{
   background:url("...") left / cover no-repeat;
}
.navBarChild{
   display: flex;
   flex-direction: row;
   position: relative;left: 290px;top: 17px;
}
#linkBla{
   font-weight: bold;
   text-decoration: none;
   font-size: 26px;
   font-family: monospace;
   color: black;
}
#linkBla:hover{
   color: orangered;
}
#linkBlaBla{
   position: relative;left: 50px;
   font-weight: bold;
   text-decoration: none;
   font-size: 26px;
   font-family: monospace;
   color: black;
}
#linkBlaBla:hover{
   color: orangered;
}

Answer №1

Create a 'navBarContainer' Element

Start by moving the 'navBarChild' outside of the 'navBar' like this:

<div class="navBar">
  <div>
    <h1 id="Title">A Random Website</h1>
  </div>
</div>
<div class="navBarChild">
  <a href="blabla.html" id="linkBla">Notepad</a>
          
  <a href="blablabla.html" id="linkBlaBla">Help</a>
</div>

Create a new 'navBarContainer' and place both elements inside it

<div class="navBarContainer">
  <div class="navBar">
    <div>
      <h1 id="Title">A Random Website</h1>
    </div>
  </div>
  <div class="navBarChild">
    <a href="blabla.html" id="linkBla">Notepad</a>
          
    <a href="blablabla.html" id="linkBlaBla">Help</a>
  </div>
</div>

In your CSS, set '.navBarContainer' to have 'position: relative;'

position: relative;

For 'navBarChild', set it to

position: absolute;
display: flex; //to keep the a-links together

You can then adjust its position as needed

top: 0; //important
left: 75%;
height: 100%;

Z-index shouldn't be necessary at this point

Finally

Add some padding to #linkBla and #linkBlaBla, and set the display to 'flexbox'

#linkBla, #linkBlaBla {
  padding: 40%;
  display: flexbox;
}

Preview the entire setup in this pen https://codepen.io/emekaorji/pen/mdOMMRr

Answer №2

It seems that achieving this without the use of javascript may not be possible, but you can incorporate the script directly into the HTML code:

<html>
<body>
...
<script>
  function removeOutline() {
    document.getElementsByClassName("navBar")[0].style.border = "2px solid transparent";
  }

  function addOutline() {
    document.getElementsByClassName("navBar")[0].style.border = "2px solid black";
  }
</script>
</body>
</html>

You can then utilize it like this:

<div class="navBarChild" onmouseover="removeOutline()" onmouseout="addOutline()">

Regarding CSS limitations, it's worth noting that modifying elements higher in the hierarchy from a child element is restricted. Essentially, altering the parent element based on the child element is not supported, although the reverse is achievable through child selectors.

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

Relocating to the middle of the webpage (Automatically resize for mobile compatibility if feasible)

Apologies for my poor English. Are there any suggestions on how to center this without using margins or other methods? Perhaps there are alternative solutions. https://i.sstatic.net/dXfwL.png Also, is it feasible for the image and video to automatically a ...

Creating interactive <td> elements in HTML with JavaScript editor capabilities

Currently, I am working on creating an editable table feature and managed to find a good example to follow. However, I have encountered some issues along the way. <td contenteditable="true" class="hover" onBlur="saveToDatabase(this,'question' ...

Employing the power of JavaScript to enable the seamless toggle of an overlay

I have a div named "navbar_menu". On clicking this div, I want the visibility of the div named "nav_overlay" to fade in. Upon another click, I want it to fade back and become invisible again. Currently, I have set the div 'nav_overlay" to have ' ...

Differences in Cross-Browser Styling for ListView Item Templates

I'm following up on my previous query regarding ListView's ItemTemplate table styling. I am still working on achieving the desired look for the ItemTemplate: ______________________________________________ | |___________Title_______ ...

Tips for resolving Layout Shift issues in responsive images utilizing the picture & source HTML tags

One issue I encountered with the <source> element is that even when specifying the width and height attributes for both the <source> and image fallback, there is still significant layout shift. This could be due to using images with varying dim ...

Guide on how to display matching options in an input box using HTML datalist based on user input at the beginning

I am a beginner in React and I am looking to create an autocomplete suggestion box for a text input field. I want the suggestions to only match the first letters of the available options, unlike the current behavior of HTML's <datalist>. Althou ...

Troubleshooting Problem with Website Responsiveness on iPhones Using Bootstrap 5

I am currently experiencing a challenge involving the responsiveness of a website I am developing, particularly when it comes to iPhones. The site utilizes Bootstrap 5 and displays correctly on Android devices and in Chrome Dev Tools. However, upon testing ...

What is the process for incorporating an additional input in HTML as you write?

I am looking to create a form with 4 input boxes similar to the layout below: <input type="text" name="txtName" value="Text 1" id="txt" /> <input type="text" name="txtName2" value="Text 2" id="txt" /> <input type="text" name="txtName3" valu ...

The Angular Animation constantly resets with each new action taken

In my Angular project, I am working on a scaling animation for a list. I want the animation to only trigger when specific buttons (red and green) are pressed. Currently, the animation restarts regardless of what I click on. Can anyone help me troubleshoot ...

Designing a multi-platform web browser application for Android, iOS, and Windows 10

I've developed several web applications using HTML5 technology and now I'm interested in creating my own custom web browser specifically tailored for my apps/sites. I want it to have a native appearance, but essentially just wrap around my web ap ...

Dim the shading of a portion of the picture

If you have an image that covers an entire element through CSS like this #myDiv {background-image: url("../images/background.jpg "); background-repeat: no-repeat; background-size: cover; background-position: center;} Now, what if you want to add a gray ov ...

Making the Select Tag function as an on-click event in JQuery

So I currently have a tab set up that functions as on click links when viewed on desktop. However, for tablet and mobile devices, I need it to be transformed into a select drop down list while maintaining the same functionality. Below is the code used for ...

Where can I find a style element using Selenium in Python?

Hey there! I'm looking to find a style element or style elements using Selenium in Python. <div style="flex-direction: column; padding-bottom: 65px; padding-top: 0px;"> I've tried various methods such as: self.driver.find_element ...

Using various span elements with distinct alignment within an h1 tag in Bootstrap 5

How can I nest three span tags inside an h1 tag, aligning them differently? The first on the left, the second in the center, and the third on the right. Can this be achieved using Bootstrap or does it require custom CSS? ...

Textfield style in Struts2 customization

How can I change the appearance of the textfield? [https://i.sstatic.net/TkQ0i.png] I am trying to remove the background color The code snippet in question: <s:form action ="Update_datos_XML"> <s:hidden id="id" name="id_sesion" value=" ...

Can you include an optional selector in SASS?

Here is an interesting concept: adding a CSS-selector optionally to the existing selector stack within a mixin. This is what I envision: @mixin myMixin(mobileSelector:true) { @if(mobileSelector) { .foo .mobile:before, } .classXY .subclass:before ...

What is the best way to ensure that a Flex div and its child images stay confined within the boundaries of its parent container?

I'm struggling to create a responsive grid of images. I can't get the images to be both responsive with max-height and max-width, while also making sure the parent div takes up their full width. On the other hand, if I make the parent div the cor ...

Creating a dynamic drop-down box with editable text input using HTML5

I am looking to enhance text editing functionality by incorporating a drop down box and a custom scrollbar. This will allow the end user to input data directly or select from the dropdown options. The final value provided by the user should be saved. I w ...

Change Dropdown menu orientation to vertical

Struggling to convert a CSS drop down menu into a vertical menu. I've tried multiple solutions but can't seem to achieve the desired result. Here is my CSS and HTML code, can anyone point out what I might be missing? #sddmT { margin: 0; pa ...

Material-UI LeftNav with a sticky header

I attempted to create a fixed header, specifically a Toolbar, inside a LeftNav so that when the LeftNav is scrolled, the Toolbar remains in place. However, for some reason, setting position: 'fixed' on the Toolbar does not work within the LeftNav ...