简体   繁体   English

在CSS中,如何一起选择子元素?

[英]In Css, how can I select an elements children together?

Hey guys I'm trying to put a border around a few elements on this page using Stylish (Chrome Extension). 大家好,我正在尝试使用时尚(Chrome扩展程序)在此页面上的几个元素周围添加边框。 Here is the structure of how the page is, the letters representing a different class.. 这是页面结构的结构,字母代表不同的类。

A
  B
  B
  C
  C

I want to put the border around the "C"s, if I do it just by calling the C class, I would end up with border's between the two C classes. 我想在“ C”周围加上边框,如果仅通过调用C类来做到这一点,我最终将在两个C类之间使用边框。 So is there a way I can select both the C's together as one element. 因此,有一种方法可以将两个C一起选择为一个元素。

With Stylish you cannot change the HTML of a page, just css, I know I could just nest it in HTML, but that's not an option. 使用时尚,您不能更改页面的HTML,只能更改CSS,我知道我可以将其嵌套在HTML中,但这不是一个选择。

Edit: C also appears in other area's of the page. 编辑:C也会出现在页面的其他区域。

Is this what you want to achieve? 这是您要达到的目标吗?

 .a>.c { border: solid red; border-width: 0 1px; } .a>*:not(.c) + .c, .a>.c:first-child { border-top-width: 1px; } .a>.c + *:not(.c) { border-top: 1px solid red; } .a>.c:last-child { border-bottom-width: 1px; } 
 <div class="a"> <div class="b">B</div> <div class="b">B</div> <div class="c">C</div> <div class="c">C</div> <div class="d">D</div> </div> <hr /> <div class="a"> <div class="c">C</div> <div class="b">B</div> <div class="b">B</div> <div class="c">C</div> <div class="c">C</div> <div class="d">D</div> </div> <hr /> <div class="a"> <div class="a">A</div> <div class="b">B</div> <div class="b">B</div> <div class="c">C</div> <div class="c">C</div> <div class="d">D</div> <div class="c">C</div> </div> <hr /> <div class="a"> <div class="c">C</div> <div class="c">C</div> <div class="c">C</div> </div> <hr /> <div class="a"> <div class="c">C</div> </div> 

The only "trick" is that I have to use the top border of an immediately following element after a .c in order to place the "bottom" border of the last .c in a group of .c s. 唯一的“技巧”是,我必须使用.c之后紧随其后的元素的顶部边框,以便将最后一个.c“底部”边框放置在一组.c s中。 Currently there's no way to select a group of immediate siblings with a certain class. 当前,无法选择具有特定类别的一组直系兄弟姐妹。 The point in evaluation when you know the last element with .c in a group is the last is when you evaluate the next item and see it doesn't have .c . 当您知道组中带有.c的最后一个元素是最后一个元素时,评估的意义就在于评估下一项而不具有.c But at that point, you can't style the element before, because CSS only parses forward, never backwards. 但是到那时,您不能在样式上设置元素,因为CSS只会向前解析,而不会向后解析。

In short, if you have a margin between elements, there is no way to do only with CSS ( * see note). 简而言之,如果元素之间有边距,则无法仅使用CSS*请参见注释)。 It would be trivial with javascript (I can provide it, if of any help). 使用javascript会显得很琐碎(如果有任何帮助,我可以提供)。


* Actually, you could make this work even if with margins between elements, by null -ing the margins between .c and any :not(.c) and applying the difference to the first child of the :not(.c) instead. *其实,你可以做这项工作,即使与元素之间的空间,通过null -ing之间的利润.c任何:not(.c)和应用的差异的第一个孩子:not(.c)来代替。 It's cumbersome to find a magical solution that would work in any case. 找到一个在任何情况下都可行的神奇解决方案是很麻烦的。 Most times, these things are coded looking at the existing elements and finding tricks to make it "look like" it works. 大多数时候,这些东西都是经过编码的,着眼于现有元素并找到使它“看起来像”可行的技巧。
That's all we have right now. 这就是我们现在所拥有的。 :) :)

Assuming that there is no space between and of your <div> 假设<div>和之间没有空格

 div{ border: 1px solid #dadada; min-height: 50px; } .c{ border: 1px solid red; } .c + .c{ border-top-color: white; margin-top: -1px; } 
 <div class="a"> <div class="b"></div> <div class="b"></div> <div class="c"></div> <div class="c"></div> <div class="b"></div> </div> 

You will need to set the border-top-color to that of the background of .c - if you have margins at the bottom of .c , you will need to add that margin to the border width and apply the negative margin at the top. 您需要将border-top-color设置为.c背景的border-top-color -如果您在.c的底部有空白,则需要将该空白添加到边框宽度上,并在顶部应用负空白。

If you want a solution which will account for any number of .c elements, you need to apply: 如果您想要一个解决方案,该解决方案可以考虑任何数量的.c元素,则需要应用:

  • a border-left and a border-right to all .c elements 所有.c元素的左边框和右边框
  • a border-top to the first .c element 第一个.c元素的边框顶部
  • a border-bottom to the last .c element 最后一个.c元素的边界底部

But. 但。 There is no :nth-of-class pseudo-selector. 没有:nth-of-class伪选择器。

So: 所以:

  • the first .c element can be selected (instead) with .b + .c . 可以使用.b + .c选择(替代)第一个.c元素。
  • the last .c element can be selected (instead) with :last-of-type 可以使用:last-of-type选择(代替)最后一个.c元素

Working Example: 工作示例:

 section { width: 120px; text-align: center; background-color: rgb(191, 191, 191); } div { padding: 3px; } .c { border-right: 2px solid rgb(255, 0, 0); border-left: 2px solid rgb(255, 0, 0); } .b + .c { border-top: 2px solid rgb(255, 0, 0); } div:last-of-type.c { border-bottom: 2px solid rgb(255, 0, 0); } 
 <section> <div class="a">a</div> <div class="b">b</div> <div class="b">b</div> <div class="c">c</div> <div class="c">c</div> </section> 

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

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