如何將引導程序 4 中的內容連續居中? (How to center in bootstrap 4 the content in a row?)


問題描述

如何將引導程序 4 中的內容連續居中? (How to center in bootstrap 4 the content in a row?)

我想將我擁有的內容(例如圖像)放在行和列的中心。

我嘗試使用 justify‑content‑center,或 align‑item 和 text‑align: center ,但沒有任何效果。

我需要圖像位於行的中心。就像,如果我有 4 列,我需要讓所有 4 列居中。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf‑8">
    <title>Bootstrap Lessons</title>
    <meta name="viewport" content="width=device‑width, initial‑scale=1">

    <!‑‑ Stilurile mele ‑‑>
    <link rel="stylesheet" type="text/css" href="styles.css">

    <!‑‑ Fisier spre jQuery ‑‑>
    <script src="https://code.jquery.com/jquery‑3.4.1.js" integrity="sha256‑WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="crossorigin="anonymous"></script>

    <!‑‑ Fisier spre javascript ‑‑>
    <script type="text/javascript" src="file.js" async></script>

    <!‑‑ FontAwesome ‑ Icons ‑‑>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font‑awesome/4.7.0/css/font‑awesome.min.css">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384‑ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384‑JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script src="https://code.jquery.com/jquery‑3.3.1.slim.min.js" integrity="sha384‑q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384‑UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  </head>
<body>

  <div class="wrapper">
    <div class="container">
        <div class="row justify‑content‑center">
                <div class="col‑xl‑12">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
        </div>
    <div class="row justify‑content‑center">
                <div class="col‑xl‑4">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
                    <div class="col‑xl‑4">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
                    <div class="col‑xl‑4">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png">
                </div>
        </div>
    </div>
</div>

</body>
</html>

參考解法

方法 1:

You can use the following CSS on the images:

display: block;
margin: 0 auto

Full code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf‑8">
    <title>Bootstrap Lessons</title>
    <meta name="viewport" content="width=device‑width, initial‑scale=1">

    <!‑‑ Stilurile mele ‑‑>
    <link rel="stylesheet" type="text/css" href="styles.css">

    <!‑‑ Fisier spre jQuery ‑‑>
    <script src="https://code.jquery.com/jquery‑3.4.1.js" integrity="sha256‑WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="crossorigin="anonymous"></script>

    <!‑‑ Fisier spre javascript ‑‑>
    <script type="text/javascript" src="file.js" async></script>

    <!‑‑ FontAwesome ‑ Icons ‑‑>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font‑awesome/4.7.0/css/font‑awesome.min.css">

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384‑ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384‑JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>

    <script src="https://code.jquery.com/jquery‑3.3.1.slim.min.js" integrity="sha384‑q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384‑UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  </head>
<body>

  <div class="wrapper">
    <div class="container">
        <div class="row justify‑content‑center">
                <div class="col‑xl‑12">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
        </div>
    <div class="row justify‑content‑center">
                <div class="col‑xl‑4">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
                    <div class="col‑xl‑4">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
                    <div class="col‑xl‑4">
                <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png" style="margin:0 auto; display:block">
                </div>
        </div>
    </div>
</div>

</body>
</html>

方法 2:

Bootstrap 4 has new class which you can add with row class to center the content.

<div class="row justify‑content‑center">
  !‑‑ your content here ‑‑!
</div>

Hopefully will help you. This is also answered in similar question Center Contents of Bootstrap row container

方法 3:

Just Add text‑center class to each column you want its content to be aligned center.

      <div class="col‑xl‑4 text‑center">
          <img class="img‑fluid" src="https://i.ibb.co/6WddWMk/test.png">
      </div>

Here is the link to your code with this class added to every column.

Link

(by n0moreCRYBishakh GhoshCh AsjadAhmad Karimi)

參考文件

  1. How to center in bootstrap 4 the content in a row? (CC BY‑SA 2.5/3.0/4.0)

#bootstrap-4 #row #css #twitter-bootstrap #html






相關問題

在不同選項卡上時如何隱藏特定選項卡 (How to hide specific tab when on different tab)

如何將引導程序 4 中的內容連續居中? (How to center in bootstrap 4 the content in a row?)

構建 JavaScript 時缺少模塊 (Missing Module When Building JavaScript)

React-Bootstrap 選項卡 defaultActiveKey 不隨 stateChange 改變 (React-Bootstrap Tabs defaultActiveKey is not changing with stateChange)

表格內容的對齊方式 (alignment of the content in table)

切換器圖標無法調整導航欄寬度 (Toggler icon does not working on adjusting the navbar width)

如何根據設備大小使用屏幕 (How to use the screens based on the device size)

如何在引導程序 4 中為 col 調整大小設置動畫? (How to animate col resize in bootstrap 4?)

html頁面Bootstrap頂部的白條 (White bar at top of html page Bootstrap)

如何從 reactstrap 輸入覆蓋表單控制類? (How can I override form-control class from a reactstrap input?)

文檔似乎沒有變化,但為什麼寬度在 Bootstrap 4 和 5 之間給出不同的圖像比例? (It seems to be no change in docs, but why does width give different image scale between Bootstrap 4 and 5?)

引導表拆分單元格 (Bootstrap table split cells)







留言討論