如何使用CSS为导航栏设置下划线的格式?

问题描述 投票:0回答:1

我想出了如何在我的导航栏的一部分,当我把鼠标悬停在单词上时,在下面划一个下划线。我试图让它以同样的格式,当我在那个页面上,使下划线留在那里。我不明白这个问题。希望得到一些帮助,谢谢:)

*{
    margin:0;
    padding:0;
    border:0;
}


.topnav {
    background-color: purple;
    overflow: hidden;
  }
  
  .topnav a {
    float: left;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
    position: relative;
  }

  .topnav a:before {
    content: "";
    position: absolute;
    width: 84%;
    height: 2px;
    bottom: 3px;
    left: 8%;
    background-color: white;
    visibility: hidden;
    transform: scaleX(0);
    transition: all 0.3s ease-in-out 0s;
  }

 .topnav a:hover:before {
    visibility: visible;
    transform: scaleX(1);
  }
<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="style.css">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>
    Change
  </title>
</head>

<body>
  <div class="topnav">
    <a class="active" href="index.html">Home</a>
    <a href="#news">DontUse</a>
    <a href="#contact">DontUse</a>
    <a href="about.html">About</a>
  </div>
  <p>
    This is the home page
  </p>
</body>

</html>
html css format underline
1个回答
1
投票

在这里,你去,使用jquery。还做了一个新的活动菜单类 .topnav a.active-menu:before

$("a").click(function(){
   $("a.active-menu").removeClass("active-menu");
   $(this).addClass("active-menu");
});
*{
    margin:0;
    padding:0;
    border:0;
}


.topnav {
    background-color: purple;
    overflow: hidden;
  }
  
  .topnav a {
    float: left;
    color: #f2f2f2;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 17px;
    position: relative;
  }

  .topnav a:before {
    content: "";
    position: absolute;
    width: 84%;
    height: 2px;
    bottom: 3px;
    left: 8%;
    background-color: white;
    visibility: hidden;
    transform: scaleX(0);
    transition: all 0.3s ease-in-out 0s;
  }

 .topnav a:hover:before {
    visibility: visible;
    transform: scaleX(1);
  }
.topnav a.active-menu:before {
    content: "";
    position: absolute;
    width: 84%;
    height: 2px;
    bottom: 3px;
    left: 8%;
    background-color: white;
    visibility: visible;
    transform: scaleX(1);
    transition: all 0.3s ease-in-out 0s; 
    }
    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="style.css">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>
    Change
  </title>
</head>

<body>
  <div class="topnav" id="topnav">
    <a class="active-menu" href="index.html">Home</a>
    <a class="link" href="#news">DontUse</a>
    <a class="link" href="#contact">DontUse</a>
    <a class="link" href="about.html">About</a>
  </div>
  <p>
    This is the home page
  </p>
</body>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.