html - CSS child/nth child/typeof 选择器

标签 html css css-selectors

我不知道如何在 CSS 中选择元素。

<div class="text">
    <img src="./img1.jpg">
    <p>Lorem</p>
</div>
<br> 
<div class="text">
    <img src="./img2.jpg">
    <p>Lorem</p>
</div>


如何选择文本 div 中的每隔一个图像?我想让第一个向左浮动,第二个向右浮动。无论我尝试什么,它们都向左浮动。正如我所理解的奇数选择器,只需选择它们两个。

img:nth-of-type(even){
float: right;
}
img:nth-of-type(odd){
float: left;
}

我的目标是:enter image description here

最佳答案

首先,id是唯一标识符,每页只能使用一次。你最好改成class .和 <br>在此示例中不需要标记,您可以使用 css 设置任何边距的样式:

<div class="text">
  <img src="./img1.jpg">
  <p>Lorem</p>
</div>
<div class="text">
  <img src="./img2.jpg">
  <p>Lorem</p>
</div>

然后,每个奇数 block 和偶数 block 内的图像可以设置为:

.text:nth-child(even) > img {
  float: left;
}
.text:nth-child(odd) > img {
  float: right;
}

关于html - CSS child/nth child/typeof 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46866518/

相关文章:

css - 选择最后一个 child ,除非它也是第一个 child

html - CSS 中的多个点/句点

javascript - 通过自定义设置,Kommunicate 聊天小部件变得透明

php - 使用 php jquery 和 ajax 的 html2fpdf - 使用完整的 CSS 导出

html - 水平和垂直居中 img

html - 使用 selenium 抓取网页时缺少 HTML 内容

html - CSS:与字段集在同一行的图例

javascript - AppleScript 从网页特定部分的 URL 保存文档

html - 当我折叠时页面 div 消失

php - 导航栏问题 : only show links in nav bar when logged in as admin