Bootstrap开关为什么不与div中的其他元素在同一行显示?

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

Why does bootstrap switch not appear in same line as other elements in the div

问题

以下是翻译好的代码部分:

我有以下代码:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.global-wrap {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}

.header {
  width: 1024px;
  background-color: purple;
  padding: 7px;
  margin: 0;
}

div {
  overflow: hidden;
  white-space: nowrap;
}

.contents svg {
  height: 25px;
  width: 25px;
  vertical-align: middle;
}

.contents .title {
  color: white;
  margin: 0;
  font-size: 16px;
  vertical-align: middle;
  margin-left: 15px;
}

.contents .switch {
  align-content: right;
}

<!-- language: lang-html -->
<head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
</head>

<div class="global-wrap">
  <div class="header">
    <div class="contents">
      <div class="form-inline">
        <div class="form-group">
          <svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" fill="white" class="bi bi-list" viewBox="0 0 16 16">
            <path fill-rule="evenodd" d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z"/>
          </svg>
          <span class="title">Title</span>
          <span class="form-check form-switch">
            <input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault">
            <label class="form-check-label" for="flexSwitchCheckDefault">Text</label>
          </span>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- end snippet -->

然而,Bootstrap开关未出现在与汉堡包和Title Text相同的行上。为什么Bootstrap元素没有出现在新行上,如何确保开关出现在右对齐的同一行上?

英文:

I have the code below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.global-wrap {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.header {
width: 1024px;
background-color: purple;
padding: 7px;
margin: 0;
}
div {
overflow: hidden;
white-space: nowrap;
}
.contents svg {
height: 25px;
width: 25px;
vertical-align: middle;
}
.contents .title {
color: white;
margin: 0;
font-size: 16px;
vertical-align: middle;
margin-left: 15px;
}
.contents .switch {
align-content: right;
}

<!-- language: lang-html -->

&lt;head&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;/head&gt;
&lt;div class=&quot;global-wrap&quot;&gt;
&lt;div class=&quot;header&quot;&gt;
&lt;div class=&quot;contents&quot;&gt;
&lt;div class=&quot;form-inline&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;25&quot; height=&quot;25&quot; fill=&quot;white&quot; class=&quot;bi bi-list&quot; viewBox=&quot;0 0 16 16&quot;&gt;
&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z&quot;/&gt;
&lt;/svg&gt;
&lt;span class=&quot;title&quot;&gt;Title&lt;/span&gt;
&lt;span class=&quot;form-check form-switch&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; role=&quot;switch&quot; id=&quot;flexSwitchCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexSwitchCheckDefault&quot;&gt;Text&lt;/label&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

However, the bootstrap switch does not appear on the same line as the hamburger and Title Text. Why doesn't bootstrap element appear on a new line and how can I make sure that the switch appears on the same line being right aligned?

答案1

得分: 1

你需要将 d-inline-flex 类添加到 form-check 类中。同时,将 position-absolute 类添加到 form-check 类,将 position-relative 类添加到其父元素。最后,添加以下 CSS 以将开关对齐到右侧:

.form-check.form-switch {
  right: 0px;
}

请查看下面的代码片段。

英文:

You need to add the d-inline-flex class to the form-check class. Also, add the position-absolute class to the form-check class and the position-relative class to its parent. Finally, add the following CSS to align the switch right:

.form-check.form-switch {
right: 0px;
}

See the snippet below.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.global-wrap {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.header {
width: 1024px;
background-color: purple;
padding: 7px;
margin: 0;
}
div {
overflow: hidden;
white-space: nowrap;
}
.contents svg {
height: 25px;
width: 25px;
vertical-align: middle;
}
.contents .title {
color: white;
margin: 0;
font-size: 16px;
vertical-align: middle;
margin-left: 15px;
}
.contents .switch {
align-content: right;
}
.form-check.form-switch {
right: 0px;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC&quot; crossorigin=&quot;anonymous&quot;&gt;
&lt;div class=&quot;global-wrap&quot;&gt;
&lt;div class=&quot;header&quot;&gt;
&lt;div class=&quot;contents&quot;&gt;
&lt;div class=&quot;form-inline&quot;&gt;
&lt;div class=&quot;form-group d-flex align-items-center position-relative&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;25&quot; height=&quot;25&quot; fill=&quot;white&quot; class=&quot;bi bi-list&quot; viewBox=&quot;0 0 16 16&quot;&gt;
&lt;path fill-rule=&quot;evenodd&quot; d=&quot;M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z&quot;/&gt;
&lt;/svg&gt;
&lt;span class=&quot;title&quot;&gt;Title&lt;/span&gt;
&lt;span class=&quot;form-check form-switch d-inline-flex position-absolute&quot;&gt;
&lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; role=&quot;switch&quot; id=&quot;flexSwitchCheckDefault&quot;&gt;
&lt;label class=&quot;form-check-label&quot; for=&quot;flexSwitchCheckDefault&quot;&gt;Text&lt;/label&gt;
&lt;/span&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 1

默认情况下,.form-check 类被显示为 block,这意味着在常规流中,它会移到自己的一行。

要创建一个内联表单检查,可以在.form-check 类旁边使用.form-check-inline 类。

详见:https://getbootstrap.com/docs/5.0/forms/checks-radios/#inline

&lt;span class=&quot;form-check form-check-inline ...&quot;&gt;
  ...
&lt;/span&gt;
英文:

By default the .form-check class is displayed as a block, which means in regular flow it moves to its own line.

To create an inline form check, use the .form-check-inline class along side the .form-check class.

See https://getbootstrap.com/docs/5.0/forms/checks-radios/#inline

&lt;span class=&quot;form-check form-check-inline ...&quot;&gt;
  ...
&lt;/span&gt;

huangapple
  • 本文由 发表于 2023年4月6日 22:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950601.html
匿名

发表评论

匿名网友

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

确定