Full-Width Bootstrap Sticky Containerized

I am trying to create a sticky sidebar that fills the full width of the container in Bootstrap 4. I have written the code below, and while the sticky functionality works perfectly, the sidebar does not span the full width of the container. Can someone please assist me? Here is my HTML and CSS:

        <!DOCTYPE html>
    <html lang="en">
      <style>
      .content-section {
        background: lavender;
        height: 1000px;
        min-height: 100vh;
        padding: 1em;
      }

      .sidebar-section {
        height: 100%;
      }

      .sidebar-content {
        background: cornflowerblue;
        padding: 1em;
      }
      </style>
      <head>
        <meta charset="utf-8" />
        <title>Sticky Position Sidebar</title>
        <link
          rel="stylesheet"
          href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
          integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
          crossorigin="anonymous"
        />
        <style>
        </style>
      </head>
    <body>
      <article>
        <div class="container-fluid">
          <div class="row">
            <div class="col">
              <div class="title-section">
                <h1>Stacking Sticky Sidebars with Bootstrap 4</h1>
              </div>
            </div>
          </div>
          <div class="row">
            <div class="col-7">
              <div class="content-section">
                Content Section
              </div>
            </div>
            <div class="col-5">
              <div class="sidebar-section">
                <div class="sidebar-item sticky-top">
                  <div class="sidebar-content">
                    Container 1
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </article>
    </body>
    </html>

Answer №1

When using Bootstrap, keep in mind that the columns will span the full width with a 15px padding on the left and right sides, preventing your content from reaching the edge of the column. To remove this padding, you can simply add no-gutters to the row.

The container div also adds 15px padding which is offset by -15px margins on the rows. By applying px-0 to the container, you can make the rows extend to the full width of the screen within the container's padding.

Make sure to place any additional styles inside the head section rather than before it. It's also recommended to use Stackoverflow’s snippet function when sharing code for better visibility and understanding.

Answer №2

Here is a simple solution without modifying the html or css:

.sidebar-section {
       margin: 0 -2rem 0 -2rem;
       width: auto;
     }

<!DOCTYPE html>
        <html lang="en">
          <style>
          .content-section {
            background: lavender;
            height: 1000px;
            min-height: 100vh;
            padding: 1em;
          }

          .sidebar-section {
            height: 100%;
          }

          .sidebar-content {
            background: cornflowerblue;
            padding: 1em;
          }
          
          .sidebar-section {
            margin: 0 -2rem 0 -2rem;
            width: auto;
           }
          </style>
          <head>
            <meta charset="utf-8" />
            <title>Sticky Position Sidebar</title>
            <link
              rel="stylesheet"
              href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
              integrity="sha384- 
     JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z"
              crossorigin="anonymous"
            />
            <style>
            </style>
          </head>
        <body>
          <article>
            <div class="container-fluid">
              <div class="row">
                <div class="col">
                  <div class="title-section">
                    <h1>Stacking Sticky Sidebars with Bootstrap 4</h1>
                  </div>
                </div>
              </div>
              <div class="row">
                <div class="col-7">
                  <div class="content-section">
                    Content Section
                  </div>
                </div>
                <div class="col-5">
                  <div class="sidebar-section">
                    <div class="sidebar-item sticky-top">
                      <div class="sidebar-content">
                        Container 1
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </article>
        </body>
        </html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Spinning an object using JQuery

I am currently working on a project to test my skills. I have set up a menu and now I want to customize it by rotating the icon consisting of three vertical lines by 90 degrees every time a user clicks on it. This icon is only visible on smartphones when t ...

I am unable to switch my carousel to full-screen mode

I'm struggling to make the carousel fullscreen even though I've set the width to 100%. Bootstrap was used in this project. Any help would be appreciated. Thank you! <html> <head> <link href="homepage.cs ...

How can you ensure a line stays fixed to the bottom of a box regardless of the text size in SCSS and HTML?

I have a CSS rule that displays a line within a div only when the div is active. The specific SCSS class I am using is: .active-tab:after { content: ''; display: inline-block; height: 4px; width: 100px; right: -7px; background-color: ...

Achieving perfect alignment and spacing of columns with HTML/CSS

Just getting started with web development, very new to it. I am in the process of creating a SharePoint site for my workplace using the Modern Script Editor web part. The site will essentially be a resource page with linked buttons on it. I am facing diffi ...

Bootstrap 4.6 - new feature: flexible vertical pills and sleek horizontal tabs (inactive tab no longer highlighted)

I recently implemented Bootstrap 4.6 into my project. The page I am working on includes both vertical pills and horizontal tabs. While the vertical pills are displaying correctly, I have encountered an issue with the styling of the horizontal tabs when th ...

Troubleshooting a Problem with CSS Div Multi-column Formatting

I have designed a multi-column div that displays correctly in Firefox but has formatting issues in Chrome and IE. In Firefox, the three columns appear properly with the frame of the last item in column one ending correctly. However, in Chrome and IE, the f ...

CSS problem with padding and width

Currently, I'm working on a conditional rendering issue where I want to give a div full width but due to padding applied to its parent, it's not expanding to the container's full width. Here is the snippet of the code: import Image from &qu ...

Dividing image styles within css

I am trying to incorporate 4 arrow images into a CSS class (up, down, right, left). I started with a basic arrow css class: .arrow { background-repeat: no-repeat; background-position: center; } Next, I created subclasses like: .arrow .up { ...

The aesthetic of react-bootstrap and material ui aesthetics is distinctive and visually appealing

As I develop my web application, I have incorporated react-bootstrap for its React components based on Bootstrap. However, wanting to give my project a customized look inspired by Material design, I came across bootstrap-material-design. I am curious if I ...

Changing the Color of Hamburger Menu Lines in Bootstrap

How can I change the color of the hamburger menu icon's lines for medium and smaller screen sizes? I have attempted changing it through style attributes and using !important in my CSS file, but so far nothing has been successful. ...

"Media queries in CSS don't consistently apply all styles when reaching the minimum

The CSS styles I am using are as follows: @media (min-width: 1280px) { .window-padding { padding-top: 20px; padding-bottom: 20px; padding-left: 30px; padding-right: 30px; } } @media (min-width: 769px) { .windo ...

"Verifying the dimensions of the input sizes with the i

It's possible that this question has been asked before on this platform. However, I haven't come across a satisfactory answer yet. How can I adjust the size of inputs in the iCheck library? The current size is too large for my website. Css: .ic ...

Is there a way to clear the search box in a wenzhixin bootstrap table without refreshing the page when clicked on?

Is there a way to clear the search box in a wenzhixin bootstrap table without refreshing the page when clicking anywhere on the page? <table id="view_table" data-toggle="table" data-search="true" data-page-list="[5, 10, 20]" data- ...

Are there any jQuery plugins available that animate elements as the page is scrolled?

Looking for a library that can create entry animations for elements as the page is scrolled? I previously used the animate it library, which was great - lightweight and easy to use. However, it doesn't seem compatible with the latest jQuery version (3 ...

Can someone explain why the color of a <select> element in Chrome is being influenced by the CSS property "font-family: inherit"? Here is an example

I've always been curious as to why the <select> element appears in different colors on various web pages. I've noticed that when the font-family:inherit css property affects the select element, it ends up looking blue! <!-- BLACK --> ...

What is preventing us from setting a child's width and height to match that of the parent element?

Parent element is a div with a width of 300px and a height of 40px, containing a child input. In the following code snippet: myinput = document.getElementById("e2"); myinput.style.cssText ='width:100%;height:100%;padding:0px;margin:0px;' div{ ...

The importance of scope in ng-style

I am attempting to dynamically change the font color in an input field based on the value entered into that field. However, I have encountered an issue with using ng-style as it is not recognizing the value of the scope and therefore the color does not upd ...

placing an empty gap within a visual depiction

My gallery lightbox displays images along with descriptions. However, the descriptions are all showing up in the same line with the same style and color. I want to separate each description on a new line to give some space. Here's an example of how th ...

Cannot retrace steps in file tree using ../

My website has a file structure set up like this: css style.css other files... templates index.html other files... I am attempting to reference the style.css file from my index.html document. My initial attempt was to navigate back a directory u ...

Excessive Width Issue Caused by Bootstrap 4 Navbar Items

Having trouble with my Bootstrap 4 Navbar menu implementation. When I add more items, they overflow the container instead of floating correctly. Any CSS suggestions to temporarily fix this issue? <!DOCTYPE html> <html lang="en"> <head> ...