Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

css - How to prevent Navbar going under elements on mobile?

I have a responsive Navbar with hamburger for mobile (in React) and when it expands from off screen it floats under my form element as opposed to overlaying the whole screen. The transition and styling is all perfect. I have tried z-index and changing position settings and cant get this to work at all.

Nav

nav {
    display: flex;
    justify-content: space-around;
}

media - 
 .nav {
        z-index: 1000;
    }

    .nav-links {
        position: fixed;
        right: 0px;
        height: 92vh;
        top: 8vh;
        display: flex;
        flex-direction: column;
        align-items: center;
        background-color: #5D4994;
        transform: translateX(100%);
        transition: transform 0.3s ease-in;
    }

    .nav-links-toggle {
        position: absolute;
        right: 0px;
        height: 92vh;
        top: 8vh;
        display: flex;
        flex-direction: column;
        align-items: center;
        z-index: 1000;
    }

jsx

<nav className = {toggle ? 'nav-active' : ''}>
            <div className = "logo">
                <img src = {logo} alt = 'logo' className='logo-pic' />
                <p>HearthStone Deck Creator</p>
            </div>
            <ul className = {toggle ? 'nav-links-toggle' : 'nav-links'} id = "nav">
                <li><Link to ='/'>Home</Link></li>
                <li><Link to ='/deckbuilder'>DeckBuilder</Link></li>
                <li><Link to ='/contact'>Contact</Link></li>
                <li><Link to ='/about'>About</Link></li>

            </ul>
            <div className={toggle ? 'toggle burger' : 'burger'} id = "burger" onClick = {handleClick}>
                <div className="line1"></div>
                <div className="line2"></div>
                <div className="line3"></div>
            </div>
        </nav>

form css

.input-search {
    width: 60vw;
    height: 6vh;
    border-radius: 10px;
    font-size: 1.5rem;
    transition: all 0.30s ease-in-out;
    outline: none;
    margin-bottom: 1.8rem;
    padding: 3px 0px 3px 3px;
    border: 1px solid #DDDDDD;
}

    


  [1]: https://i.stack.imgur.com/qTmeJ.png
  [2]: https://i.stack.imgur.com/GvVY7.png
question from:https://stackoverflow.com/questions/65902710/how-to-prevent-navbar-going-under-elements-on-mobile

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have fixed this with setting postion:relative and the z-index.

I then had to add a lower z-index to elements that would be covered

little bit of a bodge, but its working perfectly


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...