The relevant corresponding CSS would be the following, where padding-bottom is one weird trick that is used to set a 16:9 ratio on the div containing the image. Liquorice candy macaroon soufflé jelly cake. There are three CSS tricks that I used to make the devices resizable: calc (), a CSS function that can perform calculations, even when inputs have different units -size-divisor, a CSS custom property used with the var () function media queries separated by min-width Let’s take a look at each of them. An aria-label is encouraged to take the place of the alt attribute that would normally be present on a regular img tag. Using the media query, the user can change the style of a particular element for different sizes of screen. The media queries allow the users to change or customize the web pages for many devices like desktops, mobile phones, tablets, etc without changing the markups. So using the example of templated cards, here's how you might set up using the background-size: cover solution.įirst, the HTML, where the image is inserted into the style attribute as a background-image. The CSS Media Query can be used to make an HTML div responsive. Let's look at how to use each solution, and learn when to select one over the other.Ī decade of my background was creating highly customized WordPress themes and plugins for enterprise websites. The alternative method makes use of the standard img tag, and uses the magic of: object-fit : cover And boom Youre done The only real bummer is. I used this plugin on ~30 sites prior to the following property becoming more supported (aka IE < 9 dropping in total market share): background-size : cover Īccording to, this property and value have been well supported for over 9 years! But websites that are intertwined with using Backstretch or another homegrown solution may not yet have updated. Set the flex property of each images wrapper div to the images aspect ratio (its width divided by its height). In the not to distant past when jQuery was King of the Mountain and CSS3 was still worth being designated as such, the most popular tool for responsive background images was the Backstretch jQuery plugin. This is episode #3 in a series examining modern CSS solutions to problems Stephanie Eckles has been solving over the last 14+ years as a front-end dev.
0 Comments
Leave a Reply. |