简体   繁体   English

使用 css last-child 更改边框

[英]Use css last-child change border

Is that possible to use CSS last-child to do this?可以使用 CSS last-child来做到这一点吗?

在此处输入图片说明

Can I use the same class to add last-child and make the second border to smaller px?我可以使用相同的类来添加last-child元素并将第二个边框设置为更小的像素吗? Or the only way to do so is by adding the different class name?或者唯一的方法是添加不同的类名?

 .border { border: 3px solid black; width: 80px; display: inline-block; }
 <div class="border"></div> <div class="border"></div>

You need to wrap border divs and after that, with the use of :last-child you can achieve this.您需要包装border div,然后使用:last-child可以实现这一点。

The reason behind using wrapper is :last-child will only try to match the very last child of a parent element使用wrapper的原因是:last-child只会尝试匹配父元素的最后一个子元素

 .border { border: 3px solid black; width: 80px; display: inline-block; } .border:last-child { width: 40px; }
 <div class="wrapper"> <div class="border"></div> <div class="border"></div> </div>

Yeh, You can use the last-child .是的,您可以使用last-child last-child selector need a parent div. last-child选择器需要一个父 div。 Please check the code.请检查代码。

 .border { border: 3px solid black; width: 80px; display: inline-block; } div.border:last-child { width: 40px; }
 <div> <div class="border"></div> <div class="border"></div> </div>

What about an easier way with one element and without complex selector:使用一个元素而不使用复杂选择器的更简单方法呢?

 .border { height:5px; width: 160px; background: /*the gradient will create the separation of the background*/ linear-gradient(#fff,#fff) 100px 0 /5px 100% no-repeat, #000; }
 <div class="border"></div>

And here is how you can do it to make it like your image:以下是您如何做到这一点,使其像您的形象一样:

 .border { height:80px; width: 200px; background: linear-gradient(red,red) left 20px top 50% /100px 5px, linear-gradient(green,green) right 20px top 50% /50px 5px, orange; background-repeat:no-repeat; }
 <div class="border"></div>

Related question for more details: Using percentage values with background-position on a linear-gradient有关更多详细信息的相关问题:在线性渐变上使用具有背景位置的百分比值

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

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