問題描述
如何將引導程序 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.
(by n0moreCRY、Bishakh Ghosh、Ch Asjad、Ahmad Karimi)