javascript - 如何使用 bootstrap4 修复图像对齐?
问题描述
存在对齐问题的图像应始终附有图像中给出的左右角。现在我正在使用bootstrap container
这个。
我的代码:-
.p-relative {
position: relative;
}
.p-static {
position: static;
}
.img-1 {
position: absolute;
top: 0px;
left: 0;
}
.img-2 {
position: absolute;
top: 0px;
left: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<section class="pt-5 pb-5 p-relative">
<div class="container p-static">
<div class="row">
<div class="col-6 p-static">
<img src="https://i.stack.imgur.com/Lrz2v.jpg" class="img-1 img-fluid" alt="">
</div>
<div class="col-6 pl-5">
<h3>Heading</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
</div>
</div>
<div class="row">
<div class="col-6 pl-5">
<h3>Heading</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
</div>
<div class="col-6 p-static">
<img src="https://i.stack.imgur.com/Lrz2v.jpg" class="img-2 img-fluid" alt="">
</div>
</div>
</div>
</section>
</body>
</html>
答案将被占用,谢谢!
解决方案
我已删除所有位置设置,因为我认为它们没有必要。我使用 bs4no-gutters
和pt-5
类来设置边距和填充以对齐图像和文本。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<section class="pt-5 pb-5">
<div class="container">
<div class="row no-gutters">
<div class="col-6">
<img src="https://i.stack.imgur.com/Lrz2v.jpg" class="img-fluid" alt="">
</div>
<div class="col-6 pl-5">
<h3>Heading</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
</div>
</div>
<div class="row no-gutters">
<div class="col-6 pl-5 pt-5">
<h3>Heading</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
</div>
<div class="col-6 pt-5">
<img src="https://i.stack.imgur.com/Lrz2v.jpg" class="img-fluid" alt="">
</div>
</div>
</div>
</section>
</body>
</html>
我已更新代码以将图像附加到左侧和右侧部分。
.img-fluid {
width: 100%;
height: 80% !important; /* You may edit the height accordingly */
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<section class="pt-5 pb-5">
<div class="container-fluid px-0">
<div class="row no-gutters">
<div class="col-6">
<img src="https://i.stack.imgur.com/Lrz2v.jpg" class="img-fluid" alt="">
</div>
<div class="col-6 pl-5">
<h3>Heading</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
</div>
</div>
<div class="row no-gutters">
<div class="col-6 pl-5">
<h3>Heading</h3>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. </p>
</div>
<div class="col-6">
<img src="https://i.stack.imgur.com/Lrz2v.jpg" class="img-fluid" alt="">
</div>
</div>
</div>
</section>
</body>
</html>
推荐阅读
- vue.js - 从 Vue 组件的 Javascript 部分到资产的路径
- python - 在 Python 中定义类变量时列表理解中未定义的变量
- c# - C# 反射方法 GetProperties(BindingFlags.Instance) 不返回子类对象
- ios - CollectionView 中的水平分页,就像 iPhone 相机中的一样(视频/慢动作/照片/等)
- blender - Cesium:gltf 文件的随机轮廓渲染
- python - 在python类的方法中获取self和class对象
- phpmyadmin - phpmyadmin mysql 数据库托管
- azure - Azure Blob 存储,如何实现任何文档或文件的版本
- php - 如何在找到单个记录时执行 mysql 查询,目标是返回 2 行
- javascript - setInterval 调用函数和html样式没有变化?