I need to create an empty topbar and an empty sidebar as below. I want to stack them neatly so that the top of the sidebar touches the bottom of the topbar. For that, I am using percentage. However, I just can't get them to do that.
I saw some other threads suggesting to set html and body with height: 100%; and width : 100%. But in my app, the layout below is not in the main html document.
#nav-bar {
position: absolute;
top: 0;
left: 0;
background-color: blue;
height: 10%;
width: 100%;
}
#side-bar {
position: absolute;
top: 0;
left: 0;
background-color: #011a21;
height: 90%;
width: 7.5%;
/*margin-top: 10%;*/
}
<div id="nav-bar"> </div>
<div id="side-bar"></div>
Your #nav-bar has a height: 10%, and you can set the same value for the top rule, selector #side-bar.
#nav-bar {
position: absolute;
top: 0;
left: 0;
background-color: blue;
height: 10%;
width: 100%;
}
#side-bar {
position: absolute;
top: 10%;
left: 0;
background-color: #011a21;
height: 90%;
width: 7.5%;
/*margin-top: 10%;*/
}
<div id="nav-bar"> </div>
<div id="side-bar"></div>
I tend to use flex box for this, Maybe grid would be smarter though. I took also care of some scrolling things. Using min with and height 0 on flex parents is key for that.
body,
section {
min-height: 0;
min-width: 0;
}
body {
padding: 0;
margin: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
nav {
height: 2rem;
background: green;
}
section {
flex: 1 1 0%;
display: flex;
}
aside {
width: 4rem;
background: blue;
height: 100%;
}
main {
background: pink;
flex: 1 1 0%;
overflow: auto;
padding: 1rem;
}
div {
border: 1px black dashed;
height: 300%;
}
<nav></nav>
<section>
<aside></aside>
<main>
<div>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br> Lorem ipsum dolor sit amet consectetur adipisicing elit. Rerum, tempora. Voluptates, iure quis enim hic eius ullam sequi suscipit maiores, eum consectetur nesciunt quae dolorum! Voluptate adipisci praesentium illum dolorem.
<br>
</div>
</main>
</section>
Related
When we set a block level element height to 100% it will take the full height min-height: 100%; of the page or at least the full height of its parent, however what is strange for me as I'm currently learning CSS is that when I shrink the height of the browser window
Here is a few seconds video to show what I mean. Video
As you can already see from the video, when I make the height of the browser very short and then scroll down I see that the content section no longer takes the full height until I make the browser height a little bit longer, however the sidebar and the profile sections don't do that since their width are a less than the content section.
So the question is: How to make the div to keep its full height when the browser height shrinks and then scroll down as you can see from the video?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
height: 90%;
}
body {
position: relative;
overflow-y: scroll;
height: 100%;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar {
position: absolute;
width: 15%;
left: 0;
top: 0;
background-color: darkblue;
color: white;
padding: 2em;
margin: 2em;
min-height: 100%;
}
.content {
position: absolute;
width: 60%;
left: calc(15% + 2em);
top: 0;
background-color: green;
color: white;
padding: 2em;
margin: 2em;
min-height: 100%;
}
.profile {
position: absolute;
width: 15%;
top: 0;
left: calc(75% + 4em);
padding: 2em;
margin: 2em;
background-color: firebrick;
color: black;
min-height: 100%;
}
button {
display: block;
margin-top: 2em;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
My code on codepen: Code
CSS-Grid solution
One easy way is to use CSS-Grid. I cut your code down to the part that is actually "usefull" and removed all unecessary or non-working code.
display: grid activates CSS-Grid
grid-template-columns: 15% auto 15% creates your 3 column layout while auto occupies the remaining space.
min-height: 100vh sizes the boxes to be at least as heigh as the viewport.
body {
display: grid;
grid-template-columns: 15% auto 15%;
grid-gap: 2em;
padding: 2em;
min-height: 100vh;
}
/* original CSS cut down */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar,
.content,
.profile {
padding: 2em;
}
.sidebar {
background-color: darkblue;
color: white;
}
.content {
background-color: green;
color: white;
}
.profile {
background-color: firebrick;
color: black;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
Flexbox solution:
To use Flexbox you have to use display: flex. Same as the CSS-Grid solution you also give the body a min-height: 100vh to fill up at least the entire viewport.
To get your intended 3 column layout, you have to define your left and right side as width: 15%.
To have the middle column fill up the remaining space, you just need to add: flex-grow: 1;
body {
display: flex;
gap: 2em;
padding: 2em;
min-height: 100vh;
}
.sidebar,
.profile {
width: 15%;
}
.content {
flex-grow: 1;
}
/* original CSS cut down */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
h2 {
margin-bottom: 2em;
text-align: center;
}
p {
text-align: center;
}
.sidebar,
.content,
.profile {
padding: 2em;
}
.sidebar {
background-color: darkblue;
color: white;
}
.content {
background-color: green;
color: white;
}
.profile {
background-color: firebrick;
color: black;
}
<div class="sidebar">
<h2>Sidebar</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="content">
<h2>Content</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
<div class="profile">
<h2>Profile</h2>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Laboriosam, voluptatibus tenetur. Et facere provident voluptatem praesentium illum explicabo vel architecto eum repellat facilis. Eum repudiandae nobis ad aliquid aut. Ut.</p>
</div>
This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 1 year ago.
I am trying to create one design where i need to center some content in section from top to bottom like in column,
here is the code below
1:HTML
<div class="section">
<div class="content">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti
soluta, molestiae quia aperiam doloremque odit dolores veritatis tempora
architecto illo facere, iure reprehenderit ab, odio perferendis ex sint
temporibus quidem!
</p>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti
soluta, molestiae quia aperiam doloremque odit dolores veritatis tempora
architecto illo facere, iure reprehenderit ab, odio perferendis ex sint
temporibus quidem!
</p>
</div>
Here is SCSS/CSS
.section {
background-image: url(".././assets/scss/images/top_section_bg.jpg");
background-size: cover;
padding: 100px;
.content {
position: relative;
display: flex;
align-items: center;
justify-content: center;
width: 800px;
height: 500px;
border-radius: 5px;
margin: 0 auto;
&::before {
content: "";
background: $bg-dark 0% 0% no-repeat padding-box;
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
opacity: 0.75;
}
p {
position: relative;
color: #ffffff;
margin:100px;
line-height: 0.9;
}
}
}
Above code is in which I am trying to make that black bg tranparent and text over it to appear clearer
You need to add flex-direction: column to .content container as the default direction for flex is row which is left-to-right but if you need to change to top-to-bottom then you need to change the flex-direction.
.section {
background-image: url(".././assets/scss/images/top_section_bg.jpg");
background-size: cover;
padding: 100px;
}
.section .content {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 800px;
height: 500px;
border-radius: 5px;
margin: 0 auto;
}
.section .content::before {
content: "";
background: black 0% 0% no-repeat padding-box;
position: absolute;
top: 0px;
right: 0px;
bottom: 0px;
left: 0px;
opacity: 0.75;
}
.section .content p {
position: relative;
color: #fff;
margin: 100px;
line-height: 0.9;
}
<div class="section">
<div class="content">
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti soluta, molestiae quia aperiam doloremque odit dolores veritatis tempora architecto illo facere, iure reprehenderit ab, odio perferendis ex sint temporibus quidem!
</p>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Deleniti soluta, molestiae quia aperiam doloremque odit dolores veritatis tempora architecto illo facere, iure reprehenderit ab, odio perferendis ex sint temporibus quidem!
</p>
</div>
</div>
If I understand the question correctly all you need to do is assign flex-direction: column to .content so that paragraphs could be displayed in a column. Otherwise the default is row.
what I am trying to accomplish is to make a frosty glass effect for a header. The header has a white background with black text and is fixed. The idea is that the content that is behind this header (when scrolling down) is shown with the frosty glass effect.
I succeeded partly by changing the opacity of my header to 0.9, but I can't get the blur effect (via filter) to work. Anybody an idea what I have to change? Here is my code:
html,
body {
position: relative;
width: 100%;
height: 100%;
overflow: auto;
overflow-x: hidden;
color: black;
}
* {
margin: 0;
padding: 0;
}
body {
box-sizing: border-box;
font-family: "Roboto", sans-serif;
font-size: 16px;
}
main {
width: 100%;
background: lightblue;
section {
padding: 80px;
}
h1 {
margin-bottom: 20px;
}
p {
margin-bottom: 20px;
}
}
.outer-header {
// background-repeat: no-repeat;
// background-attachment: fixed;
// background-size: cover;
// background-position: top;
// background-image: url(https://4bp.blogspot.com/-1f1sdfix3dy/Uh92eZAQ90I/AAAAAAAAHM4/5oiB4zC_tQ4/s1600/Photo-Background-White4.jpg);
background: white;
height: 80px;
position: sticky;
top: 0px;
left: 0px;
opacity: 0.9;
overflow: hidden;
z-index:4;
&::after {
z-index: -1;
content: "";
background: inherit;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
box-shadow: inset 0 0 200px rgba(255, 255, 255, 0.05);
filter: blur(10px);
// filter: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg'><filter id='svgMask'><feGaussianBlur stdDeviation='10' /></filter></svg>#svgMask");
}
}
header {
display: flex;
align-items: center;
justify-content: space-between;
height: 100%;
}
img {
height: 50px;
cursor: pointer;
object-fit: cover;
}
h1 {
position: relative;
}
.logo-text {
font-size: 33px;
position: absolute;
left: 50px;
top: 6px;
}
nav,
ul {
display: flex;
grid-column-gap: 20px;
justify-items: end;
align-items: center;
}
<div class="outer-header">
<header>
<h1>
<img src="https://source.unsplash.com/random/287x287" />
<span class="logo-text">Company</span>
</h1>
<nav>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
</nav>
</header>
</div>
<main>
<section>
<h1>Heading</h1>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Hic est autem vel
nam laborum ad at! Labore iusto quae porro nostrum dicta esse consequatur
sit. Repellat quia deleniti enim in.
</p>
</section>
<section>
<h1>Heading</h1>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Hic est autem vel
nam laborum ad at! Labore iusto quae porro nostrum dicta esse consequatur
sit. Repellat quia deleniti enim in.
</p>
</section>
<section>
<h1>Heading</h1>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Hic est autem vel
nam laborum ad at! Labore iusto quae porro nostrum dicta esse consequatur
sit. Repellat quia deleniti enim in.
</p>
</section> <section>
<h1>Heading</h1>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Hic est autem vel
nam laborum ad at! Labore iusto quae porro nostrum dicta esse consequatur
sit. Repellat quia deleniti enim in.
</p>
</section> <section>
<h1>Heading</h1>
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Hic est autem vel
nam laborum ad at! Labore iusto quae porro nostrum dicta esse consequatur
sit. Repellat quia deleniti enim in.
</p>
</section>
</main>
You are looking for the (still experimental) backdrop-filter property. However, browser support is still a bit scarce. For Firefox, it will land in the upcoming version 70 behind a developer flag. Safari still requires the -webkit- vendor prefix. But you can test this working example in a current version (76+) of Chrome.
body {
background: url(http://placekitten.com/800/600) no-repeat;
}
#overlay {
width: 50vw;
height: 50vh;
background: rgba(255,255,255,0.4);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
<div id="overlay"></div>
I've been trying different ways but couldn't achieve what I want.
<div id="parent">
<div id="child-1"></div>
<div id="child-2"></div>
<div id="child-3"></div>
</div>
So I have the #parent at height: 100vh.
#child-1 should have height: 100% of parent.
#child-2 and #child-3 should have width: 100% and height: auto and they should be stacked on top of each other at position bottom: 0.
I've been trying to set parent relative and two childs absolute but the first child's height gets ignored.. I tried with display flex but first child's height is not 100% of parent.. I'm very confused how to do this.
Can someone help?
Here is what I'm trying to achieve: jsfiddle.net
You have to first get the bottom value of #child-2 dynamically as you said it should be on the top of #child-3.
You need to apply jQuery to get the height of #child-3 dynamically and then applying the height value of #child-3 to the bottom value of child-2, just like
#child-2 {
bottom: height-of-child-3;
}
Look at this Codepen
Or look at the snippet below:
height_child_three = $('#child-3').height();
$('#child-2').css({
position: 'absolute',
bottom: height_child_three
});
#parent {
width: 100vw;
height: 100vh;
background: #000;
position: relative;
}
#child-1 {
width: 100%;
height: 100%;
background: #eee;
}
#child-2 {
width: 100%;
background: #a0ea0e;
}
#child-3 {
position: absolute;
bottom: 0;
width: 100%;
background: #30e30e;
}
body { margin: 0; } /* A small reset */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
<div id="child-1">
<strong>I'm child 1</strong>
</div>
<div id="child-2">
<strong>I'm child 2</strong>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ratione deleniti voluptate commodi distinctio, repellendus qui, placeat laboriosam eligendi! Ducimus reiciendis officiis debitis placeat adipisci quae hic tempore vitae suscipit nemo.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam sed aliquid, laborum nisi quos excepturi hic! Molestias hic consectetur dolor! Perferendis iste, quisquam quaerat ab, odio ducimus! Odio, minima error?</p>
</div>
<div id="child-3">
<strong>I'm child 3</strong>
</div>
</div>
Hope this helps!
Is this what you need?
HTML:
<div id="parent">
<div class="child-1"></div>
<div class="child-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Error voluptatum necessitatibus dolorem soluta laudantium cupiditate maiores neque, aliquid accusamus autem saepe tempora, itaque possimus, eaque deleniti odio atque enim omnis.</div>
<div class="child-3">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa, illo est dolor dolores placeat deleniti quae consequuntur eum ipsum blanditiis laboriosam quod repellendus fugit! Odio quis rem vel a dolores.</div>
</div>
CSS:
html,
body,
div {
margin: 0;
padding: 0;
}
*,
*:after,
*:before {
box-sizing: border-box;
}
#parent {
position: relative;
width: 100vw;
height: 100vh;
background: #ccc;
}
.child-1 {
width: 100%;
height: 100%;
background: red;
}
.child-2 {
width: 100%;
height: auto;
padding: 30px;
background: blue;
}
.child-3 {
width: 100%;
height: auto;
padding: 30px;
background: green;
}
Here you can see a solution just using plain CSS. CODEPEN
How can I make the same width of the NavWrapper as parent?
I want these links at a fixed position even the main section overflows.
I know how to do this without Flex. Is there any pure CSS way to do that?
body {
padding:0;
margin:0
}
.wrapper {
display: flex;
}
nav {
flex: 1 1 150px;
background: gray;
}
.nav-wrapper {
width: 100%;
position: fixed;
top: 0; left: 0;
display:flex;
flex-direction: column;
}
.nav-wrapper a {
flex: 1 1;
border: 1px solid red;
}
section {
flex: 5 1 500px;
padding: 20px;
}
<div class="wrapper">
<nav role="navigation">
<div class="nav-wrapper">
Home
About
</div>
</nav>
<section>
<p>Lorem</p>
</section>
</div>
You don't need fixed position- you can see why I say this after looking at the example below:
Remove the fixed positioning and add height: 100vh to nav:
nav {
flex: 1 1 150px;
background: gray;
height: 100vh;
}
Now wrap the contents on a section into an inner div that is positioned absolute like this:
section {
flex: 5 1 500px;
padding: 20px;
position: relative;
overflow-y: auto;
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
This will allow the section to remain at 100vh of the nav-wrapper and the extra height will overflow.
body {
padding: 0;
margin: 0
}
.wrapper {
display: flex;
}
nav {
flex: 1 1 150px;
background: gray;
height: 100vh;
}
.nav-wrapper {
width: 100%;
display: flex;
flex-direction: column;
}
.nav-wrapper a {
flex: 1 1;
border: 1px solid red;
}
section {
flex: 5 1 500px;
padding: 20px;
position: relative;
overflow-y: auto;
}
.inner {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="wrapper">
<nav role="navigation">
<div class="nav-wrapper">
Home
About
</div>
</nav>
<section>
<div class="inner">
<p>Lorem</p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae molestiae, libero inventore nobis et veritatis, laborum vitae, vel eaque omnis ad adipisci quia velit blanditiis qui. Cum voluptas quisquam itaque possimus accusamus repellendus quia iure
asperiores. Unde, rerum nihil maiores nisi, iusto voluptate id cumque incidunt, perspiciatis facilis perferendis explicabo.
<p>Lorem</p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae molestiae, libero inventore nobis et veritatis, laborum vitae, vel eaque omnis ad adipisci quia velit blanditiis qui. Cum voluptas quisquam itaque possimus accusamus repellendus quia iure
asperiores. Unde, rerum nihil maiores nisi, iusto voluptate id cumque incidunt, perspiciatis facilis perferendis explicabo.
<p>Lorem</p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quae molestiae, libero inventore nobis et veritatis, laborum vitae, vel eaque omnis ad adipisci quia velit blanditiis qui. Cum voluptas quisquam itaque possimus accusamus repellendus quia iure
asperiores. Unde, rerum nihil maiores nisi, iusto voluptate id cumque incidunt, perspiciatis facilis perferendis explicabo.
</div>
</section>
</div>
Check this out and let me know your feedback. Thanks!