Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I have the gradient background of #section2 to be fullwidth (to fill the viewport in width)?
I use bootstrap and I would like to respect these constraints:

  • no javascript
  • not removing container class from <main>
  • not adding horizontal scrolling (the background should not be wider than the viewport, even when a vertical scrollbar appears)

Maybe, is there a way to compensate the margins introduced by container and section?

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>
question from:https://stackoverflow.com/questions/65672203/bootstrap-container-child-with-fullwidth-background

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
210 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use Puedo Elements to do so:

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

Codepen: https://codepen.io/manaskhandelwal1/pen/OJRoaXZ


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...