简体   繁体   English

CSS的上一个子项不覆盖父边界

[英]CSS last-child not overriding parent border

First of all let me start by saying I'm fairly new to CSS, mostly done styling inline. 首先,我首先要说我对CSS还是相当陌生,主要是内联样式。 What I'm doing is using an <ul> as a menu, where the <li> items are menu items. 我正在做的是使用<ul>作为菜单,其中<li>项是菜单项。 Right borders are used as dividers between the menu items, but visually it would be better if the last menu item doesn't have the right border. 右边框用作菜单项之间的分隔线,但从视觉上看,如果最后一个菜单项没有右边框会更好。

CSS code CSS代码

.gf-menu.l1 > li > .item:after
{
    border-right: 1px solid #E3E3E3;
    bottom: 0;
    content: "";
    position: absolute;
    right: 0;
    top: 0;
}
.gf-menu.l1 > li:first-child
{
    margin-left: 5px;
}
.gf-menu.l1 > li:last-child 
{
    margin-right: 5px;
    border-right: 0;
} 

HTML HTML

<ul class="gf-menu l1 ">
    <li class="item487">
        <a class="item" href="link1">
            menu_item_1
        </a>      
    </li>
    <li class="item488">
        <a class="item" href="link2">
            menu_item_2
        </a>      
    </li>
    <li class="item489">
        <a class="item" href="link1">
            menu_item_2
        </a>      
    </li>
</ul>

I can only assume it has something to do with the after element as the margin is actually being added in the right place, but I can't figure it out. 我只能认为它与after元素有关,因为实际上在正确的位置添加了边距,但是我无法弄清楚。

Thanks in advance. 提前致谢。

I've used your HTML and wrote minimal CSS for you to understand 我已经使用了HTML并编写了最少的CSS供您理解

TIP : Give classes and id's name which have semantic meaning to it. 提示:提供具有语义含义的类和id的名称。 It is difficult to code when you give class names like these. 当给这些类名时,很难编码。

 .gf-menu.l1 li { display: inline-block; padding: 0 10px 0 10px; border-right: 1px solid red; } .gf-menu.l1 li:last-child { border-right: none; } 
 <ul class="gf-menu l1 "> <li class="item487"> <a class="item" href="link1"> menu_item_1 </a> </li> <li class="item488"> <a class="item" href="link2"> menu_item_2 </a> </li> <li class="item489"> <a class="item" href="link1"> menu_item_2 </a> </li> </ul> 

You don't need any :after . 您不需要任何:after

Here is a solution with a few advices : 这是一些建议的解决方案:

https://jsfiddle.net/c4Lrmpfx/ https://jsfiddle.net/c4Lrmpfx/

 .gf-menu.l1 { list-style: none; } .gf-menu.l1 > li { float: left; text-align: center; } .gf-menu.l1 > li a.item { /* Better to apply the style to the A instead of the LI, also to style the clickable area */ display: block; padding: 10px; text-decoration: none; background-color: rgba(0,0,0,0.1); } .gf-menu.l1 > li a.item:hover { background-color: rgba(0,0,0,0.2); } .gf-menu.l1 > li:not(:last-child) a.item{ border-right: 1px solid #000; } 
 <ul class="gf-menu l1 "> <li class="item487"> <a class="item" href="link1"> menu_item_1 </a> </li> <li class="item488"> <a class="item" href="link2"> menu_item_2 </a> </li> <li class="item489"> <a class="item" href="link1"> menu_item_2 </a> </li> </ul> 

Here I used the selector :not instead of having an other line to define the last-child as border-right: 0; 在这里,我使用选择器:not而不是用另一行将last-child定义为border-right: 0;

Here :not specifies that the :last-child won't be included for the following properties. 在这里:not指定:last-child不包含在以下属性中。

It looks like you're not far off... If you're trying to get them to display next to each other then you need to change your CSS to: 看来您离我们并不遥远...如果要使它们彼此相邻显示,则需要将CSS更改为:

gf-menu.l1 li {
display: inline;
border-right: 1px solid black;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM