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
207 views
in Technique[技术] by (71.8m points)

html - css hover img not work on other class opacity

I've this HTML file. I'm trying to implement :hover property so when I pass over .linux-face element the .info element set opacity to 0. I'm trying to get out of this for about 30 minutes. I've also try to map :hover to other class but nothing seems working.

<!DOCTYPE html>
<html>
    
    <head>
        <!-- Setup -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@300;700&display=swap" rel="stylesheet"> 
        <link rel="stylesheet" href="main.css" />
        <title> Linus Torvald aka Linux </title>
    </head>

    <body id="main">
        
        <section class="main-section">
            <div class="container">
                <h1 id="title" class="main-title"> Linus Torvald </h1>
                
                <div class="image-section" id="img-div">
                    
                    <img id ="image" class="linux-face" src="img/linus_torvald.jpg" alt="Linus Torvald smiling face :)">
                    <p id="img-caption">Linus Torvald smiling face :) </p>
                </div>
                
                <div class="info">
                    <p id="tribute-info"> <span class="linux_info"> Hello, World! </span> </p>
                </div>
             

            </div>
    </body>
</html>

That's my scss file:

#main {
    display: flex;
    justify-content: center;
}
.main-title {
    font-family: 'Josefin Sans', sans-serif;
}

.linux-face { 
    &:hover {
        .info{ 
            opacity:0;   
    }
}}

If I try something like

.linux-face:hover {
    opacity: 0;
}

the hover effect on .linux-face works fine. Also this work fine

.container:hover {
    .info {
        opacity: 0;
    }
}
question from:https://stackoverflow.com/questions/65841684/css-hover-img-not-work-on-other-class-opacity

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

1 Reply

0 votes
by (71.8m points)
.linux-face { 
    &:hover {
       .info { 
          opacity: 0;   
       }
    }
 }

gets compiled into

.linux-face:hover .info { opacity: 0; }

but .linux-face is an img element so you are actually targeting a nested element inside the image which clearly doesn't exist —

if your goal is to change the opacity of the .info element when you hover the image then you can't simply do it with the given markup: the most you could do is .image-section:hover + .info { opacity: 0 } so it will work if you hover either the image or the caption paragraph.


As a side note you could use a more meaningful and semantic markup

<figure>
   <img src="img/linus_torvald.jpg" alt="Linus Torvald smiling face" />
   <figcaption>Linus Torvald smiling face</figcaption>
</figure>

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

...