为什么我的锚点在点击时不会显得凹陷?

发布于 2025-01-11 19:24:58 字数 1968 浏览 2 评论 0原文

我正在参加编码课程,我们正在制作一个带有可点击链接的导航栏。问题是我的根本不起作用。我添加了位置:相对;顶部:0;这样按钮在单击后会向下移动,并且在单击链接后也会删除框阴影。它似乎没有做任何事情。有谁知道可能是什么问题?抱歉,如果我的问题没有任何意义。

body {
  margin: 10%;
  font-family: Tahoma, Verdana, Geneva, sans-serif;
}

nav {
  overflow: hidden;
  width: 43%;
  margin: 0 auto;
  padding-bottom: 1em;
}

nav ul {
  list-style-type: none;
  /* Step 2: Remove bullets */
}

nav ul li {
  float: left;
  /* Step 3: Float li's left to line up horizontally */
}


/* Psuedoclass Class Selectors - State of links LVHA */

nav a:link {
  /* Default state of link */
  display: block;
  /* Step 4: Gives anchor tag structure */
  width: 6em;
  /* Increases width of links and makes all buttons a standard width */
  border: 2px solid rgb(175, 175, 175);
  text-align: center;
  text-decoration: none;
  padding: .5em 1em;
  /* Gives breathing room between content and inside of border */
  margin: 0 5px;
  border-radius: 6px;
  background-color: rgb(190, 190, 190);
  color: white;
  text-shadow: #666 .1em .1em .1em;
  /* Color, right, bottom, blur */
  box-shadow: rgba(0, 0, 0, .5) 0 5px 3px;
  /* Color, right, bototm, blur */
  /* Sets position of each button to relative and sets positions to zero */
  position: relative;
  top: 0;
}

nav a:visited {
  border: 2px solid rgb(175, 175, 175);
  color: white;
}

nav a:hover {
  background-color: #fdca00;
  border-color: #fda700;
}

nav a:active a:focus {
  /* Moves button 3px down and removes shadow on click */
  top: 3px;
  box-shadow: rgba(0, 0, 0, .5) 0 0 0;
}
<nav>
  <ul>
    <li><a href="#">Men</a></li>
    <li><a href="#">Women</a></li>
    <li><a href="#">Kids</a></li>
    <li><a href="#">SALE</a></li>
  </ul>
</nav>

I'm taking a coding class and we were making a navigation bar with clickable links. The problem is mine wouldn't work at all. I added position: relative; and top: 0; so that way the buttons would move down after clicking and also removed the box-shadow after clicking on the link. It doesn't seem to do anything. Does anyone know what the problem might be? Sorry if my question doesn't make any sense.

body {
  margin: 10%;
  font-family: Tahoma, Verdana, Geneva, sans-serif;
}

nav {
  overflow: hidden;
  width: 43%;
  margin: 0 auto;
  padding-bottom: 1em;
}

nav ul {
  list-style-type: none;
  /* Step 2: Remove bullets */
}

nav ul li {
  float: left;
  /* Step 3: Float li's left to line up horizontally */
}


/* Psuedoclass Class Selectors - State of links LVHA */

nav a:link {
  /* Default state of link */
  display: block;
  /* Step 4: Gives anchor tag structure */
  width: 6em;
  /* Increases width of links and makes all buttons a standard width */
  border: 2px solid rgb(175, 175, 175);
  text-align: center;
  text-decoration: none;
  padding: .5em 1em;
  /* Gives breathing room between content and inside of border */
  margin: 0 5px;
  border-radius: 6px;
  background-color: rgb(190, 190, 190);
  color: white;
  text-shadow: #666 .1em .1em .1em;
  /* Color, right, bottom, blur */
  box-shadow: rgba(0, 0, 0, .5) 0 5px 3px;
  /* Color, right, bototm, blur */
  /* Sets position of each button to relative and sets positions to zero */
  position: relative;
  top: 0;
}

nav a:visited {
  border: 2px solid rgb(175, 175, 175);
  color: white;
}

nav a:hover {
  background-color: #fdca00;
  border-color: #fda700;
}

nav a:active a:focus {
  /* Moves button 3px down and removes shadow on click */
  top: 3px;
  box-shadow: rgba(0, 0, 0, .5) 0 0 0;
}
<nav>
  <ul>
    <li><a href="#">Men</a></li>
    <li><a href="#">Women</a></li>
    <li><a href="#">Kids</a></li>
    <li><a href="#">SALE</a></li>
  </ul>
</nav>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2025-01-18 19:24:59

问题出在这个选择器上:nav a:active a:focus

它转换为一个锚元素,其焦点位于导航元素内活动的锚元素内。你不想要这样。您需要一个包含两个单独的选择器的列表,这两个选择器都源自 nav 元素:

nav a:active, nav a:focus {}

这解决了元素的活动(当前正在单击)和聚焦(尚未选择其他元素)状态。

body {
  margin: 10%;
  font-family: Tahoma, Verdana, Geneva, sans-serif;
}

nav {
  overflow: hidden;
  width: 43%;
  margin: 0 auto;
  padding-bottom: 1em;
}

nav ul {
  list-style-type: none;
  /* Step 2: Remove bullets */
}

nav ul li {
  float: left;
  /* Step 3: Float li's left to line up horizontally */
}


/* Psuedoclass Class Selectors - State of links LVHA */

nav a:link {
  /* Default state of link */
  display: block;
  /* Step 4: Gives anchor tag structure */
  width: 6em;
  /* Increases width of links and makes all buttons a standard width */
  border: 2px solid rgb(175, 175, 175);
  text-align: center;
  text-decoration: none;
  padding: .5em 1em;
  /* Gives breathing room between content and inside of border */
  margin: 0 5px;
  border-radius: 6px;
  background-color: rgb(190, 190, 190);
  color: white;
  text-shadow: #666 .1em .1em .1em;
  /* Color, right, bottom, blur */
  box-shadow: rgba(0, 0, 0, .5) 0 5px 3px;
  /* Color, right, bototm, blur */
  /* Sets position of each button to relative and sets positions to zero */
  position: relative;
  top: 0;
}

nav a:visited {
  border: 2px solid rgb(175, 175, 175);
  color: white;
}

nav a:hover {
  background-color: #fdca00;
  border-color: #fda700;
}

nav a:active, nav a:focus {
  /* Moves button 3px down and removes shadow on click */
  top: 3px;
  box-shadow: rgba(0, 0, 0, .5) 0 0 0;
}
<nav>
  <ul>
    <li><a href="#">Men</a></li>
    <li><a href="#">Women</a></li>
    <li><a href="#">Kids</a></li>
    <li><a href="#">SALE</a></li>
  </ul>
</nav>

The problem lies with this selector: nav a:active a:focus

It translates to an anchor element with focus inside an anchor element that's active inside a nav element. You don't want that. You want a list with two separate selectors, both originating from the nav element:

nav a:active, nav a:focus {}

This addresses both the active (currently being clicked) and focused (no other element has been selected yet) states of the element.

body {
  margin: 10%;
  font-family: Tahoma, Verdana, Geneva, sans-serif;
}

nav {
  overflow: hidden;
  width: 43%;
  margin: 0 auto;
  padding-bottom: 1em;
}

nav ul {
  list-style-type: none;
  /* Step 2: Remove bullets */
}

nav ul li {
  float: left;
  /* Step 3: Float li's left to line up horizontally */
}


/* Psuedoclass Class Selectors - State of links LVHA */

nav a:link {
  /* Default state of link */
  display: block;
  /* Step 4: Gives anchor tag structure */
  width: 6em;
  /* Increases width of links and makes all buttons a standard width */
  border: 2px solid rgb(175, 175, 175);
  text-align: center;
  text-decoration: none;
  padding: .5em 1em;
  /* Gives breathing room between content and inside of border */
  margin: 0 5px;
  border-radius: 6px;
  background-color: rgb(190, 190, 190);
  color: white;
  text-shadow: #666 .1em .1em .1em;
  /* Color, right, bottom, blur */
  box-shadow: rgba(0, 0, 0, .5) 0 5px 3px;
  /* Color, right, bototm, blur */
  /* Sets position of each button to relative and sets positions to zero */
  position: relative;
  top: 0;
}

nav a:visited {
  border: 2px solid rgb(175, 175, 175);
  color: white;
}

nav a:hover {
  background-color: #fdca00;
  border-color: #fda700;
}

nav a:active, nav a:focus {
  /* Moves button 3px down and removes shadow on click */
  top: 3px;
  box-shadow: rgba(0, 0, 0, .5) 0 0 0;
}
<nav>
  <ul>
    <li><a href="#">Men</a></li>
    <li><a href="#">Women</a></li>
    <li><a href="#">Kids</a></li>
    <li><a href="#">SALE</a></li>
  </ul>
</nav>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文