【问题标题】:How to change image border and text simultaneously on hover?如何在悬停时同时更改图像边框和文本?
【发布时间】:2021-12-02 10:52:38
【问题描述】:

我正在构建一个 Netflix 克隆,只是作为一个个人项目。

在悬停时的 Netflix 个人资料部分,图像周围的边框和用户名都会突出显示。

当我尝试使用我的代码时,在悬停时,边框和文本会分别更改。我在一个多小时内尝试了多种方法,但没有成功。我知道我遗漏了一些简单化的东西,并且可能想多了,但我不确定如何处理这个问题,无论是创建一个 JavaScript 函数,还是在 CSS 中。 (我正在努力将 JS 添加到我的项目中,这就是我构建这个克隆的原因!)?

提示或提示会很棒,因为我仍在努力解决问题

body, html {
    height: 100%;
    margin: 0px;
    margin-top: -11px;
    margin-left: 22.5px;
    padding: 0px;
    color: white;
    font-family: 'Roboto', sans-serif;
    background-color: rgb(17, 17, 17);
}

.container {
    text-align: center;
    position: relative;
    top: 10%;
}

/* 
----------------
PROFILES SECTION
----------------
*/

.profiles {
    margin: 20px;
    padding: 20px;
    display: flex;
    justify-content: center;
}


.prfImg {
    margin: -6px;
    padding: 12px;
}

.prfImg > img {
    margin: 2px;
    padding: 2px;
    border: 0.5px solid rgba(255, 255, 255, 0.061);
    border-radius: 3px;
}

.prfImg > img:hover {
    color: rgba(255, 255, 255, 0.787);
    border: 3px solid rgba(255, 255, 255, 0.787);
    border-radius: 3px;
}

#user {
    font-size: 25px;
    color: grey;
}

/* 
----------------
BUTTON
----------------
*/

.btn {
    margin: 30px;
    padding: 30px;
    text-align: center;
}

button {
    margin-top: 30px;
    font-size: 22px; 
    color: grey;
    padding: 10px;
    background-color: transparent;
    border: 1.5px solid grey;
}

button:hover {
    color: rgba(255, 255, 255, 0.863);
    border: 1.5px solid rgba(255, 255, 255, 0.863);
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="profiles.css">
    <!-- google font -->
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">
    <title>NETFLIX - Profiles</title>
</head>

<body>
    <header>
        <img src="Img_d.profiles/netflix_logo.png" alt="netflix logo TM" width="99.2px">
    </header>

    <div class="container">
        <div class="header-text">
            <h1 style="font-size: 50px;">Who's watching?</h1>
        </div>
        <div class="profiles">
            <div class="prfImg">
                <img src="Img_d.profiles/profile_one.png" alt="Profile One" width="75%" label="hello">
                <p class="prfImg" id="user">A name</p>
            </div>
            <div class="prfImg">
                <img src="Img_d.profiles/profile_two.png" alt="Profile Two" width="75%">
                <p class="prfImg" id="user">A name</p>
            </div>
            <div class="prfImg">
                <img src="Img_d.profiles/profile_three.png" alt="Profile Three" width="75%">
                <p class="prfImg" id="user">A name</p>
            </div>
            <div class="prfImg">
                <img src="Img_d.profiles/profile_four.png" alt="Profile Foud" width="75%">
                <p class="prfImg" id="user">A name</p>
            </div>
        </div>
        <!-- button class -->
    </div>

    <div class="btn">
        <button>Manage Profiles</button>
    </div>

</body>

</html>

我自己!*

【问题讨论】:

  • 对我来说,您共享的代码 sn-p 似乎可以正常工作。它不适用于真实图像(代码 sn-p 是用替代文本代替)吗?

标签: javascript html css hover


【解决方案1】:

您可以将悬停事件放在父div上。

改变这个:

.prfImg > img:hover {
    color: rgba(255, 255, 255, 0.787);
    border: 3px solid rgba(255, 255, 255, 0.787);
    border-radius: 3px;
}

到这里

.prfImg:hover {
    color: rgba(255, 255, 255, 0.787);
    border: 3px solid rgba(255, 255, 255, 0.787);
    border-radius: 3px;
}

【讨论】:

  • 我试过了,但它悬停在图像和文本周围的边框上。我想回头看看是不是我的 HTML 做错了导致 CSS 代码倾斜?
  • @ZiPage 将p 的类名更改为不同的名称。喜欢prfImg-text
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 1970-01-01
  • 2012-07-28
  • 2017-03-18
相关资源
最近更新 更多