CSS tricks for achieving a "flat extended shadow" effect on containers with minimal CSS code

I have created a "flat long shadow" effect based on Matt Lambert's tutorial. The code is quite lengthy, so I'm looking for ways to make it more concise without using SASS or other tools.

body {
  background-color: #ddd;
}
div {
  min-height: 64px;
  width: 64px;
  text-align: center;
  background-color: cyan;
  border: 1px solid blue;
  box-shadow: #8c8c8c 1px 1px, #8c8c8c 2px 2px, #8c8c8c 3px 3px, #8c8c8c 4px 4px...
}
<div>
  wow that's a lot of CSS!
</div>

I want to add this effect to my website, but the code is overwhelming in its length. Graphic alternatives are not suitable for this case. I've explored options for writing more compact CSS code, but haven't found a solution without using advanced tools like SASS.

Here's some information I found, but it doesn't offer much hope:

Your example above is pretty much the only way to do it with pure CSS, and while it does look pretty crazy - it will let you adjust those text-shadows using transitions and such. - @Lost Left Stack

I'm reaching out to the stackoverflow community to see if there's a more efficient way to achieve this effect.

Answer №1

Using two pseudos with skew can create a simulated shadow effect

div {
  min-height: 64px;
  width: 64px;
  text-align: center;
  background-color: cyan;
  border: 1px solid blue;
  position: relative;
}

div:before, div:after {
  content: "";
  position: absolute;
  top: -1px;   /* offset border in the root element */ 
  left: -1px;
  right: -1px;
  bottom: -1px;
  z-index: -1;
  transform-origin: right bottom;
}

div:before {
  transform: skewX(45deg);
  box-shadow: 1px 60px 0px 0px gray, 1px 120px 0px 0px lightgray;  
}

div:after {
  transform: skewY(45deg);
  box-shadow: 60px 0px gray, 120px 0px lightgray;
}
<div>
  Amazing CSS effects!
</div>

An advantage of this technique is the ability to easily adjust the angle of the shadow

body {
  background-color: #ddd;
}
div {
  min-height: 64px;
  width: 64px;
  text-align: center;
  background-color: cyan;
  border: 1px solid blue;
  position: relative;
}

div:before, div:after {
  content: "";
  position: absolute;
  top: -1px;   
  left: -1px;
  right: -1px;
  bottom: -1px;
  z-index: -1;
  transform-origin: right bottom;
}

div:before {
  transform: skewX(60deg);
  box-shadow: 1px 34px 0px 0px gray;  
}

div:after {
  transform: skewY(30deg);
  box-shadow: 60px 0px gray;
}
<div>
  Amazing CSS effects!
</div>

To make longer shadows, increase the size of the pseudos:

div {
  min-height: 64px;
  width: 64px;
  text-align: center;
  background-color: cyan;
  border: 1px solid blue;
  position: relative;
}

div:before, div:after {
  content: "";
  position: absolute;
  right: -1px;
  bottom: -1px;
  z-index: -1;
  transform-origin: right bottom;
}

div:before {
  height: 200px; 
  left: -1px;
  transform: skewX(45deg);
  box-shadow: 1px 200px 0px 0px gray;  
}

div:after {
  width: 200px;  
  top: -1px;
  transform: skewY(45deg);
  box-shadow: 200px 0px gray;
}
<div>
  Amazing CSS effects!
</div>

Answer №2

If working with SASS or SCSS, you can separate the element and its box-shadow property for easier management:

div {
    box-shadow: /* your lengthy value */;
}

Next, move it to a different SASS/SCSS file. Let's say you call it boxShadow.scss. Then, simply import it into your main SASS/SCSS file using @import 'boxShadow'. This way, you streamline your main SASS/SCSS file and save space.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Centered Menu with Left Alignment

I am trying to figure out how to align a sublist directly under a main list point that is centered. I have attached some images to show what I am aiming for: Currently, when I center the main list point (LEISTUNGEN), I am facing issues placing the sublist ...

Assignment on Ionic's Cascading Style Sheets classes

As I work on styling my app, I find myself struggling with the extra CSS classes that are added when I preview the source in a running app. It's particularly confusing when all I want to do is change the menu header background. In my app.html file, I ...

How do I alter the color of a Vue 3 font awesome icon?

I am currently using Vue version 3.2.1 and have followed the official Font Awesome documentation for Vue 3 found at https://github.com/FortAwesome/vue-fontawesome. I also referenced a helpful video tutorial: https://www.youtube.com/watch?v=MoDIpTuRWfM Des ...

What is the method for accessing a web driver's DOM elements even when the driver instance is not currently active in Selenium?

While using Selenium, I am currently focused on navigating web pages. Experimenting with multithreading, my approach involves processing a page in the background once it's fully loaded, interacting with its DOM elements, and then moving on to the nex ...

Developing a tool that generates a custom CSS file

While experimenting with adding CSS rules using JavaScript, I encountered this interesting line of code. I am having trouble understanding how the return statement works and how I can adapt it for my own project. var sheet = function() { var style ...

Tips for manually adding CSS/JS files for offline usage with Vue.js CLI

I am currently utilizing Vue.js CLI alongside AP.NET Core in a single-page application. I have identified the need to bundle some JavaScript/CSS files locally instead of relying on a CDN due to potential deployment scenarios without internet access. The co ...

Leveraging the power of animate.css library to enhance page transitions on Nuxt

I've been working on implementing the animate.css library for nuxt page transitions, but I'm encountering some issues. I have successfully added animate.css to the nuxt.config.js file and it loads without any problems. However, when I try to use ...

Codepen fails to function properly after being uploaded to a server

I have encountered an issue with my code on CodePen. After exporting it as a .zip file and attempting to run it on a local server, it failed to work. I am unsure of the exact reason behind this. I made sure to include all the necessary dependencies in the ...

Instructions for adding a Key every time a user clicks the space Key within an Input field

I built a Movie Search Page with the OMDB API. My current issue is that the API returns an error when searching for a movie with more than one word because the API URL requires a + key between each word. I am looking for a way to automatically add the + ke ...

Share more documents - tips for uploading and capturing

I need to allow a user to upload multiple files by providing input file fields for each one they want to add. Currently, I have implemented this code: <input type="file" name="images[]" />. However, I am unsure how to handle these files on the serve ...

Can you explain the purpose of the URL function in the context of url("#foobar"

Currently, I am developing a CSS concatenator and one of the tasks involved is URL to absolute URL rewriting. For this process, I exclude any absolute URLs that start with http, https, etc. In the case of Django projects, they designate the following URL ...

What is the proper way to request permission for allowing others to access the mailto function?

I want to create a feature where users can open email on click using JavaScript. However, I have encountered an issue with using the mailto function in Chrome, as it requires changing the handlers settings to make it work. My query is whether it is possib ...

Issue with display of absolute/relative positioned element in IE8

Within a list element (a div) styled with inline-block, there is a ul positioned relatively that is initially hidden. To make the div absolute, I added a class but encountered issues specifically in IE8/9 where clicking on the list does not reveal it as ex ...

Incorporate HTML into a webpage using JavaScript along with variables

So, I need to dynamically add and remove content to an HTML page with complex styling and data sets that will change based on the clicked element. An example scenario could be like this... <li class="foo"> <div class="slider" data-one="foo" d ...

Why is the angular navigation bar not appearing on the screen?

Currently, I am in the process of learning Angular through a Udemy course instructed by Maximilian Schwarzmuller. One of the topics he covers is creating a navigation bar. I have tried to implement the same code for my navigation bar, but unfortunately, it ...

What Causes mat-bottom-sheet-container to Overflow Instead of Remaining Anchored at the Bottom of the Mobile Screen?

I am encountering an issue with a popup window in my Angular app. Despite testing it on various screen resolutions using Chrome's DevTools Screencast, the popup appears correctly styled and positioned on mobile devices. However, when tested on a real ...

What is the solution to determining the width in AngularJS without taking the scrollbar size into account

How can I retrieve the window inner height without including the scrollbar size when using $window.innerHeight? ...

Switch effortlessly between one animation and another with Angular.js

I have two animations named "prev" and "next". Upon clicking the prev button, the "prev" animation should play. Similarly, when you click on the "next" button, the "next" animation should be triggered. I am using ng-class to achieve this behavior. How can ...

Experiencing problems with CSS and other scripts failing to load following navigation with nodeJs

Currently, I'm trying to extract data from a MongoDb database and then proceed to view, modify, and delete it. The process of extracting the data is going smoothly, however, I am encountering difficulties with editing. To address this issue, I created ...

Arranging Nav Items in a Stack with Bootstrap 4

Struggling to create a unique navigation bar design with fixed elements on both the left and right sides. The challenge arises when scaling down to mobile, as the icons on the right end up stacking. Any suggestions on how to resolve this issue? <div ...