A single background image divided among two separate divs

Seeking help once again after encountering difficulties the first time. Any feedback is greatly appreciated.

I am currently working on a website that includes 2 divs with background images, but I'm struggling to make them align perfectly across all devices.

Currently, the background images align correctly on screens up to 1920px wide, but as the screen size increases, they start to shift out of alignment.

If anyone could assist me with this issue, I would be very grateful.

Please refer to the image here for reference.

.productTopSection {
background: url("http://mcauliffe.testcre8.co.uk/assets/images/home/mcauliffe-brownfield-experts-homepage-about-image.jpg") no-repeat center;
min-height: 895px;
background-size: auto 100%, cover;
}

.mc-key-points {
background: url("http://mcauliffe.testcre8.co.uk/assets/images/home/mcauliffe-brownfield-experts-homepage-key-points-image.jpg") no-repeat center;
min-height: 895px;
background-size: auto 100%, cover;
}

@media only screen and (min-width: 1921px) {
.productTopSection {
background: url("http://mcauliffe.testcre8.co.uk/assets/images/home/mcauliffe-brownfield-experts-homepage-about-image.jpg") no-repeat center center;
min-height: 895px;
background-size: cover;
}

.mc-key-points {
background: url("http://mcauliffe.testcre8.co.uk/assets/images/home/mcauliffe-brownfield-experts-homepage-key-points-image.jpg") no-repeat center center;
min-height: 895px;
background-size: cover;
}
}

@media only screen and (max-width: 1200px) {
.productTopSection {
background: url("http://mcauliffe.testcre8.co.uk/assets/images/home/mcauliffe-brownfield-experts-homepage-about-image-mobile.jpg") no-repeat center center;
background-size: cover;
}

.mc-key-points {
background: none;
}
}
<div class="productTopSection g-py-200">
<!-- Content Goes Here -->
</div>

<div class="mc-key-points g-py-200">
<!-- Content Goes Here -->
</div>

Answer №1

@media only screen and (max-width: 767px)
.productTopSection {
    background-size: cover;
    height: 200px;
    min-height: auto;
}

It appears that the above code is correct based on the fact that there is no content present. Please let me know if my assumption is incorrect.

Answer №2

Your provided example is not functioning properly, but fear not, I will make an effort to comprehend your message

  1. Initially, one must grasp the concept of how it operates

https://i.sstatic.net/Vsz2e.jpg

The blue frame represents your div in full-screen mode (acting as a single element for the entire page). Since the div lacks height, one can assign it a min-height or fixed height (using the height property), which functions similarly when solely featuring a background until content is inserted within the div.

It is essential to note that the specified height does not impact the image's inherent height itself; therefore, the min-height will remain unchanged until sufficient text is added within it (for instance, any content with a given height).

  1. In the demonstration image, the background-size property was exemplified with a value of 100% auto. Referencing the documentation, we observe that 100% width is set while leaving the height as auto by default with equivalent background-image positioning (background-position: 0% 0%).

If articulated in this manner:

background-size: 100% auto;
background-position: 0 0; // although unnecessary since this is the default setting and used here merely as an example

the outcome mirrors the depiction above, wherein the image is stretched widthwise relative to the screen's proportions.

Upon reducing the screen's width, the div modifies its width accordingly, conforming to the screen's dimensions automatically. https://i.sstatic.net/8NUgE.png

What transpires? A vacant region beneath! In your scenario, the height was denoted as 100%, adjusting the width automatically.

background-size: auto 100%;

Furthermore, refrain from utilizing multiple background image syntax in such cases:

background-size: auto 100%, cover; // singular background image employed in this context

This mirrors the non-functional output you encountered:

https://i.sstatic.net/GTcyh.jpg

Note: It appears that center positioning is utilized, leading to dual gaps! A commendable achievement!

What remedy is suggested? Embrace an image (via the HTML img tag) instead of a background.

For instance:

<img aria-hidden="true" class="bg-fix" src="https://i.picsum.photos/id/767/1200/800.jpg?hmac=lGBpi_Bt_UPPi17TX-TUBQitEe14QlbeSJ-GYhwZBvw" alt="">
<style>
    img {
        display: block; // Eliminate native inline spacing
        width: 100%;
    }
</style>

Alternatively, implement media queries to regulate the div's height (utilizing vh over pixels or CSS-based responsive designs)

<style>
    div {
        /*.....*/
        -webkit-background-size: 100% auto;
        background-size: 100% auto;
        min-height: 60vh;
    }

    /* OR */

    div {
        /*....*/
        min-height: 875px;
    }
    @media screen and (max-width: 1024px) {
        div {
            min-height: 500px; 
        }
    }
</style>

Answer №3

It's uncertain if you intended to crop the image sides on mobile, but using an <img> tag in HTML instead of background-image in CSS can simplify things.

The challenge lies in maintaining the aspect ratio when scaling the height with the width. In your code, the height is effectively set to 895px with min-height, causing cropping unless more content is added to the div.

On mobile screens, the height remains at 895px, resulting in gradually less cropped image sides until reaching 1920px in width. Beyond this width, the image stretches wider, leading to top and bottom cropping. This misalignment is caused by centering the images based on percentage values, which may not align as expected.

Using the <img> tag simplifies this process. By adding content width/height to the container, it allows for expansion or shrinking to fit the content. Setting both the container and img width to 100% ensures they fill up the space while preserving the aspect ratio, eliminating top/bottom cropping and ensuring aligned edges.

An absolute positioned container over the img can then be used for text content placement. For cropping image sizes on smaller screens, consider implementing media queries for breakpoints and adjustments.

Here's a simplified example using <img> for better control and straightforward implementation:

.productTopSection, .mc-key-points {
  position: relative;
  width: 100%;
}

.responsive-img {
  width: 100%;
}

.overlay-content {
  position: absolute;

  /* initial content positioning */
  top: 50%;
  transform: translateY(-50%);
}
<div class="productTopSection g-py-200">
  <img class="responsive-img" src="http://example.com/image1.jpg">
  <div class="overlay-content">
    Content Goes Here!
  </div>
</div>


<div class="mc-key-points g-py-200">
  <img class="responsive-img" src="http://example.com/image2.jpg">
  <div class="overlay-content">
    Content Goes Here!
  </div>
</div>

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

Passing a variable between two PHP files and then showcasing its value in a separate third file

bookincome.php receives several inputs, and bi.php calculates the total net income based on the values provided by bookincome.php. It then displays a table containing all the user-given values and the calculated total book income ($tnibeforetax) value. How ...

JavaScript: Finding the full Base-URL - A Step-by-Step Guide

Is there a way to incorporate JavaScript into external files without relying on the use of base/root URLs in HTML/PHP templates? Everything is functioning properly except for the need to determine the base/root URL in an included JS file. Dilemma: I am se ...

Launch a nearby hyperlink in a separate tab

I am currently facing an issue where all the URLs in a Text get linked as Hyperlinks. However, when a user types www., the browser interprets it as a local URL and associates the link with the application's URL. Does anyone know how to open a local U ...

Embedding PHP code within HTML causing functionality issues

I'm attempting to include PHP within CSS, but the outcome is not what I expected. Instead of displaying the desired results, it's showing me the variable names and an echo statement: <!doctype html> <?php require_once("mysqlconnect.php" ...

The Material UI button shifts to a different row

I need help adjusting the spacing between text and a button on my webpage. Currently, they are too close to each other with no space in between. How can I add some space without causing the button to move to the next line? const useStyles = makeStyles((the ...

Is it possible to edit the HTML DOM of an external URL that is opened using window.open?

Imagine a scenario where I have a page located at: www.mydomain.com When a user clicks on a button, it triggers the opening of a new window using: newWin = window.open("https://www.otherdomain.com","a","height=800,width=1000"); Now, my objective is to m ...

Can the tooltip of an element be changed while the old tooltip is still visible without having to move the mouse away and then back again?

When I have an HTML anchor element with a tooltip that appears when the mouse is hovered over it, and then I use JavaScript to change the tooltip's text using element.title = "Another Tooltip", the new tooltip text does not immediately update visually ...

Comparing Traditional Tables to Div Tables

Is this process: <table> <tr> <td>Hello</td> <td>World</td> </tr> </table> Achievable with the following code? <div> <div style="display: table-row;"> <di ...

I need help with importing CSS in Vue.js

When working with the Vue package, I have a need to import a CSS file directly into the index.html file. Currently, the 'index.html' file mounts #app > 'App.vue', which then mounts Todo.vue using the router. Any assistance on how t ...

How to determine if an Angular list has finished rendering

I am facing an issue where I have a large array that is being loaded into a ul list using ng-repeat in Angular. The loading of the list takes too long and I want to display a loader while it's loading, but hide it only when the ul list is fully render ...

Unable to set height:auto for the background image

For more information, visit this link. I am looking to customize the comments area by giving it a solid color that dynamically adjusts its height based on the number of comments. The background color should expand if there are many comments and shrink if ...

JavaScript for validating dimension text input has unique functionalities

I am looking for guidance on validating an ASP textbox using JavaScript. My goal is to validate a user's input in the format of 4-1/2, 80-1/2, 6/8, or simply 5. Only these types of patterns should be allowed, and each input must contain only one numbe ...

Is it possible to assign a specific value to a row within a table?

For each row (tr) generated from my database, I would like to assign a unique row value. Let's consider the table being created: while ($row = $database->fetch_array($result)) { $i=1-$i; $class = "row".$i; echo "<tr class='{$class} produ ...

Come see the media Rule on display (viewable on a mobile phone)!

Seeking a solution to the issue of making the "IN PC" item visible only on computer screens and not on mobile phones. This menu item continues to display on mobile devices, despite efforts to resolve the problem. Any insights on where the problem lies and ...

What is the best way to conceal a broken image icon without sacrificing all of the stylesheet?

Can someone assist me in understanding and modifying this code? Here is the code snippet: <table> <tr> <td> <img src="" id="img" class="img" style="width:100%;height:200px;background-color:#ccc;border:2p ...

The tab key seems to be malfunctioning when triggered during a jquery change event

I encountered an unusual issue with my jQuery events, and I'm not entirely sure if it's a problem related to jQuery. In hopes that some jQuery experts can shed some light on this. The code snippet below was included in my HTML page to automatica ...

Angular, a Self-sufficient Module of Cascading Style Sheets

I have developed a factory that generates HTML content. I want to showcase this generated HTML within a specific section of my application, without its CSS affecting the rest of the site, and vice versa. While using an iframe is one option, I believe there ...

Solving the challenge of HTML checkbox design

I have successfully styled checkboxes with a background color, but now I want each checkbox to have a different color without having to rewrite a lot of code for each one. Check out the demo here .checkbox { display: inline-block; cursor: point ...

Creating custom styles using Material-UI's makeStyles function with both before and after

Currently, I'm tasked with a project that involves utilizing the CSS code below. .hexagon, .hexagon::before, .hexagon::after { width: 67px; height: 116px; border-radius: 18%/5%; } I am wondering if there is a method to apply the specified style ...

Adjust text size within a div when hovering over it

I have a div with many elements inside. When the user hovers over the div, I want to increase the font size within that specific div. Here is the HTML code structure: <div class="col-sm-5 home-grid-2x"> <div class="home-grid-2 ...