我有以下设计,并希望使用Bootstrap实现它:

enter image description here

正如您所看到的,图像应该从第8个网格开始,一直到页面末尾,不同于网格布局,我已经用codesen创建了一个工作代码片段:

`https://codepen.io/zakarya-noori/pen/KKGQMJo`

以下是我的代码:

img {
  display: block;
  object-fit: cover;
  width: 100%;
  height: 600px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<main>
  <div class="container py-5">
    <div class="row align-items-center">
      <div class="col-6">
        <h1>Right side text <br> and title</h1>
        <h1>Right side text <br> and title</h1>
        <h1>Right side text <br> and title</h1>
      </div>
      <div class="col"></div>
      <div class="col-5">
        <img src="https://images.unsplash.com/photo-1683339708262-b1208394ffec?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=987&q=80" alt="image">
      </div>
    </div>
  </div>
</main>

推荐答案

Bootstrap根据媒体查询设置最大宽度,然后使用Margance:AUTO将内容居中.您可以通过将右页边距设置为零,然后将装订宽度添加到图像的整体宽度来将图像推到边缘.我不得不增加.tainer规则的专用性,使其覆盖Bootstrap的默认设置.

img {
  display: block;
  object-fit: cover;
  width: calc(100% + var(--bs-gutter-x,.75rem));
  height: 600px;
}

main > .container {
  margin-right:0;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<main>
  <div class="container py-5">
    <div class="row align-items-center">
      <div class="col-lg-6">
        <h1>Right side text <br> and title</h1>
        <h1>Right side text <br> and title</h1>
        <h1>Right side text <br> and title</h1>
      </div>
      <div class="col"></div>
      <div class="col-lg-5">
        <img src="https://images.unsplash.com/photo-1683339708262-b1208394ffec?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=987&q=80" alt="image">
      </div>
    </div>
  </div>
</main>

编辑后添加:这里有一个解决方案,使用Java脚本来监听窗口调整大小事件,并相应地调整图像大小.

window.onload = () => {
  window.addEventListener('resize', setImageWidth);
  setImageWidth();
}

function setImageWidth() {
  const img = document.querySelector('#myimage');
  const imgRect = img.getBoundingClientRect();
  const mainRect = document.querySelector('#mymain').getBoundingClientRect();
  img.style.width = (mainRect.width - imgRect.x) +"px";
}
img {
  display: block;
  object-fit: cover;
  width: 100%;
  height: 600px;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
<main id='mymain'>
  <div class="container py-5">
    <div class="row align-items-center">
      <div class="col-lg-6">
        <h1>Right side text <br> and title</h1>
        <h1>Right side text <br> and title</h1>
        <h1>Right side text <br> and title</h1>
      </div>
      <div class="col"></div>
      <div class="col-lg-5">
        <img id='myimage' src="https://images.unsplash.com/photo-1683339708262-b1208394ffec?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=987&q=80" alt="image">
      </div>
    </div>
  </div>
</main>

Image showing margins are the same below: enter image description here

Html相关问答推荐

使用tauri构建的Next.js应用程序不显示我的404页面

如何在不影响子列表的情况下在HTML(OL Li)上同时加数字和列表?

HTML+CSS+JS动态选中文本高亮 colored颜色 变化

如何在伪元素背景中定位多个CSS径向梯度

未知高度正在应用于前标记

当溢出到滚动条中时, bootstrap 按钮样式会更改

如何从elementor中的滑块中获取文本?

如何将文本环绕心形(而不仅仅是圆形)?

如何将tmap按钮和图例更改为位于页脚后面?

如何使用 Tailwind CSS 分隔导航栏中的元素?

:after 伪元素没有出现,即使它有 content 属性

在Angular中,类型"HTMLDivElement"不能分配给类型"Node"

网页设计不适合移动设备

不完整的悬停

我正在使用 NVDA 并多次读取关闭按钮,但它的读取非常完美

如何将自定义图像插入 Shiny plot header?

标签背景 colored颜色 的 html 和 css 技巧说明

CSS 斜角与 3 行对齐

辅助功能:alt 或 alt="" 用于装饰图像

弹出窗口溢出溢出:自动,我不明白为什么