Fixed header&sidebar z-index issue - html

it's been a while!
I am currently trying to solve a problem and I was hoping somebody could help me out.
I have a fixed sidebar and a header. Both cast dropshadow. However, I don't want the header to cast shadow on the sidebar (I want them to be on the same level). At the same time, header contains drop-downs and these need to hover over everything.
Since it's rather complex, I've created a jsfiddle.
Simple example
I've been forced to paste the code here as well for some reason (SO input validation).
<div class="layout">
<div class="header z-depth-2">
<div class="dropdown-toogle">
<div class="dropdown-menu">
</div>
</div>
</div>
<div class="page-wrapper">
<div class="sidebar z-depth-2">
</div>
<div class="content">
</div>
</div>
</div>
And css
.layout {
width: 100%;
height: 100%;
position: relative;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 70px;
background-color: blue;
z-index: 101;
}
.sidebar {
position: fixed;
top: 70px;
left: 0;
width: 200px;
background-color: green;
bottom: 0;
z-index:101;
}
.content {
padding-left: 200px;
width: 100%;
height: 100%;
}
.page-wrapper {
padding-top: 70px;
height: 100%;
}
.dropdown-toogle {
margin-right: 100px;
float: right;
position: relative;
height: 100%;
width: 50px;
background-color: yellow;
}
.dropdown-menu {
position: absolute;
top: 100%;
right: 0;
width: 400px;
height: 200px;
background-color: grey;
z-index:1000;
}
.z-depth-2 {
box-shadow: 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}
Notice how when you change view-port width, drop-down slides behind sidebar even when it's z-index is greater.
If I increase z-index of header, it starts casting shadow on the sidebar (drop-down starts working) which I want to avoid. I've been playing with different combination but was unable to sort it out properly.
Hope I managed to make it clear, help much appreciated!

With your current layout this isn't possible. Since the header is set to a z-index behind the sidebar essentially any child of the header will also be behind the sidebar. (Read more on stacking contexts)
In order to solve this issue what you can do is use an inset box-shadow on the content pane. This will make it so there is a shadow as if it's being cast by the header and sidebar but it's really the container casting it on itself. This way you don't need to worry about the header casting onto the sidebar and the sidebar can safely sit "behind" the header. With this you don't need to fiddle with z-index at all (though I didn't remove them in case it's needed for other things on the page).
In order to get this to work properly I had to change the padding you were using to position the content element with margin but honestly this is a better property to use for adding space around an element. I recommend you also read up a bit on when to use padding vs margin.
https://jsfiddle.net/79gykeu3/6/
HTML
<div class="layout">
<div class="header z-depth-2">
<div class="dropdown-toogle">
<div class="dropdown-menu">
</div>
</div>
</div>
<div class="page-wrapper">
<div class="sidebar z-depth-2">
</div>
<div class="content">
</div>
</div>
</div>
CSS
The thing to note is the .content class now has the box-shadow
html {
height: 100%;
}
body {
width: 100%;
height: 100%;
margin: 0;
}
.layout {
width: 100%;
height: 100%;
position: relative;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 70px;
background-color: blue;
z-index: 103;
}
.sidebar {
position: fixed;
top: 70px;
left: 0;
width: 200px;
background-color: green;
bottom: 0;
z-index:101;
}
.content {
margin-left: 200px;
width: 100%;
height: 100%;
box-shadow: inset 0 4px 5px 0 rgba(0, 0, 0, 0.14), 0 1px 10px 0 rgba(0, 0, 0, 0.12), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
}
.page-wrapper {
margin-top: 70px;
height: 100%;
}
.dropdown-toogle {
margin-right: 100px;
float: right;
position: relative;
height: 100%;
width: 50px;
background-color: yellow;
}
.dropdown-menu {
position: absolute;
top: 100%;
right: 0;
width: 70%;
height: 200px;
background-color: grey;
z-index:1000;
}
.z-depth-2 {
}

First of all, big thanks goes to Don, his approach is totally valid, clever and possibly even better than what I used.
However I figured I'll post my final solution as well as somebody might value having 2 approaches. All I did is I've added a "fake" gradient absolutely positioned bellow the header and to the right of sidebar.
The reason why I've done so is basically I used technologies that I know and can rely on. I didn't like the overflow auto on the content as well. Anyways, this is it:
https://jsfiddle.net/79gykeu3/11/
<div class="layout">
<div class="header">
<div class="header__shadow">
</div>
<div class="dropdown-toogle">
<div class="dropdown-menu">
</div>
</div>
</div>
<div class="page-wrapper">
<div class="sidebar">
<div class="sidebar__shadow">
</div>
</div>
<div class="content">
</div>
</div>
</div>
Css
.layout {
width: 100 %;
height: 100 %;
position: relative;
}
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 70px;
background - color: blue;
z - index: 103;
}
.sidebar {
position: fixed;
top: 70px;
left: 0;
width: 200px;
background - color: green;
bottom: 0;
z - index:101;
}
.content {
padding - left: 200px;
height: 8000px;
}
.page - wrapper {
padding - top: 70px;
height: 100 %;
}
.dropdown - toogle {
margin - right: 100px;
float: right;
position: relative;
height: 100 %;
width: 50px;
background - color: yellow;
}
.dropdown - menu {
position: absolute;
top: 100 %;
right: 0;
width: 400px;
height: 200px;
background - color: grey;
z - index:1000;
}
.header__shadow {
position: absolute;
height: 7px;
left: 200px;
right: 0;
top: 100 %;
background: linear - gradient(to bottom, rgba(0, 0, 0, 0.35) 0%,rgba(0, 0, 0, 0) 100%);
}
.sidebar__shadow {
position: absolute;
left: 100 %;
top: 0;
bottom: 0;
width: 7px;
background: linear - gradient(to right, rgba(0, 0, 0, 0.35) 0%,rgba(0, 0, 0, 0) 100%);
}

Related

How to place 'Background Image over another div'?

This question maybe not cool but want to know proper way to solve this task. Of course using HTML,CSS,Bootstrap.
Something like this:
Use CSS transform:
.cover {
background: blue;
height: 100px
}
.block {
transform: translate(0, -65px);
padding: 0 20px;
}
.text {
background: red;
height: 150px;
}
<div class="cover"></div>
<div class="block">
<div class="text">Hello, "Background Image over another div"</div>
</div>
Quick example, use negative margin top to place section over other section, replace styles with background image if you wish.
<section style="background:red;width:100%;height:300px">
</section>
<section style="width:100%;margin-top:-150px">
<div class="container">
<div style="width:100%;height:300px; background: white;"></div>
</div>
</section>
This should do what you want. https://codepen.io/anon/pen/KevYXE
Basically I created a DIV overlay and a background DIV. The background DIV is split into two sections. The top section uses a background image and the bottom section is a solid color. I used a relative position for the overlay and used percentages to make sure everything stays center.
HTML
<div id="wrap">
<div id="bg-overlay"></div>
<div id="bg-wrap">
<div id="bg-top"></div>
<div id="bg-bottom"></div>
</div>
</div>
CSS
body {
margin: 0;
padding: 0;
}
#wrap {
width: 100%;
height: 600px;
background: #ccc;
margin: 0 auto;
position: relative;
left: 0;
right: 0;
}
#bg-overlay {
position: absolute;
height: 70%;
max-width: 800px;
width: 80%;
top: 15%;
background: #fff;
left: 0;
right: 0;
margin: 0 auto;
-webkit-box-shadow: 0 6px 6px -6px #666;
-moz-box-shadow: 0 6px 6px -6px #666;
box-shadow: 0 6px 6px -6px #666;
}
#bg-wrap {
width: 100%;
height: 100%;
}
#bg-top {
height: 50%;
width: 100%;
float: left;
background: url("https://picsum.photos/2200/300") no-repeat;
background-position: center center;
}
#bg-bottom {
height: 50%;
width: 100%;
float: left;
background: #cccccc;
}

How do I make all sections of a website have the scroll-over effect?

I'm currently working on a website and wanted to have a "scroll-over" effect for the different sections of my website. If you don't know what I mean by the "scroll-over" effect, please visit https://sevenbits.github.io/Mac-Linux-USB-Loader/ and scroll down slightly. The successor sections scroll over their predecessors, which become fixed when they reach the top.
I'm trying to figure out how to do this with every section, and not only the top one. It would be great if this could be a short amount of code, but it would also be OK if it was a lot.
Thanks in advance!
* {
padding: 0;
margin: 0;
}
body {
position: relative;
}
.navbar {
position: fixed;
left: 0;
top: 0;
width: 100vw;
height: 20vh;
background-color: #446;
z-index: 100;
box-shadow: 0 0 20px rgba(0,0,0,.4);
}
.splash {
position: fixed;
left: 0;
top: 20vh;
width: 100vw;
height: 100vh;
background-color: #8ef;
z-index: 0;
}
.container {
position: absolute;
left: 0;
top: 0;
width: 100vw;
height: 100vh;
margin-top: 80vh;
background-color: #fff;
z-index: 50;
box-shadow: 0 0 20px rgba(0,0,0,.4);
}
<header>
<div class="navbar"></div>
<div class="splash"></div>
</header>
<div class="container"></div>

How to make header like that in css?

My site code is very usual
<div class="header"></div>
<div class="site-inner"></div>
<div class="footer"></div>
How can I make header background like on the image?
Is the whole site content have to be position absolute and margin-top:-500px ?
Is that only case to do it?
I assume you mean the overlap.
Negative margin is one way.
.header {
height: 50px;
background: lightblue;
}
.site-inner {
width: 80%;
margin: auto;
height: 50px;
background: lightgrey;
margin-top: -30px;
box-shadow: 0 -2px 2px black;
}
<div class="header"></div>
<div class="site-inner"></div>
You can use:
.header{
width: 80%;
height: 75px;
margin: 0 auto;
margin-top: -20px;
background:#3A3A3A;
}
Take a look at positioning: Positioning, also z-index might be relevant: Z-index, notice in my example the negative index on .header-bg
A quick example:
.header-bg {
width: 100%;
height: 200px;
z-index: -1;
background: lightblue;
position: absolute;
top: 0;
left: 0;
}
.header {
margin-top: 50px;
height: 50px;
background: grey;
z-index
}
.menu {
height: 80px;
}
.site-inner {
height: 400px;
width: 100%;
background: red;
}
<div class="header-bg"></div>
<div class="header"></div>
<div class="menu">menu</div>
<div class="site-inner">Site inner</div>
<div class="footer"></div>
A negative z-index lets you put elements behind others. The answer is simple enough then.
<div class="color"></div>
<div class="fixed">
<div class="header">
</div>
<div class="nav">
Text
</div>
<div class="body">
</div>
</div>
html, body
{
height: 100;
margin: 0;
}
div.color
{
position: absolute; /*Take out of the flow*/
top: 0; /*Move to top left*/
left: 0;
z-index: -1; /*Place below normal elements in the flow*/
width: 100%; /*Fill whole width*/
height: 300px; /*300px tall*/
background: #c7edfb; /*Color specified*/
}
div.fixed
{
margin: 50px auto 0; /*push whole document down 50px and center*/
width: 600px; /*document is 600px wide*/
}
div.header
{
height: 150px; /*top gray block is 150px tall*/
background: #222; /*dark gray*/
}
div.nav
{
padding: 25px 0; /*Gap between blocks above and below*/
}
div.body
{
min-height: 300px; /*Force a height*/
background: #777; /*Light gray*/
box-shadow: 0 0 8px black; /*Drop shadow*/
}
JSFiddle

Fullwidth website with vertically split background

I am implementing a website which have a layout like below
I am a bit confuse about the HTML structure for this layout blue section is almost half divide my background and content will be centered and blue section will always be exactly divided in this ratio can anyone assist me how to make it possible. One more thing I need to use blue section as background color may be in RGBA.
A pseudo-element could work here
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
}
body {
background-image: url(http://lorempixel.com/output/abstract-q-c-100-100-9.jpg);
}
body::before {
content: '';
position: absolute;
top: 0;
left: 50%;
height: 100vh;
width: 50%;
background: rgba(0, 0, 255, 0.5);
z-index: -1;
}
.navbar {
margin-top: 25px;
height: 50px;
background: green;
border: 1px solid black;
}
<div class="navbar"></div>
Place this DIV after body tag:
<body>
<div id="blue"></div>
content
</body>
body {
background:url('img');
height:100%;
width:100%;
}
#blue {
position: fixed;
top: 0;
left: 50%;
background: rgba(0, 0, 255, 0.35);
height: 100%;
width: 1000px;
}
JSFiddle

How to insert a shadow below the slider

I have the following slider in my asp.net page:
<div class="sliderContent">
<div id="sliderFrame">
<div id="slideIT" class="sliderHolderMain">
<div u="slides" class="sliderHolder">
<div>
<img u="image" src="theImages/slider/1.jpg" />
<div u="caption" t="CLIP|L" class="sliderCapMain">
<div class="sliderCapBG"></div>
<div class="sliderCapText">
<span class="sliderCapTextHdr">Healthy Cooking</span>
<br />
<span class="sliderCapTextFtr">Discover simple solutions to cook delicious and healthy meals for you and your family. Cooking tips, how-to guides and more!</span>
</div>
</div>
</div>
<div>
<img u="image" src="theImages/slider/2.jpg" />
<div u="caption" t="CLIP|L" class="sliderCapMain">
<div class="sliderCapBG"></div>
<div class="sliderCapText">
<span class="sliderCapTextHdr">Healthy Cooking</span>
<br />
<span class="sliderCapTextFtr">Discover simple solutions to cook delicious and healthy meals for you and your family. Cooking tips, how-to guides and more!</span>
</div>
</div>
</div>
</div>
</div>
<div style="width: 1100px; min-width: 1000px; height: 10px; background: url(theImages/dropShadow.png) repeat-x; position: relative; margin: 0 auto; bottom: 0;"></div>
</div>
</div>
CSS:
.sliderContent
{
width: 100%;
overflow: hidden;
margin: 0 auto;
text-align: center;
}
#sliderFrame
{
position: relative;
width: 100%;
margin: 0 auto; /*center-aligned*/
}
.sliderHolderMain
{
position: relative;
margin: 0px;
padding: 0px;
float: left;
top: 0px;
left: 0px;
width: 1100px;
height: 337px;
overflow: hidden;
}
.sliderHolder
{
cursor: move;
position: absolute;
left: 0px;
top: 0px;
width: 1100px;
height: 337px;
overflow: hidden;
}
.sliderCapMain
{
position: absolute;
top: 0px;
left: 0px;
width: 300px;
height: 337px;
text-align: center;
}
.sliderCapBG
{
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.75);
filter: alpha(opacity=75);
}
.sliderCapText
{
position: absolute;
top: 5%;
left: 5%;
color: #000;
text-align: left;
}
.sliderCapTextHdr
{
color: #0074C9;
font-weight: normal;
}
.sliderCapTextFtr
{
color: #000;
font-size: small;
}
Attached image of what it looks like:
As you can see the shadow is at the top of the slider pointed by the red arrow.
How can I modify the CSS so it appears below the slider, giving it a 3D view
2 ways: in your HTML, you have an inline style (eeeeew!!!) as follows:
<div style="width: 1100px; min-width: 1000px; height: 10px; background: url(theImages/dropShadow.png) repeat-x; position: relative; margin: 0 auto; bottom: 0;"></div>
and at the same time, your slider has a fixed height, so it's really easy to know the size.
So you can do the following:
1) remove the inline styles. Really.
2) add a class to that div, let's say .shadow
3) add the following styles to your CSS style sheet:
.shadow{
width: 1100px;
min-width: 1000px;
height: 10px;
position: relative;
margin: 0px auto;
top: 327px; //it was originally 327 but corrected to 337
background: url('theImages/dropShadow.png') repeat-x scroll 0% 0% transparent;
}
3.a) now you can use what you have now, which is a .png image (but you'll have to flip it vertically)
3.b) or you can use box-shadow property:
.shadow{
width: 1100px;
min-width: 1000px;
height: 10px;
position: relative;
margin: 0px auto;
top: 327px; //it was originally 327 but corrected to 337
box-shadow: -20px -5px 6px rgba(0, 0, 0, 0.75) inset;
}
EDIT: I see you edited my answer and somehow the edit was approved, but the edit is incorrect, so I'll explain it so you also understand how it works: the top position is the height of the container element (337px) minus the height of the div with the shadow (10px), so position is 327px. If you want a shadow div with (say) 50px height, then position would be 287px (337-50) and so on. Explaining this because this answer may be of help for you but also for other people as well, and having this corrected would end with the whole solution not working (literally disappearing off sight)