根据类更改跨度标记上从左到右的绝对位置

发布于 2024-08-10 06:23:00 字数 1260 浏览 5 评论 0原文

这是我在这里提出的第一个问题,但我相信还会有更多问题。 我有一个小位置问题,我想知道是否有可能实现。

我有一个带有浮动

  • 的无序列表 (
      ),对于主要选项,我将其浮动到左侧,对于联系和支持选项,我将其浮动让它们向右浮动,到目前为止一切顺利。现在我在每个
    • 内都有一个 ,它显示在水平列表下,该列表用于菜单。这些 作为菜单选项的描述,对于我使用 position:absolute; 的普通选项。顶部:30px; left:0; 这正如怀疑的那样工作。现在我想更改那些向右浮动的菜单项的位置属性,以便跨度获得 position:absolute;顶部:30px; right:0; 这根本不起作用。似乎不可能用比另一个更具体的 css 规则来改变它,但浮动效果很好。
  • html:

    <div id="menu">
    <ul>
        <li>Menu1<span>Info1</span></li>
        <li>Menu2<span>Info2</span></li>
        <li class="support">Support1<span>Info3</span></li>
    </ul>
    

    css:

    #menu{position:relative;}
    #menu ul{list-style:none;}
    #menu ul li{float:left;}
    #menu ul li.support{float:right;}
    #menu ul li span{display:none;}
    #menu ul li:hover span{display:block; position:absolute; top:30px; left:0;}
    #menu ul li.support:hover span{display:block; position:absolute; top:30px; right:0}
    

    css 中的最后一行没有区别! 如果有人有答案或解决方法,我非常感谢您的帮助。

    问题得到解答,问题得到解决。检查詹姆斯·阿伦斯或墨卡托的帖子。

    This is my first question here but I'm sure there are many more to come.
    I have a small position problem that I would like to know if it's even possible to achieve.

    I have an unordered list (<ul>) with floated <li>s, for the main options i float these left, and for the contact and support options I float them right, so far so good. Now i have a <span> inside each <li> which i display under the horizontal list, the list is for a menu. These <span>s works as description to menu choice and for the normal options I use position:absolute; top:30px; left:0; this works as suspected. Now I would like to change the position attributes for those menu items that i float right so that the span gets position:absolute; top:30px; right:0; and this doesn't work at all. It seems like it's impossible to change this with a more specific css rule then the other, yet the float works great.

    the html:

    <div id="menu">
    <ul>
        <li>Menu1<span>Info1</span></li>
        <li>Menu2<span>Info2</span></li>
        <li class="support">Support1<span>Info3</span></li>
    </ul>
    

    The css:

    #menu{position:relative;}
    #menu ul{list-style:none;}
    #menu ul li{float:left;}
    #menu ul li.support{float:right;}
    #menu ul li span{display:none;}
    #menu ul li:hover span{display:block; position:absolute; top:30px; left:0;}
    #menu ul li.support:hover span{display:block; position:absolute; top:30px; right:0}
    

    The last line in the css make no difference!
    If anyone has an answer or a work around I greatly appreciate the help.

    Question answered and problem solved. Check James Arons or mercator's posts.

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

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

    发布评论

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

    评论(3

    沫尐诺 2024-08-17 06:23:00

    即使您设置了 right:0,您的 left:0 仍然会被继承。您需要设置 left:auto 来覆盖 support 类的该样式。

    Even thought you set right:0, your left:0 is still being inherited. You need to set left:auto to override that style for the support class.

    祁梦 2024-08-17 06:23:00

    从该 CSS 来看,您似乎没有正确理解 CSS,或者至少没有正确使用它。您可能需要阅读有关级联、特异性和继承的 SitePoint 文章,以获得详细的介绍。

    不起作用的原因是该 CSS 的最后两行适用于具有 support 类的列表项。倒数第二行适用于所有 列表项,最后一行添加到support 列表项。因此,由于最后一行中没有 left 属性,因此 left 属性从上一行向下级联,使其成为 left: 0 也是

    当您使用类来标记特殊情况时,您不应该重复一般情况的所有 CSS,而只提供更改它所需的 CSS:

    #menu ul li:hover span {display:block; position:absolute; top:30px; left:0;}
    #menu ul li.support:hover span {left:auto; right:0;}
    

    该 CSS 将意味着 support -class 列表项获取其他列表项获取的所有 CSS,但将 left 属性重置为默认值,并设置 right 属性。

    Judging from that CSS, you don't seem to understand CSS correctly, or you aren't using it correctly at least. You might want to read the SitePoint article about the cascade, specificity and inheritance for a good introduction.

    The reason that doesn't work is because the last two lines of that CSS apply to the list items with the support class. The second-to-last line applies to all list items, and the last line then adds to that for the support list items. So, since there's no left property in that last line, the left property cascades down from the previous line, making it left: 0 too.

    When you're using a class to mark a special case, you shouldn't be repeating all the CSS of the general case, but only provide the CSS necessary to change it:

    #menu ul li:hover span {display:block; position:absolute; top:30px; left:0;}
    #menu ul li.support:hover span {left:auto; right:0;}
    

    That CSS will mean the support-class list items get all the CSS the other list items get, but with the left property reset to default, and the right property set instead.

    空‖城人不在 2024-08-17 06:23:00

    嗯很奇怪。我无法告诉你为什么你的代码不起作用,应该起作用,因为 support:hover span 是一个更具体的选择器。

    不过找到了修复方法:
    左边的绝对元素位置:0;默认情况下。
    如果删除 left: 0;从跨度选择器它将起作用。

    你的代码看起来像这样:

    #menu{position:relative;}
    #menu ul{list-style:none;}
    #menu ul li{float:left;}
    #menu ul li.support{float:right;}
    #menu ul li span{display:none;}
    #menu ul li:hover span{display:block; position:absolute; top:30px;}
    #menu ul li.support:hover span{display:block; position:absolute; top:30px; right:0}
    

    Hmm weird. I can't tell you why your code does not work, should work since the support:hover span is a more specific selector.

    Found a fix though:
    Position absolute elements got left: 0; by default.
    If you remove left: 0; from the span selector it will work.

    Your code would look like this:

    #menu{position:relative;}
    #menu ul{list-style:none;}
    #menu ul li{float:left;}
    #menu ul li.support{float:right;}
    #menu ul li span{display:none;}
    #menu ul li:hover span{display:block; position:absolute; top:30px;}
    #menu ul li.support:hover span{display:block; position:absolute; top:30px; right:0}
    
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文