• DEVHIDE
        • Home (current)
        • About
        • Contact
        • Cookie
        • Home (current)
        • About
        • Contact
        • Cookie
        • Disclaimer
        • Privacy
        • TOS
        Login Or Sign up

        How can I make an element visible on hovering an element from another section?

        198 Views Asked by Dipayan Mukherjee At 17 January 2023 at 14:16 2025-12-05T10:48:52.881000

        HTML

        <nb-sidebar class="menu-sidebar>
          <nb-menu class="sidebar-menu">
             <ul class="menu-items">
                <li class="menu-item ng-tns-c140-0 ng-star-inserted" >
                  <a class="ng-tns-c140-0 ng-star-inserted active" ng-reflect-router-link="/pages/dashboard"   title="Dashboard" href="/pages/dashboard">
                </li>
             </ul>
          </nb-menu>
        </nb-sidebar>
        
        <div class="content">
          <router-outlet>
            <div class="text">Message</div>
          </router-outlet>
        </div>
        
        

        Make router-outlet visible when I hover on li.

        I tried like this

        router-outlet {
          display: none;
        }
        
        nb-sidebar li:hover + .content router-outlet {
             display: block;
        }
        

        I want to do it with css/scss only. I want router-outlet to be visible when I hover on li.

        css hover scss-lint
        Original Q&A
        2

        There are 2 best solutions below

        2
        Adam Adam On 17 January 2023 at 14:51 BEST ANSWER

        In your example because the .content router-outlet element isn't a direct sibling of the li element the Adjacent Sibling Combinator won't work (see MDN for details)

        However there's a couple of ways of doing this. The first uses the :has() pseudo class which is neat as it doesn't need any javascript but isn't currently supported on all browsers. There's a nice introduction to this from Kevin Powell. Also caniuse.com is a good resource to check for browser compatibility.

        Note: I've added some text on your html to give you something to hover over.

        router-outlet {
          display: none;
        }
        
        /* added this */
        nb-sidebar:has(li:hover) + .content router-outlet {
             display: block;
        }
        <nb-sidebar class="menu-sidebar>
          <nb-menu class="sidebar-menu">
             <ul class="menu-items">
                <li class="menu-item ng-tns-c140-0 ng-star-inserted" >
                  <a class="ng-tns-c140-0 ng-star-inserted active" ng-reflect-router-link="/pages/dashboard"   title="Dashboard" href="/pages/dashboard">This is the anchor inside the li</a>
                </li>
             </ul>
          </nb-menu>
        </nb-sidebar>
        
        <div class="content">
          <router-outlet>
            <div class="text">Message</div>
          </router-outlet>
        </div>

        The more generally supported way would be to use javascript which detects you mousing over the element then adding and removing a class to override the display: none rule as follows. The code below should be self-evident but if you have any questions just pop a comment below and I'll elaborate.

        window.onload = () => {
          const elem = document.querySelector('.menu-item');
          elem.addEventListener('mouseover', (e) => {
            document.querySelector('.content router-outlet').classList.add('display-block');
          });
        
          elem.addEventListener('mouseout', (e) => {
            document.querySelector('.content router-outlet').classList.remove('display-block');
          });
        }
        router-outlet {
          display: none;
        }
        
        .display-block {
          display: block;
        }
        <nb-sidebar class="menu-sidebar>
          <nb-menu class=" sidebar-menu ">
             <ul class="menu-items ">
                <li class="menu-item ng-tns-c140-0 ng-star-inserted " >
                  <a class="ng-tns-c140-0 ng-star-inserted active " ng-reflect-router-link="/pages/dashboard "   title="Dashboard " href="/pages/dashboard ">This is the anchor inside the li</a>
                </li>
             </ul>
          </nb-menu>
        </nb-sidebar>
        
        <div class="content ">
          <router-outlet>
            <div class="text ">Message</div>
          </router-outlet>
        </div>

        0
        FuryForever FuryForever On 17 January 2023 at 14:49

        You can use jQuery hover() method:

        p {
          background: orange
        }
        <!DOCTYPE html>
        <html>
        <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
        <script>
        $(document).ready(function(){
          $("p").hover(function(){
            $('h1').css("display", "block");
            }, function(){
            $('h1').css("display", "none");
          });
        });
        </script>
        </head>
        <body>
        <p>Hover the mouse pointer over this paragraph.</p>
        <h1>Title</h1>
        
        </body>
        </html>

        In your code it would be:

        $(document).ready(function(){
          $("li").hover(function(){
            $('.router-outlet').css("display", "block");}, function(){
           $('.router-outlet').css("display", "none");});
          });

        Related Questions in CSS

        • CSS Class is not applying to element (border width,color,and style attributes)
        • How do I find the fonts that are not loading in a CORS situation ( MoovWeb )?
        • Positioning child at bottom of parent with scroll
        • Play multiple audio files in a slider
        • How to set text over image?
        • Website zoomed out on Android default browser
        • Writing/Overwriting to specific XML file from ASP.NET code behind
        • My navbar is not expanding after collapse
        • when a checkbox is checked how to display a different hidden element using javascript
        • Gaps Vertically Using Dividers
        • CSS: Preventing a property to affect on element until the end of transition
        • Polygon Button with pure CSS
        • transform-origin doesnt apply in safari
        • show/hide multiple div tags at once and change the size of the remaining div tag
        • Mask the image/maps/div (circle) using css and jquery

        Related Questions in HOVER

        • change a class:hover background-color with js
        • Internet Explorer hover/rgba/opacity/css3 compatibility
        • How to hover reciprocally on different elements with jquery.
        • How to create both onClick and mouseover to a link jQuery
        • CSS - Hover a container without pushing further elements down
        • Hover delay / Hover effect staying true after cursor leaving area of effect
        • Changing the hover behavior of dynamically generated classes
        • Pictures fade from BW to color while responsive
        • Dropdown menu with scroll not working on iPad
        • CSS :hover transform scale command rotate object instead
        • hover properties declaration confusion
        • svg rect doesnt respond with another div fixed with a high z-index
        • setTimeout dont work on .hover() leave/stop
        • Issue with HTML/CSS tab when it hover or active
        • Css Hover <a> inside <td>

        Related Questions in SCSS-LINT

        • Pseudo-class nesting / SCSS Linter Warning
        • Disable scss.lint rule for one file in VS Code
        • How to apply CSS to specific children of an element
        • Angular: Accessing Variables in a Function
        • Can I use SCSS in a <style> tag?
        • MEDIA QUERY For orientation: landscape but not desktop. how?
        • where shall I put custom.scss file to customize bootstrap component?
        • How to add an exclamation mark to the end of last list item using css
        • How to find CSS/SCSS classes that contains single property
        • Error: Cannot find module 'stylelint' Webpack
        • how to include sass file in reactjs
        • how to use ng-class with multiple condition in ionic 5
        • Installing scss-lint with npm without Python
        • Typescript + webpack cannot build scss module
        • How can I make an element visible on hovering an element from another section?

        Trending Questions

        • UIImageView Frame Doesn't Reflect Constraints
        • Is it possible to use adb commands to click on a view by finding its ID?
        • How to create a new web character symbol recognizable by html/javascript?
        • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
        • Heap Gives Page Fault
        • Connect ffmpeg to Visual Studio 2008
        • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
        • How to avoid default initialization of objects in std::vector?
        • second argument of the command line arguments in a format other than char** argv or char* argv[]
        • How to improve efficiency of algorithm which generates next lexicographic permutation?
        • Navigating to the another actvity app getting crash in android
        • How to read the particular message format in android and store in sqlite database?
        • Resetting inventory status after order is cancelled
        • Efficiently compute powers of X in SSE/AVX
        • Insert into an external database using ajax and php : POST 500 (Internal Server Error)

        Popular # Hahtags

        javascript python java c# php android html jquery c++ css ios sql mysql r reactjs

        Popular Questions

        • How do I undo the most recent local commits in Git?
        • How can I remove a specific item from an array in JavaScript?
        • How do I delete a Git branch locally and remotely?
        • Find all files containing a specific text (string) on Linux?
        • How do I revert a Git repository to a previous commit?
        • How do I create an HTML button that acts like a link?
        • How do I check out a remote Git branch?
        • How do I force "git pull" to overwrite local files?
        • How do I list all files of a directory?
        • How to check whether a string contains a substring in JavaScript?
        • How do I redirect to another webpage?
        • How can I iterate over rows in a Pandas DataFrame?
        • How do I convert a String to an int in Java?
        • Does Python have a string 'contains' substring method?
        • How do I check if a string contains a specific word?
        .

        Copyright © 2021 Jogjafile Inc.

        • Disclaimer
        • Privacy
        • TOS
        • Homegardensmart
        • Math
        • Aftereffectstemplates