简体   繁体   English

对除最后一个元素以外的所有元素都应用border-right

[英]Applying border-right to all elements except the last one

I have Wordpress pages looking like this: Page 1 | 我的Wordpress页面如下所示: Page 2 | 第2页 Page 3 | 第3页

I don't want a border-right on Page 3. How can I delete it? 我不需要第3页的边框。如何删除它?

 .primary-navigation { float: left; } .primary-navigation a { margin-top: 16px; margin-bottom: 12px; padding-left: 23px; padding-right: 23px; border-right: 1px dotted #7b7f82; position: relative; line-height: 1; } .primary-navigation .menu-item-has-children a { padding-right: 35px } 
 <div id="primary-navigation" class="primary-navigation" role="navigation" itemscope itemtype="http://schema.org/SiteNavigationElement"> <nav id="navigation" class="navigation clearfix mobile-menu-wrapper"> <a href="#" id="pull" class="toggle-mobile-menu"> <?php _e( 'Menu'); ?> </a> <?php if (has_nav_menu( 'primary-menu')) { ?> <?php wp_nav_menu(array( 'theme_location'=>'primary-menu', 'menu_class' => 'menu clearfix', 'menu_id' => 'menu-primary-menu', 'container' => '', 'walker' => new mts_menu_walker)); ?> <?php } else { ?> <ul class="menu clearfix" id="menu-primary-menu"> <?php wp_list_pages( 'title_li='); ?> </ul> <?php } ?> </nav> </div> 

Use the :last-child pseudo-class to set border-right: none; 使用:last-child伪类设置border-right: none; on the last <a> in your .primary-navigation . .primary-navigation的最后一个<a>上。

.primary-navigation a {
    margin-top: 16px;
    margin-bottom: 12px;
    padding-left: 23px;
    padding-right: 23px;
    border-right: 1px dotted #7b7f82;
    position: relative;
    line-height: 1;
}

.primary-navigation li:last-child a {
    border-right: none;
}

More on the :last-child pseudo-class on MDN . 有关MDN上的:last-child伪类的更多信息。

Add this style to you css: 将此样式添加到您的CSS中:

 .primary-navigation { float: left; } .primary-navigation ul { margin-top: 16px; margin-bottom: 12px; padding-left: 23px; padding-right: 23px; border-right: 1px dotted #7b7f82; position: relative; line-height: 1; } .primary-navigation ul:last-child { border-right: none; } 
 <li class="primary-navigation"> <ul class="menu clearfix" id="menu-primary-menu">Page 1</ul> <ul class="menu clearfix" id="menu-primary-menu">Page 2</ul> <ul class="menu clearfix" id="menu-primary-menu">Page 3</ul> <ul class="menu clearfix" id="menu-primary-menu">Page 4</ul> </li> 

.primary-navigation a {
    margin-top: 16px;
    margin-bottom: 12px;
    padding-left: 23px;
    padding-right: 23px;
    /* border-right: 1px dotted #7b7f82;   <-- REMOVE from this declaration block */
    position: relative;
    line-height: 1;
}

.primary-navigation a:not(:last-child) {
    border-right: 1px dotted #7b7f82;
}

Using the :not() negation and :last-child pseudo-classes, all anchors are given the border, except the last one. 使用:not()否定:last-child伪类,除最后一个锚点外,所有锚点都具有边框。

Just FYI, this method may be simpler: 仅供参考,此方法可能更简单:

a + a {
    border-left: 1px dotted #7b7f82;
}

Using the adjacent sibling selector , a left-side border can be applied to all anchors immediately following another anchor. 使用相邻的同级选择器 ,可以将左侧边框立即应用于紧随另一个锚点的所有锚点。 This means no left-side border on the first anchor, and no right-side border on the last anchor. 这意味着第一个锚点没有左侧边界,而最后一个锚点没有右侧边界。

You can use CSS selector :not(:last-child) to select all your element BUT the last. 您可以使用CSS选择器:not(:last-child)选择所有元素,但最后一个。

 ul.menu { list-style-type : none; padding : 0px; } ul.menu > li { display : inline-block; padding-right : 2px; } ul.menu > li:not(:last-child) { border-right : solid 1px black; } 
 <ul class="menu"> <li>Page 1</li> <li>Page 2</li> <li>Page 3</li> </ul> 

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

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