What is the best way to overlay content on top of a background image, filling only half of the width?

I'm currently working on achieving a specific design effect where the background image fills the entire width of its parent div while being positioned in the top left corner relative to the image size, which is set at 600x375. The challenge lies in maintaining the desired visual impact with content overlaying on top of this background image. I have experimented with the background-size property, but encountered issues as it covers the whole div instead of the intended area. My goal is to keep the red background-color intact and have the background image occupy only half of the parent div depending on the size of the image.

After trying various approaches, such as adjusting the width and height attributes within the :after property, I found that they did not produce the desired outcome. Can someone suggest a proper technique to resize and stretch the background-image across the designated area without covering the entire parent div?

Link to the code snippet: https://jsfiddle.net/TheAmazingKnight/6xgrftLj/4/

.container {
            z-index: 10;
            position: relative;
        }
        #parent {
            padding-top: 70px;
            padding-bottom: 35px;
            content: '';
            background-color: red;
            background-repeat: no-repeat;
            width: 100%;
            height: 500px;
            background-position-y: 50%;
            background-position-x: 50%;
            position: relative;
        }
        #parent:after {
            content: '';
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            background-repeat: no-repeat;
            background-position-y: 50%;
            background-position-x: 50%;
            background-image: url(https://via.placeholder.com/600x375);
            height: 675px;
            width: 900px;
        }
<div id="parent">
            <div class="container" style="z-index: 10; position: relative;">
                <h1>Heading 1</h1>
                <p>Lorem ipsum... mollit anim id est laborum.</p>
                <span>Lorem ipsum... sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
                <h3>Lorem ipsum dolor</h3>
                <p><a href="#">Download</a></p>
            </div>
        </div>

Answer №1

After carefully analyzing your issue, I have crafted a solution for you without using the image as a background. Instead, I incorporated the image as a child element of the div with the ID "parent" and positioned it absolutely. I have also made some adjustments to both your HTML and CSS files. I trust that these modifications will be beneficial to you.

.container {
    z-index: 10;
    position: relative;
}
#parent {
    position: relative;
    content: '';
    background-color: red;
    background-repeat: no-repeat;
    width: 100%;
    height: 500px;
}

#parent img {
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0; 
  width: 100%;
  height: 250px;
  object-fit: cover;
}
<div id="parent">
        <div class="container"
>
          <img src="https://via.placeholder.com/600x375" alt="">
            <h1>Heading 1</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
            <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
            <h3>Lorem ipsum dolor</h3>
            <p><a href="#">Download</a></p>
        </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

Exploring AngularJS directives, watching for changes with the $watch function, and

Question: I'm facing an issue with my HTML page that includes an AngularJS directive. Here is the code snippet: <menufileupload visible="rightVisible" alignment="right"> <input type="text" ng-model='expr'/> <!--Ot ...

Saving and restoring the cursor/caret position in CKEditor 4: A how-to guide

While browsing similar questions on this topic, I realized that none of them really addressed my specific issue (or maybe I just need a better understanding of the concept). Initially, I embarked on the journey to acquire and manipulate the caret position ...

Guide to positioning a singular image at the center of a page with Bootstrap 4

This seems like a simple task, but despite my efforts, I have yet to come across a solution. The issue is straightforward - I have a single image that I want to display in the center of the page. That's it. Below is the CSS code that successfully ac ...

Issue with Bootstrap Grid functionality

I've been working solo on a project and it's been smooth sailing so far. However, after making some changes to my code, the grids are not displaying as desired. Instead of three columns, only one is showing up and I can't seem to pinpoint th ...

Steps for accessing a particular item with *ngfor in Angular 2

I'm struggling to figure out why both elements of my array are displaying simultaneously in my browser instead of separately. Take a look at this screenshot to see what I mean: https://i.stack.imgur.com/0EKSn.png Is there a method to specifically ac ...

What is the best way to show only the weekdays on the x-axis?

My goal is to create a scatter-linked graph using D3.js, showcasing the people count for various shifts on different dates in January 2020. Here is the code snippet I am working with: <!DOCTYPE html> <html lang="en" > <head> & ...

The issue with background-repeat not functioning correctly

I have created a basic HTML webpage with an image displayed as a header. However, I am encountering an issue where the image is not repeating. When I specify the width to be 100%, the td (side) disappears completely. If I remove the width attribute from b ...

The disappearance of the Bootstrap carousel on mobile devices has caused confusion for

After following some tutorials, I managed to create a carousel with bootstrap that appears perfectly on my laptop's screen. However, when I switch to my smartphone, the carousel disappears and only the indicator arrows along with the image alt text ar ...

Navigating through an array of objects with Node.js

Recently, I integrated the ChartJS library into my Node web app to visualize data. The following is nested in a script tag on an EJS template page: <script> let playerStatChart = new Chart(myChart, { type: 'bar', data: { la ...

Disable the "top" property for the Material UI Drawer component

I'm currently working on implementing a Persist Left Drawer from Material UI that slides in from the left side. However, I don't want the drawer to cover the entire left side of the page. Essentially, I want to remove the "top" style property men ...

Combining CSS and HTML with IE10

With the release of Windows8 and IE10 as developer previews, I am curious about what features IE10 does not support that IE9/8 did, as well as what new features it now supports. Thank you for your assistance! ...

Achieving tile wrapping in Bulma CSS: A simple guide

I am working on a Rails application using the Bulma CSS framework. My challenge involves displaying a long list of items in tile format, but unfortunately, they are overflowing off the screen instead of wrapping to the next line. While checking out the Bu ...

Enhancing navigation functionality using CSS

I have two separate pages with navigation included in both. This way, it's easier to edit the navigation menu once instead of doing so on each page individually. However, I am now facing uncertainty on how to implement the 'active' class usi ...

Display the output based on checkbox selection using JavaScript

I am working on a feature where I need to capture checkbox inputs using JavaScript and then store them in a PHP database. Each checkbox corresponds to a specific column in the database. If a checkbox is checked, I want to insert its value into the databa ...

What is the best way to insert events into your Google Calendar directly from a website?

Looking for some help with integrating Google Calendar and adding event features to my HTML page. I've successfully embedded the calendar onto my website, but I'm struggling to add events directly from the webpage. Is there a way to make this fea ...

Div that automatically expands

Currently facing a bit of an issue. I’m attempting to expand and retract the height of #inner upon clicking, with the intention for the height of #container to increase automatically without manually setting it to ‘auto’. Unfortunately, it seems that ...

Retrieve numerical data from the element and enclose it within a span tag

My goal was to indent a number, but the HTML generated from my CMS is making it difficult to target the number and apply a specific style. <div class="content"> <b>01. Header</b> Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...

Utilizing the Grid-A-Licious plugin multiple times within a single webpage

Currently, I am utilizing the Grid-A-Licious plugin for my project. It functions perfectly when used on a single page with one call to the plugin. However, due to my design requirements, I need to implement it within 3 tabs on the same page. Unfortunately, ...

CSS background image with a see-through overlay

I am trying to add a black overlay with an opacity of 0.7 to a full-screen background image. This is the CSS code I currently have: body { background:url(../images/bg.jpg) no-repeat center center fixed; -webkit-background-size: cover; -moz-backgr ...

"Incorporating text input from a textbox into a <ul> list

Recently I started a new project which involves adding user-entered text from a textbox into a ul list in Bootstrap. The script for this functionality is placed within the head section of my HTML document. <script type="text/javascript"> fun ...