The background image is cropped at both the top and bottom edges

Check out this JSFiddle link: http://jsfiddle.net/SoSoDef/uhx3o62f/1/. The code seems to be running fine, but I'm facing an issue where the image is being slightly cut off at the top and bottom for some reason.

-webkit-background-size: cover;

Can anyone explain why this is happening?

Thanks for your assistance!

Answer №1

Due to the CSS styling in relation to the dimensions of the JSFiddle Result viewport, the background image is getting cut off.

The 'background-size: cover' property in CSS is scaling and constraining the overall proportional size of the image based on the width of the JSFiddle viewport.

In the body style's background property, using 'center center fixed' centers the image's center point in a fixed position relative to the viewport. If the scaled image is taller than the viewport, it will extend beyond the upper and lower bounds of the JSFiddle Result viewport, resulting in clipping.

To fix this issue, you can try removing 'center center fixed' from the background property on the body style:

    body {
      background: url("http://f.cl.ly/items/260T100F3j2Y3L1S0g1w/bg.jpg") no-repeat #292929;
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }

By doing this, the image will be positioned at the top of the viewport without being clipped along the upper edge. As you narrow the viewport, the image will not be clipped along the bottom once it has resized sufficiently.

If you want to scale your image to fit the available dimensions of the viewport without clipping, consider allowing the image to scale in proportion to the width of the viewport, as suggested in this Answer on Stack Overflow.

If the image content must be fully viewable without clipping, use an 'img' element instead of CSS for positioning the image. This way, the viewport will be scrollable if the image exceeds the viewport's dimensions.

For a demonstration of this alternative approach, you can check out my codepen example.

Answer №2

As cited on [W3Schools]

Cover: The background image is scaled to fill the entire background area, even if part of it is cut off from view.

In essence, this behavior is standard where the body widens and trims the top and bottom of the background image.

An alternative approach would be to use contain with a white background to ensure the entire image remains visible at all times.

-webkit-background-size: cover;
background-color:white;

Answer №3

The background-size attribute allows your div to increase or decrease in size based on the dimensions. It is responsive and for further information, you can visit 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

Enhancing productivity with a more streamlined process for creating a responsive website design through the optimization of

I recently had the task of placing two images in a band on a webpage and ensuring they were responsive to different screen sizes. Image 'a' represented a circular logo, while image 'b' was a rectangular logo. The red band served as the ...

Preventing special characters in an input field using Angular

I am trying to ensure that an input field is not left blank and does not include any special characters. My current validation method looks like this: if (value === '' || !value.trim()) { this.invalidNameFeedback = 'This field cannot ...

Implementing a feature in React to restrict access to my URL for a specified duration

I'm currently developing my React project and I need to make a specific page on my website accessible for only a limited time, like 5 minutes. For example, I want to send the user an email with a unique url, allowing them to access the designated web ...

Trouble achieving center alignment of navigation bar using @media query in Firefox and IE

Looking for help with creating a video hub that includes a navigation bar? Instead of using an accordion nav collapse, I prefer the menu to carry onto the next line and be centered. Here is the code snippet I used to achieve this effect: @media screen and ...

Unable to transmit form information using HTML/JavaScript/Express Node application

I'm facing a challenge in developing an app that allows me to input event information and then display it on a map. I'm encountering difficulties at the initial stage when it comes to saving the data. Even though Chrome's Inspect tool indica ...

What is the process for publishing HTML webpages using IIS?

After successfully developing several webpages, I am now ready to deploy them on my localhost. But how exactly can I go about this process? I attempted to create a virtual directory and transferred the HTML pages, CSS, JS, and images into it. Unfortunatel ...

It is not possible to submit a form within a Modal using React Semantic UI

I am working on creating a modal for submitting a form using React semantic UI. However, I am encountering an issue with the submit button not functioning correctly and the answers not being submitted to Google Form even though I have included action={GO ...

The slideshow fails to show correctly after being loaded from an external JavaScript file

Utilizing standard code, I have set up a Slideshow at the top of a website: HTML: <body id="Top" onload="showSlides()"> ... <div id="slides"> <div id="slide1" class="slides fade"></div> <div id="s ...

The option to "open in new tab" is absent from the right-click menu when clicking a link on a website

Why does the option to open a link in a new tab not appear when using JavaScript or jQuery, but it works with an anchor tag? I have tried using window.location and window.open, as well as adding the onclick attribute to a div, but the option still doesn&ap ...

Tips for transforming the <img> element into a Nextjs <Image> component

I have been working with Next.js for some time now, but I still find myself using the <img> tag for certain image components, like this one (using TailwindCSS). <div className="relative"> <div className= ...

Using Jquery to preserve the selected value in a radio button

Having two radio buttons, <div class="class1"> <span><input name="chk1" type="radio" checked="checked" id="check1"/>Option 1</span> <span><input name="chk2" type="radio" id="check2"/>Option 2</span> </div> ...

The application of CSS rule shadowing is not consistently enforced

Why does the text in the selected element not appear yellow like the "char_t" link above, even though they have the same class? It seems like it should be yellow based on the inspector, but it's not displaying as such on the actual page. This issue is ...

Is there a way to automatically calculate the sum of two boxes and display the result in a separate

I am working with two boxes: one is called AuthorizedAmount and the other is called LessLaborToDate: <div class="col-md-6"> <div class="form-group"> <label>Authorized Amount</label> <div class="input-group"> & ...

Fonts appear altered on a Windows computer

Working on my new website watr.io, I've noticed that the font appears differently on Windows versus Mac. The one displayed on Mac is the desired outcome, but I can't seem to figure out what's causing the discrepancy. I've been troublesh ...

Click on the entire table to be redirected to the specified link

Currently, I am facing an issue with styling my tables as links. I have 3 tables and I want each table to be a clickable link. However, after implementing the links, I am unable round the corners of the tables. I envision the final look to be similar to t ...

Is there a common method for generating complex identifiers to be used as the HTML element's id, class, or name attributes

Is there a recommended method for "encoding" complex identifiers such as {Source:"ARCH.1", Code: "456-789.456 A+", SubNumber:##2} to be used in HTML elements' id, class, and name attributes? I could come up with something myself, but perhaps there is ...

Is it possible to target an iframe and display text simultaneously?

Trying to create a unique menu that interacts with an iframe and displays text in a separate div upon clicking. Example: • Menu item 1 clicked. • "https://wikipedia.org" loads in the iframe. • Address of the wikipedia page displayed in a diffe ...

Angular-UI keypress event allows users to interact with components and

I am facing challenges while using the keypress feature in the following code snippet- <div ng-controller="GBNController"> ... <input id="search-field" type="text" placeholder="JSON Query" ng-model="queryText" ui-keypress="{enter: foo()}"/> .. ...

Is it necessary to have col-xs-12 in Bootstrap 3?

In order to achieve the desired display for different screen sizes, we have implemented the following code: <div class="col-md-6">left</div> <div class="col-md-6">right</div> When viewed on smaller screens than md, this code succe ...

Can javascript be used to swap out the folder in my URL?

I have been searching for solutions on how to change the language of my website using jQuery, but so far I have not found anything that works for me. Let's take my website as an example: www.domain.com I have separate folders for different languages. ...