I made a sidebar with bootstrap classes and when i need to expand the height of sidebar full the height of screen it doesn’t work with height:"100%" css property but when i added one more property position:"fixed" and it started working and my sidebar take full height now. What is the relation between position and height?
Code:
import React, { useState } from 'react'
export default function Bar() {
return (
<>
<div className="d-flex flex-column flex-shrink-0" style={{width: "4.5rem",backgroundColor:"#cee8ff",height:"100%",position: "fixed"}}>
<a href="" className="d-block p-3 link-dark text-decoration-none">AMS</a>
<ul className='nav nav-pills nav-flush flex-column mb-auto text-center'>
<li className="nv-item my-2">
<a href="" className="nav-link active py-3 border-bottom" aria-current="page"><i class="fa-solid fa-house"></i></a>
</li>
<li className='my-2'>
<a href="" className="nav-link py-3 border-bottom"><i class="fa-solid fa-address-card"></i></a>
</li>
<li className='my-2'>
<a href="" className="nav-link py-3 border-bottom"><i class="fa-sharp fa-solid fa-bolt"></i></a>
</li>
<li className='my-2'>
<a href="" className="nav-link py-3 border-bottom"><i class="fa-regular fa-calendar-days"></i></a>
</li>
<li className='my-2'>
<a href="" className="nav-link py-3 border-bottom"><i class="fa-solid fa-gear"></i></a>
</li>
<li className='my-2'>
<a href="" className="nav-link py-3 border-bottom"><i class="fa-solid fa-user"></i></a>
</li>
</ul>
</div>
</>
)
}
2
Answers
When you set
height: 100%
, you don’t set it to 100% of the page, you set it to 100% of its’ parent. When you setposition: fixed
, the parent position and sizes are ignored, and thus it fills the whole page height.Just to add to everyone else’s comments, set the
body { height: 100vh; }
so that the div height, at 100% matches the parent’s height (i.e. the body). Code below to show you the effect: