简体   繁体   English

当悬停在一个li元素上时,为什么嵌套的ul元素会降低整个li元素

[英]Why does the nested ul element bring down the entire li elements when hovering over one li element

This is a two part question. 这是一个两部分的问题。 The first part is my problem when hovering over an li element that is supposed to reveal a nested ul element. 第一部分是我将鼠标悬停在应该显示嵌套ul元素的li元素上时遇到的问题。 It brings the rest of the li tags at the bottom of the nested ul element. 它将其余的li标签带到嵌套ul元素的底部。 I don't know what I'm doing wrong but it pushes the initial li elements downward. 我不知道我在做什么错,但它会将最初的li元素向下推。 I wish someone can explain what I'm doing wrong. 我希望有人能解释我在做什么错。 Here is the code 这是代码

The second question I have is how do I create li elements that specificly have a hovering effect on them and not any nested li elements? 的第二个问题是如何创建对它们有悬浮效果的li元素,而不是嵌套的li元素? I wanted to create a menu list that changes the color of the text when you hover over it, but I didn't want the nested li elements to also have the hover effect 我想创建一个菜单列表,当您将鼠标悬停在其上时会更改文本的颜色,但是我不希望嵌套的li元素也具有悬停效果

HTML 的HTML

<div id="container">

  <ul class="list-inline">
    <li><a href="">Fire</a>
        <ul>
          <li><a href="">charmander</a></li>
          <li><a href="">magmar</a></li>
          <li><a href="">vulpix</a></li>
        </ul>
    </li>
    <li><a href="">Grass</a>
        <ul>
          <li><a href="">bulbasaur</a></li>
          <li><a href="">bellsprout</a></li>
          <li><a href="">oddish</a></li>
        </ul>
    </li>
    <li><a href="">Electric</a>
        <ul>
          <li><a href="">pichu</a></li>
          <li><a href="">magneton</a></li>
          <li><a href="">voltorb</a></li>
        </ul>
    </li>
    <li><a href="">Water</a>
        <ul>
          <li><a href="">squirtle</a></li>
          <li><a href="">poliwag</a></li>
          <li><a href="">krabby</a></li>
        </ul>
    </li>

  </ul>

</div>

SCSS SCSS

$green: #33cc33;
$blue : #0099ff;
$yellow: #ffcc00;
$red: #ff3333;
@mixin secondUl($color) {
        li {
            color: white;
            background: $color;
            min-width: 100px;
            border-bottom: 1px solid white;
        }
        a {
            color: white;
            font-weight: normal;
            text-align: center;
            display: block;
            width:100% ;
            padding: 5px 0;
        }
    } //secondUl

#container {
    width: 600px;
    margin: auto;
    margin-top: 30px;
    border-top: 1px solid black;
    color: black;
    font-family: arial,
    sans-serif;


    ul {


        margin: 15px 0;
        position: relative;

    } //ul
} //container

.list-inline {
    ul {
        position: absolute;
        padding: 0;
        top: 0;
        left: -25% ;
        z-index: 2;
        display: none;
        li {
            display: inherit;
            min-width: 100px;
            margin: 0;
        } //li
    } //ul

    li:nth-of-type(1) ul {
        @include secondUl($red);
    }

    li:nth-of-type(2) ul {
        @include secondUl($green);
    }

    li:nth-of-type(3) ul {
        @include secondUl($yellow);
    }
    li:nth-of-type(4) ul {
        @include secondUl($blue);
    }

    li {
        list-style: none;
        display: inline-block;
        margin: 0 10px;
      &:hover ul {
            display: block;
        }
    } //li 

     li:nth-child(1) a:hover {
        color: $red;
    }

     li:nth-child(2) a:hover {
        color: $green;
    }

    li:nth-child(3) a:hover {
        color: $yellow;
    }

    li:nth-of-type(4) a:hover {
        color: $blue;
    }


    &:first-child a {
        text-decoration: none;
        color: black;
        text-transform: capitalization;
        font-weight: 600; 
        -webkit-transition: color 1s;
        transition: color 1s; 
        -moz-transition: color 1s;

    }
}//list-inline

For problem 1 add vertical-align:top to your li 对于问题1,向您的li添加vertical-align:top

li {
    list-style: none;
    display: inline-block;
    margin: 0 10px;
    vertical-align: top;

  &:hover ul {
        display: block;
    }
 }

For Problem 2 use child instead of decendant selector. 对于问题2,请使用子代而不是子代选择器。 EG: 例如:

> li:nth-child(1)>a:hover {
    color: $red;
}

See: http://codepen.io/anon/pen/rrpVaV 请参阅: http//codepen.io/anon/pen/rrpVaV

First you have used global css for ul under #container #container ul {position:relative;} this css will be apply on every child ul which is in #container , and its overriding .list-inline ul{position:absolute} , you need to set immediate child selector css #container > ul {position:relative;} . 首先,您已经在#container #container ul {position:relative;}下对ul使用了全局css,该css将应用于#container每个子ul ,并且其覆盖的.list-inline ul{position:absolute} ,您需要设置直接子选择器css #container > ul {position:relative;}

For second ans. 对于第二个ans。 you need to do same thing, use immediate child selector css 您需要做同样的事情,使用immediate child selector css

http://codepen.io/anon/pen/LRZVRO http://codepen.io/anon/pen/LRZVRO

It works well. 它运作良好。 you have to add width of li 你必须添加宽度li

.list-inline li {
   min-width: 110px;
}

And

.list-inline li:hover ul {
   display: table;
}

DEMO 演示

 https://codepen.io/Dhaarani/pen/vXpONj

暂无
暂无

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

相关问题 如何选择 <li> 通过将鼠标悬停并在 <ul> 元件 - How to select <li> element by hovering and clicking over them in a <ul> element 为什么 <li> 元素悬停在图像上后将鼠标悬停在元素上时,颜色和宽度不会改变吗? - Why does the <li> element not change color and width when I hover the mouse over it after hovering over the image? 单击向上和向下按钮时 UL LI 元素自动滚动 jQuery - UL LI Element autoscroll when clicking Up and Down buttons jQuery 为什么 <li> 下的元素 <div> 呈现不同于 <li> 下的元素 <ul> ? - why does <li> element under <div> renders different from <li> element under <ul>? 在悬停元素时更改列表(ul)的所有其他元素li的css属性 - Change css property of all the other elements li of a list (ul) while hovering an element 元素验证错误<br>在<ul><li>元素</li></ul> - Wrong validation on element <br> in <ul> <li> elements 使用jQuery将类添加到嵌套ul li中的top li元素 - adding class to top li element in nested ul li with jquery 使用jQuery在嵌套的UL LI结构中查找LI元素的ID - Find id of an LI element in a nested UL LI structure using jQuery 我如何通过悬停在此ul块之外的另一个li元素来更改li元素的css属性 - How I can change css property of a li element by hovering another li element which is outside of this ul block 为什么ul和first li元素之间有空格? - Why is there a space in between the ul and first li element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM