Home > Net >  Bootstrap prevent sidebar from overlaping with main content
Bootstrap prevent sidebar from overlaping with main content

Time:01-05

I have the following layout

Example

Im having a little issue tho when I rezie the window the overlap with the main content

Example of the issue

How do I prevent this issue and retain a padding ?

Here is my html and Css

<body>
    <div >
        <a href="#">test</a>
        <a href="#">test</a>
        <a href="#">test</a>
    </div>
    <div  id="fade">
        <div  style=" background-color:rgb(240, 240, 255)">


        </div>
    </div>
</body>

.sidebar {
    margin: 0;
    padding: 0;
    width: 200px;
    background-color: #f1f1f1;
    position: fixed;
    height: 100%;
    overflow-y: auto;
}

.sidebar a {
    display: block;
    color: black;
    padding: 16px;
    text-decoration: none;
}

.sidebar a.active {
    background-color: #04AA6D;
    color: white;
}

.sidebar a:hover:not(.active) {
    background-color: #555;
    color: white;
}

@media screen and (max-width: 700px) {
    .sidebar {
        width: 100%;
        height: auto;
        position: relative;
    }
    .sidebar a {
        float: left;
    }
    div.content {
        margin-left: 0;
    }
}

@media screen and (max-width: 400px) {
    .sidebar a {
        text-align: center;
        float: none;
    }
}

I tried to add padding however this did not solve the issue

CodePudding user response:

Two main things to consider:

  1. Bootstrap col should always be wrapped in a row.
  2. Change position: fixed; (this broke Bootstrap grid) to position: sticky;. Also, add height: 100vh;.

See the snippet below.

.sidebar {
  margin: 0;
  padding: 0;
  background-color: #f1f1f1;
  position: sticky;
  height: 100vh;
  overflow-y: auto;
}

.sidebar a {
  display: block;
  color: black;
  padding: 16px;
  text-decoration: none;
}

.sidebar a.active {
  background-color: #04AA6D;
  color: white;
}

.sidebar a:hover:not(.active) {
  background-color: #555;
  color: white;
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL jjXkk Q2h455rYXK/7HAuoJl 0I4" crossorigin="anonymous"></script>

<body>
  <div >
    <div >
      <a href="#">test</a>
      <a href="#">test</a>
      <a href="#">test</a>
    </div>
    <div  id="fade">
      <div  style="background-color:rgb(240, 240, 255)">
      </div>
    </div>
  </div>
</body>

  • Related