将Bootstrap卡片与左侧的筛选表单对齐

huangapple go评论73阅读模式
英文:

Align bootstrap cards with filter forms on the left

问题

我正在尝试使用Bootstrap的rowcol-类来对齐卡片。前三个按预期工作,但第四个不起作用。有人知道为什么会发生这种情况,还能提供解决方案吗?

问题截图

这是我的代码:

<div class="container-fluid">
  <div class="row">
    <div class="col-12">
      <div class="form">
        <i class="fa fa-search"></i>
        <input type="text" class="form-control form-input" placeholder="搜索...">
        <span class="left-pan"><i class="fa fa-microphone"></i></span>
      </div>
    </div>

    <div id="mobile-filter">
      <!-- 这里是移动设备的筛选器内容 -->
    </div>

    <section id="sidebar">
      <!-- 这里是侧边栏内容 -->
    </section>

    <div class="col-12 col-sm-12 col-md-6 col-lg-6 col-xl-3 col-xxl-3 boat" *ngFor="let boat of boats">
      <div class="card">
        <img src="/{{ boat.src }}" class="card-img-top" alt="{{ boat.name }}">
        <div class="card-body">
          <h5 class="card-title">{{ boat.name }}</h5>
          <p class="card-text">{{ boat.description }}</p>
          <p>{{ boat.city }}</p>
        </div>
      </div>
    </div>
  </div>
</div>

这是相应的SCSS文件:

.form {
  position: relative;
  /* 其他样式 */
}

.form .fa-search {
  position: absolute;
  top: 20px;
  left: 20px;
  color: #9ca3af;
  /* 其他样式 */
}

.form span {
  position: absolute;
  right: 17px;
  top: 13px;
  padding: 2px;
  border-left: 1px solid #d1d5db;
  /* 其他样式 */
}

/* 其他样式 */

.card {
  margin-top: $m;
  width: 100%;
  height: 350px;
  border-radius: $br-100;
  overflow: hidden;
  /* 其他样式 */
}

.card img {
  width: 100%;
  height: 200px;
  object-fit: cover;
  object-position: 50% 50%;
  /* 其他样式 */
}

#sidebar {
  width: 20%;
  padding: 10px;
  margin: 0;
  float: left;
  /* 其他样式 */
}

/* 其他样式 */

.boat {
  padding-left: $p-xs;
  padding-right: $p-xs;
  /* 其他样式 */
}
英文:

I am trying to align the cards using row and col- classes from bootstrap. The first three work as its supposed to but the fourth one doesnt do what i want it to do. Does anyone have an idea as to why this is happening and maybe also have a solution for me?

The problem in question

this is my code:

&lt;div class=&quot;container-fluid&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-12&quot;&gt;
&lt;div class=&quot;form&quot;&gt;
&lt;i class=&quot;fa fa-search&quot;&gt;&lt;/i&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control form-input&quot; placeholder=&quot;Search anything...&quot;&gt;
&lt;span class=&quot;left-pan&quot;&gt;&lt;i class=&quot;fa fa-microphone&quot;&gt;&lt;/i&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id=&quot;mobile-filter&quot;&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;div class=&quot;border-bottom&quot;&gt;&lt;/div&gt;
&lt;ul class=&quot;list-group&quot;&gt;
&lt;label for=&quot;customRange1&quot; class=&quot;form-label&quot;&gt;Price per hour&lt;/label&gt;
&lt;input type=&quot;range&quot; class=&quot;form-range&quot; id=&quot;customRange1&quot;&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul class=&quot;list-group&quot;&gt;
&lt;label for=&quot;customRange1&quot; class=&quot;form-label&quot;&gt;Price per hour&lt;/label&gt;
&lt;input type=&quot;range&quot; class=&quot;form-range&quot; id=&quot;customRange1&quot;&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;section id=&quot;sidebar&quot;&gt;
&lt;div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;div class=&quot;form-check&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; value=&quot;&quot; id=&quot;flexCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexCheckDefault&quot;&gt;
Default checkbox
&lt;/label&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;border-bottom&quot;&gt;&lt;/div&gt;
&lt;ul class=&quot;list-group&quot;&gt;
&lt;label for=&quot;customRange1&quot; class=&quot;form-label&quot;&gt;Price per hour&lt;/label&gt;
&lt;input type=&quot;range&quot; class=&quot;form-range&quot; id=&quot;customRange1&quot;&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul class=&quot;list-group&quot;&gt;
&lt;label for=&quot;customRange1&quot; class=&quot;form-label&quot;&gt;Price per hour&lt;/label&gt;
&lt;input type=&quot;range&quot; class=&quot;form-range&quot; id=&quot;customRange1&quot;&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/section&gt;
&lt;div class=&quot;col-12 col-sm-12 col-md-6 col-lg-6 col-xl-3 col-xxl-3 boat&quot; *ngFor=&quot;let boat of boats&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;img src=&quot;/{{ boat.src }}&quot; class=&quot;card-img-top&quot; alt=&quot;{{ boat.name }}&quot;&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;h5 class=&quot;card-title&quot;&gt;{{ boat.name }}&lt;/h5&gt;
&lt;p class=&quot;card-text&quot;&gt;{{ boat.description }}&lt;/p&gt;
&lt;p&gt;{{ boat.city }}&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

This is the scss file that goes with it:

.form {
position: relative;
}
.form .fa-search {
position: absolute;
top: 20px;
left: 20px;
color: #9ca3af;
}
.form span {
position: absolute;
right: 17px;
top: 13px;
padding: 2px;
border-left: 1px solid #d1d5db;
}
ul {
list-style: none;
}
li a {
text-decoration: none;
}
li a:hover {
text-decoration: none;
}
.left-pan {
padding-left: 7px;
}
.left-pan i {
padding-left: 10px;
}
.form-input {
height: 55px;
text-indent: 33px;
border-radius: $br-160;
}
.form-input:focus {
box-shadow: $shadow-black-lg;
border: $b-1 $b-solid $grey;
}
.card {
margin-top: $m;
width: 100%;
height: 350px;
border-radius: $br-100;
overflow: hidden;
}
.card img {
width: 100%;
height: 200px;
object-fit: cover;
object-position: 50% 50%;
}
#sidebar {
width: 20%;
padding: 10px;
margin: 0;
float: left;
}
ul {
padding: 5px;
}
li a {
color: $grey;
}
li a:hover {
color: $tertiary;
}
.navbar-nav {
margin-left: 35%;
}
.nav-item {
padding-left: 20px;
}
#mobile-filter {
display: none;
}
.boat {
padding-left: $p-xs;
padding-right: $p-xs;
}

All help is highly appreciated!

答案1

得分: 2

好的开始,你应该将整个宽度视为一行 - 然后将 col-4 与 col-8 分别设置为侧边栏和主内容或右侧。你的第四艘船正在滑动,因为... 嗯,那里有空间!如果你将它(所有船只)的容器设置为整个屏幕的 col-8,然后在内部创建 flex 或 grid,你可以随意布局,它不会让你的项目滑出定义的空间 - 它会将元素包装到下方的第一张卡片下方,或者允许你创建无限的水平滚动,所有的船只都在一行中可见。

英文:

Good start, you should treat the whole width as row - then set col-4 vs col-8 to sidebar and your main content or right respectively.

Your forth boat is sliding because... well, it has space there! If you set it's (all boats) container to be col-8 of the whole screen, you can then create flex or grids inside as you will and it won't let your items to slide outside of defined space - it will wrap element down, moving it below first card or allow you to make eg. infinite horizontal scroll with all boats being visible in one row.

huangapple
  • 本文由 发表于 2023年3月3日 22:08:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628118.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定