te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Bootstrap 5 aligning buttons in a row - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Bootstrap 5 aligning buttons in a row - Stack Overflow

programmeradmin1浏览0评论

I'm trying to get the buttons on my webpage to be side by side in the middle of the page, these are the 2 bootstrap 5 classes I've attempted and they aren't as I want them to look:

For reference, some questions will have answers with multiple buttons (up to 10: ), I would also like them to be equally spaced in a row, something like this:

I'm not sure if this changes anything but the buttons are added using a for loop in javascript.

Any tips would be much appreciated, I've been quite stuck on this

I'm trying to get the buttons on my webpage to be side by side in the middle of the page, these are the 2 bootstrap 5 classes I've attempted and they aren't as I want them to look:

  1. https://gyazo./c23f2eade4614380aec547b11e61387a
  2. https://gyazo./e40a678b02c9f641f746b1cfbe83d03f

For reference, some questions will have answers with multiple buttons (up to 10: https://gyazo./15d810f8c0f79f23d12463db9ba50e2a), I would also like them to be equally spaced in a row, something like this: https://gyazo./ca1ab61aa7fef54f1cf1a099cb56a5e0

I'm not sure if this changes anything but the buttons are added using a for loop in javascript.

Any tips would be much appreciated, I've been quite stuck on this

Share Improve this question edited Dec 5, 2021 at 16:44 Maik Lowrey 17.6k7 gold badges53 silver badges98 bronze badges asked Dec 5, 2021 at 11:17 Tester321Tester321 1432 gold badges2 silver badges9 bronze badges 0
Add a ment  | 

7 Answers 7

Reset to default 7

I have success with class="btn-group" Exemple:

<div class="btn-group">
  <button type="button" class="btn btn-primary">Apple</button>
  <button type="button" class="btn btn-primary">Samsung</button>
  <button type="button" class="btn btn-primary">Sony</button>
</div>

View more detail in: https://www.w3schools./bootstrap/bootstrap_button_groups.asp

You could wrap the buttons in a flex, and then control the width of your flex-div using custom css, or Bootstrap Grid System. JSFiddle

<div class="row">
  <div class="col-6 mx-auto">
    <div class="d-flex align-items-center justify-content-evenly">
      <button class="btn btn-primary">Button 1</button>
      <button class="btn btn-primary">Button 2</button>
    </div>
  </div>
</div>

Use the flex CSS in the example, the counter is just for numbering the buttons, it's not required.

.center {
  display: flex;
  flex-flow: row nowrap;
  justify-content: space-between;
  counter-reset: button;
}

.btn::before {
  counter-increment: button;
  content: counter(button);
}
<link href="https://maxcdn.bootstrapcdn./bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">
<main class='container'>
  <section class='row'>
    <div class='col center'>
      <button class='btn btn-primary btn-lg'></button>
      <button class='btn btn-primary btn-lg'></button>
      <button class='btn btn-primary btn-lg'></button>
      <button class='btn btn-primary btn-lg'></button>
      <button class='btn btn-primary btn-lg'></button>
    </div>
  </section>

Add a element that fill have all the buttons, in your css folder, style that div with these styles:

{ 
  display:flex; align-item:center;
  justify-content: space-around;
}

If they are many buttons, let the display: flex;, to be display: inline-flex;.

You dont need any classes by default they will align themselfs in a row and we can use text-center on the container to make them centered horizontally

<script src="https://unpkg./@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>


<div class="container text-center">
  <button class="btn btn-primary" type="submit">Button</button>
  <button class="btn btn-primary" type="submit">Button</button>
  <button class="btn btn-primary" type="submit">Button</button>
</div>

You dont need bootstrap for that. You can easily do alignment using flex layout. Bootstrap can be used for only styling elements.

Read More: a-guide-to-flexbox

.container{
  margin-top:20px;
  display: flex;
}

.container{
  display: flex;
}
.cl-1{
  justify-content: center;
}
.cl-1 button{
  margin-right: 20px;
}
.cl-2{
  justify-content: space-between;
}
.cl-3{
  justify-content: space-evenly;
}
.cl-4{
  justify-content: space-around;
}
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>
<h2>Center align</h2>
<div class="container text-center cl-1">
  <button class="btn btn-primary" type="submit">Cancel</button>
  <button class="btn btn-primary" type="submit">Submit</button>
</div>

<h2>space-beetween</h2>
<div class="container text-center cl-2">
  <button class="btn btn-primary" type="submit">Cancel</button>
  <button class="btn btn-primary" type="submit">Submit</button>
</div>

<h2>space-evenly</h2>
<div class="container text-center cl-3">
  <button class="btn btn-primary" type="submit">Cancel</button>
  <button class="btn btn-primary" type="submit">Submit</button>
</div>

<h2>space-around</h2>
<div class="container text-center cl-4">
  <button class="btn btn-primary" type="submit">Cancel</button>
  <button class="btn btn-primary" type="submit">Submit</button>
</div>

If you want a row of buttons, but with a small gap in between them - unlike what you get with .btn-group class, which hugs the buttons together - then you can use d-flex and gap-[number] classes in a surrounding div.

<div class="d-flex gap-2">
  <button class="btn btn-primary" type="button">Button 1</button>
  <button class="btn btn-primary" type="button">Button 2</button>
  <button class="btn btn-primary" type="button">Button 3</button>
</div>

Or for a more 'responsive' variation you can use d-grid, gap-[number] and d-md-block classes, again in a surrounding div:

<div class="d-grid gap-2 d-md-block">
  <button class="btn btn-primary" type="button">Button 1</button>
  <button class="btn btn-primary" type="button">Button 2</button>
  <button class="btn btn-primary" type="button">Button 3</button>
</div>

As explained in the Bootstrap docs:

Here we create a responsive variation, starting with vertically stacked buttons until the md breakpoint, where .d-md-block replaces the .d-grid class, thus nullifying the gap-2 utility. Resize your browser to see them change.

Obviously can change the breakpoint from md to something else, e.g. xs or sm.

Both of these approaches work in Bootstrap v5 or later.

发布评论

评论列表(0)

  1. 暂无评论