悬停期间 html 对齐不正确

发布于 2025-01-07 05:12:08 字数 494 浏览 6 评论 0原文

我的网页上有一个菜单,我想在鼠标悬停的元素上添加一些强调。 在我将鼠标悬停之前,菜单看起来不错并且对齐方式看起来不错。为了添加强调,我添加了一个小箭头(或“>”符号)。问题是现在文本向右跳了 2 个字符。

我想添加“>”没有我的菜单文本向右跳转的字符。正确的行为是文本保留在 p 中 花边和箭头出现在悬停菜单项左侧 2 个空格处。

请注意,我试图改变我的对齐方式,但整个事情看起来很糟糕。只有 text-align: left 才能提供正确的对齐方式。

#leftMenu {
  position: absolute;
  top:28%;
  left:24%;
  height: 20%;
  width: 20%;
  font-weight:400
  font-size:0.5em;
}

#leftMenu a:hover:before {
  content: "> ";
}

如何添加箭头而不使文本跳到右侧?

I have a menu on a web page and I want to add some emphais onto the element that my mouse is hovering over.
Before I hover my mouse the menu looks good and the alignment looks good. To add emphsis, I added a small arrow (or '>' sign). The problem is that now the text jumps to the right by 2 characters.

I want to add the '>' character without my menu text jumping the right. The correct behavior is that the text remains in p
lace and the arrow appears 2 spaces to the left of the hovered menu item.

Note I tried to change my alignment and the whole thing looks like crap. Only text-align: left gives it the correct alignment.

#leftMenu {
  position: absolute;
  top:28%;
  left:24%;
  height: 20%;
  width: 20%;
  font-weight:400
  font-size:0.5em;
}

#leftMenu a:hover:before {
  content: "> ";
}

How can I add the arrow without the text jumping to the right?

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

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

发布评论

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

评论(2

玩套路吗 2025-01-14 05:12:08

只要有“>”已经存在,只是不可见。这样,它将占据菜单上的空间,因此当它出现时,任何内容都不会移动。这是一个小例子:

CSS:

#leftMenu {
  position: absolute;
  top:28%;
  left:24%;
  height: 20%;
  width: 20%;
  font-weight:400;
  font-size:0.5em;
}

.marker{
  visibility:hidden;
}

#leftMenu li:hover .marker{
  visibility:visible;
}

#leftMenu ul{
  list-style:none;
}

HTML:

<div id="leftMenu">
  <ul>
     <li><span class="marker">></span><a href="#">Link 1</a></li>
     <li><span class="marker">></span><a href="#">Link 2</a></li>
     <li><span class="marker">></span><a href="#">Link 3</a></li>
  </ul>
</div>

我不太确定菜单的确切布局,但我认为上面的内容涵盖了总体思路。

Just have the ">" already exist, just not visible. This way it will take up the real estate on the menu, so nothing will move when it appears. Here is a small example:

CSS:

#leftMenu {
  position: absolute;
  top:28%;
  left:24%;
  height: 20%;
  width: 20%;
  font-weight:400;
  font-size:0.5em;
}

.marker{
  visibility:hidden;
}

#leftMenu li:hover .marker{
  visibility:visible;
}

#leftMenu ul{
  list-style:none;
}

HTML:

<div id="leftMenu">
  <ul>
     <li><span class="marker">></span><a href="#">Link 1</a></li>
     <li><span class="marker">></span><a href="#">Link 2</a></li>
     <li><span class="marker">></span><a href="#">Link 3</a></li>
  </ul>
</div>

I'm not really sure of the exact layout of your menu, but I think the above gets across the general idea.

迷迭香的记忆 2025-01-14 05:12:08

您可以为正常条目添加一个左填充,其宽度与箭头稍后的宽度完全相同,并在将鼠标悬停在箭头上时将其删除。

然而,这可能会很混乱,因为箭头在每个浏览器中可能不具有相同的宽度(即使使用相同的字体和字体大小)。

另一种可能性是使用定位属性将箭头移动到正确的位置。例如,如果您在其上使用 position:absolute;,它将不再移动其他东西。请注意,它将绝对定​​位在没有标准位置(静态)的下一个父级内部。

You could add a padding-left for normal entries that takes exactly the width the arrow would have later and remove it when hovered in favor of the arrow.

However this could be quite a mess since the arrow may not have the same width in every browser (even with same font and font-size).

An other possibility is to use positioning properties to move the arrow to the right location. If you for example use position: absolute; on it, it will no longer move other things around. Be aware that it then will be positioned absolutely inside the next parent that does not have standard position (static).

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