html - 在我的网站上交替显示帖子

标签 html css

我有一个类似这样的博客/网站,我试图让每篇文章以交替顺序显示。第一个帖子的图片显示在右侧,第二个帖子的图片显示在左侧...

<html>
<head>
    <title>My Blog</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>

<div class="section">
    <article>
      <a href="#"><img src="/img/picture1.jpg" width="100" height="100" /></a>    
      <h2>Post Title 1</h2>
      <p>post contents post contents</p>
    </article>

    <article>
      <a href="#"><img src="/img/picture2.jpg" width="100" height="100" /></a>    
      <h2>Post Title 2</h2>
      <p>post contents post contents</p>
    </article>

    <article>
      <a href="#"><img src="/img/picture3.jpg" width="100" height="100" /></a>    
      <h2>Post Title 3</h2>
      <p>post contents post contents</p>
    </article>

    <article>
      <a href="#"><img src="/img/picture4.jpg" width="100" height="100" /></a>    
      <h2>Post Title 4</h2>
      <p>post contents post contents</p>
    </article>  
</div> <!-- /.section -->

</body>
</html>

我知道 CSS nth-child 和 nth-of-type 选择器,但对于如何正确使用它仍然是新手。

我有根据此处的答案建模的示例 CSS 代码:Problem with odd/even child elements in nth-child但我无法正确设置文章元素内标签的样式。

div.section article img:nth-of-type(odd) {
    float: right;
}

div.section article img:nth-of-type(even) {
    float: left;
}

我搜索了一些模型设计,发现这个与我的网站类似。

enter image description here

非常感谢任何帮助。

最佳答案

在文章中附加类型

div.section article:nth-of-type(odd) img {
    float: right;
}

div.section article:nth-of-type(even) img {
    float: left;
}

关于html - 在我的网站上交替显示帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38411871/

相关文章:

javascript - 无法获得所需的悬停状态

css - 相对填充和 vh 单位 - 错误或规范误解?

javascript - 使 div 及其所有子项忽略父类样式

jquery slideToggle 在使用 bootstrap v4 时不起作用

php - 适用于所有类型屏幕的交互式 CSS

javascript - 扩展内联 block 跳转到上面的行

html - CSS优先顺序?我的演讲幻灯片是否正确?

html - 如何在 div 中水平对齐 Logo 和 h1

css - Firefox:使用最小宽度和最大高度时如何防止水平滚动条?

javascript - html 中的列表是否从父列表继承 id?