【问题标题】:Trying to create text hover effect where the underline highlights the text upon hovering尝试创建文本悬停效果,其中下划线在悬停时突出显示文本
【发布时间】:2020-08-30 00:12:18
【问题描述】:

当我将鼠标悬停在“Olivia Carino”下方时,我试图在文本下方添加下划线以突出显示该文本。当我将鼠标悬停在名称上时,该行丢失了,而是填充了整个背景。关于为什么会发生这种情况或如何解决它的任何想法?

这是我的代码:https://jsfiddle.net/oliviacarino/qvz9u7dy/

    <!DOCTYPE html>
<html class="no-js">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Olivia Carino</title>
    <meta name="description" content="This is my revamped portfolio :)">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@300;400;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="main.css">
</head>

<body>

    <nav class="navbar">
        <ul class="navbar-nav">
            <li class="nav-item">
                <a class="nav-link" href="#">
                    <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="home"
                        class="svg-inline--fa fa-home fa-w-18" role="img" xmlns="http://www.w3.org/2000/svg"
                        viewBox="0 0 576 512">
                        <path fill="currentColor"
                            d="M280.37 148.26L96 300.11V464a16 16 0 0 0 16 16l112.06-.29a16 16 0 0 0 15.92-16V368a16 16 0 0 1 16-16h64a16 16 0 0 1 16 16v95.64a16 16 0 0 0 16 16.05L464 480a16 16 0 0 0 16-16V300L295.67 148.26a12.19 12.19 0 0 0-15.3 0zM571.6 251.47L488 182.56V44.05a12 12 0 0 0-12-12h-56a12 12 0 0 0-12 12v72.61L318.47 43a48 48 0 0 0-61 0L4.34 251.47a12 12 0 0 0-1.6 16.9l25.5 31A12 12 0 0 0 45.15 301l235.22-193.74a12.19 12.19 0 0 1 15.3 0L530.9 301a12 12 0 0 0 16.9-1.6l25.5-31a12 12 0 0 0-1.7-16.93z">
                        </path>
                    </svg>
                    <span class="link-text">Home</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">
                    <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="code"
                        class="svg-inline--fa fa-code fa-w-20" role="img" xmlns="http://www.w3.org/2000/svg"
                        viewBox="0 0 640 512">
                        <path fill="currentColor"
                            d="M278.9 511.5l-61-17.7c-6.4-1.8-10-8.5-8.2-14.9L346.2 8.7c1.8-6.4 8.5-10 14.9-8.2l61 17.7c6.4 1.8 10 8.5 8.2 14.9L293.8 503.3c-1.9 6.4-8.5 10.1-14.9 8.2zm-114-112.2l43.5-46.4c4.6-4.9 4.3-12.7-.8-17.2L117 256l90.6-79.7c5.1-4.5 5.5-12.3.8-17.2l-43.5-46.4c-4.5-4.8-12.1-5.1-17-.5L3.8 247.2c-5.1 4.7-5.1 12.8 0 17.5l144.1 135.1c4.9 4.6 12.5 4.4 17-.5zm327.2.6l144.1-135.1c5.1-4.7 5.1-12.8 0-17.5L492.1 112.1c-4.8-4.5-12.4-4.3-17 .5L431.6 159c-4.6 4.9-4.3 12.7.8 17.2L523 256l-90.6 79.7c-5.1 4.5-5.5 12.3-.8 17.2l43.5 46.4c4.5 4.9 12.1 5.1 17 .6z">
                        </path>
                    </svg>
                    <span class="link-text">Projects</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">
                    <svg aria-hidden="true" focusable="false" data-prefix="fas" data-icon="briefcase"
                        class="svg-inline--fa fa-briefcase fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg"
                        viewBox="0 0 512 512">
                        <path fill="currentColor"
                            d="M320 336c0 8.84-7.16 16-16 16h-96c-8.84 0-16-7.16-16-16v-48H0v144c0 25.6 22.4 48 48 48h416c25.6 0 48-22.4 48-48V288H320v48zm144-208h-80V80c0-25.6-22.4-48-48-48H176c-25.6 0-48 22.4-48 48v48H48c-25.6 0-48 22.4-48 48v80h512v-80c0-25.6-22.4-48-48-48zm-144 0H192V96h128v32z">
                        </path>
                    </svg>
                    <span class="link-text">Experience</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">
                    <svg aria-hidden="true" focusable="false" data-prefix="fab" data-icon="github"
                        class="svg-inline--fa fa-github fa-w-16" role="img" xmlns="http://www.w3.org/2000/svg"
                        viewBox="0 0 496 512">
                        <path fill="currentColor"
                            d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z">
                        </path>
                    </svg>
                    <span class="link-text">GitHub</span>
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">
                    <svg aria-hidden="true" focusable="false" data-prefix="fab" data-icon="linkedin"
                        class="svg-inline--fa fa-linkedin fa-w-14" role="img" xmlns="http://www.w3.org/2000/svg"
                        viewBox="0 0 448 512">
                        <path fill="currentColor"
                            d="M416 32H31.9C14.3 32 0 46.5 0 64.3v383.4C0 465.5 14.3 480 31.9 480H416c17.6 0 32-14.5 32-32.3V64.3c0-17.8-14.4-32.3-32-32.3zM135.4 416H69V202.2h66.5V416zm-33.2-243c-21.3 0-38.5-17.3-38.5-38.5S80.9 96 102.2 96c21.2 0 38.5 17.3 38.5 38.5 0 21.3-17.2 38.5-38.5 38.5zm282.1 243h-66.4V312c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9V416h-66.4V202.2h63.7v29.2h.9c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9V416z">
                        </path>
                    </svg>
                    <span class="link-text">LinkedIn</span>
                </a>
            </li>
        </ul>
    </nav>

    <main>
        <a class="name-highlight" href="#">
            <h1 class="jumbo-text-first">Olivia</h1>
        </a>
        <a class="name-highlight" href="#">
            <h1 class="jumbo-text-last">Carino</h1>
        </a>



    </main>



    <script src="script.js" async defer></script>
</body>
:root {
    font-size: 16px;
    font-family: 'Roboto Mono';
    --text-primary: black;
    --text-secondary: white;
    --bg-primary: #3d3d3d;
    --bg-secondary: #161616;
}

body {
    color: black;
    background-color: white;
    margin: 0;
    padding: 0;
}

body::-webkit-scrollbar {
    width: 0.25rem;
}

body::-webkit-scrollbar-track {
    background: #1e1e24;
}

body::-webkit-scrollbar-thumb {
    background: #35FCCE;
}

main {
    margin-left: 5rem;
    padding: 1rem;
}

.navbar {
    position: fixed;
    background-color: var(--bg-primary);
    transition: width 200ms ease;
}

.navbar-nav {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
}

.nav-item {
    width: 100%;
}

.nav-item:nth-last-child(2) {
    margin-top: auto;
}

.nav-item:last-child {
    margin-top: 0;
}

.nav-link {
    display: flex;
    align-items: center;
    height: 5rem;
    color: var(--text-secondary);
    text-decoration: none;
    filter: grayscale(100%) opacity(0.7);
    transition: var(--transition-speed);
}

.nav-link:hover {
    filter: grayscale(0%) opacity(1);
    background: var(--bg-secondary);
    color: var(--text-secondary);
}

.link-text {
    display: none;
    margin-left: 1rem;
}

.nav-link svg {
    min-width: 2rem;
    margin: 0 1.5rem;
}

.navbar:hover {
    width: 15rem;
}

.navbar:hover .link-text {
    display: block;
}

.jumbo-text-first {
    font-size: 8em;
    margin: 0;
    padding: 0;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);

}

.jumbo-text-last {
    font-size: 8em;
    margin: 0;
    padding: 0;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(25%);
}

.name-highlight {
    color: black;
}

.name-highlight:before {
    content: '';
    display: block;
    height: 4px;
    width: 100%;
    background: #35FCCE;
    position: absolute;
    bottom: 0;
    left: 0;
    transition: height .7s;
    z-index: -1;
}

.name-highlight:hover:before {
    height: 100%;
}




/* Small Screens */
@media only screen and (max-width: 600px) {
    .navbar {
        bottom: 0;
        width: 100vw;
        height: 5rem;
    }

    .navbar-nav {
        flex-direction: row;
    }

    .nav-link {
        justify-content: center;
    }

    main {
        margin: 0;
    }

    .jumbo-text-first {
        font-size: 4em;
        margin: 0;
        padding: 0;
        text-align: center;
        position: absolute;
        top: 30%;
        left: 50%;
        transform: translateX(-50%) translateY(-50%);

    }

    .jumbo-text-last {
        font-size: 4em;
        margin: 0;
        padding: 0;
        text-align: center;
        position: absolute;
        top: 30%;
        left: 50%;
        transform: translateX(-50%) translateY(25%);
    }
}

/* Large Screens */
@media only screen and (min-width: 600px) {
    .navbar {
        top: 0;
        width: 5rem;
        height: 100vh;
    }

    .navbar:hover {
        width: 16rem;
    }

    .navbar:hover .link-text {
        display: inline;
        transition: opacity var(--transition-speed);
    }


}

/* Screens above 1920px */
@media (min-width: 1920px) and (max-width: 2560px) {
    .jumbo-text-first {
        font-size: 10em;
        margin: 0;
        padding: 0;
        text-align: center;
        position: absolute;
        top: 30%;
        left: 50%;
        transform: translateX(-50%) translateY(-50%);

    }

    .jumbo-text-last {
        font-size: 10em;
        margin: 0;
        padding: 0;
        text-align: center;
        position: absolute;
        top: 30%;
        left: 50%;
        transform: translateX(-50%) translateY(25%);
    }
}

【问题讨论】:

  • 我们不需要您的整个页面,只需要相关代码
  • 不知道什么是相关的或最重要的,但感谢您的回复!

标签: html css css-animations


【解决方案1】:

下面的代码会覆盖您的悬停样式

.name-highlight:hover:before {
    height: 100%;
}
.name-highlight:before {
    content: '';
    display: block;
    height: 4px;
    width: 100%;
    background: #35FCCE;
    position: absolute;
    bottom: 0;
    left: 0;
    transition: height .7s;
    z-index: -1;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2017-10-25
    • 1970-01-01
    • 2014-01-02
    • 2015-09-21
    • 2014-02-11
    • 2021-11-08
    相关资源
    最近更新 更多