CSS3: Shadow only without parent background - html

I have a problem with CSS3 Shadows that I did not expirience before.
It seems like a box-shadow, that is applied to a div via the :before and :after selector, is only possible, if the container of the div has no background-color set.
Is there any way to make this possible?
<div class="container">
<div class="shadow-box">
test
</div>
</div>
The .container must not have a background-color set. I created an example on http://jsfiddle.net/v1utr15n/

You need to make sure that the .container will start a new stacking order. You can do this by either setting a position: relative; z-index: 0 or a opacity other than 1, e.g. opacity: .9999.
.container {
background-color: #fff;
height: 500px;
position: relative;
z-index: 0;
}
.shadow-box {
background-color: #fff;
position: relative;
width: 300px;
height: 300px;
}
.shadow-box:before,
.shadow-box:after {
content: "";
position: absolute;
z-index: -1;
-webkit-box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
top: 50%;
bottom: 0;
left: 10px;
right: 10px;
-moz-border-radius: 100px / 10px;
border-radius: 100px / 10px;
}
<div class="container">
<div class="shadow-box">
test
</div>
</div>
See http://philipwalton.com/articles/what-no-one-told-you-about-z-index/ for some background information on z-index and stacking order context.

Put the code of the box-shadow in the shadow-box class, not in before or after.
URL: http://jsfiddle.net/v1utr15n/1/
.container {
background-color: #fff;
height: 500px;
}
.shadow-box {
background-color: #fff;
position: relative;
width: 300px;
height: 300px;
-webkit-box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
-moz-box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
box-shadow: 0 0 20px rgba(0, 0, 0, 0.8);
}
.shadow-box:before,
.shadow-box:after {
content: "";
position: absolute;
z-index: -1;
top: 50%;
bottom: 0;
left: 10px;
right: 10px;
-moz-border-radius: 100px / 10px;
border-radius: 100px / 10px;
}
<div class="container">
<div class="shadow-box">
Shadow box
</div>
</div>

Related

CSS shadow such that some div's look they are behind

Currently I have a HTML & CSS that results in a page like below
However, I want the shadows above B & C tabs so that it looks like they are behind.
Can anyone hele achieve this?
body {
background-color: rgb(245, 165, 61);
--border-rad: 5px;
}
.wrapper {
width: 80vh;
margin: 5%;
}
.tabs {
display: flex;
justify-content: space-around;
}
.tab {
width: 20%;
color: #000;
background-color: #fff;
margin: 2px 2px 0% 2px;
border-top-left-radius: var(--border-rad);
border-top-right-radius: var(--border-rad);
position: relative;
text-decoration: none;
text-align: center;
}
.tab:before,
.tab:after {
content: "";
position: absolute;
height: 10px;
width: 20px;
bottom: 0;
}
.tab:before {
left: -20px;
border-radius: 0 0 var(--border-rad) 0;
box-shadow: var(--border-rad) 0 0 0 #fff;
}
.tab:after {
right: -20px;
border-radius: 0 0 0 var(--border-rad);
box-shadow: calc(var(--border-rad) * -1) 0 0 0 #fff;
}
.content {
background-color: #fff;
height: 75vh;
box-shadow: 0 10px 10px rgba(0, 0, 0, 0.3), 0 -3px 5px rgba(0, 0, 0, 0.3);
border-radius: 10px;
text-align: center;
}
<div class="wrapper">
<div class="tabs">
<span class="tab">A</span>
<span class="tab">B</span>
<span class="tab">C</span>
</div>
<div class="content">content</div>
</div>
You need to temper with the z-index of the different elements. Remember you can only modify the z-index if the element itself has a position set (e.g. position: relative)
Below is a working example. Note that I have also added an "active" class to the currently active tab.
You would need to create JavaScript to make it full functional, but this is the starting point.
Good luck!
body {
background-color: rgb(245, 165, 61);
--border-rad: 5px;
}
.wrapper {
width: 80vh;
margin: 5%;
}
.tabs {
display: flex;
justify-content: space-around;
}
.tab {
position: relative;
width: 20%;
color: #000;
background-color: #fff;
margin: 2px 2px 0% 2px;
border-top-left-radius: var(--border-rad);
border-top-right-radius: var(--border-rad);
position: relative;
text-decoration: none;
text-align: center;
}
.tab.active {
z-index: 2;
}
.tab:before,
.tab:after {
content: "";
position: absolute;
height: 10px;
width: 20px;
bottom: 0;
}
.tab:before {
left: -20px;
border-radius: 0 0 var(--border-rad) 0;
box-shadow: var(--border-rad) 0 0 0 #fff;
}
.tab:after {
right: -20px;
border-radius: 0 0 0 var(--border-rad);
box-shadow: calc(var(--border-rad) * -1) 0 0 0 #fff;
}
.content {
position: relative;
z-index: 1;
background-color: #fff;
height: 75vh;
box-shadow: 0 10px 10px rgba(0, 0, 0, 0.3), 0 -3px 5px rgba(0, 0, 0, 0.3);
border-radius: 10px;
text-align: center;
}
<div class="wrapper">
<div class="tabs">
<span class="tab active">A</span>
<span class="tab">B</span>
<span class="tab">C</span>
</div>
<div class="content">content</div>
</div>
Just add these to your content and tab css classes:
.content {
position: relative;
z-index: 2;
}
.tab
z-index: 1;
}
Edit: you need the relative positioning for z-index to work.
You can always try the following css witch will give the box the black shadow and the border bottom you want.
box-shadow: rgb(0 0 0 / 25%) 0px 54px 55px, rgb(0 0 0 / 12%) 0px -12px 30px, rgb(0 0 0 / 12%) 0px 4px 6px, rgb(0 0 0 / 17%) 0px 12px 13px, rgb(0 0 0 / 5%) 0px -3px 5px;
border-bottom: solid;
border-width: thin;
z-index: 50;

How do I get an absolute positioned div to sit centred horizontally with a fixed div?

I am trying to get a div(main) to sit centred withing another div(centre), the div(main) needs to be static as I don't want it to scroll but the div(centre) has to be absolute so it can sit on a separate div with an onclick function.
current HTML and css:
<div className='meal_popup'>
<div className='meal_popupElement'>
<p>testing1</p>
</div>
<div onClick={this.mealOne_boxClickHandler} className='meal_popupBackground' />
</div>
.meal_popup {
position: fixed;
display: flex;
align-items: center;
top: 0;
width: 100%;
height: 100%;
z-index: 30;
}
.meal_popupElement {
position: absolute;
width: 100%;
height: 100%;
background: white;
border-radius: 15px;
height: 35rem;
margin-left: 1rem;
margin-right: 1rem;
box-shadow: 0px 2px 1px -1px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14),
0px 1px 3px 0px rgba(0, 0, 0, 0.12);
z-index: 2;
}
.meal_popupBackground {
position: relative;
background: rgba(00, 00, 00, 0.6);
width: 100%;
height: 100%;
z-index: 1;
}
Result:
Goal:
(this visually looks how I want but the problem is the whole div is clickable and only want that function on the background HTML and css:)
<div onClick={this.mealTwo_boxClickHandler} className='meal_background'>
<div>
<p>testing2</p>
</div>
</div>
.meal_background {
position: fixed;
display: flex;
align-items: center;
top: 0;
width: 100%;
height: 100%;
background: rgba(00, 00, 00, 0.6);
z-index: 30;
}
.meal_background div {
background: white;
border-radius: 15px;
height: 35rem;
width: 100%;
margin-left: 1rem;
margin-right: 1rem;
box-shadow: 0px 2px 1px -1px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14),
0px 1px 3px 0px rgba(0, 0, 0, 0.12);
}
To center any element with position property different than static, you can use the following code. However, be clear that the following code takes slightly more CPU time than other methods;
To center Horizontally
left:50%;
transform: translateX(-50%);
To center vertically
top: 50%;
transform: translateY(-50%);
And particularly for this answer apply right: 0px; on the container with fixed positioning and also apply the code to center horizontally on the element you want to center.
So the problem appears to be with setting left a or right margin values at the edge to create a space either side like I normally would. so instead of width 100% and 1rem left/right margin (which id use as is great for scaling. I had set a preset width and implement the code above provided by #RitanshuSingh.
so my code looks like this:
.meal_popup {
position: fixed;
display: flex;
align-items: center;
top: 0;
width: 100%;
height: 100%;
z-index: 30;
}
.meal_popupElement {
position: absolute;
width: 90%;
left: 50%;
transform: translateX(-50%);
background: white;
border-radius: 15px;
height: 35rem;
box-shadow: 0px 2px 1px -1px rgba(0, 0, 0, 0.2), 0px 1px 1px 0px rgba(0, 0, 0, 0.14),
0px 1px 3px 0px rgba(0, 0, 0, 0.12);
z-index: 2;
}
.meal_popupBackground {
position: relative;
background: rgba(00, 00, 00, 0.6);
width: 100%;
height: 100%;
z-index: 1;
}
preferably would like to select a margin but this works for now if you know how to get that working please post a fix.
result:

:after adjust height of parent

I have a progress bar, and I am trying to adjust the height of the parent item based on the content of the :after pseudo element. Once you run the code, you will see that the 30% is partially inside the div.
Is there a way for me to adjust the height of the element that the :after is attached to?
I tried changing the display property of the :after item but that didn't do anything.
.pure-progress {
display: block;
position: relative;
width: 100%;
min-height: 10px;
}
.pure-progress>.pure-progress--bar {
position: absolute;
background-color: blue;
height: 100%;
transition: width 200ms ease-in-out;
}
.pure-progress:after {
position: absolute;
height: inherit;
line-height: 1.8rem;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
width: 100%;
text-align: center;
content: attr(data-progress);
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2), inset 0 2px 3px rgba(0, 0, 0, 0.2);
}
<div class="pure-progress" data-progress="30%">
<div class="pure-progress--bar" style="width:30%"></div>
</div>
You can change absolute to relative and add display: block - see demo below:
.pure-progress {
display: block;
position: relative;
width: 100%;
min-height: 10px;
}
.pure-progress>.pure-progress--bar {
position: absolute;
background-color: blue;
height: 100%;
transition: width 200ms ease-in-out;
}
.pure-progress:after {
/*position: absolute;*/
position: relative; /* ADDED */
display: block; /* ADDED */
height: inherit;
line-height: 1.8rem;
/*
left: 0;
right: 0;
top: 0;
bottom: 0;
*/
margin: auto;
width: 100%;
text-align: center;
content: attr(data-progress);
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2), inset 0 2px 3px rgba(0, 0, 0, 0.2);
}
<div class="pure-progress" data-progress="30%">
<div class="pure-progress--bar" style="width:30%"></div>
</div>
You can update the css of .pure-progress:after to
.pure-progress:after {
display: block;
height: inherit;
line-height: 1.8rem;
margin: auto;
width: 100%;
text-align: center;
content: attr(data-progress);
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2), inset 0 2px 3px rgba(0, 0, 0, 0.2);
}
demo
Update
My answer does not work when the progress bar is overlapping the content text. Therefore it is necessary to position it using position: relative as suggested by kukkuz
The updated styles look like:
.pure-progress:after {
position: relative;
display: block;
height: inherit;
line-height: 1.8rem;
text-align: center;
content: attr(data-progress);
box-shadow: inset 0 0 3px rgba(0, 0, 0, 0.2), inset 0 2px 3px rgba(0, 0, 0, 0.2);
}
Updated demo

Get fixed div element in sidebar

<nav class="offcanvas-menu">
<div class="user-details"></div>
<ul class="test">
<li>content</li>
</ul>
<div class=navfooter>
footer content
</div>
</nav>
i dont know how to get the .navfooter on the bottom of the sidebar. it is allways unter the .test container. i tryed couple of things but its not getting work
these are the styles for it
<style>
.test{
left: 0;
top: 20%;
height: 400px;
width: 200px;
background: #ECF0F1;
-webkit-box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
box-shadow: 0px 2px 7px rgba(0, 0, 0, 0.40);
}
.navfooter {
position: relative;
bottom: 0;
z-index: 999;
height: 64px;
width: 100%;
background: #1e67cb;
box-shadow: 0 -1px 5px rgba(0,0,0,.6);
-webkit-box-shadow: 0 -1px 5px rgba(0,0,0,.6);
}
.offcanvas-menu {
position: fixed;
top: 0;
left: 0;
z-index: 1031;
/*visibility: hidden;*/
background: #fff;
border-right: 1px solid #CCC;
width: 250px;
height: 100%;
-webkit-transition: all 0.5s;
transition: all 0.5s;
}
</style>
Have you tried changing the .navfooter to absolute?
.navfooter {
position: absolute;
bottom: 0;
z-index: 999;
height: 64px;
width: 100%;
background: #1e67cb;
box-shadow: 0 -1px 5px rgba(0,0,0,.6);
-webkit-box-shadow: 0 -1px 5px rgba(0,0,0,.6);
}

Tabs with dropshadow only around active

Is there a way to realize the following kind of shadow with CSS only?
So far I only managed to draw the shadow around the complete box without the recess around the inactive tab:
The Code Here
HTML:
<div class="box">
<div class="tabs">
<span class="tab active">Bild</span>
<span class="tab">Text</span>
</div>
<div class="content"></div>
</div>
CSS:
.box {
width: 200px;
height: 250px;
box-shadow: 0 0 2.5px 2px rgba(0, 0, 0, 0.18);
margin: 16px;
}
.tabs {
height: 30px;
}
.tab {
display: inline-block;
width: calc(50% - 2px);
height: 30px;
text-align: center;
line-height: 30px;
}
.tab:not(.active) {
/* Should be removed in the final solution with correct shadows... */
background-color: rgba(0, 0, 0, 0.18);
}
The solution doesn't need to take care of legacy browsers (< IE 10).
Thanks
Use This CSS
.tab.active {
box-shadow: 0 -5px 2.5px 2px rgba(0, 0, 0, 0.18);
position: relative;
z-index: 99999;
}
.tab {
background: #fff none repeat scroll 0 0;
display: inline-block;
height: 30px;
line-height: 30px;
text-align: center;
width: calc(50% - 2px);
}
.content {
box-shadow: 0 0 2.5px 2px rgba(0, 0, 0, 0.18);
margin-top: 0;
min-height: 50px;
position: relative;
z-index: 999;
}
Edit Your CSS
.box {
- Remove this-
/*box-shadow: 0 0 2.5px 2px rgba(0, 0, 0, 0.18); */
height: 250px;
margin: 16px;
width: 200px;
}