用css完成根据子元素不同书写样式的方法

这篇文章主要介绍了用css完成根据子元素不同书写样式的方法的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

我们需要达到的效果:

需要什么

1张图片的, 2张图片的, 3张图片的样式各不相同。可以使用js完成子元素的判断,但是这里我使用css来完成

核心知识点

使用css选择器完成子元素的判断

例子:

用css选择器匹配只有一个元素

 div { &:last-child:nth-child(1) { // 相关样式 } }

很好理解:div下面即是最后一个元素也是第一个元素不就是只有一个子元素吗?

用css选择器匹配只有两个子元素

 div{ &:nth-last-child(2):nth-child(2) { } }

依样画瓢:最后第二个元素也是第二个元素不就代表,这个div下只有两个元素吗

完成样式

html部分

 
{{item.name}}
{{item.createTime}}
{{item.fabulousNumber}}

css部分

 .box { padding: 0.26rem; .header { display: flex; align-items: center; img { width: 0.58rem; height: 0.58rem; margin-right: 0.17rem; } } .bottom { display: flex; justify-content: space-between; align-items: center; color: #999999; font-size: 0.17rem; img { width: 0.17rem; height: 0.17rem; } } .content { display: flex; margin: 0.17rem 0; img { flex: 1; height: 1.37rem; width: 0; margin-right: 0.09rem; &:last-child { margin-right: 0; } &:last-child:nth-child(1) { height: 2.75rem; } } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是用css完成根据子元素不同书写样式的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » CSS 教程