简体   繁体   English

下拉菜单垂直而不是水平显示(使用 React)。 我将 OL 设置为 display: block 但它仍然水平显示下拉菜单

[英]Dropdown menu appears vertically instead of horizontally (using React). I set the OL to display: block but it still displays the dropdown horizontally

I have a navbar and i want there to be a dropdown menu when the user scrolls on English (the default language).我有一个导航栏,当用户滚动英语(默认语言)时,我希望有一个下拉菜单。 The dropdown menu is in the Dropdown.js file and I made it an ordered list.下拉菜单位于 Dropdown.js 文件中,我将其设为有序列表。 The last item in my CSS file is the CSS is.nav ol and the display is set to block which should go vertically but instead the dropdown menu shows up horizontally.我的 CSS 文件中的最后一项是 CSS is.nav ol,并且显示设置为块,这应该 go 垂直显示,而不是下拉菜单显示。

Navbar.js导航栏.js

import React, { useState, useEffect } from 'react'
import { Link, useMatch, useResolvedPath } from "react-router-dom"
import { useTheme, useThemeLanguage } from './pages/Context'
import  Dropdown  from './Dropdown'

export default function Navbar() {
  const LOCAL_STORAGE_KEY = 's';
  const path = window.location.pathname;
  const loggedIn = useTheme();
  const language = useThemeLanguage();

  const [dropdown, setDropdown] = useState(false);
  const onMouseEnter = () => {   
      setDropdown(true);
    };

  const onMouseLeave = () => {   
      setDropdown(false);
    };


  
 

  function conversion(x) {
    if (x === false)
    {return false;}
    if (x === 'false')
    {return false}
    else return true

  }


 
  return (
    <>
  <nav className="nav">
    <Link to ="/" className="site-title">My Site</Link>
    <ul>
      <CustomLink to= "/pricing"> Pricing </CustomLink>
      <CustomLink to= "/about"> About </CustomLink>       
       
      { conversion(loggedIn) ? <CustomLink to= "/logout"> Logout </CustomLink> : <CustomLink to= "/login"> Login </CustomLink>   }
      <h1  onMouseEnter={onMouseEnter}  onMouseLeave={onMouseLeave}> {language}  {dropdown && <Dropdown />} </h1>
      
    </ul>
  
  </nav>
 
  </>
  )  
  
}


function CustomLink({ to, children, ...props }) {
  const resolvedPath = useResolvedPath(to)
  const isActive = useMatch({path: resolvedPath.pathname, end: true})

  return (
    <li className={ isActive ? "active" : ""}>
      <Link to = {to} {...props}>
        {children}
      </Link>

    </li>
  )
}

Dropdown.js Dropdown.js

import React, { useState, useEffect } from 'react'
import {Languages} from './Languages'
import "./styles.css"

export default function Dropdown() {
    return(
        <>
        <ul >
       { Languages.map((item, index) => {
          return (
            <ol className ="new">
            <li key={index} >              
                {item.title}              
            </li>
            </ol>
          );
        })}
      </ul>
        </>
    );
};

CSS CSS

*{

    box-sizing:bor-box;
}

body {
    margin: 0
}

.container {
    margin: 1rem;
    text-align: center;
}

.nav {
    background-color: rgb(213, 67, 67);
    color: rgb(50, 200, 27);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 2rem;
    padding: 0 1rem;
    font: italic;

}

.nav ul {
    padding:0;
    margin: 0;
    list-style: none;
    display: flex;
    gap: 1rem;

}

.nav a {
    color: inherit;
    text-decoration: none;
    height: 100%;
    display: flex;
    align-items: center;
    gap: 2rem;
    padding: .25 rem;

}

.nav li.active {
    background-color: #b71d67
}


.nav li:hover {
    background-color: rgb(82, 38, 212)
}

.nav h1 {
    display:flex;
    height: 100%;
    width: 100%;
    text-decoration: none;
    color: rgb(11, 1, 1);
    padding: 10px;
    position: right;
    text-align: left;
  }

.uldropdown {
    display:block;

}

.nav ol {
    color:rgb(64, 159, 55);
    background-color: #f1d221;
    display: inline-block;
    width: 20%;
    font:700;
    padding: 10px;
}

if you insist on using flex ...如果你坚持使用flex ...

  • you have to use flex-direction: column;你必须使用flex-direction: column; in css .nav ul section在 css .nav ul部分
  • otherwise flex will treat them as items to be in the row否则 flex 会将它们视为要在行中的项目
  • check flex docs检查弹性文档
  • consider not using display: flex everywhere (this example will work out of the box without flex)考虑不要在任何地方使用display: flex (这个例子在没有 flex 的情况下可以开箱即用)

 .nav ul { list-style: none; display: flex; /* magic happens here */ flex-direction: column; }
 <div class="nav"> <ul> <li> a </li> <li> b </li> <ul> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM