Ways to ensure a div matches the size of the div above it within a Boostrap grid column

Hello there, this is my very first question on this platform. I'm still in the early stages of learning all these concepts, so please be patient with me 🙂

Currently, I am working on creating a practice page using only HTML, Sass, and bootstrap-grid.css. Specifically, I'm focusing on utilizing rows and columns and have excluded card classes from the file as I styled them separately. However, I've come across a seemingly simple issue that's been keeping me up at night.

I am trying to ensure that all .card-text divs (the white background ones) visually match the width of the images above them. Simply resizing or absolutely positioning the .card-text divs doesn't seem like the right solution as it may lead to other issues. Additionally, I want to maintain the use of .col- for easier responsive web design outcomes. The .card-text and .card-img divs inherit the width of the .col-, while also ensuring that the images maintain their correct proportions. The problem seems to lie mainly within the .card-text div (at least visually).

The column structure within a .row is structured as follows:

      <div class="col-md-4">
        <div class="card">
          <div class="card-img">
            <img src="images/r1i1.jpg" alt="image">
          </div>
          <div class="card-text">
            <h6>Ebony & Ivory</h6>
            <h5>Branding</h5>
          </div>
        </div>
      </div>

The styling for this section in SCSS format is outlined below:

    .portfolio {
  background-color: $color-background;
  .card {
    background-color: $color-background;
    padding: 20px;
    .card-img {
      height: 300px;
      margin: 0 44px;
      overflow: hidden;
      img {
      vertical-align: bottom;
      height: 100%;
      }
    }
    .card-text {
      background-color: $color-body;
      padding-top: 29px;
      padding-bottom: 27px;
    }
    h5 {
      margin: 2px 0 0 0;
      font-weight: lighter;
      font-family: $font-decor;
      color: #737373;
      font-size: 14px;
    }
    h6 {
      font-family: $font-text;
      font-weight: bold;
      font-size: 18px;
      margin: 0;
      color: #333333;
    }
  }
  .col {
    padding-top: 45px;
  }
}

Screenshots displaying the inspection of the divs can be viewed here:

.card

.card-text

EDIT:

After some trial and error, I was able to find SCSS styles that worked effectively. Here they are:

    .portfolio {
  background-color: $color-background;
  .card {
    background-color: $color-background;
    margin: 0 44px 50px 44px;
    .card-img {
      overflow: hidden;
      img {
      vertical-align: bottom;
      width: 100%;
      }
    }
    .card-text {
      background-color: $color-body;
      padding-top: 29px;
      padding-bottom: 27px;
      h5 {
        margin: 2px 0 0 0;
        font-weight: lighter;
        font-family: $font-decor;
        color: #737373;
        font-size: 14px;
      }
      h6 {
        font-family: $font-text;
        font-weight: bold;
        font-size: 18px;
        margin: 0;
        color: #333333;
      }
    }  
  }
  .col {
    padding-top: 45px;
  }
}

In conclusion, the root cause of the issue was setting a fixed height to .card-img, resulting in unforeseen challenges.

Answer â„–1

To achieve equal width for both the .card-img and .card-text elements within their parent div, simply apply margin: 0 44px to the .card-text element along with width: 100% to both elements.

For a practical demonstration of this concept, refer to the following Code Snippet:

html, body {margin: 0px; padding: 0; width: 100%; height: 100%;}
.portfolio {
  background-color: #222;
}
.portfolio .card {
  background-color: #222;
  padding: 20px;
}
.portfolio .card .card-img {
  height: 300px;
  margin: 0 44px;
  overflow: hidden;
}
.portfolio .card .card-img img {
  vertical-align: bottom;
  height: 100%;
  width: 100%;
}
.portfolio .card .card-text {
  background-color: #333;
  padding-top: 29px;
  padding-bottom: 27px;
  width: 100%;
  margin: 0 44px;
}
.portfolio .card h5 {
  margin: 2px 0 0 0;
  font-weight: lighter;
  color: #737373;
  font-size: 14px;
}
.portfolio .card h6 {
  font-weight: bold;
  font-size: 18px;
  margin: 0;
  color: #333333;
}
.portfolio .col {
  padding-top: 45px;
}
<script src="https://code.jquery.com/jquery-3.4.1.slim.js"></script><link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.css" /><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.js"></script>

<section class="portfolio">
  <div class="col-md-4">
    <div class="card">
      <div class="card-img">
        <img src="https://picsum.photos/240/360" alt="image">
      </div>
      <div class="card-text">
        <h6>Ebony & Ivory</h6>
        <h5>Branding</h5>
      </div>
    </div>
  </div>
</section>

Answer â„–2

  • Adjust your image width to 100% instead of setting the height.
  • Eliminate padding in .card
  • Delete the margin from .card-img

Hopefully that solution works for you!

Answer â„–3

To enhance the appearance of card-img, it would be advisable to specify a width and then relocate .card-text div inside card-img. Additionally, consider adjusting the CSS styles for card-img.

The code has been refined and included in the snippet below.

Please take a moment to review it.

.card {
  background-color: #fff;
  padding: 20px;
}

.card-img {
    width: 150px;
    margin: 0 44px;
    overflow: hidden;
}

img {
    width: 100%;
}

.card-text {
    position: relative;
    text-align:center;
    top: -4px;
    background-color: aliceblue;
    padding-top: 29px;
    padding-bottom: 27px;
}

h5 {
    margin: 2px 0 0 0;
    font-weight: lighter;
    color: #737373;
    font-size: 14px;
}

h6 {
    font-weight: bold;
    font-size: 18px;
    margin: 0;
    color: #333333;
}

.col {
    padding-top: 45px;
}
<div class="col-md-4">
    <div class="card">
        <div class="card-img">
            <img src="https://p1.pxfuel.com/preview/844/972/952/suit-model-businessman-corporate-royalty-free-thumbnail.jpg" alt="image">
            <div class="card-text">
                <h6>Ebony & Ivory</h6>
                <h5>Branding</h5>
            </div>
        </div>
        
    </div>
</div>

Image Source: Google

Hopefully this adjustment is beneficial!

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

Guide on executing edit form alongside file upload

Is there a way to update a form without having to upload a new image? I have successfully implemented multer for image uploads when creating a form. My frontend is built using ReactJS and the backend server side is Node.js. This is the front end code usi ...

Tips for maintaining table elements in place using JavaScript

My goal is to keep the tags within the table fixed when printing. The code I've written functions correctly in View mode, however, when printed using JavaScript, if one of the columns is too large, the rest of the columns get displaced. I want all col ...

Display the final row by hovering over the line

<table> <thead> <tr> <th>First</th> <th>Second</th> <th></th> </tr> </thead> <tbody> <tr> <td>Data1</td> <td>Dat ...

Personalize the file button for uploading images

I have a button to upload image files and I want to customize it to allow for uploading more than one image file. What is the logic to achieve this? <input type="file" />, which renders a choose button with text that says `no files chosen` in the sa ...

jQuery UI - Add character right before the final element

My slider is set to range from 10 to 1000 with increments of 20 (10-30-50, etc). I'm looking to add a character before the value on the last step (1000). Currently, it shows values like "560 kr/h" or "400 kr/h", but I want it to display ">1000 kr ...

Having difficulty adding a Dropdown Menu in a Pop-up Box

I am trying to incorporate a select menu inside of a popover, but every time I attempt to do so, the popover content block remains blank. The popover is associated with a side menu item called "Date History". Below is the code snippet I am using to constr ...

Customizing jQuery dialog: What is the best way to change the appearance of the close button?

I am struggling to style the close tag in my jQuery dialog box. I have looked through the documentation and tried various CSS alterations, but nothing seems to be working. Any suggestions or insights would be greatly appreciated. My code is provided below ...

Tips on formatting vueJS HTML5 with tidy without compromising its structure or content

Using the tidy tool on this VueJS code causes it to completely break: Here is the initial VueJS HTML code: $ cat sample_vue.html ... <tbody> <tr v-for="task in tasks"> <td><strong>{{task.title}}< ...

Having trouble with sending information to the PHP server, unable to determine the root cause

Can someone help me figure out why the data from a form being sent to a php script for sending an email isn't working correctly? It's part of a template code but I suspect there might be an error. Specifically, I believe the {'userName' ...

A date picker pops up when the user clicks on an Angular input field of type text

I am looking to edit a dateTime value using Angular/Ionic, but unfortunately there is no dateTime picker available. To work around this issue, I have split the field into separate date and time fields. In order to incorporate placeholder text and format th ...

Ways to display a Tooltip automatically without needing to hover using only CSS

I have a tooltip that is set to appear when we hover over the div element, Is there a way to display the tooltip automatically when the page is refreshed without using Javascript? I am looking for a solution in pure CSS only. I attempted to remove displa ...

How do I use PHP to dynamically generate lists in HTML with the <li> tag?

Is there a way to repeat the <li> element in this code snippet? I have been attempting to create a loop for the "<li>" block, but it keeps showing me an error. <?php $html .= " <li> <a href=''> <span& ...

The HTML slideshow is not automatically showing up as intended

I need to make a few adjustments to the picture slideshow on my website. Currently, all the pictures are displayed at once when you first visit the site, and it only turns into a slideshow when you click the scroll arrows. I want it to start as a slideshow ...

CSS allows for the creation of fixed bars that remain at the top of the

I am looking to create a fixed bar that is off-center with a scrolling background. The code I have so far either places the bar on the surface but fixes it to the background, or it positions the bar beneath the image and fixes it to the window - neither ...

Generating DOM elements at specific intervals using Jquery

I am looking to dynamically create 1 div container every x seconds, and repeat this process n times. To achieve this, I have started with the following code: $(document).ready(function() { for (var i = 0; i < 5; i++) { createEle(i); } }); f ...

Issue with jQuery dropdown navigation bar

I am interested in creating a drop-down menu using jquery (here's the corresponding jsFiddle). HTML: <ul id="mainmenu"> <li><a href="http://myszki/">Aktualności</a></li> <li class="parent item13"><a href=" ...

Carousel-item height in Bootstrap 4.6

I am facing an issue with a component in Angular 14 using Bootstrap v4.6: <div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel"> <div class="carousel-inner"> <div cl ...

Using Python's Beautiful Soup library to retrieve text from h1 and id tags

I've been attempting to retrieve the text from an HTML id called "itemSummaryPrice," but I'm having trouble figuring it out. html = "" <div id="itemSummaryContainer" class="content"> <div id=&quo ...

Multiple images overlapping each other in a dynamic parallax effect

Despite my fear of receiving down votes, I have been unsuccessful in finding the answer to my question so I will proceed with asking it. I am attempting to replicate a parallax effect similar to the one showcased on this website, particularly the image of ...

I am encountering issues with an HTML table that is in need of fixing, and I must find a solution without relying on

Can someone help me troubleshoot the structure of this table in the given image? It seems like my rowspan and colspan attributes are not working as expected. I've only used CSS to specify the width and height of the cells. https://i.sstatic.net/VtHbH ...