Home > Software design >  How can I make my hover dropdown menu stay visible over my carousel
How can I make my hover dropdown menu stay visible over my carousel

Time:07-20

Here is my code I created a dropdown menu bar where I have many dropdown menus on hover but later when I created a carousel, and when I am hovering on my dropdown menu it is behind the carousel but I want them in front of the carousel so I can access the links in the dropdown menu here the code of dropdown menu and carousel.

Dropdown.jsx

import "./dropdown.css"
import styled from "styled-components"

const Container = styled.div`
height: 50px;
left: 0px;
top: 61px;
background: #efefef;
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);

`
const Dropdown = () => {
  return (
    <div>
        <Container>
            <div className="menu-area">
                <ul>
                    <li>Fashion
                        <ul className="dropdown">
                            <li>Men</li>
                            <li>Women</li>
                            <li>Kids</li>
                        </ul>
                    </li>
                    <li>Grocery
                        <ul className="dropdown">
                            <li>Vegetables</li>
                            <li>Oil</li>
                            <li>Bakery</li>
                            <li>Cerelas</li>
                            <li>Dairy</li>
                        </ul>
                    </li>
                    <li>Phone
                        <ul className="dropdown">
                            <li>Apple</li>
                            <li>MI</li>
                            <li>Samsung</li>
                            <li>Realme</li>
                            <li>Nothing</li>
                            <li>Oneplus</li>
                        </ul>
                    </li>
                    <li>Home
                        <ul className="dropdown">
                            <li ><a href="/"> Appliances</a> </li>
                            <li>Decors</li>
                            <li>Utensils</li>
                            <li>Bedroom</li>
                            <li>Bathroom</li>
                        </ul>
                    </li>
                    <li>Beauty
                        <ul className="dropdown">
                            <li>Moisturizers</li>
                            <li>Perfumes</li>
                            <li>Lipsticks</li>
                            <li>Hair Colors</li>
                        </ul>
                    </li>
                    <li>Electronics
                        <ul className="dropdown">
                            <li>Washing Machine</li>
                            <li>Refrigerator</li>
                            <li>TV</li>
                            <li>Microwave</li>
                            <li>Laptop</li>
                        </ul>
                    </li>
                </ul>
            </div>
        </Container>
    </div>
  )
}

export default Dropdown

dropdown.css

.menu-area ul{
    list-style: none;
}
.menu-area {
  
    display: inline-block;
    text-align: center;
    position: absolute;
    /* top: 10%; */
    left: 50%;
    transform: translate(-50%);
}
.menu-area li:hover{
    background-color: rgb(216, 216, 216);
}

.menu-area > ul{
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    /* background-color: white; */
    width: 100vw;
    
}
.menu-area > ul >li{
    position: relative;
    cursor: pointer;
    font-size: 16px;
    letter-spacing: 1px;
    float: left;
    width: 136px;
    height: 50px;
    line-height: 50px;
    margin: 0px 30px;
    
}
.dropdown{
    position: absolute;
    top: 100%;
    left: 0;
    width: 100%;
    padding: 0;
}
.dropdown li{
    background: rgba(218, 218, 218, 0.332);
    display: none;
}
.dropdown li:hover{
background: rgb(216, 216, 216);
opacity: 1;

}
.menu-area li:hover > .dropdown li{
    display: block;
}

Slider.jsx

import styled from "styled-components"
import ArrowLeftIcon from '@mui/icons-material/ArrowLeft';
import ArrowRightIcon from '@mui/icons-material/ArrowRight';
import SliderItems from './sdata'
import { useState } from "react";

const Container = styled.div`
    width: 100%;
    height :40vh ;
    display: flex;
    position: relative;
    overflow: hidden;
    margin-top: 10px;
`;

const Wrapper = styled.div`
    height: 80vh;
    display: flex;
    transition: all 1.5s ease;
    transform: translateX(${props=>props.slideIndex * -100}vw);

`

const Slide = styled.div`
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
`

const ImgContainer = styled.div`
height: 100%;
flex: 1;
width: 100vw;

`
const Image = styled.img`
height: 80%;
object-fit: cover;
`


const Arrow = styled.div`
    width: 50px;
    height: 50px;
    background-color: #fff7f7;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    top:0;
    bottom: 0;
    left: ${props => props.direction === 'right' && "10px"};
    right: ${props => props.direction === 'left' && "10px"};
    cursor: pointer;
    margin: auto;
    opacity: 0.5;
    z-index: 2;
`

const Slider = () => {
    const [slideIndex, setslideIndex] = useState(0)
    const handleClick = (direction) => {
        if (direction ==="left") {
            setslideIndex(slideIndex> 0 ? slideIndex - 1 : 2)
        } else {
            
            setslideIndex(slideIndex> 2 ? slideIndex   1 : 0)
        }
    }

    return (
        <Container>
            <Arrow direction="left" onClick={() => { handleClick("left") }}>
            <ArrowRightIcon />
            </Arrow>
            <Wrapper slideIndex={slideIndex}>
                {SliderItems.map((item, index) => (

                    <Slide  key={index}>
                        <ImgContainer >
                            <Image src={item.img} alt="image" />
                        </ImgContainer>
                        
                    </Slide>

                ))}
            </Wrapper>
            <Arrow direction="right" onClick={() => { handleClick("right") }}>
               
                <ArrowLeftIcon />
            </Arrow>
        </Container>
    )
}

export default Slider

here is the code

CodePudding user response:

If two positioned elements overlap without a z-index specified, the element positioned last in the HTML code will be shown on top, and from the looks of your code, your slider has a higher stack order. Source

To fix this, try giving the .dropdown a higher z-index so it goes on top of the slider.

Fix

.dropdown {
  z-index: 100;
} 

Or

It may be better if you give the menu itself the z-index property so the entire menu overlaps the slider and not the dropdown alone.

.menu-area {
  display: inline-block;
  text-align: center;
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  z-index: 100;
}
  • Related