DEV Community

Ashley Armstrong
Ashley Armstrong

Posted on

2 2

Responsive Background Images Mixin with SCSS

So responsive images are BRILLIANT with Bootstraps handy html class

.img-fluid
Enter fullscreen mode Exit fullscreen mode

I also use EWWW for optimizing my images on Wordpress and converting images to WEBP.

However I wanted my background images to use have the same proportions whilst working responsively.

I used the below mixin and it has changed my life. Give it try.

@mixin responsive-bg-image($image-width, $image-height) {
  background-size: cover;
  height: 0;
  padding-bottom: percentage($image-height / $image-width);
  display: block;
  position: relative;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay