Adjusting the background width results in the container position being altered as well

Currently, I am working with two background images, each accompanied by a red box positioned absolutely inside a bootstrap container. The first red box is located on the right side and the second red box is on the left side.

So far, everything is running smoothly.

Now, my challenge lies in adjusting the width of the background image to create space on the left for the first image and on the right for the second image.

The issue arises when this adjustment impacts the position of the container, which I want to remain constant.

Without any added space:

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

Please refer to the image below with space included:

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

.bodywrapper {
  margin-top: 40px;
}

.container {
  position: relative;
  height: 100%;
}

.bannerBg {
  height: 350px;
  background-size: cover;
  background-repeat: no-repeat;
  margin-bottom: 30px;
  width: 92%;
}

.wrapperOne {
  background-image: url('https://mdbootstrap.com/img/Photos/Slides/img%20(45).jpg');
}

.wrapperTwo {
  background-image: url('https://mdbootstrap.com/img/Photos/Slides/img%20(46).jpg');
}

.box {
  background-color: red;
  height: 220px;
  width: 320px;
  position: absolute;
  bottom: 0
}

.rightside {
  right: 0;
}

.leftside {
  left: 0;
}
<div class="bodywrapper">

  <div class="wrapperOne bannerBg ml-auto">
    <div class="container">
      <div class="box rightside"></div>
    </div>
  </div>

  <div class="wrapperTwo bannerBg mr-auto">
    <div class="container">
      <div class="box leftside"></div>
    </div>
  </div>

</div>

I am seeking guidance on whether or not this adjustment is possible without altering the container's position.

Answer №1

Your code isn't generating the same results as the 2 images you provided, so I made some adjustments.

.bodywrapper {
  margin-top: 40px;
  text-align: right;
}

.container {
  position: relative;
  height: 100%;
  width: 70%;
  display: inline-block;
}

.bannerBg {
  height: 350px;
  background-size: cover;
  background-repeat: no-repeat;
  margin-bottom: 30px;
  width: 100%;
}

.wrapperOne {
  background-image: url('https://mdbootstrap.com/img/Photos/Slides/img%20(45).jpg');
  text-align: center;
  background-size: 70% 100%;
  background-position: right;
  display: inline-block;
}

.wrapperTwo {
  background-image: url('https://mdbootstrap.com/img/Photos/Slides/img%20(46).jpg');
  text-align: center;
  background-size: 70% 100%;
  background-position: left;
  display: inline-block;
}

.box {
  background-color: red;
  height: 220px;
  width: 320px;
  position: absolute;
  bottom: 0
}

.rightside {
  right: 0;
}

.leftside {
  left: 0;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8>
  
  <title></title>
  <link rel="stylesheet" href="menu.css">
</head>
<body>
  <div class="bodywrapper">

    <div class="wrapperOne bannerBg ml-auto">
      <div class="container">
        <div class="box rightside"></div>
      </div>
    </div>

    <div class="wrapperTwo bannerBg mr-auto">
      <div class="container">
        <div class="box leftside"></div>
      </div>
    </div>

  </div>
</body>
</html>

The key is to add the following code to both wrapper elements:

text-align: center;
background-size: 70% 100%;
background-position: right; //in wrapper one and  left in wrapper two
display: inline-block;

The goal is to reduce the width of the image in the wrapper without changing the container's position.

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 never-ending wait of RegEx

Encountered an interesting issue with RegEx replacement in .NET. My regEx was designed to remove html tags from a string, leaving only the <br> tags intact. Here is the HTML I tested it with. <h3 class="a-spacing-mini" style="box-sizing: border-b ...

What is the process for a webpage to save modifications made by JavaScript?

I am working on a simple web page with a form that contains checkboxes representing items from a database. When the submit button is clicked, these items may be retrieved. Additionally, there is an option to add a new item at the bottom of the page. My go ...

What is the best way to place content in a single div without it being divided into several separate boxes

Here is my code snippet: <div class="col-md-9"> <div id="statbox"> {% for obj in product_type %} {% for obj1 in vastu %} <script type="text/javascript"&g ...

Is it possible to delete a <div> tag based on screen size using jQuery or JavaScript?

Hello, I was curious if it's possible to dynamically remove a specific div tag using jQuery or JavaScript when the screen reaches a certain size, for example 500px. ...

Embed a unique custom design sheet within a Facebook iframe

I'm looking to customize the design of my Facebook feed by adding a custom style sheet inside a Facebook iframe. Here's what I have: <iframe width="1000px" height="1000px" frameborder="0" name="f1e348592ed856c" allowtransparency="true" allo ...

Absence of peer dependencies

As a novice in the world of npm and angular projects, I have encountered an issue while using <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89ebe6e6fdfadddbf8ed9aa7beb1aea1b0">[email protected]</a> in my package. ...

Blazor: Passing a CSS class as a parameter to a child component

Is there a way to ensure css isolation works properly when passing a class as an argument to a child component? In the following code snippet, I anticipated the child component would display in blue, but that's not happening. Parent.razor <div cla ...

Ways to reduce the width of a flex box

I am facing an issue with my main container where I have two items placed on the same line. Item 1 is an image linked to a URL and it should be positioned at the far left of the container. On the other hand, item 2 contains a font awesome icon that needs t ...

Show only the selected option with jQuery's on change event and disable or remove the other options

My goal is to make it so that when a user selects an option from a dropdown menu, the other options are disabled or hidden. For example, if option "1" is selected, options "2", "3", and "4" will be removed: <div class="abc"> <div class="xyz"> ...

Tips for expanding an input to fit the width of a div without altering the dimensions of a preceding span element

Let me explain, I have a form that includes a span and an input: <form onSubmit={e => handleSubmit(e)}> <Box className='form_box'> <span> <a href="/cd ...

Issues with Angular-Trix compatibility with Internet Explorer 10

I am currently using the Angular-trix rich text editor, which is functioning perfectly in all browsers including IE 11. However, I am encountering issues with it not working in IE10 or earlier versions. An error message that keeps appearing states: Una ...

"What's the best way to update the src attribute of an iFrame when a user clicks

I'm new to coding in PHP or JavaScript, so please bear with me if my question isn't quite right. I'm looking for a way to dynamically pass and embed a URL from a link into the src attribute of an iframe. Essentially, I want users to click o ...

HTML formatting for text

I recently created an HTML website using Dreamweaver. I faced an issue while trying to design a section within an article using tables to incorporate both text and images. Unfortunately, the tables did not have rounded edges as I desired. I am reaching out ...

Establish the lowest possible height for Angular Material Expansion Panel Content

Is there a way to specify a minimum height for the content of an Angular Material expansion panel when it is open? I have searched for examples on setting the expandedHeight and collapsedHeight for the header, but not for the content itself. The Angular Do ...

Custom pagination page size in AngularJS trNgGrid

I have been working on developing an Angular table using trNgGrid. It's functioning fine, but I am looking to incorporate custom pagination where the user can choose the number of page items to display per page. I checked out the TrNgGrid Global Opti ...

Restoring the background color to its original shade following alterations made by jQuery

In my table, I have multiple rows with alternating white and grey backgrounds achieved through CSS. .profile tr:nth-child(odd) { background:#eee; } .profile tr:nth-child(even) { background:none; } However, I now want to allow users to select a row ...

Interacting with a card in vuejs won't trigger any action

Is there a way to make an overlay disappear when clicking anywhere on the screen except for a specific card positioned on top of it? The current setup makes the overlay disappear even when the card is clicked. How can this behavior be corrected? <div ...

My keyframes seem to be malfunctioning, despite exhausting all my troubleshooting options

Could someone please help me figure out why the @keyframes rule is not working for me? I've tried adding -webkit- as a fix, but it doesn't seem to be helping at all. .keyframe { height: 15px; width: 50px; background-color: red; -webkit ...

There is a significant distance between the columns, causing the text to appear misaligned and not left-

I'm encountering an issue with the alignment of a row with two columns. The first column contains an image, and the second column contains text. However, the text is not aligning properly next to the image. <link href="https://cdn.jsdelivr.net/ ...

Setting line height as a pixel value does not yield the desired result

$(element).css(options.css).attr(options.attr).addClass(options.class) //ensure line-height is correctly assigned if($(element).is('p') && _.isString(options.css['line-height'])){ console.log('line-height assigned: &apos ...