我想讓我的圖片和文本垂直居中對齊。 一個接一個的出現在新的生產線上。 特別是左側,這里有圖像、文本1和文本2 理想的情況是
Image |
| text1 |
| text2
全部垂直和水平居中
實施:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" />
<!-- Custom styles for this template -->
<link href="./index.css" rel="stylesheet" />
<title>Login</title>
</head>
<body>
<div class="container-fluid ps-md-0">
<div class="row g-0">
<div class="col-md-4 col-lg-6 left-side">
<div class="d-flex align-items-center justify-content-center text-center">
<img src="../pictures/logo.png" alt="" width="72" height="72" />
<h1>text1</h1>
<h3>text2</h3>
</div>
</div>
<div class="col-md-8 col-lg-6">
<div class="login d-flex align-items-center py-5">
<div class="container">
<div class="row">
<div class="col-md-9 col-lg-8 mx-auto">
<h3 class="login-heading mb-4">Welcome back!</h3>
<!-- Sign In Form -->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<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://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
將d-flex和justify-content-center添加到包含所有元素的父容器的父容器。然后將text-center添加到所有元素的父容器中。
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" />
<!-- Custom styles for this template -->
<link href="./index.css" rel="stylesheet" />
<title>Login</title>
</head>
<body>
<div class="container-fluid ps-md-0">
<div class="row g-0">
<div class="col-md-4 col-lg-6 left-side d-flex justify-content-center">
<div class="text-center">
<img src="https://via.placeholder.com/500x500.png" alt="" width="72" height="72" />
<h1>Text</h1>
<h3>Text</h3>
</div>
</div>
<div class="col-md-8 col-lg-6">
<div class="login d-flex align-items-center py-5">
<div class="container">
<div class="row">
<div class="col-md-9 col-lg-8 mx-auto">
<h3 class="login-heading mb-4">Welcome back!</h3>
<!-- Sign In Form -->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
要將圖像和文本堆疊在一起,可以使用d-flex和flex-direction-column使每個div堆疊在一起。因為軸已經從水平變為垂直,所以使用align-items-center使文本居中。
要使它垂直顯示,同樣使用d-flex和align-items-center,但是使用h-100將div設置為父級的高度。我人為地增加了body元素的大小,因為它會收縮到內容,否則。在下面做標記。您可能需要全屏顯示代碼片段來查看示例的工作情況
我會避免用html屬性來調整圖片的大小。現在最好使用CSS。
body {
height: 80vh;
}
<link rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" />
<div class="col-md-4 col-lg-6 left-side h-100 d-flex align-items-center">
<div class="d-flex flex-column align-items-center">
<img src="https://placekitten.com/200/200" alt="" width="72" height="72" />
<h1>text1</h1>
<h3>text2</h3>
</div>
</div>
使用類將flex-direction:列添加到父容器中。d-flex (或者使用將flex-direction設置為column的引導類)
另外,我注意到你有很多!在你的CSS中很重要。 消除盡可能多的!越重要越好。
祝你好運!