pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

帶有嵌套元素的 CSS nth-of-type 選擇器

CSS nth-of-type selector with nested elements(帶有嵌套元素的 CSS nth-of-type 選擇器)
本文介紹了帶有嵌套元素的 CSS nth-of-type 選擇器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有許多特定類 .box 的 div,我想為其設置交替的背景顏色.但是,有些 div 被放置在另一個 div .inner-container 中:

I have a number of divs of a particular class .box for which I want to set an alternating background color. However, some of the divs are placed inside another div .inner-container:

<div class="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="inner-container">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
</div>

因此,使用 nth-of-type(even) 或 nth-child(even) 每秒更改 .box 的顏色在這里不起作用.是否可以僅使用 CSS 來實現交替背景?

So using nth-of-type(even) or nth-child(even) to change the color for every second .box does not work here. Is it possible to achieve the alternating background anyhow with using CSS only?

注意:我不知道有多少盒子是 .container 的直接子級,以及 .inner-container 里面有多少.

Note: I dont know how many boxes will be direct children of .container and how many will be inside the .inner-container.

jsfiddle

推薦答案

我基本上需要一個選擇器來計算盒子,就好像它們都是同一個父級 .container 的直接子級一樣(就好像 .inner-container 不存在一樣).

I basically need a selector that counts the boxes as if they were all direct children of the same parent .container (as if the .inner-container would not exist).

假設只有一個內部容器——除了 .box.inner-container 之外沒有其他元素——你需要使用 :內部容器上的 nth-child() 確定其相對于其 .box 兄弟(而不是其 .box 子級)的位置,從而確定是否以一種或另一種方式交替其內容的背景:

Assuming there will only be exactly one inner container — and no other elements besides .box and .inner-container — you'll need to use :nth-child() on the inner container to determine its position relative to its .box siblings (not its .box children), and thus determine whether to alternate the background on its contents one way or the other:

.container > .box:nth-child(even) {
    background-color: #bb3333;
}

.container > .inner-container:nth-child(odd) > .box:nth-child(even),
.container > .inner-container:nth-child(even) > .box:nth-child(odd) {
    background-color: #bb3333;
}

這里有一個demo,其中的方框已適當標記,以便您了解每個選擇器的工作原理.

Here's a demo with the boxes appropriately labeled so you can see how each selector works.

請注意,如果您有任何可能出現在內容器之后的框,您需要能夠計算內容器的子容器數量,然后才能確定如何開始計數從那時起.僅使用 CSS 是不可能的,因為 選擇器不能從內部元素上升到外部元素.有使用 JavaScript 的解決方法,但我懷疑這超出了當前問題的范圍.

Note that if you have any boxes that could appear after the inner container, you'll need to be able to count the number of children the inner container has before you can determine how to start counting from that point. This will not be possible with just CSS because selectors cannot ascend from inner elements to outer elements. There are workarounds using JavaScript, but I suspect this is outside the scope of the question at hand.

這篇關于帶有嵌套元素的 CSS nth-of-type 選擇器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

CSS selector when :target empty(:target 為空時的 CSS 選擇器)
Does the CSS direct decendant (gt;) not have any value in selectivity?(CSS 直接后代 (gt;) 在選擇性方面沒有任何價值嗎?)
Using querySelectorAll(). Is the result returned by the method ordered?(使用 querySelectorAll().方法返回的結果是否有序?)
Safari bug :first-child doesn#39;t update display:block when items are removed with JS(Safari 錯誤:當使用 JS 刪除項目時,first-child 不更新 display:block)
nth-Child CSS selectors(nth-子 CSS 選擇器)
Using same ID for multiple HTML tags?(對多個 HTML 標簽使用相同的 ID?)
主站蜘蛛池模板: 灌木树苗-绿化苗木-常绿乔木-价格/批发/基地 - 四川成都途美园林 | 缝纫客 | 山楂片_雪花_迷你山楂片_山楂条饼厂家-青州市丰源食品厂 | 矿用履带式平板车|探水钻机|气动架柱式钻机|架柱式液压回转钻机|履带式钻机-启睿探水钻机厂家 | 深圳网站建设-高端企业网站开发-定制网页设计制作公司 | 国际船舶网 - 船厂、船舶、造船、船舶设备、航运及海洋工程等相关行业综合信息平台 | 广州印刷厂_广州彩印厂-广州艺彩印务有限公司| 切铝机-数控切割机-型材切割机-铝型材切割机-【昆山邓氏精密机械有限公司】 | 广州工业氧气-工业氩气-工业氮气-二氧化碳-广州市番禺区得力气体经营部 | 东莞工厂厂房装修_无尘车间施工_钢结构工程安装-广东集景建筑装饰设计工程有限公司 | 阿米巴企业经营-阿米巴咨询管理-阿米巴企业培训-广东键锋企业管理咨询有限公司 | 盘古网络技术有限公司| 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 开云(中国)Kaiyun·官方网站-登录入口| 贴片电容-贴片电阻-二三极管-国巨|三星|风华贴片电容代理商-深圳伟哲电子 | 双能x射线骨密度检测仪_dxa骨密度仪_双能x线骨密度仪_品牌厂家【品源医疗】 | 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 服务器之家 - 专注于服务器技术及软件下载分享 | 选矿设备,选矿生产线,选矿工艺,选矿技术-昆明昆重矿山机械 | 磁棒电感生产厂家-电感器厂家-电感定制-贴片功率电感供应商-棒形电感生产厂家-苏州谷景电子有限公司 | LED显示屏_LED屏方案设计精准报价专业安装丨四川诺显科技 | 拖鞋定制厂家-品牌拖鞋代加工厂-振扬实业中国高端拖鞋大型制造商 | 一体式钢筋扫描仪-楼板测厚仪-裂缝检测仪-泰仕特(北京) | 武汉EPS线条_EPS装饰线条_EPS构件_湖北博欧EPS线条厂家 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 服务器之家 - 专注于服务器技术及软件下载分享 | 冷轧机|两肋冷轧机|扁钢冷轧机|倒立式拉丝机|钢筋拔丝机|收线机-巩义市华瑞重工机械制造有限公司 | 清水混凝土修复_混凝土色差修复剂_混凝土色差调整剂_清水混凝土色差修复_河南天工 | 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 哈希PC1R1A,哈希CA9300,哈希SC4500-上海鑫嵩实业有限公司 | 避光流动池-带盖荧光比色皿-生化流动比色皿-宜兴市晶科光学仪器 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 临海涌泉蜜桔官网|涌泉蜜桔微商批发代理|涌泉蜜桔供应链|涌泉蜜桔一件代发 | 接地电阻测试仪[厂家直销]_电缆故障测试仪[精准定位]_耐压测试仪-武汉南电至诚电力设备 | 施工电梯_齿条货梯_烟囱电梯_物料提升机-河南大诚机械制造有限公司 | EPDM密封胶条-EPDM密封垫片-EPDM生产厂家 | 宝元数控系统|对刀仪厂家|东莞机器人控制系统|东莞安川伺服-【鑫天驰智能科技】 | 北京宣传片拍摄_产品宣传片拍摄_宣传片制作公司-现像传媒 | 水质监测站_水质在线分析仪_水质自动监测系统_多参数水质在线监测仪_水质传感器-山东万象环境科技有限公司 | 讲师宝经纪-专业培训机构师资供应商_培训机构找讲师、培训师、讲师经纪就上讲师宝经纪 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 |