Set the background image to always cover the entire page

My goal:

When the site has a widescreen aspect ratio, I want the background image to scale to 100% horizontally, cropping the top and bottom. This can be achieved easily using background-size: cover;

However, if the site has a vertical aspect ratio (such as on a smartphone), the image repeats vertically instead of scaling to 100% vertically, which would crop the left and right parts. How can I achieve both scenarios using only CSS?

Answer №1

One way to achieve the desired result is by utilizing media queries in your main CSS file. Media queries allow you to adjust your class attributes based on the screen size.

body {
    background-size: cover;
    background-repeat: no-repeat;
}

/* ----------- iPhone 6 ----------- */

/* Portrait */
@media only screen 
  and (min-device-width: 375px) 
  and (max-device-width: 667px) 
  and (-webkit-min-device-pixel-ratio: 2)
  and (orientation: portrait) {
   body {
    background-repeat: repeat-y;
   }
}

To learn more about media queries, check out this resource.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Merge the date within every individual element

I am trying to combine two dates together. I thought my code was correct, as I first created the current year and then added 1 month to get to 2019. However, for some reason, the result is not displaying. function combineDates() { var currentDate = ...

Concentrating on individual text inputs is ineffective in this situation

Before we proceed, please take a moment to check out this fiddle link: http://jsfiddle.net/asif097/z6zsc What I am aiming for is to have this jQuery code execute independently for each text input with the specified class. Currently, it is affecting both t ...

JQuery fixed div bounces unpredictably when scrolling

Currently, I have a scenario on my webpage where there is a div called RightSide positioned on the far right side below another div named TopBanner. The TopBanner div remains fixed at the top of the screen even when the user scrolls down, which is exactly ...

Get rid of the standard shadow located on the bottom and right of the input text field

My input fields have some border rounding and are displaying an unwanted shadow on the right and bottom. I've tried using the box-shadow property to control the width, color, and other properties of the shadow, but it's not working as expected. H ...

What is the best way to transform a CSS linear gradient into JavaScript code for use in a React Native application

Struggling to convert my input data from Figma CSS into a format that works with react native. Are there any helpful libraries for this task? If so, how should I go about writing it out? The CSS code in question can be found here. ...

Presenting images in a row

Hello, I am facing an issue with displaying images retrieved from the database. They are not showing in-line. I have tried to put the images from a URL, and they display in-line, but the problem persists with images from the database. Any suggestions on ...

Center-aligned text animation in CSS3

My custom text includes a CSS3 animation. Check it out on this fiddle: http://jsfiddle.net/pwLxo78k/ In the animation, one part of the text ("I love") remains static while the other part changes dynamically. I'm trying to center my text while ensuri ...

Identify whether the page is being accessed through the Samsung stock browser or as an independent web application

Hey there! I am on a mission to determine whether my webpage is being accessed through Samsung's default browser or as a standalone web app saved on the homescreen. Unfortunately, the javascript codes I have come across only seem to work for Safari an ...

Steps to turn off lazy loading for a particular webpage

On my website www.elektronka.sk, I have enabled lazy loading when scrolling down. I'm seeking assistance in turning off lazy loading on scroll for just one specific page: Currently, the content is displayed only after scrolling down, but I would lik ...

What is the best way to ensure a floated div fills up the rest of the available horizontal

Apologies for the unclear title of my issue. I struggled to find the right words to describe it while searching for a solution. I have fixed-size divs that need to be floated left and behave like inline blocks. On the right side of the page, there is a co ...

Is it possible to open an image using the tag "a"?

When building my webpage, I opted to utilize webpack. In order to display multiple images on the page, I sought out a library called "lightbox2" online. This particular library allows for image popups when clicked and comes with a simple example: <a ...

Instructions on creating a "ripple" effect on a webpage using CSS

I am attempting to create a wavy border effect where two sections meet on a page (refer to the image below). What is the most effective way to achieve this? The waves should be uniform in size. EDIT: To whoever flagged this as 'already answered' ...

Get Your Business Rolling with the Shopify Cruise Theme

I'm currently utilizing the ride theme on my Shopify website and I've embedded a video in an index.json file: "video_url": "https:\/\/www.youtube.com\/watch?v=KlxiEKrhWIQ", Is there a way to make the video aut ...

Is there a way to create a delay before the audio tag plays?

I've implemented an MP3 player using the <audio> tag in HTML5. <audio controls="controls" autoplay="autoplay"> <source src="song.mp3" type="audio/mp3" /> </audio> While everything is working smoothly, I'm interested i ...

The stretched link in the card is not expanding as expected

I'm looking to achieve this effect Check out my code with added Bootstrap columns and rows The link in the code is not stretched as expected, it appears as a standard button link. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/ ...

What is the reason for WordPress theme CSS and JS files loading after the head tag?

I'm experiencing an issue where my Wordpress theme's CSS and JS files are loading after the head tag. Additionally, when I view the source in Firefox, they are showing up in red, indicating incorrect markup. Can anyone help me figure out what&ap ...

Failure to link style sheet to document

Experiencing some difficulties with a seemingly simple task that I can't figure out. When I check my code in the browser, none of my styles are being applied to the HTML elements. The style sheet is located in the same folder as the main document, and ...

Is there a way to apply multiple colors to a h1 tag in HTML?

I need help with adding multicolor to a heading on my website using CSS. I am trying to define colors using the span element with different classes for each text section. However, the colors are not showing up as expected and there seems to be spacing issu ...

Prevent my buttons from altering their color when hovered over

I have created some visually appealing buttons that I am satisfied with, but when a user clicks on one, the text color changes and the button's hover behavior is altered. I utilized this generator to design the buttons: Below is the CSS code for my b ...

Performing a form submission on a dropdown menu using the onchange event without the need for

Is it possible to trigger a postback using an onchange event and then retrieve the value in a Request[]? Here is the code snippet: <select name="nameOnSelection" id="idOnSelection" onchange=""> <option value="1"> random string ...