Place background image in the center with a background color overlay

After browsing through this post on Stack Overflow, I managed to stretch my background image using CSS with the background-size: cover; property.

However, I realized that this solution doesn't completely meet my needs. I'm dealing with an image of incredibly wide resolution like 3840px x 1024px and I'm struggling to figure out a nice way to achieve the following:

Requirements:

  1. The image should be centered
  2. If the viewport width is less than 1280px: Maintain a fixed width of 1280px with horizontal scrolling enabled. When the viewport width is equal to or greater than 1280px, disable horizontal scrolling and display more of the background image.
  3. If the content extends beyond the 1024px height mark, I want to add a colored overlay above the image that matches the upper part of the picture, creating a seamless blend between the background and the overlay.

My current implementation involves cutting the image into three equal parts. The middle section serves as the background for the content area. As the screen width increases, the divs positioned to the left and right of the center expose the remaining portions of the image. Unfortunately, there are rendering issues in Firefox, particularly with the left div's offset calculation.

Here's the code snippet for the existing setup:

var PageWidth = 1280;
var SideImageWidth = 1280;
function calculateImageSet(){

    var bodyWidth = $('body').width();
    var fillerSize = Math.floor((bodyWidth - PageWidth)/2);

    if(bodyWidth < PageWidth){
        $('#fillerLeft').hide();
        $('#fillerRight').hide();
    }
    else{
        var imageOffset = SideImageWidth - fillerSize;
        var mainHeight = $('#main').outerHeight();
        $('#fillerLeft').width(fillerSize).height(mainHeight).show();
        # Doesn't seem to work
        if($.browser.mozilla){
            $('#fillerLeft').css('background-position', '-'+imageOffset+'px 0px');
        }
        $('#fillerRight').width(fillerSize).height(mainHeight).show();
    }
}

Is there a cleaner approach to achieving this without relying on JavaScript?

If any of the requirements are unclear, feel free to ask for clarification.

Thank you!

Edit: I've come up with a nearly functional solution:

#main{
    background-color: #d4eaff;
    background-image: url('forest-wide.jpg');
    background-repeat: no-repeat;
    background-size: auto 1280px;
    background-position: center top;
    min-width: 1280px;
}

This works well when the content doesn't exceed 1024px in height. However, if it does, the blue overlay extends to the bottom, requiring me to adjust the background-position to center bottom at that point.

Answer №1

Hey there! If you're looking to achieve a fully stretched background image, here is some CSS code that may be helpful for you. This method is purely CSS-based and should work in Safari 3+, Chrome, IE 9+, Opera 10+, and Firefox 3.6+

html { 
  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
}

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

Updating HTML Pages with Dynamic Content

Dealing with a massive project consisting of 50,000 pages (20,000 aspx forms, 10,000 asp forms, and 10,000 html pages) can be overwhelming. With only 2 days to complete the task of adding content after the body tag on all pages, I am seeking advice on ho ...

Creating a dynamic tbody element on button click with the help of javascript or jquery

I am working with the following code: $(document).ready(function() { //Optimizing by selecting tbody first using jquery children var tbody = $('#myTable').children('tbody'); //If no tbody found, select the table itself ...

Tips for incorporating JavaScript code into back4app.com using Objective-C:1. Start by accessing the

Currently, I am trying to retrieve "ServerDate" from back4app.com using PFCloud. Unfortunately, I have encountered the following issue: Invalid function: "getServerDate" (Code: 141, Version: 1.13.0) When I attempted to use the code below: [PFCloud ...

Image placed as background for registration form

In my Vue project, I am trying to create a login form with an image as the background. However, I am facing an issue where the image is not displaying on the page. Can someone guide me on how to add a picture behind the form? HTML: Below is the HTML code ...

Flipping the order of elements in an array using JavaScript as described in the

While I am aware there are simpler methods to reverse arrays in JavaScript, I am seeking assistance to fully understand how this particular reverse method works. Your help in clarifying this is greatly appreciated. function reverseArrayInPlace(array) ...

Show picture during the process of loading an external webpage

Utilizing a script to load an external page (external meaning a page within my website) inside a div. Loading the page takes time, so I want to display an image until the page is fully loaded. JAVASCRIPT function HideLoader() { $('#loader' ...

Bindings with Angular.js

I have developed an application similar to Pastebin. My goal is to allow users to paste code snippets and display them with syntax highlighting and other visual enhancements, regardless of the programming language used. To achieve this, I utilize Google&ap ...

Error: Unable to change image -- TypeError: Cannot assign value to null property 'src'

As I work my way through the textbook for one of my classes, I am practicing by building an image swapping page. The concept is simple - clicking on a thumbnail swaps out the main image and enlarges it as if it were linking to another web page. Despite fol ...

Is there a way to use CSS to swap out the content of one HTML element with another HTML element's content?

Having an issue with editing or locating a plugin's HTML template on WordPress. Wondering if it is possible to replace the content of one HTML element with another using just CSS? Appreciate any help! ...

Using Vue.js to dynamically assign CSS classes based on variables

Is it possible to achieve this with a dynamic class like the following? <div :class="'outofstock.item_' + item.id ? 'bg-green-500' : 'bg-red-500' "> I have been struggling to find the correct syntax for this. T ...

Success function in AJAX triggered when no JSON data is returned

Despite its simplicity, I'm having trouble getting this right. I'm utilizing jQuery/AJAX to fetch data (in the form of JSON) from my database. Upon successful retrieval, I have a function to display the data. While this works as expected, I now a ...

Guide to connecting two geometric shapes together with the help of three.js

Is there a way to link two spheres together with a line? I'm looking for a solution that mimics two balls connected by a rope or something elastic. Does anyone have any examples to share? ...

Modifying the input's value dynamically alters the selection choices in Vuetify

Choose the First option Fpo, when you select the first item, the first object in the list is assigned to the variable test. I have used test.name as a model for that input field. Strangely, when I try to modify the input field, the select option also chang ...

Is there a way to determine if a submit button has been clicked in order to prevent my function from executing?

I'm currently developing a platform for uploading images. Once a user uploads an image successfully, it gets saved in 4 different sizes within separate folders. They are then redirected to the same page with new content, where they can proceed to crop ...

Highlighting the menu items when hovering over them in an ASP menu

Recently, I have been working on my menu code and here is what I currently have: <td><a href="Products.asp?isnew=true"><img height="21" border="0" src="images/productmenu/new_items<%if request.querystring("isnew")="" then%><%else%& ...

Bootstrap overrides the color of the calendar year in jQuery UI

I am facing an issue where the CSS styles of my jQuery UI calendar are being overridden by the Bootstrap CSS styles. Take a look at the screenshot provided. As you can see, the text color of the calendar's year is not black as intended. https://i.ss ...

What are some ways to customize the appearance of VueJS components?

I am new to Vue, so I apologize if this question seems silly or if I have overlooked something. I believed that you could target a custom element like this: my-element { styles go here} However, when I created an element, it appears that I can only targe ...

Unable to perform a POST request and send JSON data using AJAX with jQuery at the

It seems like there may be a server issue causing this problem, and unfortunately, I don't have access to our server to troubleshoot. I was hoping that someone else might have a solution or could help me understand the root cause of the issue. The is ...

Exploring the power of Jade and Angular through implementing a for loop within a table structure

I'm brand new to using Jade and Angular, and I could really use a hint from someone more experienced. ... - for (var i = 0; i < p.length; i++) tr td= i + 1 td= price(value='p[i].somedbstuff') ... I want the la ...

Initiate node.js execution upon system boot

Just diving into node.js. In the process of setting up integration tests for a node.js app using mocha, I found this helpful guide: Here's how I created a server: var http = require('http'); this.server = http.createServer(function (req, ...