批改状态:合格
老师批语:写的很认真 仔细!
<div class="box" id="boxid"><div class="boxitem">1</div><div class="boxitem">2</div><div class="boxitem">3</div><div class="boxitem">4</div><div class="boxitem">5</div><div class="boxitem">6</div><div class="boxitem">7</div><div class="boxitem">8</div><div class="boxitem">9</div></div>
* {padding: 0;margin: 0;}.box {width: 500px;background: linen;display: flex;justify-content: space-between;align-items: center;flex-wrap: wrap;}/* 简单选择器 *//* id选择器 */#boxid {background: lightcyan;}/* id+元素选择器 */#boxid div {width: 150px;height: 150px;text-align: center;line-height: 150px;margin-bottom: 15px;background: lightgreen;/*绿色*/border: 1px solid lightgreen;}/* 类选择器比元素选择器优先级高,所以会覆盖上面样式的绿色背景颜色 */#boxid .boxitem {background: lightpink;/*粉色*/}

/* 上下文选择器 *//* 表示选择box类下所有类名称为boxitem的子类 */.box .boxitem {background: lightyellow;}/* 加>表示只选择第一个子类 */.box > .boxitem {background: lightskyblue;}
<div class="box" id="boxid"><div class="boxitem">1</div><div class="boxitem">2</div><div class="boxitem">3</div><div class="boxitem">4</div><div class="boxitem five_item">5</div><div class="boxitem">6</div><div class="boxitem">7</div><div class="boxitem">8</div><div class="boxitem">9</div></div>
/*兄弟选择器*//* 如将第6个的背景色改为蓝色,兄弟选择器优先级小于子类选择器,所以要加important */.box .boxitem.five_item + .boxitem {background: mediumblue !important;}/*同类选择器,~表示选中后面所有five_item后面所有含boxitem类的元素*/.box .boxitem.five_item ~ .boxitem {background: red !important;}

/*表示.box下排除span的子元素,其他子元素背景颜色设置为紫色*/.box :not(span) {background: mediumpurple;}
<div class="box" id="boxid"><div class="boxitem">1</div><div class="boxitem">2</div><div class="boxitem">3</div><div class="boxitem">4</div><div class="boxitem five_item">5</div><span class="boxitem">6</span><span class="boxitem">7</span><span class="boxitem">8</span><span class="boxitem">9</span></div>
/* .box下的子类.boxitem会根据元素类型div、span,分为2个类型 *//* last-of-type表示分组后最后一个元素,所以第5个和第9个背景颜色会改变 */.box .boxitem:last-of-type {background: mediumpurple;}

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号