I want an absolute positioned child element (.tag-preview-container) of a flex item (.image-container) to overlap other elements in the DOM (for instance the header .header). I'm setting the z-index of the absolute positioned element to be 2 (even 1 should work). However, the element doesn't overlap any DOM element outside the flex container(.tags-panel). In fact, it is completely cut off by the container. I am not setting z-index of any other element in the DOM, so the root should be the stacking context.
<div class="panel-container">
<div class="header">
</div>
<div class="tags-panel">
<div class="image-container">
<div class="tag-preview-container">
</div>
</div>
<div class="image-container">
</div>
<div class="image-container">
</div>
<div class="image-container">
</div>
</div>
</div>
This is the scss code and here's the JSFiddle
.panel-container {
width: 400px;
height: 400px;
position: relative;
margin: auto;
.header {
width: 100%;
height: 40px;
background-color: green;
}
.tags-panel {
position: absolute;
background-color: red;
top: 40px;
bottom: 0px;
width: 100%;
overflow-y: auto;
display: flex;
flex-flow: row wrap;
align-content: flex-start;
}
}
.image-container {
flex: 0 0 33%;
box-sizing: border-box;
border: 1px solid black;
margin-top: 5px;
margin-left: 1px;
height: 80px;
background-color: yellow;
position: relative;
.tag-preview-container {
background-color: black;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 200px;
height: 200px;
z-index: 2;
opacity: 0.5;
}
}
The z-index is affected by the overflow property in .tags-panel, removing overflow-y: auto; will solve the issue. Here is the updated fiddle https://jsfiddle.net/yc5xanax/
overflow-y: auto; will automatically wrap/hide the content outside the .tags-panel container.
Related
How do I make div3 & div4 above div1 and div2 without using "ID" selector on CSS level?
.container {
margin: 50px;
padding: 20px;
text-align: center;
border: 1px dashed #999966;
}
.position {
height: 120px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
margin-bottom: 10px;
opacity: 0.8;
border: 1px dashed #999966;
background-color: #ffffcc;
}
.position .position {
margin-top: 60px;
margin-left: 100px;
width: 50%;
}
.relative {
position: relative;
border: 1px dashed #669966;
background-color: #ccffcc;
}
.fixed {
position: fixed;
border: 1px dashed #669966;
background-color: #e3e3ff;
align-items: flex-start;
}
.fixed.target-block {
width: 200px;
height: 300px;
top: 130px;
right: 140px;
z-index: 999;
}
<div class="container">
<p>
DIV: CONTAINER<br>
position: relative;
</p>
<div class="position">
<p>
DIV #1<br>
no position;
</p>
<div class="position relative">
<p>
DIV #3<br>
position: relative;<br>
parent
</p>
<div class="position fixed target-block">
<p>
DIV #4<br>
position: fixed;<br>
child
</p>
</div>
</div>
</div>
<div class="position">
<p>
DIV #2<br>
no position;
</p>
</div>
</div>
If we refer to the specification we can read this:
All positioned, opacity or transform descendants, in tree order that
fall into the following categories:
...
All opacity descendants with opacity less than 1, in tree order,
create a stacking context generated atomically.
And in your case Div #2 will create it's own stacking context like Div #1 and since none of them is positioned with z-index you cannot achieve what you want by simply controling their child elements.
So the only way I see here is to make one of the divs positioned and apply z-index to it or both of them positionned with different z-index. The purpose is to avoid following the tree order like specified above.
.container {
margin: 50px;
padding: 20px;
text-align: center;
border: 1px dashed #999966;
}
.position {
height: 120px;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
margin-bottom: 10px;
opacity: 0.8;
border: 1px dashed #999966;
background-color: #ffffcc;
}
.position .position {
margin-top: 60px;
margin-left: 100px;
width: 50%;
}
.relative {
position: relative;
border: 1px dashed #669966;
background-color: #ccffcc;
}
.fixed {
position: fixed;
border: 1px dashed #669966;
background-color: #e3e3ff;
align-items: flex-start;
}
.fixed.target-block {
width: 200px;
height: 300px;
top: 130px;
right: 140px;
z-index: 999;
}
<div class="container">
<p>
DIV: CONTAINER<br> position: relative;
</p>
<div class="position" style="position:relative;z-index:1;">
<p>
DIV #1<br> no position;
</p>
<div class="position relative">
<p>
DIV #3<br> position: relative;<br> parent
</p>
<div class="position fixed target-block">
<p>
DIV #4<br> position: fixed;<br> child
</p>
</div>
</div>
</div>
<div class="position">
<p>
DIV #2<br> no position;
</p>
</div>
</div>
Some related questions:
Stacking order of elements affected by opacity
What has bigger priority: opacity or z-index in browsers?
I'm trying to center a group of relative positioned divs dynamically inside of a parent div that is also relative positioned.
The parent div is 620px wide, and the child divs are each 200px wide. There can be 1 to 3 child divs per line, thus what I meant by trying to center the group of child divs within the parent div dynamically. For example, if there is only 1 child div, that child div should be centered in the parent div, or if there are only 2 child divs, those child divs should be centered in the parent div.
I would use inline block for the child divs, however inside the child divs there are also divs that are absolute positioned to the child divs, so inline-block wouldn't work for the absolute positioning.
This is my css, or you can see a working example here: https://jsfiddle.net/y3ghpkvs/
.parentClass {
position: relative;
width: 620px;
height: 500px;
background-color: gray;
}
.childClass {
position: relative;
width: 200px;
height: 400px;
float: left;
margin-left: 5px;
background-color: white;
}
.insideChildDiv1 {
position: absolute;
width: 100%;
height: 95px;
top: 0;
left: 0;
background-color: black;
color: white;
text-align: center;
line-height: 100px;
}
.insideChildDiv2 {
position: absolute;
width: 100%;
height: 200px;
top: 100px;
left: 0;
background-color: green;
}
.insideChildDiv3 {
position: absolute;
width: 100%;
height: 95px;
bottom: 0;
left: 0;
color: white;
text-align: center;
line-height: 100px;
background-color: black;
}
I can't seem to figure out how to center the 2 childClass divs inside the parentClass div. Anyone have any tips?
Solution 1: Using Flex
Try to use flex css. Using flex it will be easy for you align the items vertically or horizontally center to the parent.
Use justify-content: center; to align the divs center.
Updated Fiddle
.parentClass {
width: 620px;
background-color: gray;
display: flex;
justify-content: center;
margin: auto;
flex-wrap: wrap;
}
.childClass {
width: 200px;
height: 400px;
background-color: white;
position: relative;
margin: 3px;
}
.insideChildDiv1 {
position: absolute;
width: 100%;
height: 95px;
top: 0;
left: 0;
background-color: black;
color: white;
text-align: center;
line-height: 100px;
}
.insideChildDiv2 {
position: absolute;
width: 100%;
height: 200px;
top: 100px;
left: 0;
background-color: green;
}
.insideChildDiv3 {
position: absolute;
width: 100%;
height: 95px;
bottom: 0;
left: 0;
color: white;
text-align: center;
line-height: 100px;
background-color: black;
}
<div class="parentClass">
<div class="childClass">
<div class="insideChildDiv1">George</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">CEO</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
</div>
Solution 2: Using display inline-block
.parentClass {
width: 620px;
background-color: gray;
margin: auto;
text-align: center;
}
.childClass {
width: 200px;
height: 400px;
background-color: white;
position: relative;
margin: 4px 0;
display: inline-block;
margin-left: 2px;
}
.insideChildDiv1 {
position: absolute;
width: 100%;
height: 95px;
top: 0;
left: 0;
background-color: black;
color: white;
text-align: center;
line-height: 100px;
}
.insideChildDiv2 {
position: absolute;
width: 100%;
height: 200px;
top: 100px;
left: 0;
background-color: green;
}
.insideChildDiv3 {
position: absolute;
width: 100%;
height: 95px;
bottom: 0;
left: 0;
color: white;
text-align: center;
line-height: 100px;
background-color: black;
}
<div class="parentClass">
<div class="childClass">
<div class="insideChildDiv1">George</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">CEO</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
<div class="childClass">
<div class="insideChildDiv1">John</div>
<div class="insideChildDiv2"></div>
<div class="insideChildDiv3">Vice President</div>
</div>
</div>
I hope this will help you!
Basically I will try to keep this simple.
I am trying to do something like this (ignore any design aspects except what I state):
I have started this on JSFiddle here.
.header-wrapper {
display: flex;
flex-direction: column;
align-items: flex-end;
}
.header {
background-color: #0091cc;
border-radius: 20px;
height: 100px;
width: 90%;
margin-bottom: 20px;
}
.circle {
background-color: pink;
height: 400px;
width: 400px;
border-radius: 50%;
position: absolute;
top: -100px;
left: -100px;
}
<div class="header-wrapper">
<div class="header"></div>
<div class="header"></div>
</div>
<div class="circle"></div>
Basically the problem is I cannot make it appear that the circle connects through the two rectangles and have two different colours, like in the image. While still cutting out the rest of the circle, that spills out of the rectangle.
I hope this makes sense.
Thanks in advance.
I had to change your mark up. In order to get the old mark up to work, it would have been too hacky. Basically I assigned a circle for each header, and I set overflow to hidden on the header. Then I play with the top property to decide which part of the circle I want to display. There is slight blue showing up on the border to the left, but I'm sure it won't take long to figure out why.
.header-wrapper {
display: flex;
flex-direction: column;
align-items: flex-end;
}
.header {
background-color: #0091cc;
border-radius: 20px;
height: 100px;
width: 90%;
margin-bottom: 20px;
position: relative;
overflow: hidden;
}
.circle {
position: absolute;
top: -100px;
left: -100px;
height: 200px;
width: 200px;
border-radius: 200px;
background-color: #fff;
}
.circle.top {
top: 0;
}
.yellow {
background-color: yellow;
}
.pink {
background-color: pink;
}
<div class="header-wrapper">
<div class="header">
<div class="circle top pink"></div>
</div>
<div class="header">
<div class="circle yellow"></div>
</div>
</div>
You can actually do this with even less HTML markeup and use a ::before or ::after psuedo-element to create the circle: https://developer.mozilla.org/en-US/docs/Web/CSS/::after
This creates a child element for a circle in each header and setting overflow:hidden on the header conceals the parts of the circle you don't want to be visible.
.header-wrapper {
align-items: flex-end;
display: flex;
flex-direction: column;
}
.header {
background-color: #0091cc;
border-radius: 20px;
height: 100px;
margin-bottom: 20px;
overflow: hidden;
position: relative;
width: 90%;
}
.header::after {
border-radius: 50%;
content: "";
height: 400px;
left: -100px;
position: absolute;
width: 400px;
}
.header:nth-child(1)::after {
background-color: pink;
top: -100px;
}
.header:nth-child(2)::after {
background-color: orange;
bottom: -100px;
}
<div class="header-wrapper">
<div class="header"></div>
<div class="header"></div>
</div>
https://jsfiddle.net/od62shsp/4/
I don't understand why the float: right doesn't work on the other box.
Anyone who can help me about this?
This is my code:
.main-box {
height: 400px;
width: 400px;
position: relative;
background: black;
}
.right-box {
float: right;
width: 100px;
height: 100px;
background: red;
}
.left-box {
width: 100px;
height: 100px;
background: red;
}
.bottom-boxes {
position: absolute;
bottom: 0;
}
<div class="main-box">
<div class="top-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
<div class="bottom-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
</div>
This is the resulting image of my code:
This is the resulting image I want to achieve:
Because of position: absolute on bottom-boxes so you need to add width: 100%
.main-box {
height: 400px;
width: 400px;
position: relative;
background: black;
}
.right-box {
float: right;
width: 100px;
height: 100px;
background: red;
}
.left-box {
width: 100px;
height: 100px;
background: red;
}
.bottom-boxes {
position: absolute;
bottom: 0;
width: 100%;
}
<div class="main-box">
<div class="top-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
<div class="bottom-boxes">
<div class="right-box"></div>
<div class="left-box"></div>
</div>
</div>
But here is better solution using flexbox
.main-box {
height: 200px;
width: 200px;
display: flex;
flex-direction: column;
justify-content: space-between;
background: black;
}
.row {
display: flex;
justify-content: space-between;
}
.box {
width: 50px;
height: 50px;
background: red;
}
<div class="main-box">
<div class="row">
<div class="box"></div>
<div class="box"></div>
</div>
<div class="row">
<div class="box"></div>
<div class="box"></div>
</div>
</div>
Here's a working fiddle
When you put absolute position on a container, you have to specify also top, right and left property with bottom property to set a width and a height of it.
.bottom-boxes{
position:absolute;
bottom: 0;
left: 0;
right: 0;
}
In this case, left: 0; and right: 0; are equivalent to width: 100%; and top: 0 and bottom: 0; are equivalent to height: 100%;
When you don't specify a value, by default it's "auto;"
float won't work on an absolutely positioned element - you need to give top or bottom and right or left parameters to it (the default setting is top: 0; and left: 0;, i.e. the upper left corner of the parent element).
I have a design using some bootstrap styling which has a white column on the right. The height should be 100%, but it isn't rendering at 100%. It renders at 100% of the initial screen height, but when you scroll down it's no longer white.
I've looked at several other CSS solutions on this site. I've tried making all parent elements 100% height. I've tried making it a flexbox column. I've tried putting "position: relative;" in the body. Nothing has worked yet. I'd prefer not to use JS to achieve this.
Simplified version of my HTML:
<body>
<div class="container">
<div class="main">
<h1>This is the main content area</h1>
</div>
<div class="right pull-right">
<div class="content">
</div>
<div class="content">
</div>
<div class="content">
</div>
<div class="content">
</div>
</div>
</div>
</body>
The CSS:
body,html {
height: 100%;
background-color: #aaa;
}
body {
position: relative;
}
.main {
display: inline-block;
}
.container {
height: 100%;
}
.right {
display: inline-flex;
flex-direction: column;
background-color: #fff;
width: 350px;
height: 100%;
min-height: 100%;
border: 1px solid #E1E6E9;
margin-right: 100px;
position: absolute;
right: 100px;
}
.content {
height: 300px;
min-height: 300px;
margin: 10px 10px;
background-color: #ccc;
border: 1px solid #000;
}
Change your .right class to have height: auto;
It will size itself to fit with its content.
.right {
display: inline-flex;
flex-direction: column;
background-color: #fff;
width: 350px;
height: auto;
min-height: 100%;
border: 1px solid #E1E6E9;
margin-right: 100px;
position: absolute;
right: 100px;
}
http://codepen.io/smlariviere/pen/WrWgxQ