gpt4 book ai didi

html - 如何在 bootstrap 中将图像居中放置在 2 列上

转载 作者:行者123 更新时间:2023-11-28 02:25:47 28 4
gpt4 key购买 nike

我想创建一个分屏 Bootstrap 4 页面,并将图像放在与两列重叠的中心。我创建了两个相等的列,但我不知道如何放置图像以使其重叠。

我已经尝试了 position:absolutefloat:left 但它没有帮助。感谢您的帮助!

预期: Photo

html,
body {
height: 100%;
margin: 0;
}

body {
overflow-x: hidden;
}

col {
height: 100vh;
}

img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
<!DOCTYPE html>
<html lang="en">

<head>
<title>Website</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<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.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
</head>

<body>
<img src="https://imgur.com/Qk5Qa9M.jpg" class="mx-auto d-block" style="width:auto">
<div class="row h-100">
<div class="col" style="background-color:blue">
Left
</div>
<div class="col" style="background-color:red">
Right
</div>
</div>
</body>

</html>

最佳答案

这正在解决您的问题:

html,
body {
height: 100%;
margin: 0;
}

body {
overflow-x: hidden;
}

col {
height: 100vh;
}

img {
position: absolute;
bottom: 0; /* i deleted top:0; */
left: 50%; /* this and below code for excellent centered */
transform:translateX(-50%);
z-index:2;
}
<!DOCTYPE html>
<html lang="en">

<head>
<title>Website</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<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.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
</head>

<body>
<img src="https://picsum.photos/400/500">
<div class="row h-100">
<div class="col" style="background-color:blue">
Left
</div>
<div class="col" style="background-color:red">
Right
</div>
</div>
</body>

</html>

另外,我从 img 标签中删除了 classstyle 属性。因为他们没有必要。

关于html - 如何在 bootstrap 中将图像居中放置在 2 列上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54064094/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com