Changing body background when hovering over a link

后端 未结 3 1844
情话喂你
情话喂你 2020-12-11 14:12

I would like to set up my page so that when the user hovers over a link in a div, the color of the body background is changed, but only while hovering. This is the css

3条回答
  •  一生所求
    2020-12-11 14:34

    Short answer: It's not possible to do so in pure CSS yet, however...


    Long answer: There's a pseudoclass :has() that can select element, that has specific properties. In your case it would be something like this: body:has(div a:hover).

    Unfortunatelly, it has no support yet.


    However it's possible to do so using HTML and elements except you can't change background-color of element, because start tag is generated automatically by the browser before any element that should be inside it no matter where in code it's actually written.

    HTML:

    
        
        
    
    

    CSS:

    html, body, div {
        margin: 0;
        height: 100%;
    }
    #magic:hover + div {
        background: red;
    }
    input {
        position: fixed;
        left: -100%;
    }
    

提交回复
热议问题