Adjusting the size of the image based on the content with a background-size cover feature

I've been spending days trying to figure out the best way to properly display a background image on various screen sizes.

Check out this working example at . You can also view it on JSFiddle for better clarity.

The issue I'm facing is as follows: The original image size is 1920x920.
Below is the SASS class used for the header image.

$header-min-height: 920px;
.l-header {
  min-height: $header-min-height;
  height: 110%;
  width: 100%;
  @include background-size(cover);
  background-position: center top;
  background-repeat: no-repeat;
  background-image:url('../images/background-header.jpg');
  @include respond-to-less-than(desktops) {
    min-height: $header-min-height*1.6;

  }
  @include respond-to-less-than(tablets) {
    min-height: $header-min-height*1.8;
  }
}

The initial header background image looks decent but when the screen size decreases, the image scales due to the background-size property which affects the image quality. Adjustments have been made to the min-height for smaller screens to accommodate content within the image.

In cases of square images, cropping could be a solution to show only part of the image while hiding the rest under another div. However, with an image like mine that has rounded borders, achieving the same results with CSS alone is challenging.

I aim to achieve a result similar to the desired look shown below:

To create this picture, I cropped a section of the original image equal to the previous screenshot.

Like this

I'm struggling to find a solution to make the image look good on different screens. Is changing images according to screen size (like using srcset) the only viable option? Any suggestions or help would be greatly appreciated.

Thank you!

Answer №1

Consider making changes to your overall layout design:

.l-header {
    width: 100%;
    height:auto;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
    background-position: center top;
    background-repeat: no-repeat;
    background-image: url(../images/background-header.jpg);
}
@media only screen and (max-width: 992px)
.l-header {
    background-image: url(../new picture with portrait orienbtation);
}
.l-header-wrapper {
    width: 100%;
    height: auto;
}
.l-our-services {
    padding: 50px 0;
}

Take out the following rules:

@media only screen and (max-width: 992px)
.l-header {
    min-height: 1472px;
}
.l-our-services {
    padding-left: 0; 
    padding-right: 0;
    padding-top: 50px;
}
.l-header-wrapper {
    width: 100%;
    height: 100%;
}

One possible solution is to use a different image (with portrait orientation) for mobile display. The new image should be around 1500px in height and 950px in width for optimal appearance on mobile devices.

Answer №2

To address this issue, I suggest revising the HTML structure to incorporate a fixed background featuring stars, with icons positioned on top of it. For the curved bottom edge effect, consider utilizing an SVG white element instead of embedding it in the image.

You can implement something similar to the following:

<body>
    <section class="fixed_background"></section>
        <header id="header-main" class="header l-header container-fluid">
          Insert Services content here...
        </header>
    <section class="curvedEdge"></section>
    <main id="main-content" class="main-content l-main-content">
          Include Some other content here
    </main>
    <footer id="footer-main" class="footer-main l-footer-main">
</footer>
</body>

Feel free to view my modified version in this JSFiddle fork, which is not finalized but provides a general idea. I cleared out .l-header and appended additional CSS rules at the conclusion.

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

The middle color of Ion.RangeSlider

I am currently working on implementing Ion.RangeSlider to create a slider that changes color starting from the center and moving towards the slider control until it reaches it. The current setup I have is as follows: However, I would prefer the color to t ...

Ways to implement collapsible functionality into table rows

I have a table on my website and I want to initially display only 3 rows. When the user clicks on the 'more' button, I want the rest of the rows to be displayed. However, when I tried implementing this with code, I encountered rendering issues. I ...

use ajax to retrieve response using jquery

I am trying to implement a full div modal in Bootstrap 4 that fetches content using AJAX/PHP response. The PHP code I have contains the #ofTheLinkCallAction identifier. <a href="#demoModal" data-toggle="modal" data-target="#demoModal" class="modal-acti ...

What is the best method to extract the country_code from JSON data using jQuery or JavaScript?

{ "status": "success", "description": "Data successfully received.", "data": { "geo": { "host": "2405:204:3213:5500:b012:b9d5:e4db:47b6", "ip": "2405:204:3213:5500:b012:b9d5:e4db:47b6", "rdns": "2405:204:3213:5500:b012:b9d5:e4db ...

Steps for extracting a specific portion of a value contained within an attribute

In my code, there are multiple hyperlinks with different values attached to an onclick function. Here is an example: <a onclick="Utils.decideOffer('', {'unlockFeature': 'cars'});">cars text</a> <a onclick="Util ...

The feature of interpolation within attributes has been eliminated. Please use either v-bind or the colon shorthand in its place

I am struggling to grasp how I can pass a value to Vue using HTML. I keep encountering this error message: Interpolation inside attributes has been removed. Use v-bind or the colon shorthand instead. Edit: I am trying to pass the value "country" to the Vu ...

Is there a way to combine multiple incoming WebRTC audio streams into one cohesive stream on a server?

I am currently working on developing an audio-conferencing application for the web that utilizes a WebSocket server to facilitate connections between users and enables streaming of audio. However, I am looking to enhance the system by transforming the serv ...

Troubleshooting a CSS template with padding and margin discrepancies

I have a question regarding my code: http://jsfiddle.net/spadez/rAQYL/1/ Within my code, I am attempting to accomplish three tasks but I seem to be encountering some difficulty. Eliminate padding below "admin" (where you see the green background) Get ...

Encountering issues with AJAX requests using jQuery

Hey there, I'm attempting to make an AJAX call in a C# page and I'm running into some issues. Below is my jQuery code: $(document).ready(function () { $.ajax({ type: "POST", url: "conteudo.aspx/GetNewPost", data: { i ...

I have a desire to use Javascript to control CSS styles

Within the CSS code below, I am seeking to vary the size of a square randomly. This can be achieved using the Math.random() property in Javascript, but I am uncertain how to apply it to define the size in CSS. #square { width: 100px; height: ...

Ways to enlarge a div's dimensions initially by adjusting its height and then expanding its width

.box { background-color: DarkSeaGreen; padding: 0px; margin: 0px; float: left; min-height: 50px; min-width: 50px; max-height: 100px; max-width: 100px; } <div class="box"> ab cd ef gh ij kl mn </div> In the provided code snipp ...

Click on the entire Material-UI formControlLabel to select it, not just the text

I have recently started working with the material UI. Currently, I am dealing with a form that looks like this: <FormControl variant="outlined" className={css.formControl} margin="dense" key={"abc_" + index} > <FormControlLabel cont ...

Request made to Ajax fetching complete webpage content

I have a page full of random tips at http://www.javaexperience.com/tips To display these tips on other pages of the website, I am using an ajax call to add the content returned by the call to a specific div's HTML. The code for the div is: <div ...

Click on the image to view in a separate window

I am looking for assistance with a script that can open an image in a new page, not a new window. I have a specific HTML page designated for displaying various images when clicked on. If anyone has any ideas or guidance on how to achieve this, it would be ...

Utilize text wrapping to ensure a fixed maximum height for content display

I am in need of a div that contains text spanning multiple lines, with both a fixed width and a maximum height. Currently, I have applied the CSS property overflow: hidden;. However, my issue arises when the last line of text exceeds the maximum height of ...

Retrieving the text content from a JSON key

I have the following JSON data saved in a jQuery variable called "jsondata": var jsondata = { "Name": { "Jaken": {}, "Test": {}, "Hello": {} }, "Date": { "Today": {}, "Tomorrow": {}, "Wednesday": {} }, "Description": { ...

Error in JSON data structure while transferring data from Jquery to PHP

Having some trouble with my first attempt at sending a JQuery request to an API I'm developing. My PHP code keeps reporting that the JSON is malformed. Interestingly, if I create a JSON array in PHP and send it through, everything works perfectly. Bu ...

Tips for moving a div to reveal new content whenever a toggle-slide is activated

I'm attempting to alternate between two divs every time the toggle-slide button is clicked. Additionally, the other button should hide/show each time the switch button is clicked. Here is the HTML code: <div id="divParticipants"> Click &a ...

Utilize flexbox to achieve center left alignment of elements within a DIV container

I've encountered a puzzling issue with my inherited markup. The width is defined on the outermost div and then inherited by each child div, creating a dynamic width scenario. The challenge I face is aligning the pink wrapper div to match the exact wi ...

Radio button is selected but textbox does not display

Struggling to get the radio button to display the textbox as intended, but unfortunately encountering issues. Here is the HTML code in the JSP page: <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> ...