Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS - Background image stretch horizontally and repeat vertically?

Using background-size: cover I am able to make the background stretch to fit the full width of the page.

Using repeat-y I am able to make the background repeat or tile vertically, to the full height of the page.

Is there any way to combine these two effects? Unfortunately they don't seem to work together (at least in Chrome). It appears that I can either stretch in both directions with background-size: cover, or not stretch in the x direction but repeat in the y direction.

Edit: Using background-size: 100% Npx (where N is the height of the image) I can accomplish the above, but it skews the background image as it's only stretched in one direction. Is there a way to keep it scaling proportionally?

like image 260
user35358 Avatar asked Dec 07 '16 19:12

user35358


People also ask

How do I stretch a background image in CSS vertically?

Syntax: background-size: auto|length|cover|contain|initial|inherit; cover: This property value is used to stretch the background-image in x and y direction and cover the whole area. length: This property value is used to scale the background-image.

Which code helps to repeat background image horizontally or vertically in CSS?

repeat-x: This property is used to repeat the background image horizontally.

How do I stretch a background image horizontally?

Instead of cover, use background-size: 100% auto; to size the background image to full browser width while maintaining aspect ratio for its height. Use this in conjunction with background-repeat: repeat-y; to tile it vertically.


1 Answers

Instead of cover, use background-size: 100% auto; to size the background image to full browser width while maintaining aspect ratio for its height. Use this in conjunction with background-repeat: repeat-y; to tile it vertically.

like image 52
Jon Uleis Avatar answered Sep 21 '22 18:09

Jon Uleis