I am trying to implement a popup menu in html using z-index. The page is composed of a top bar an a tab bar below it.
I want to apply box-shadow to both of them, so I can't use position:static.
The tabs-bar should have a z-index larger than the nav-bar's so it hides the box-shadow of the nav-bar (so when I hide the tabs-bar I would like to see the box-shadow of the nav bar)
The drop down menu should be place on top of both, but for some strange reason, it is placed on top of the nav-bar, but behind the tab-bar, although its z-index is set to a value higher than both.
I read about z-index contexts, but I can't seem to understand what is going on here.
here is a link to codepen:
http://codepen.io/anon/pen/WvgMXX
HTML:
<div class="body">
<div class="container">
<div class="navbar">
Nav bar
<div class="menu"> drop down menu</div>
</div>
<div class="tabs"> tabs bar </div>
</div>
</div>
css:
.body {
height: 100%;
margin: 0px;
padding: 0px;
width: 100%;
overflow-x: hidden;
overflow-y: hidden;
background-color: gold;
position: static;
}
.container {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
overflow-x: hidden;
overflow-y: hidden;
width: 100%;
margin: 0;
background-color: green;
}
.navbar {
width: 100%;
margin-bottom: 0;
z-index: 100;
box-shadow: 0px 3px 11px -5px #000;
height: 30px;
background-color: red;
position: relative;
}
.tabs {
width: 100%;
font-size: 1.1em;
box-shadow: 0px 3px 11px -5px #000;
z-index: 101;
position: absolute;
height: 30px;
background-color: gold;
top: 30px;
}
.menu {
width: 6em;
height: 6em;
position: absolute;
left: 200px;
background-color: white;
z-index: 2000;
}
Any ideas?
Your dropdown menu is contained in .navbar. A z-index value of child wouldn't affect if the parent's z-index is smaller to item in comparison. You need to increase .navbar z-index.
See the updated pen here http://codepen.io/anon/pen/VLGXvP
On a side note, there's huge z-index abuse, you can step with multiples of 3s or 9s. using 100 and 2000 is incredible waste. Same goes with unnecessary positioning.
Since your dropdown menu is placed inside the nav bar, you can set it's z-index as high as you want, but it will never be higher than the one of it's parent. You could put it outside the nav bar: http://codepen.io/anon/pen/Wvgzrg
I am not 100% sure what you want to achieve, but if you remove the "position: relative" property from your navbar, it works fine. I tried it in Chrome.
.navbar {
width: 100%;
margin-bottom: 0;
z-index: 100;
box-shadow: 0px 3px 11px -5px #000;
height: 30px;
background-color: red;
}
http://jsfiddle.net/11wkyvat/
Related
I want to use an img on my page as background-image of an image-slide banner. The reason is to include alt-text for accessibility reasons. To move the img in the background, I have to give it a negative z-index. Otherwise, it always shows on top of the content.
Tag-Lines are included on the banner as h1 titles. These titles can't be selected or interacted with, once the banner is in the negative z-index. So far, there is no problem. However, some of the background-images I want to include on some pages, were not taken by myself, so they need an image credit. The link which leads to the original-photo on the image-credit can't be clicked on. Optically, it's shown above the image and the banner, but it can't be clicked on.
So is there a way to make the content of the banner interactable. I could include the image as background-image, but in this case, how can I include alt-text to the background-image?
.slider {
width: 100%;
position: relative;
height: 600px;
z-index: -1;
}
.banner {
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.banner-image {
position: fixed;
top: 0;
left: 0;
object-fit: cover;
width: 100%;
height: 640px;
z-index: -2;
}
.image-credits {
background-color: black;
color: white;
padding: 2px 8px;
margin: 10px;
position: absolute;
bottom: 0;
right: 0;
}
.image-credits a {
color: white;
}
<div class="slider">
<div class="banner">
<img class="banner-image" src="https://via.placeholder.com/1280" alt="Description, what you see">
<div class="content">
<h1>Some tagline</h1>
<p class="image-credits">Photo by <a href="image-source" target="blank">Photographer</a\></p>
</div>
</div>
</div>
I tried setting the page up with positive z-values. But then the background-image always shows on top of the rest of the content on the page, and the content remains interactable. Also, I applied pointer-events:none; to all other elements of the slider, except of the image-credits. That also didn't work out.
Seems its not workin when you set z-index both parent and child elements. Try to remove z-index from .slider and it should work.
If you specify z-index on an element, it gonna impacts his descendants too. If you specify a negative z-index, then the corresponding elements are going "behind" <body> element. Then all your click are on <body> element. As <body> have a transparent background, you could have the impression click on your link, but you are not.
To be able to click on your link, it should have no element with greater z-index in front. Below, I have made you an example without z-index on .slider (which is one of the ascendants of your link, so it specifies indirectly z-index for him)
.slider {
width: 100%;
position: relative;
height: 600px;
}
.banner {
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.banner-image {
position: fixed;
top: 0;
left: 0;
object-fit: cover;
width: 100%;
height: 640px;
z-index: -2;
}
.image-credits {
background-color: black;
color: white;
padding: 2px 8px;
margin: 10px;
position: absolute;
bottom: 0;
right: 0;
}
.image-credits a {
color: white;
}
<div class="slider">
<div class="banner">
<img class="banner-image" src="https://via.placeholder.com/1280" alt="Description, what you see">
<div class="content">
<h1>Some tagline</h1>
<p class="image-credits">Photo by <a href="#" target="blank">Photographer</a\></p>
</div>
</div>
</div>
I wanted to make a fixed navbar on my website, but it overlaps other elements. As you can see from the snippet, <nav> is supposed to be the navbar, and <div> is meant to be some other content on the website. <div> element needs to be on space outside navbar like this: https://i.imgur.com/ugfdTUV.png
I already know about margin-left method, but my navbar doesn't have a specified width, so that method wouldn't work, and plus I'm looking for a more efficient way to solve this.
nav {
top: 0;
left: 0;
height: 100%;
padding: 2cm;
position: fixed;
border-right: 1px solid black;
}
nav a {
padding: 20px 0px;
display: block;
}
<nav>
Sample
Sample
Sample
Sample
</nav>
<div>
<h1>Sample text</h1>
</div>
I create an example, for support text at center i add class call page:
nav {
top: 0;
left: 0;
height: 100%;
padding: 2cm;
position: fixed;
border-right: 1px solid black;
z-index: 999;
display: block;
}
nav a {
padding: 20px 0px;
display: block;
}
.page{
position: absolute;
top: 0;
right: 0;
width: calc(100% - 200px);
background-color: #F4F7FA;
min-height: 100vh;
padding-bottom: 50px
}
<nav>
Sample
Sample
Sample
Sample
</nav>
<div class='page'>
<h1>Sample text</h1><br>
<p>Another element</p>
</div>
Although the margin fix is the simplest option, you can use the position: sticky; CSS property on the sidebar.
Have a look at this JSFidde I wrote.
If you run the following code snippet in Full page (Run > Full page), you'll see a vertical red 1-pixel border near the vertical scrollbar, in Chrome / Windows. Why?
I've found workarounds (see Note2 below), so my goal is not only to avoid it, but to understand the reason for this red border. There normally should be no distance between div #popup and its scrollbar (or am I wrong?) Why is this border there?
body, html { height: 100%; width: 100%; }
* { margin: 0; padding: 0; }
#popup { position: fixed; top: 0; left: 10%; width: 80%; height: 100%; background-color: white; z-index: 10; overflow-y: auto; outline: 0; }
#popupmain { height: 100%; width: 100%; font-size: 0.9em; padding: 5%; box-sizing: border-box; outline: 0; border: 0; }
#popupclose { top: 5px; right: 5px; position: fixed; cursor: pointer; color: white; }
#popupdarkbg { position: fixed; z-index: 5; left: 0; top: 0; width: 100%; height: 100%; overflow: hidden; background-color: red; }
<div id="popup">
<div id="popupmain">
<p id="popuplongdescription">a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a<br>a</p>
</div>
<div id="popupclose">X</div>
</div>
<div id="popupdarkbg"></div>
Note: the #popupdarkbg div is normally black, with some opacity: background-color: rgba(0,0,0,.75);, and the #popup (by default: hidden) is normally triggered/opened by a click on a link, with Javascript. I removed this part here to show only the core problem in this minimal example.
Note2: a solution to remove this border is to remove the #popupclose div or to move it out of #popup div. It works, but why?
Take a look at this fiddle: https://jsfiddle.net/hkbynkmf/1/
How do I let the green border flow around all the divs, with no div "overflowing" the border? The upper div is OK, but the lower one is not.
Also, I need some distance between the divs;
I know that padding and margin is transparent, so I chose (a green) border to illustrate my point. In the end, the clearance should be transparent.
HTML:
body {
position: relative;
background-color: #ff0000;
background-repeat: no-repeat;
background-attachment: fixed;
padding: 0px;
border: 10px solid #190;
margin: 0px;
}
#header {
position: relative;
margin:0 auto; /* div will be H-centered */
top: 10px;
left: 0px;
bottom: 0px;
right: 0px;
width: 960px;
height: 250px;
background-color: #DDDDDD;
overflow: hidden; /* Keep all sub-elements inside this div */
}
#intro {
position: relative;
margin:0 auto; /* div will be H-centered */
top: 15px;
left: 0;
bottom: 0px;
right: 0px;
width: 960px;
height: 150px;
background-color: blue;
overflow: hidden; /* Keep all sub-elements inside this div */
}
<body> <!--position: relative;-->
<div id="header"> <!--position: relative;-->
</div>
<div id="intro"> <!--position: relative;-->
</div>
</body>
You're using the top attribute to move your intro div 15px down, below the header. This is causing the 15px overlap with the container. When positioning items this way you should consider using margin to apply the change, rather than the positioning attributes of top, right, bottom or left.
You have a lot going on with your CSS which is making the stylesheet much more complicated than it needs to be. I have simplified your CSS as follows to achieve the same effect:
body {
background-color: #ff0000;
border: 10px solid #190;
margin: 0px;
padding: 0px;
}
a img {
border:none;
}
#header {
background-color: #DDDDDD;
height: 250px;
margin:0 auto;
overflow: hidden;
width: 300px;
}
#intro {
background-color: blue;
height: 150px;
margin:15px auto 0;
overflow: hidden;
width: 300px;
}
See updated fiddle
In your code, the #intro is positioned 15px below the #header. Doing so leaves no place for the div in body.
Not sure what you are trying to achieve here with position: relative; but the #intro can be written like
#intro
{
margin:10px auto;/* div will be H-centered */
width: 300px;
height: 150px;
background-color: blue;
overflow: hidden;/* Keep all sub-elements inside this div */
}
Using the margin top property on the #intro div will allow the green border to flow, while also having the space in between the divs. Here is the fiddle:
https://jsfiddle.net/hkbynkmf/17/
#intro
{
position: relative;
margin:15px auto 0px auto /* div will be H-centered */
left: 0;
bottom: 0px;
right: 0px;
width: 300px;
height: 150px;
background-color: blue;
overflow: hidden; /* Keep all sub-elements inside this div */
}
I have a fixed menu at the top with a z-index of 999 (to stop anything appearing in front of it in theory). I also have a div that is designed to scroll horizontally. Why are the overflow properties on the scrolling div making it appear on top of the menu bar?
Scrolling div CSS:
.product-viewer {
width: 85%;
margin-left: 15%;
display: inline-block;
border-top-left-radius: 30px;
border-top-left-radius: 3vmax;
border-bottom-left-radius: 30px;
border-bottom-left-radius: 3vmax;
height: 300px;
height: 30vmax;
background-color: #202020;
overflow-y: hidden;
overflow-x: scroll;
-webkit-overflow-scrolling: touch;
white-space: nowrap;
z-index: 0;
}
Menu bar CSS (#menu.fix is when the menu is fixed as it is a "sticky" menu):
#menu {
width: 100%;
height: 50px;
height: 5vmax;
background-color: rgba(0, 0, 0, 0.8);
background-blend-mode: hard-light;
text-align: center;
overflow: hidden;
line-height: 50px;
line-height: 5vmax;
display: inline-block;
}
#menu.fix {
position: fixed;
left: 0;
top: 0;
z-index: 999;
}
Link to site: https://www.inquaress.com/ranges/beach
Your .cover-image element has z-index: 0 !important; and that makes it flow under the following element. You should check your markup as your menu is a child element of .cover-image and your following element .section.no-select doesn't have z-index definitions thus interpreted by the browser to go over the previous element.
Your fix would be just to change your z-index something like
.cover-image {
height: 600px;
height: calc(100vh - 15vmax);
max-height: 75vh;
z-index: 1 !important;
}