Adjust the DIV to match the dimensions of the background image

Can anyone help me figure out how to make the purple box match the size of the background image in this screenshot using Bootstrap and its flex classes? I want it to maintain that size when resizing the screen.

If you'd like to try it out yourself, I've set up a Stackblitz environment for testing: https://stackblitz.com/edit/javascript-rs3ykl?file=index.html

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

.page-container {
  width: 100vw;
  height: 50vh;
}

.background-image {
  background: url('https://dummyimage.com/258x362/007aa5?text=Banner-Desktop-Large') center center / contain no-repeat;
  height: 100%;
  width: auto;
  background-origin: content-box;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="page-container">
  <div class="row w-100 h-100">
    <div class="col-4 bg-primary">
      <p>Box 1</p>
    </div>
    <div class="col-4">
      <div class="background-image">
        <div style="background: purple">
          <div class="d-flex justify-content-around" style="color: rgb(255, 255, 255)">
            <div class="d-flex flex-column">
              <div>Tage</div>
              <div>16086</div>
            </div>
            <div class="d-flex flex-column">
              <div>Stunden</div>
              <div>6</div>
            </div>
            <div class="d-flex flex-column">
              <div>Minuten</div>
              <div>13</div>
            </div>
            <div class="d-flex flex-column">
              <div>Sekunden</div>
              <div>34</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="col-4 bg-secondary">
      <p>Box 3</p>
    </div>
  </div>
</div>

Answer №1

In my opinion, using an image tag instead of a URL with a div is a better choice. I made the change accordingly.

<link
  href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a4a9a9b2b5b2b4a7b686f3e8f6e8f4">[email protected]</a>/dist/css/bootstrap.min.css"
  rel="stylesheet"
  integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
  crossorigin="anonymous"
/>
<style>
  .page-container {
    width: 100vw;
    height: 50vh;
  }

  .background-image {
    height: 100%;
    width: auto;
    background-origin: content-box;
  }
  .bk-image {
    object-fit: cover;
    width: 100%;
    height: 100%;
  }
</style>
<div class="page-container">
  <div class="row w-100 h-100">
    <div class="col-4 bg-primary"><p>Box 1</p></div>
    <div class="col-4">
      <div class="background-image">
        <div style="background: purple">
          <div class="d-flex justify-content-around" style="color: rgb(255, 255, 255)">
            <div class="d-flex flex-column">
              <div>Tage</div>
              <div>16086</div>
            </div>
            <div class="d-flex flex-column">
              <div>Stunden</div>
              <div class="">6</div>
            </div>
            <div class="d-flex flex-column">
              <div>Minuten</div>
              <div>13</div>
            </div>
            <div class="d-flex flex-column">
              <div>Sekunden</div>
              <div>34</div>
            </div>
          </div>
        </div>
        <img class="bk-image" src = "https://dummyimage.com/258x362/007aa5?text=Banner-Desktop-Large">
      </div>
    </div>
    <div class="col-4 bg-secondary"><p>Box 3</p></div>
  </div>
</div>

By making this adjustment, the purple bar now remains aligned with the background picture. Additionally, by setting the height to 100% as you did, it ensures alignment with the row height if desired.

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

Tips on modifying anchor tag attributes in cross-domain iframe source URLs to open in a new tab

Is there a way to change the target attribute of an iframe child anchor tag from "_blank" to "_parent" or completely remove it? I'm looking for a script that can help me achieve this. How can I modify or delete properties of an anchor tag within an i ...

What steps can I take to minimize the flashing of this fixed navigation bar?

My fixed navigation bar fades out when scrolling down the page and reappears when scrolling up, which all works well. However, I have noticed that if I do this very quickly with short movements, around 20 times, it doesn't have enough time to complete ...

Make the <textarea> occupy the entire available space

How can I make a <textarea> occupy all available space within a <td> element? Upon clicking inside the table cell, the <textarea> should display with precisely the same dimensions as the cell itself, resembling an Excel spreadsheet. I h ...

Determination of the input parameters

Currently, I am developing an Angular application that includes a matInput field for user input. The issue I am encountering is that when the user enters a positive or negative value in the field (e.g. +5 or -5), the final output does not reflect the inten ...

When transitioning from HTML to PHP include, the CSS 'active' class does not function properly

I have noticed a similar question was asked, but the approach taken by the other user differs from mine. My goal is to implement an HTML navigation on my PHP pages for easier future modifications. I want buttons to be highlighted when hovered over and have ...

Adjusting the height of a row in Bootstrap can sometimes result in column content overflowing

.row-2 { padding: 20px; margin: 100px auto; box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px; border-radius: 10px; height: 300px; } .fa-solid { ...

Preventing unclickable Bootstrap checkboxes with identical IDs

I have a checkbox setup like this <div class="form-group"> <div class="col-sm-3"> <div class="checkbox clip-check check-primary"> <input type="checkbox" value="machin ...

How to achieve a reverse slideToggle effect with jQuery when refreshing the page

After creating a custom menu on Wordpress using jQuery slideToggle to toggle dropdown on hover, everything seemed to be working perfectly. However, I noticed that when I refreshed the page while moving my cursor between two menu items with dropdown menus, ...

Send a variable to a specific page and locate it within a dropdown menu

I am currently working on an HTML page that displays employee leave details. The page includes a pending leave option with the ability to edit. Users should be able to edit their leave while it is still marked as PENDING. Once the edit button is clicked, t ...

Tips for organizing child cards within a parent card using Bootstrap

I'm trying to set up my child card within the parent card to look like the image provided. I'm using Bootstrap 4 and the card_deck class. https://i.sstatic.net/ihWrb.png However, I'm facing difficulties in getting the layout to match the p ...

The jQuery toggle function is malfunctioning when applied to the rows of a table

I have been experimenting with toggling table rows using jquery. Initially, the state is hidden This feature is functioning properly $('.table tr.items.' + name).each(function() { $(this).show(); }); Furthermore, this snippet of code is wor ...

The hiding/showing of elements is not being executed correctly by jQuery

My web template includes HTML and jQuery code for a project I'm working on. During the $.getJSON call, there can be a delay in loading the data. To inform the user to wait, I added a warning message in a div with the id="warning". The code properly ...

tips for choosing a specific dropdown menu item

I'm having an issue with a function that I want to apply to all my dropdown lists. The problem arises when I try to select an option from the second dropdown list - instead of displaying the correct value, it shows the value from the first group in th ...

Flipping a combination of CSS 3 and JavaScript cards will cause them to flip all together in unison

Hello! I came across a really cool card flip code that is IE compatible. I made some modifications to it and got it working, but there's one problem - instead of flipping the selected card, it flips all the cards. I tried modifying the JavaScript code ...

Loss of styling is observed with jQuery's html() function

Here is the HTML code I am working with: <div class="myList"> <select> <option value="1">Item1</option> <option value="2">Item2</option> </select> </div> Everything looks great in terms of CS ...

How to Access a div from another website using jQuery

I have a question. How can I call a div from another website using JavaScript? Here is the page: Now, I want to call the <div id="testa"> in another page. The other page is called Otherpage.html: jQuery(function($){ $(&ap ...

Is it possible to make one <td> tag bold in a table if the <tr> contains two <td> tags?

I need to make the first td tag bold and apply this style to the entire table. <table> <tr> <td><strong>Cell A</strong></td> <td>Cell B</td> </tr> </table> ...

Can you explain the purpose of the spaces on the buttons?

mysterious button spacing issue <div class="buttons"> <button id="btn-search" type="submit">Search Engine</button> <button id="btn-lucky" type="submit">Feeling Lucky Today</button> </div> .buttons { max-width: 29 ...

Tips for reducing the size of input-group-addon in Bootstrap

https://i.sstatic.net/V7ywZ.png <div class="time input-group"> <span class="input-group-addon" id="basic-addon4">Start Time </span> <timepicker [(ngModel)]="mytime" required></timepicker></di ...

Align images of varying sizes vertically within div containers, even when the image is larger than the div itself

I'm facing a challenge when it comes to aligning images vertically inside divs. The problem arises due to the following conditions: The images will have varying and unknown sizes. These images are larger than the divs they are contained in, requiri ...