Using pseudoelements for box-shadow, z-index behavior - css

I use a :before and :after pseudoelements to create a custom-shaped shadow on my text card. I use css like this (jsFiddle):
.card {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
float: left;
width: 310px;
height: 310px;
text-align: center;
color: black;
padding-top: 35px;
border: 1px solid black;
}
.card h1 {
font-size: 15px;
font-weight: bold;
line-height: 17px;
font-style: italic;
margin: 0;
}
.card .text {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
-moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
margin: 8px auto;
padding: 15px 10px;
font-size: 13px;
text-align: left;
line-height: 14px;
height: 170px;
width: 280px;
background: white;
border-radius: 5px;
position: relative;
color: #222222;
}
.card .text:before,
.card .text:after {
-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);
content: "";
display: block;
position: absolute;
z-index: -1;
top: 50%;
bottom: 0;
left: 40px;
right: 40px;
border-radius: 100px/10px;
}
.card .text:after {
-webkit-transform: skew(8deg) rotate(3deg);
-moz-transform: skew(8deg) rotate(3deg);
transform: skew(8deg) rotate(3deg);
right: 10px; left: auto;
}
So far so good. Problems start, when i add some background to my .card element, for example background: #0aabff (jsFiddle #2). Notice, pseudo-elements shadow around .text disappeared.
I tried solving this by adding z-index to my .text element, but then, instead of being behind my .text element, my :before, :after are displayed before it, despite having a z-index: -1; property (jsFiddle #3).
This makes little sense to me, and i still don't know how to solve this kind of problem. Perhaps, there is some kind of workaround, or another way to get the kind of shadow i need (original idea is the courtesy of css-tricks.com);

You can add a z-index lower than -1 to your card class.
.card {
z-index:-2;
position:relative;
}
This is because your z-index of your pseudo-elements is lower than the default z-index value of card. By default is the background transparent and you may see the shadow. When you add a background-color then this is positioned above your pseudo-elements.
See http://jsfiddle.net/71thws1a/3/
The problem lies in the stacking of css as mentioned by #Doodlebunch

Related

Stick footer at bottom

I want my footer to be at the bottom of the page with the following code:
body:not(.theme-preset-active) footer#colophon {
padding-left: 15px;
padding-right: 15px;
position: absolute;
bottom: 0px;
width: 100%;
color: #191919;
background-color: transparent;
font-size: 100%;
}
But it ends up in the middle of the content.
I have tried with position:fixed like this:
body:not(.theme-preset-active) footer#colophon {
padding-left: 15px;
padding-right: 15px;
position: fixed;
bottom: 0px;
width: 100%;
color: #191919;
background-color: transparent;
font-size: 100%;
}
But the it wont cooperate with my scrollbar, it jumps a little bit left when the scrollbar appears. Code for scrollbar if that also can be a solution:
::-webkit-scrollbar {
width: 14px;
height: 18px;
}
::-webkit-scrollbar-thumb {
height: 6px;
border: 4px solid rgba(0, 0, 0, 0);
background-clip: padding-box;
-webkit-border-radius: 7px;
background-color: rgba(65, 65, 65, 0.99);
-webkit-box-shadow: inset -1px -1px 0px rgba(0, 0, 0, 0.05), inset 1px 1px 0px rgba(0, 0, 0, 0.05);
}
::-webkit-scrollbar-button {
width: 0;
height: 0;
display: none;
}
::-webkit-scrollbar-corner {
background-color: transparent;
}
html{
position: relative;
overflow-x: hidden;
margin-right: calc(-1 * (100vw - 100%));
background-color: transparent;
}
All help appreciated.
The webpage
/ R
position: fixed is what you should use, but you can also add the following:
body {
--footer-height: 100px;
min-height: 100vh; /* never use height, because you want the page to expand */
padding-bottom: var(--footer-height); /* to avoid footer being on top of relative elements within body */
}
footer {
height: var(--footer-height);
}
I'm not sure what you mean with "it jumps a little bit left when the scrollbar appears" because you don't have anything on the page that fold out and therefor resizes the height of it. So either the scrollbar is there to begin with, or it's not.
Anways, min-height is your friend if you still want the footer to be have a absolute positioning.
If you look at this two pictures, my footer jumps left when the scrollbar appears, if it have the position: fixed.
Thanks for your answer!
/

How to use CSS to reproduce the shadow under a device

Is it possible to use CSS to reproduce the shadow under the device of the below image?
The following CSS and different variants don't work:
filter: drop-shadow(rgba(22, 22, 22, 0.25) 0px 12px 15px);
The problem is concentrating the shadow under the device and flattening it. The CSS above cannot make the shadow appear as if it's projected on the ground.
Codepen: https://codepen.io/Crashalot/pen/MWYzoJV
A pseudo element is better suited to get this done, see below example:
.object {
margin: 20px;
width: 70px;
height: 140px;
position: relative;
border-style: solid;
background: #E6E6FA;
}
.object:before {
content:"";
z-index: -1;
position: absolute;
bottom: -50%;
width: inherit;
height: inherit;
border-radius: 40%;
background: rgba(0,0,0,.55);
transform: scaleX(1.3) scaleY(0.12);
filter: blur(5px);
}
<div class="object"></div>
Alternatively, you can use box-shadow:
.object {
margin: 20px;
width: 70px;
height: 140px;
position: relative;
border-style: solid;
background: #E6E6FA;
/* This is .shadow-lg from tailwindCSS */
/* See https://tailwindcss.com/docs/box-shadow/ */
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}
<div class="object"></div>

Can't get ::after pseudo to appear below it's parent element using z-index

My goal is to position a CSS pseudo arrow below it's parent popup menu.
Setting z-index on the pseudo element to -1 works...that is, if I haven't set a z-index on the parent.
The issue is that it's a popup, it the parent must have a z-index set.
How can I set a z-index for the parent, and have the pseudo appear below?
.dropdown-menu {
top: calc(100% + 5px);
display: block;
position: absolute;
background-color: white;
padding: 5px;
border: 1px solid rgba(0, 0, 0, .15);
box-shadow: 0 3px 8px rgba(0, 0, 0, .3);
z-index: 9999;
min-width: 150px;
}
.dropdown-menu::before {
background-color: #706f6f;
content: '';
display: block;
position: absolute;
left: 50%;
height: 10px;
width: 10px;
transform: translate(-50%, 0) rotate(45deg);
box-shadow: 7px 7px 11px 0px rgba(112, 111, 111, 0.3);
margin-top: -5px;
top: 100%;
z-index: -1;
}

CSS - inline not working with position absolute

Here is an image of what I'm trying to achieve.
HTML:
<h2>Some text<br /> even more text</h2>
CSS
Works
display: inline;
line-height: 1.7em;
font-size: 65px;
color: #333;
background-color: #FFF;
box-shadow: 0 2px 10px rgba(0, 0, 0, .2);
Doesn't work
display: inline;
line-height: 1.7em;
position: absolute;
top: 80px;
z-index: 2;
font-size: 65px;
color: #333;
background-color: #FFF;
box-shadow: 0 2px 10px rgba(0, 0, 0, .2);
Any idea on how to fix this?
inline implicitly becomes block when you absolutely position an element, see
Css 2.1, 9.7 Relationships between 'display', 'position', and 'float'
You will need to use an additional inline element inside this heading, if you want to achieve this effect.
Three steps:
Wrap each part of the <h2> heading in a <span>
Give each span a display of inline-block
Apply the box-shadow to each span
Working Example:
h2:nth-of-type(1) {
display: inline;
line-height: 1.7em;
font-size: 18px;
color: #333;
background-color: #FFF;
box-shadow: 0 2px 10px rgba(0, 0, 0, .2);
}
h2:nth-of-type(2) {
display: inline-block;
position: absolute;
top: 80px;
z-index: 2;
color: #333;
background-color: #FFF;
}
h2:nth-of-type(2) span {
display: inline-block;
font-size: 18px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}
<h2>Some text<br />even more text</h2>
<h2><span>Some text</span><br /><span>even more text</span></h2>

Text wont align on horizontal and vertical centers

I am trying to work on the following example for a user profile.
I have a problem with the text on my various fields though. I would like them to be centered horizontally and vertically.
Using
vertical-align: middle;
Did not work for me, or I did something wrong.
How can I make it so each text snippet is perfectly centered on its respective container?
Here's the solution:
/* --------------------------------
Primary style
-------------------------------- */
#font-face {
font-family: 'Roboto';
src: url('../fonts/roboto/Roboto-Regular.ttf');
font-weight: normal;
font-style: normal;
}
*,
*:before,
*:after {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
div {
display: block;
}
html,
body {
background: #f1f1f1;
font-family: 'Roboto', sans-serif;
padding: 1em;
}
h1 {
text-align: center;
color: #a8a8a8;
font-size: 200%;
}
.user-data {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
position: relative;
overflow: hidden;
cursor: pointer;
display: table;
width: 100%;
height: 85px;
background: white;
margin: 0 auto;
margin-top: 1em;
margin-bottom: 20px;
border-radius: 8px;
-webkit-transition: all 250ms;
transition: all 250ms;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
}
.response-layer {
vertical-align: middle;
display: table-cell;
text-align: center;
font-size: 200%;
color: #a8a8a8;
}
#user-action-text {
color: #f1f1f1;
font-size: 400%;
}
#user-name {
border: 0;
outline: 0;
padding: 0;
margin: 0px;
margin-top: 10px;
width: 304px;
height: 55px;
color: white;
}
#user-action {
background: #e74c3c;
height: 134px;
}
#user-position {
height: 74px;
}
#user-uuid {
height: 54px;
}
section {
max-width: 650px;
text-align: center;
margin: 20px auto;
}
section img {
border: 0;
outline: 0;
padding: 0;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
display: block;
width: 100%;
margin-top: 1em;
-webkit-transition: all 250ms;
transition: all 250ms;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24);
resize: none;
}
.half {
float: left;
width: 48%;
margin-bottom: 1em;
}
.right {
width: 50%;
}
.left {
margin-right: 2%;
}
.user-data:hover {
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
-webkit-transform: translateY(-5px);
-ms-transform: translateY(-5px);
transform: translateY(-5px);
}
section img:hover {
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
-webkit-transform: translateY(-5px);
-ms-transform: translateY(-5px);
transform: translateY(-5px);
}
#-webkit-keyframes loading {
0%, 100% {
margin-top: -50px;
box-shadow: 0px 55px 40px 0px rgba(0, 0, 0, 0.3);
}
30%,
80% {
margin-top: 0px;
box-shadow: 0px 0px 0px 0px rgba(0, 0, 0, 0.5);
}
}
#keyframes loading {
0%, 100% {
margin-top: -50px;
box-shadow: 0px 55px 40px 0px rgba(0, 0, 0, 0.3);
}
30%,
80% {
margin-top: 0px;
box-shadow: 0px 0px 0px 0px rgba(0, 0, 0, 0.5);
}
}
#media (max-width: 690px) {
.half {
width: 304px;
float: none;
margin-bottom: 0;
}
}
/* Clearfix */
.activity-detail:before,
.activity-detail:after {
content: " ";
/* 1 */
display: table;
/* 2 */
}
.activity-detail:after {
clear: both;
}
<h1>New Activity Details</h1>
<section class="activity-detail">
<div class="half right" id="image-data">
<img src="img/default-avatar.png" alt="Profile Photo" style="width:304px;height:304px;" id="profile-photo">
<a class="user-data" id="user-name">
<div class="response-layer">Green Leaf</div>
</a>
</div>
<div class="half left" id="general-data">
<a class="user-data" id="user-action">
<div class="response-layer" id="user-action-text">Greetings</div>
</a>
<a class="user-data" id="user-position">
<div class="response-layer">Developer</div>
</a>
<a class="user-data" id="user-uuid">
<div class="response-layer">324124535345</div>
</a>
</div>
</section>
On the parent div .user-data I changed display to table.
On the child div .response-layer (the one containing "Greetings") I changed the display to table-cell. Now the vertical align works.
Here's the link to the codepen: Codepen
Replacing
vertical-align: middle;
with
position: relative;
top: 25%;
does the trick.
Edit: The answer above is much cleaner

Resources