【问题标题】:CSS Building a navigation bar using :before and :after inserting bracket image but :before bracket missingCSS 使用 :before 和 :after 插入括号图像但 :before 括号丢失构建导航栏
【发布时间】:2012-12-18 21:37:51
【问题描述】:

我正在构建一个导航栏,需要在按钮之前和之后插入一个括号,以便在悬停并选择时突出显示按钮,如下例所示

[  Button 1  ]    button 2    button 3    button 4

我正在使用括号宽度=7px 高度=30px 的图像。我的 CSS 有限,我在自学!如果我使用 position: relative 如下

,我已经设法让它全部工作了
#np-access a:hover:before{ 
  content: url(images/bracket-left.png); 
  position:relative;
  top: 10px; 
}

#np-access a:hover:after {
   content: url(images/bracket-right.png); 
   position:relative;
   top: 10px;
}

但这会导致导航“bounce”,当鼠标悬停时按钮会向右移动。 我改变了这一点并使用了 position: absolute ,它可以工作,但是括号显示在文本 (narrow) 上。为了将它们分开一点,我为 :before 添加了 left: 0.5px 并为 :after 添加了 right: 0.5px 可行,但第一个括号丢失,其他括号看起来不错,但太近了,即按钮 2 右括号太靠近按钮 3 左括号(无填充),但它们可以正常工作(下面的示例)。

   button 1  ][   button 2   ][  button 3  ][  button 4  ]

为了尝试在括号中添加一些填充(按钮 2 右括号太靠近按钮 3 左括号),我将锚属性的填充从 1.5em 增加到 2em强>但刹车紧随其后,外观保持不变

#np-access a {
color: #5F5B5B; 
display: block;
line-height: 3.333em;
padding: 0 1.5em;
text-decoration: none;


}

为什么使用绝对位置时缺少第一个括号?以及如何添加填充?希望有人可以解释我哪里出错了!在此先感谢我的 CSS 在下面

#np-access .current-menu-item > a:after,
#np-access .current-menu-ancestor > a:after,
#np-access .current_page_item > a:after,
#np-access .current_page_ancestor > a:after {
 content: url(images/bracket-right.png); 
 position:absolute;
 right: 0.5px;
 top: 10px;
}

#np-access .current-menu-item > a:before,
#np-access .current-menu-ancestor > a:before,
#np-access .current_page_item > a:before,
#np-access .current_page_ancestor > a:before {
     content: url(images/bracket-left.png); 
     position:absolute;
     left: -0.5px;
     top: 10px; 
}


#np-access a:hover:before{ 
    content: url(images/bracket-left.png); 
    position:absolute;
    left: -0.5px;
    top: 10px; 
}

#np-access a:hover:after {
    content: url(images/bracket-right.png); 
    position:absolute;
    right: 0.5px;
    top: 10px;

}

现在可以工作的代码

#np-access a:hover:before{ 
   content: url(images/bracket-left.png); 
    position:absolute;
    left: 1px;
    top: 10px; 
    padding: 0 10px;
 }

 #np-access a:hover:after {
    content: url(images/bracket-right.png); 
    position:absolute;
    right: 0.5px;
    top: 10px;
    padding: 0 10px;
  }

谢谢你!

【问题讨论】:

  • 你可能会错过 position: relative;对于#np-access a
  • 感谢 dmi3y 添加位置:相对于#np-access a 但没有区别我恐怕把左边:0px;他们都显示了,但首先在括号之前没有显示,有什么想法吗?

标签: css wordpress


【解决方案1】:

左边的 -0.5px 导致第一个括号超出页面。

为什么在 :before 和 :after 上没有 left:0 right:0 而是在 #np-access a:hover 中添加了一个 padding: 0 3px; ??

【讨论】:

  • 您好,感谢您的帮助! @dagfr 按照建议完成,但括号到处都是我删除了填充并放在左边:0px;并保留其余的但左括号仍然不显示??
  • 我已经尝试过更改您建议的 dagfr 的部分内容,并且我已经设法让它发挥作用,您的建议是正确的;)请查看我添加的内容以使其与您的建议一起使用! !请参阅上面的修改代码谢谢
猜你喜欢
  • 2022-10-24
  • 1970-01-01
  • 2011-08-17
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多