CSS - Background Image Stretch Horizontally And Repeat Vertically?
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet early access and see previews of new features.
Learn more about Labs CSS - Background image stretch horizontally and repeat vertically? Ask Question Asked 7 years, 11 months ago Modified 5 months ago Viewed 36k times 17Using 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?
Share Improve this question Follow edited Dec 7, 2016 at 19:23 user35358 asked Dec 7, 2016 at 19:20 user35358user35358 3712 gold badges3 silver badges9 bronze badges 3- you can do that with only background-size:100%; – Chiller Commented Dec 7, 2016 at 19:23
- @Chiller you are correct but you need to specify both x and y. i.e. background-size: 100% auto – zgood Commented Dec 7, 2016 at 19:25
- @ probablly you are right but if you see here it works without adding auto jsfiddle.net/qp4093h2 – Chiller Commented Dec 7, 2016 at 19:37
1 Answer
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 44Instead of cover, use background-size: 100% auto; to size the background image to full browser width while maintaining aspect ratio for its height.
You can use this in conjunction with background-repeat: repeat-y; to tile it vertically, but note that will also be the default behavior here if you're not overriding background-repeat.
Share Improve this answer Follow edited Jul 1 at 16:31 answered Dec 7, 2016 at 19:22 Jon UleisJon Uleis 18.6k3 gold badges35 silver badges45 bronze badges 4- 1 Since the background-size x is 100% width it shouldn't repeat-x so you actually don't need to specify repeat-y, just let it default.... but it doesn't hurt. – zgood Commented Dec 7, 2016 at 19:26
- 3 for those searching for what I was -- the auto attribute above is specifying a height, so if you put in a fixed height like in the code background-size: 100% 150px; then your image will stretch horizontally while maintaining the 150px dimension. – Daniel Thompson Commented Jan 22, 2018 at 7:26
- 3 In my case it is not working, I change it to background-size: 100% calc(100%), and it worked for me – Rishi0405 Commented Nov 29, 2018 at 10:09
- As @Rishi0405 comments, it doesn't work if the image is higher horizontally than the screen. I needed to stretch it, so calc(100%) worked fine – Faliorn Commented Sep 1, 2022 at 11:27
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Draft saved Draft discardedSign up or log in
Sign up using Google Sign up using Email and Password SubmitPost as a guest
Name EmailRequired, but never shown
Post Your Answer DiscardBy clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Not the answer you're looking for? Browse other questions tagged
or ask your own question.- The Overflow Blog
- Your docs are your infrastructure
- Featured on Meta
- More network sites to see advertising test [updated with phase 2]
- We’re (finally!) going to the cloud!
Linked
939 Stretch and scale a CSS image in the background - with CSS onlyRelated
192 Stretch background image css? 2 stretching background image 0 Need help stretching background image vertically 2 CSS background image stretch with background-image property 33 Vertically stretch background image 7 CSS Stretched Background Image 1 CSS - Background Image Stretch 0 Background-image stretch 0 CSS center background image horizontally AND THEN repeat vertically? 0 Stretch CSS Background ImageHot Network Questions
- A fantasy story with an imp in a box that paints pictures
- The British used to (still?) classify their guns by weight in pounds rather than caliber. Was that a constant across shell types?
- Can you see through a cannonball packing?
- Understanding the definition of the covariant derivative
- Mega Man: Powered Up
- What is the length scale of correlated errors observed on the 72-qubit google chip used in the quantum memory experiment
- Polynomial.java - a Java class for dealing with polynomials with BigDecimal coefficients
- Why sites like Mapgenie don't infringe intellectual property?
- Walks in Nice (Nizza)
- Meaning of "I love my love with an S—" in Richard Burton's "Arabian Nights"
- What is the proper way to say "voice direction" in German?
- What is the wave icon with a dot on the call screen?
- Are prenups legally binding in England?
- Isekai mahwa with silver haired male lead who is an illegitimate prince
- Can Martial Characters use Spell Scrolls in D&D 2024?
- What was the female equivalent of πάππας in Ancient Greek?
- Should I ask for physical recommendation letters now to avoid future issues with professors' availability?
- turn wire frame of ico sphere into a cage
- What was the significance of Mount Doom's eruption?
- Is Holy Terra Earth?
- Does the Truth not exists or does it seems to not exists due to our abstraction?
- The sum of multiple irrational numbers can be rational, even when they're not conjugates. Is this normal?
- What exactly is the cornerstone that Mark 12:10 speaks of?
- Why does Japanese not have a native "tu" sound?
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
defaultTừ khóa » Html Background Image Stretch Horizontally
-
How To Stretch And Scale An Image In The Background With CSS
-
HTML Stretch Background Image
-
Stretch Background Image In Header Only Horizontally, It Does Not ...
-
How To Stretch And Scale Background Image Using CSS?
-
How To Stretch A Background Image To Fit A Web Page - ThoughtCo
-
How To Stretch A Background Image In CSS So That It Doesn't Repeat ...
-
How To Stretch An Image Horizontally With CSS
-
Background-size - CSS-Tricks
-
Resizing Background Images With Background-size - CSS
-
CSS Background-repeat Property - W3Schools
-
CSS Object-fit Property - W3Schools
-
How To Stretch A Background Image Horizontally Css?
-
Background-size - CSS: Cascading Style Sheets - MDN Web Docs
-
Perfect Full Page Background Image - CSS-Tricks