r/web_design • • 21h ago

Can someone explain how this banner image code works, and how I can do this on my own page? VERY new to HTML and CSS. (Inspect on second slide)

6 Upvotes

8 comments sorted by

11

u/eltron 21h ago

Look below at .picture-fillbackground.

They’re using a background-image that’s too big for the parent element, but centered via background-position and letting the .Hero contain the width and height of the background image. They have multiple versions for different viewports below and the browser will pick the best one.

Personally, I don’t use a lot of background images if I can help it. They’re a pain to modify later and maintain if you’re changing content constantly. I don’t like “hiding” markup in CSs and would rather have an img element visible in the markup. However, some platforms you gotta, I’m looking at Shopify, but you can do the same thing with an img tag and few divs, and using background-cover property.

5

u/Routine_Seesaw1095 19h ago

small add to the reply above: the spans with data-src and data-media aren't read by the browser itself. the picturefill-background class looks like a small js library (BedrockStreaming/picturefill-background) that picks the span matching the screen width and sets it as the div's background-image. on your own page you can skip the script: one div with a fixed height, background-image, background-size: cover and background-position: center gives the same look.

2

u/BusinessEconomy4530 19h ago

if you go the img tag route eltron mentioned, the property on the img is object-fit: cover (background-size only applies to background images). give it width: 100% and a fixed height like 400px, and object-position controls which part of the photo stays visible when it gets cropped.

2

u/eltron 8h ago

Opps! Thanks, I had the wrong property.

2

u/noroelemliaj 16h ago

The simplest version skips that picturefill library entirely: an img tag with object-fit set to cover, inside a wrapper with a fixed height, gives you the same cropped banner look. Right click and inspect your own test page the way you did on that second slide and watch how the width and height percentages change the crop as you resize. Copy just the HTML structure first, swap in one image path, and test at a few browser widths before touching anything fancier.

2

u/dusanodalovic 13h ago

before you touch the code, pick a photo with the important stuff near the middle. on a phone the sides of a wide banner get cut off, so a face near the edge is just gone. and shrink the file before uploading, a photo straight from a camera is way bigger than a banner needs

2

u/SamiAlhammadi 7h ago

As a beginner you can skip the script. One div with background-image, background-size: cover and background-position: center gets you most of the way. Add the smaller phone image later with a media query.