CSS On hover show another element

前端 未结 3 1498
余生分开走
余生分开走 2020-12-04 23:35

I want to use CSS to show div id=\'b\' when I hover over div id=\'a\', but I cannot figure out how to do it because the div \'a\' is i

3条回答
  •  借酒劲吻你
    2020-12-05 00:13

    You can use axe selectors for this.

    There are two approaches:

    1. Immediate Parent axe Selector (<)

    #a:hover < #content + #b
    

    This axe style rule will select #b, which is the immediate sibling of #content, which is the immediate parent of #a which has a :hover state.

    div {
    display: inline-block;
    margin: 30px;
    font-weight: bold;
    }
    
    #content {
    width: 160px;
    height: 160px;
    background-color: rgb(255, 0, 0);
    }
    
    #a, #b {
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    }
    
    #a {
    color: rgb(255, 0, 0);
    background-color: rgb(255, 255, 0);
    cursor: pointer;
    }
    
    #b {
    display: none;
    color: rgb(255, 255, 255);
    background-color: rgb(0, 0, 255);
    }
    
    #a:hover < #content + #b {
    display: inline-block;
    }
    Hover me
    Show me


    2. Remote Element axe Selector (\)

    #a:hover \ #b
    

    This axe style rule will select #b, which is present in the same document as #a which has a :hover state.

    div {
    display: inline-block;
    margin: 30px;
    font-weight: bold;
    }
    
    #content {
    width: 160px;
    height: 160px;
    background-color: rgb(255, 0, 0);
    }
    
    #a, #b {
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    }
    
    #a {
    color: rgb(255, 0, 0);
    background-color: rgb(255, 255, 0);
    cursor: pointer;
    }
    
    #b {
    display: none;
    color: rgb(255, 255, 255);
    background-color: rgb(0, 0, 255);
    }
    
    #a:hover \ #b {
    display: inline-block;
    }
    Hover me
    Show me

提交回复
热议问题