简体   繁体   English

CSS:在除最后一个以外的所有div上添加管道

[英]CSS: Add pipe on all div except last

I need to add pipe between links except after the last . 除了最后一个之后,我需要在链接之间添加管道。 My html is rendered in the following way , html is rendered with same class. 我的html用以下方式呈现,html用相同的类呈现。

<!DOCTYPE html>
<html>
<head>
<style> 
p:last-of-type {
    background: #ff0000;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>


</body>
</html>

I need to add Pipe but after last link no pipe should be displayed . 我需要添加管道,但在最后一个链接之后,不应显示任何管道。 How can i do it for this case . 在这种情况下我该怎么办。

Fiddle Link 小提琴链接

PS : This can be done easily when we have different links with different class . PS:当我们在不同的班级有不同的链接时,这很容易做到。 In my case links are dynamic and after last link no pipe should be displayed 就我而言,链接是动态的,在最后一个链接之后,不应显示任何管道

Use this selector .sample:not(:last-of-type) , which will target all but the last item. 使用此选择器.sample:not(:last-of-type) ,它将定位除最后一项以外的所有项。

Do note, when combining a class name with last-of-type , it will target any element type not being the last (if there is more than 1) having the given class. 请注意,将类名与last-of-type结合使用时,它将针对具有给定类的不是最后一个(如果大于1)的任何元素类型。

Updated fiddle (added the pipe to the div so it won't pick up the color from the p ) 更新了小提琴 (将管道添加到div以便它不会从p拾取颜色)

.sample:not(:last-of-type) > div::after {
  content: ' | ';
}

Another option is last-child , .sample:not(:last-child) , which also will target all but the last. 另一个选项是last-child.sample:not(:last-child) ,它也将定位除最后一个以外的所有对象。

Do note, when using last-child , it means the last no matter class or element type, so if you have another element coming after the last sample , that will count as the last, and here is a fiddle sample showing how this rule will fail in cases like that. 请注意,使用last-child ,无论类或元素类型如何,它都表示最后一个,因此,如果在最后一个sample之后有另一个元素,则该元素将算作最后一个,这是一个小提琴样本,显示了此规则的执行方式在这种情况下失败。

Updated fiddle (added the pipe to the div so it won't pick up the color from the p ) 更新了小提琴 (将管道添加到div以便它不会从p拾取颜色)

.sample:not(:last-child) > div::after {
  content: ' | ';
}

A third option is to use the immediate sibling selecor + , which target all sibling elements with the given class but the first. 第三种选择是使用直接同级selecor + ,它以给定类(但第一个)为目标的所有同级元素为目标。

 p:last-of-type { background: #ff0000; display: inline-block; } .sample + .sample > div::before { content: ' | '; } 
 <h1>This is a heading</h1> <div class="sample" style="display:inline-block"> <div > <p class="para">link1</p> </div> </div> <div class="sample" style="display:inline-block"> <div > <p class="para">link1</p> </div> </div> <div class="sample" style="display:inline-block"> <div > <p class="para">link1</p> </div> </div> 

Here's another way: 这是另一种方式:

'Add a pipe before each link - only if it comes after another one' “在每个链接之前添加一个管道-仅当它在另一个链接之后时”

.sample + .sample .para:before {
  content: "|";
  margin-right: 10px;
  color: red;
}

 .sample + .sample .para:before { content: "|"; margin-right: 10px; color: red; } 
 <h1>This is a heading</h1> <div class="sample" style="display:inline-block"> <div > <p class="para">link1</p> </div> </div> <div class="sample" style="display:inline-block"> <div > <p class="para">link1</p> </div> </div> <div class="sample" style="display:inline-block"> <div > <p class="para">link1</p> </div> </div> 

I think you want like this. 我想你想要这样。

 .sample:last-child div { border: none; } .sample div { border-right: 1px solid #000; margin-right: 10px; padding-right: 10px; } 
 <h1>This is a heading</h1> <div> <div class="sample" style="display:inline-block"> <div> <p class="para">link1</p> </div> </div> <div class="sample" style="display:inline-block"> <div> <p class="para">link1</p> </div> </div> <div class="sample" style="display:inline-block"> <div> <p class="para">link1</p> </div> </div> </div> 

One way to do it is to add this CSS rule: 一种方法是添加以下CSS规则:

.sample:not(:last-of-type) {
    border-right: 1px solid black;
}

https://jsfiddle.net/9c8szuc5/2/ https://jsfiddle.net/9c8szuc5/2/

在 a 的所有直接子级之间添加边距<div>除了最后一个</div><div id="text_translate"><p>我希望在 Gutenberg 的博客元素之间添加一些空间。 我试过:</p><pre> .single.container &gt; *:not(:last-child) { margin-bottom: 24px; }</pre><p> 但是,这适用于不是容器的直接子元素的元素。 例如&lt;div class="wp-container-1 wp-block-column"...&gt; </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="container"&gt; &lt;p&gt;Lorem ipsum dolor sit amet&lt;/p&gt; &lt;h2&gt;Title H2&lt;/h2&gt; &lt;h3&gt;Title H3&lt;/h3&gt; &lt;h4&gt;Title H4&lt;/h4&gt; &lt;blockquote class="wp-block-quote" id="output"&gt;&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;&lt;cite&gt;&lt;strong&gt;Firstname Lastname&lt;/strong&gt; – Job at Company&lt;/cite&gt;&lt;/blockquote&gt; &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. &lt;/p&gt; &lt;div class="wp-container-3 wp-block-columns"&gt; &lt;div class="wp-container-1 wp-block-column" style="flex-basis:20%"&gt; &lt;figure... &gt;&lt;/figure&gt; &lt;/div&gt; &lt;div class="wp-container-2 wp-block-column" style="flex-basis:80%"&gt; &lt;blockquote class="wp-block-quote is-style-plain" id="output"&gt;&lt;p&gt;Use the “plain” style for blockquotes.&lt;/p&gt;&lt;cite&gt;&lt;strong&gt;Firstname Lastname&lt;/strong&gt; – Job at Company&lt;/cite&gt;&lt;/blockquote&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div> - Add margin between all direct children of a <div> except the last one

暂无
暂无

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

相关问题 在 a 的所有直接子级之间添加边距<div>除了最后一个</div><div id="text_translate"><p>我希望在 Gutenberg 的博客元素之间添加一些空间。 我试过:</p><pre> .single.container &gt; *:not(:last-child) { margin-bottom: 24px; }</pre><p> 但是,这适用于不是容器的直接子元素的元素。 例如&lt;div class="wp-container-1 wp-block-column"...&gt; </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="container"&gt; &lt;p&gt;Lorem ipsum dolor sit amet&lt;/p&gt; &lt;h2&gt;Title H2&lt;/h2&gt; &lt;h3&gt;Title H3&lt;/h3&gt; &lt;h4&gt;Title H4&lt;/h4&gt; &lt;blockquote class="wp-block-quote" id="output"&gt;&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;&lt;cite&gt;&lt;strong&gt;Firstname Lastname&lt;/strong&gt; – Job at Company&lt;/cite&gt;&lt;/blockquote&gt; &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. &lt;/p&gt; &lt;div class="wp-container-3 wp-block-columns"&gt; &lt;div class="wp-container-1 wp-block-column" style="flex-basis:20%"&gt; &lt;figure... &gt;&lt;/figure&gt; &lt;/div&gt; &lt;div class="wp-container-2 wp-block-column" style="flex-basis:80%"&gt; &lt;blockquote class="wp-block-quote is-style-plain" id="output"&gt;&lt;p&gt;Use the “plain” style for blockquotes.&lt;/p&gt;&lt;cite&gt;&lt;strong&gt;Firstname Lastname&lt;/strong&gt; – Job at Company&lt;/cite&gt;&lt;/blockquote&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre></div></div><p></p></div> - Add margin between all direct children of a <div> except the last one CSS抓取所有类名(最后一个除外) - CSS Grab All Class Name Except For Last CSS-Div填充“除z像素外的所有像素” - CSS - Div padding “all except z pixels” 添加填充到除最后一个之外的所有子级 - Add padding to all childs except last 向所有 CSS 规则添加属性,除了一些 - Add attribute to all CSS rules except some 使用CSS将表格中除最后一行以外的所有首字母大写 - Capitalize all first letters in a table except for the last row using CSS CSS 按钮对齐除最后一个之外的所有 div - CSS buttons align for all divs except the last one 如何使用CSS选择器选择除第一个和最后一个之外的所有子项 - How to select all children except first and last with CSS selectors CSS:将除最后一个之外的所有列表元素居中 - CSS: center all list elements except last one 有没有办法为“除第一个/最后一个元素之外的所有元素”指定一个 CSS 简写? - Is there any way to specify a CSS shorthand for "all elements except the first/last"?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM