Skip to content

Instantly share code, notes, and snippets.

@dbox
Last active August 1, 2021 13:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dbox/483c75dbf929149ecba4 to your computer and use it in GitHub Desktop.
Save dbox/483c75dbf929149ecba4 to your computer and use it in GitHub Desktop.
Answer for adobe ask the experts

Q: What is your favorite Css 'trick'?

Responsive images in CSS are pretty easy: just set the width: 100% and height: auto and you're good to go. Things get a little unpredictable, though, when dealing with background images. Since putting a height on the element causes lots of responsive headaches, one workaround is to have the container scale by its aspect ratio. This can be achieved by setting a few attributes to the element's :after tag:

.my-element {
  overflow: hidden;
  position: relative;
}
.my-element:after {
  content: "";
  display: block;
  height: 0;
  padding-top: 56.25%; /* Aspect ratio = height divided width times 100 */
}

See a CSS demo, or grab this handy Sass mixin.


Daniel Box
Designer, KNI

@immagimario
Copy link

Good little trick ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment