html - 如何使用 Bootstrap 在另一个 DIV 的左侧显示 DIV
问题描述
我想创建一个棋盘样式HTML
的页面 ( ) ,因此第一行的图像位于内容的左侧,第二行的图像位于内容的右侧。
从语义上讲,我可以改变 HTML 的结构来实现这一点,但是有没有办法纯粹使用 CSS + Bootstrap 4 来做到这一点,所以 HTML 不需要改变。
我最初有一个类“ left-img
”和“ right-img
”来实现这一点,但没有成功。
目前我在我CSS
的每个事件元素中都有这个。
.checker:nth-child(even) .img-wrapper {
float:right;
}
.checker:nth-child(odd) .img-wrapper {
float:left;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row checker">
<div class="col-lg-6">
<div class="">
<h2>The Title</h2>
<div class="">
some text
</div>
</div>
</div>
<div class="col-lg-6 img-wrapper">
<img src="https://via.placeholder.com/140x100" alt="">
</div>
</div>
<div class="row checker">
<div class="col-lg-6">
<div class="">
<h2>The Title</h2>
<div class="">
some text
</div>
</div>
</div>
<div class="col-lg-6 img-wrapper">
<img src="https://via.placeholder.com/140x100" alt="">
</div>
</div>
完整示例
解决方案
我希望 css 和 html 的这些变化对你有帮助!!!
.checker:nth-child(odd) > .col-lg-6:last-of-type{
float:left !important;
}
.checker:nth-child(even) > .col-lg-6:first-of-type{
float:left !important;
}
img{
width:100px;
}
.col-lg-6{
float:right;
}
.row{
float:right;
width:100%;
}
<div class="row checker">
<div class="col-lg-6">
<div class="">
<h2>The Title</h2>
<div class="">
some text
</div>
</div>
</div>
<div class="col-lg-6 img-wrapper">
<img src="https://cdn.jpegmini.com/user/images/slider_puffin_jpegmini_mobile.jpg" alt="">
</div>
</div>
<div class="row checker">
<div class="col-lg-6">
<div class="">
<h2>The Title</h2>
<div class="">
some text
</div>
</div>
</div>
<div class="col-lg-6 img-wrapper">
<img src="https://cdn.jpegmini.com/user/images/slider_puffin_jpegmini_mobile.jpg" alt="">
</div>
</div>
推荐阅读
- java - 在 NetBeans Maven 项目中设置链接源属性
- html - 在下一次加载 .html 时使用 app.js 加载从 html 表单保存的变量
- node.js - Sequelizejs从关系表中获取字段到关联belongsToMany
- ruby-on-rails - 如何更新项目中的 Rails 版本?Bundler 找不到 gem “activemodel” 的兼容版本
- c# - 如何将注册中设置的值显示到 seevalue() 函数中?范围
- javascript - 如何通过状态向单选按钮添加标签
- amazon-web-services - 未知错误,来自 fetch 的响应正文未定义
- mysql - 如何将具有许多这样的关系的列分组?
- aurelia - Aurelia 路由器,仅 html 视图,添加一些 javascript?
- prolog - 如何在序言中找到递归余数(没有算术)