首页 > 解决方案 > 具有全宽背景的引导容器子项

问题描述

如何让#section2 的渐变背景为全角(以填充视口的宽度)?
我使用引导程序,我想尊重这些约束:

也许,有没有办法补偿容器和部分引入的边距?

body * {
  border: 1px solid black;
}

section {
  height: 100px;
}

#section2 {
  color: white;
  background: linear-gradient(180deg, blue 0%, white 100%);
}
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>

<body>
  <header>header</header>
  <main class="container">
    <section>section 1</section>
    <section id="section2">section 2</section>
    <section>section 3</section>
  </main>
  <footer>footer</footer>
</body>

</html>

标签: htmlcsstwitter-bootstrap

解决方案


用于Puedo Elements这样做:

#section2:before {
  content: "";
  position: absolute;
  height: 100px;
  width: 100%;
  left: 0;
  background: linear-gradient(180deg, red 0%, yellow 100%);
}

代码笔: https ://codepen.io/manaskandelwal1/pen/OJRoaXZ


推荐阅读