我有一个包含这样内容的页面。
#################################
# __________________________ #
# | | | #
# | | | #
# | text | IMAGE | #
# | | | #
# |_______|__________________| #
# #
# __________________________ #
# | | | #
# | | | #
# | IMAGE | text | #
# | | | #
# |_______|__________________| #
#################################
我的代码如下:
<!-- <p> -> <image> -->
<div class="even_odd">
<p></p>
</div>
<div class="even_odd">
<img src="" alt="">
</div>
<!-- <image> -> p -->
<div class="even_odd">
<img src="" alt="">
</div>
<div class="even_odd">
<p></p>
</div>
现在,我想设置默认的CSS:
当号码为odd
时,会将text
设置为float: left
,将image
设置为float: right
。
当号码为even
时,会将image
设置为float: left
,将text
设置为float: right
。
目前,我必须这样做:
.even_odd:nth-child(1), event_odd:nth-child(4), ..... many and many child .... {
float: left;
}
.even_odd:nth-child(2), event_odd:nth-child(3), ..... many and many child .... {
float: left;
}
答案 0 :(得分:5)
我们尝试这个CSS选择器。
selector:nth-child(odd) {
property
}
selector:nth-child(even) {
property
}
答案 1 :(得分:1)
我从OP的请求中收集到的是img / div块的交替布局。以下是代码段的概述:
main.main
是一切都在其中的容器。
section.row
个
main.main
的弹性项目,也是弹性容器。flex-direction:row
。flex-direction:row-reverse
。img.img
都有div.txt
和section.row
。
section.row
的人都是颠倒了。 注意:关于nth
选择器需要记住的重要事项是:
nth-of-type
将过滤掉与选择器中的标记不匹配的其他兄弟。
div:nth-of-type(even) {color:red;}
每个偶数的div都是红色的,无论有多少个部分。div:nth-child(even) {color:red}
,结果仍会使div变为红色,但也会对这些部分进行计数(但不会应用红色文本颜色)。float
它们是脆弱的遗物,了解flexbox它是你的朋友。
.main {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
width: 100px;
padding: 0 5px;
height: auto;
background: grey;
}
section:nth-of-type(odd) {
display: flex;
}
section:nth-of-type(even) {
display: flex;
flex-direction: row-reverse;
}
.txt {
width: 50px;
height: 50px;
}
<main class="main">
<section class='row'>
<img src='http://placehold.it/50x50/000/fff?text=1' class='img'>
<div class='txt'>TEXT</div>
</section>
<section class='row'>
<img src='http://placehold.it/50x50/00f/eee?text=2' class='img'>
<div class='txt'>TEXT</div>
</section>
<section class='row'>
<img src='http://placehold.it/50x50/0e0/111?text=3' class='img'>
<div class='txt'>TEXT</div>
</section>
</main>
答案 2 :(得分:0)
试一试: -
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}