简体   繁体   English

如何在悬停菜单上隐藏文本并用下划线代替图像?

[英]How to hide text in menu on hover and put image with underline instead?

I want to make menu on top, and on hover mouse to hide text and display icon. 我想将菜单置于顶部,并将鼠标悬停以隐藏文本并显示图标。 And to add underline under icon.In my code i successfully change text to image but how to add underline under image and make underline 3px wight and 60px length? 并在icon下添加下划线。在我的代码中,我成功将文本更改为图像,但如何在图像下添加下划线并使下划线3像素宽和60像素长? There is my code- HTML: 我的代码是HTML:

<div class="menu topMenu">
<ul class="topMenuOptions">
    <li><span>111</span></li>
    <li><span>222</span></li>
    <li><span>333</span></li>
</ul>

CSS: CSS:

.topMenu {
text-transform: uppercase;
background-image: url('images/1.png');
position: absolute;
height: 88px;
width: 400px;
background: lightcoral;
margin-left: 332px;
padding-top: 30px;
 }

.topMenuOptions {
padding: 0;
margin: 0;
margin-left: 155px;
list-style-type: none;
}

.topMenuOptions li {
    cursor: pointer;
    padding: 10px;
    color: white;
    font-size: 14px;
    display: inline;
}

    .topMenuOptions li:hover:nth-child(1) {
        background: url('images/2.png') center center no-repeat;
        background-position: 20px 0;
        background-size: contain;
        color:transparent;
    }

    .topMenuOptions li:hover:nth-child(2) {
        background: url('images/3.png') center center no-repeat;
        background-size: contain;
        background-position: 20px 0;
        color: transparent;
    }

      .topMenuOptions li:hover:nth-child(3) {
        background: url('images/4.png') center center no-repeat;
        background-size: contain;
        background-position: 20px 0;
        color: transparent;
    }

Use border-bottom: 3px solid white; 使用下border-bottom: 3px solid white; , for example: , 例如:

.topMenuOptions li:hover:nth-child(1) {
    background: url('images/2.png') center center no-repeat;
    background-position: 20px 0;
    background-size: contain;
    color:transparent;
    border-bottom: 3px solid #fff;
}

As per your requirement, I made a fiddle which you can check. 根据您的要求,我做了一个小提琴,您可以检查一下。

for the underline, you can keep border-bottom of 3px with border-color initially as transparent. 对于下划线,您可以使3px的边框底部与最初的border-color保持透明。 this transparent color is required by default to avoid the jerking of the text when a border is added on hover. 默认情况下,此透明颜色是必需的,以避免在悬停时添加边框时文本的抖动。

I have modified your code a bit just for your understanding. 我对您的代码做了一些修改,仅供您理解。

 .topMenu { text-transform: uppercase; background-image: url('images/1.png'); position: absolute; height: 88px; width: 400px; background: lightcoral; /* margin-left: 332px; */ padding-top: 30px; } .topMenuOptions { padding: 0; margin: 0; margin-left: 155px; list-style-type: none; } .topMenuOptions li { cursor: pointer; padding: 10px; color: white; font-size: 14px; display: inline; border-bottom: 3px solid transparent; } .topMenuOptions li:hover:nth-child(1) { background: url('http://php.quicoto.com/wp-content/uploads/2013/06/css3.jpg') center center no-repeat; background-position: 0px 0; background-size: contain; color: transparent; border-bottom: 3px solid #fff; } .topMenuOptions li:hover:nth-child(2) { background: url('http://php.quicoto.com/wp-content/uploads/2013/06/css3.jpg') center center no-repeat; background-size: contain; background-position: 0px 0; color: transparent; border-bottom: 3px solid #fff; } .topMenuOptions li:hover:nth-child(3) { background: url('http://php.quicoto.com/wp-content/uploads/2013/06/css3.jpg') center center no-repeat; background-size: contain; background-position: 0px 0; color: transparent; border-bottom: 3px solid #fff; } 
 <div class="menu topMenu"> <ul class="topMenuOptions"> <li><span>111</span></li> <li><span>222</span></li> <li><span>333</span></li> </ul> 

JSFIDDLE JSFIDDLE

`<ul>
    <li class="option">
        <span id="text">111</span>
        <span id="image" class="hide">IMAGE</span>
    </li>
    <li class="option">
        <span id="text">222</span>
        <span id="image" class="hide">IMAGE</span>
    </li>
</ul>`


.option:hover .option > #image
{
display:block;
border-bottom:1px solid black;
}
.option:hover: .option > #text
{
display:none;
}
.hide
{
display:none;
}

you can do something like this, which is more efficient, inside the image span you can add a image tag, which will also do the job. 您可以执行这样的操作,这样效率更高,可以在图像范围内添加图像标签,这也可以完成任务。

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

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