简体   繁体   中英

Css border to :hover element

I'm new to css. I want to make a replica of the menu (excluding the sub-menu part) on this page: http://www.ibta-arabia.com/

This is my progress so far: https://jsfiddle.net/yny2u85j/

This is my code for the css:

.menu {
    margin: 0;
    font-family: Arial, Helvetica, sans-serif;
}

.topnav {
    overflow: hidden;
    background-color: #2C4059;
}

.topnav a {
    float: left;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
}

.topnav a:hover {
    background-color: #D90D29;
    color: white;
}

.topnav a.active {
    background-color: #4CAF50;
    color: white;
}

I'm unable to show a red line when the mouse is hovered on the menu and put separators among the menu content as on the website I linked.

Can anyone help me tweak my css code to look more the menu on the other website?

Here you can just use css pseudo elements to show the border on top: Here is you updated fiddle code: https://jsfiddle.net/yny2u85j/11/ and also remove border property to anchor.

 .menu { margin: 0; font-family: Arial, Helvetica, sans-serif; } .topnav { overflow: hidden; background-color: #2C4059; } .topnav a { float: left; color: white; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; position: relative; } .topnav a:before { content: ''; width: 100%; opacity: 0; transition: all ease .3s; background: #D90D29; position: absolute; top: 0; left: 0; height: 3px; } .topnav a:hover:before { transition: all ease .3s; opacity: 1; } .topnav a.active { background-color: #4CAF50; color: white; } 
 <div class="menu"> <div class="topnav"> <a href="#">&nbsp;</a> <a href="http://www.ibta-arabia.com/"><strong>Home</strong></a> <a href="http://www.ibta-arabia.com/contact-us/"><strong>Contact Us</strong></a> </div> </div> 

在.topnav a中添加border-top:2px纯透明,然后在.topnav a:hover和.topnav a:active中添加border-top-color:#D90D29

Check updated code with simple border style

 .menu { margin: 0; font-family: Arial, Helvetica, sans-serif; } .topnav { overflow: hidden; background-color: #2C4059; } .topnav a { float: left; color: white; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; border-top: 4px solid transparent; border-bottom: 4px solid transparent; } .topnav a:hover { /*background-color: #D90D29; color: white; */ border-top: 4px solid #d90d29; } .topnav a.active { background-color: #4CAF50; color: white; } 
 <div class="menu"> <div class="topnav"> <a href="#">&nbsp;</a> <a href="http://www.ibta-arabia.com/"><strong>Home</strong></a> <a href="http://www.ibta-arabia.com/contact-us/"><strong>Contact Us</strong></a> </div> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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