简体   繁体   English

元素底部的悬停状态

[英]Hover-State at bottom of element

I am currently developing a styleguide and the hover-state of my navigation is giving me some headache, but I am pretty sure, that it could be solved very easily, so I just want to ask, if someone has an idea.我目前正在开发一个样式指南,导航的悬停状态让我有些头疼,但我很确定它可以很容易地解决,所以我只想问一下,如果有人有想法。

My html looks like this:我的 html 看起来像这样:

<nav class='components__navigation'>
        <a href='../index.html'><img class='styleguide__logo' src='../imgs/Logo.png' alt='Logo'></a>
        <ul class='components__navListTop'>
            <li class='components__navItem'><a href='#'>Design</a></li>
            <li class='components__navItem' id='active'><a href='components.html'>Components</a></li>
            <li class='components__navItem'><a href='#'>Motionguide</a></li>
        </ul>
</nav>

and my css looks like this:我的 css 看起来像这样:

.components__navigation {
    display: flex;
    height: 75px;
    align-items: center;
    padding-left: 50px;
    border-bottom: 4px solid #f5f5f5;
}

.components__navigation img {
    width: 250px;
    height: 40px;
    margin-right: 100px;
}

.components__navListTop {
    font-size: 14px;
    display: flex;
    list-style: none;
    padding: 0;
}

.components__navListTop a {
    color: #333;
    text-decoration: none;
    margin-right: 50px;
}

.components__navItem a {
    text-decoration: none;
    background-image: linear-gradient(#ffda00,#ffda00),linear-gradient(#fff,#fff);
    background-size: 0 3px,auto;
    background-repeat: no-repeat;
    background-position: bottom;
    transition: background .2s linear;
}

.components__navItem a:hover {
    background-size: 100% 3px,auto;
}

#active a {
    background-image: linear-gradient(#ffda00,#ffda00),linear-gradient(#fff,#fff);
    background-size: 100% 3px,auto;
    background-repeat: no-repeat;
    background-position: bottom;
}

Here is a pen to make it more visual: https://codepen.io/dbljn/pen/abbMdVE这是一支使它更直观的笔: https://codepen.io/dbljn/pen/abbMdVE

The current hover-state is right under the font of the tabs, but I want the hover-state to be where the gray border is.当前的悬停状态位于选项卡字体的正下方,但我希望悬停状态位于灰色边框所在的位置。 Like so:像这样: 在此处输入图像描述

Do you have an idea?你有想法吗? And thanks a lot!!非常感谢!!

It is always a good practice to write similar styling rules when the elements are overlapping on each other.当元素相互重叠时,编写类似的样式规则始终是一个好习惯。 for example, we could use the same background-image property with nav html element instead of border property例如,我们可以使用与导航html 元素相同的背景图像属性而不是边框属性

.components__navigation {
    display: flex;
    align-items: center;
    padding-left: 50px;
    background-image: linear-gradient(transparent 90%, #f5f5f5 0);
}

remove the margin from ul element and background-image from below selectors so that the nav element will spread on li element and will be dynamic从下面的选择器中删除ul元素和background-image的边距,以便nav元素将在li元素上展开并且将是动态的

.components__navListTop {
    font-size: 14px;
    display: flex;
    list-style: none;
    padding: 0;
    margin: 0;
}

.components__navListTop a {
    color: #333;
    text-decoration: none;
}

finally give the li element its background-image and other CSS rules,最后给li元素它的背景图像和其他 CSS 规则,

.components__navItem {
    padding: 20px 0;
    margin-right: 50px;
    background-image: linear-gradient(transparent 90%,#ffda00 0);
    background-size: 0;
    background-repeat: no-repeat;
    background-position: bottom;
    transition: background .2s linear;
}

Since you want the animation while hovering on li element, the :hover pseudo attribute should be present on this li element, also it's good to use classes instead of id由于您希望 animation 悬停在li元素上,因此:hover伪属性应该出现在此li元素上,使用类而不是 id 也很好

.components__navItem:hover, .active {
    background-size: 100%;
}

Finally putting it all together,最后把所有的东西放在一起,

 body, html { margin: 0; }.components__navigation { display: flex; align-items: center; padding-left: 50px; background-image: linear-gradient(transparent 90%, #f5f5f5 0); }.components__navigation img { width: 250px; height: 40px; margin-right: 100px; }.components__navListTop { font-size: 14px; display: flex; list-style: none; padding: 0; margin: 0; }.components__navListTop a { color: #333; text-decoration: none; }.components__navItem { padding: 20px 0; margin-right: 50px; background-image: linear-gradient(transparent 90%,#ffda00 0); background-size: 0; background-repeat: no-repeat; background-position: bottom; transition: background.2s linear; }.components__navItem:hover, .active { background-size: 100%; }
 <nav class='components__navigation'> <a href='../index.html'><img class='styleguide__logo' src='../imgs/Logo.png' alt='Logo'></a> <ul class='components__navListTop'> <li class='components__navItem active'><a href='#'>Design</a></li> <li class='components__navItem'><a href='components.html'>Components</a></li> <li class='components__navItem'><a href='#'>Motionguide</a></li> </ul> </nav>

Try replacing .components__navItem a with:尝试将.components__navItem a替换为:

.components__navItem a {
    text-decoration: none;
    background-image: linear-gradient(#ffda00,#ffda00);
    background-size: 0 3px,auto;
    background-repeat: no-repeat;
    background-position: bottom;
    transition: background .2s linear;
    padding-bottom: 33px;
}

This is assuming a fixed font size though.这是假设一个固定的字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM