What is the best way to increase the size of an image within a card and minimize the amount of white space

I have a card featuring an image, accompanied by a banner placed next to it. To structure this layout, I am using col-lg-8 for the card and col-lg-4 for the banner. However, I'm facing an issue with excessive white space between the card (which has a padding-right: 20px) and the banner, as depicted in this screenshot:https://i.sstatic.net/PyyJj.png

The desired outcome is to eliminate the extra whitespace and maintain only the padding-right: 20px (or margin-right) between the card and the banner. Essentially, extending the width of the image up to the example red line while keeping the orange banner size fixed without resizing it. Consequently, creating an enlarged card with some white space (20 px) between the card and the banner followed by the fixed banner. The sole "empty whitespace" should be the 20px gap between the card and the banner in alignment with the Bootstrap grid of col-lg-8 and col-lg-4.

How can I enlarge the card without altering col-lg-8 and col-lg-4? (I also attempted col-lg-9, but found the card to be excessively large).

My attempts at solutions (seemingly correct, but problematic):

  • WIDTH IMAGE 105%: Trying a 105% width for the card initially seemed successful; however, upon resizing the window, the card merged with or overlapped the banner, losing the right padding.

  • WIDTH COLUMNS 75% and 25%: Adjusting the column widths involved changing .col-lg-8 from width: 66.66666667% to 75% and .col-lg-4 from width: 25% to achieve a total of 100%. While this seemed effective, feedback cautioned against modifying Bootstrap's column widths citing unprofessional practices.

  • REMOVE TEXT-ALIGN: RIGHT: Removing .banner {text-align: right;} prompted the banner to approach the card, but resulted in empty space on the right of the banner. Uncertain whether this aligns with the solution. Refer to another screenshot

Here is the code snippet on CodePenthe code on CodePen for further testing. Requesting guidance on the optimal solution to address my query above. Thank you all in advance!

@media (min-width: 992px) {
  .container-pc {
    display: flex;
    max-width: 1080px;
    margin-left: auto;
    margin-right: auto;
  }
}

@media (min-width: 992px) {
  .container-primapagina {
    padding-right: 20px;
  }
}

.banner {
  text-align: right;
}

.height1 {
  height: 450px;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e0828f8f949394928190a0d5ced3ced0">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
  <title>Bootstrap Example</title>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385a57574c4b4c4a5948780d160b1608">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

  <link href="style.css" rel="stylesheet">

</head>

<body>

  <div class="container-pc">

    <div class="container-primapagina col-12 col-md-8 col-lg-8">
      <div class="card text-bg-dark">
        <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="card-img height1" alt="...">
        <div class="card-img-overlay">
          <h5 class="card-title">Card title</h5>
        </div>
      </div>
    </div>

    <div class="banner col-lg-4">
      <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
    </div>
  </div>

  <div class="container-pc">
    <div class="col-lg-8" style="background-color: aqua;" ;>Col 8 (Utility only for measuring)</div>
    <div class="col-lg-4" style="background-color: rgb(81, 255, 0);" ;>Col 4 (Utility only for measuring)</div>
  </div>

</body>

</html>

Answer №1

Upon understanding your intentions, it seems that you desire the banner on the right side to maintain a fixed width while the left card occupies the remaining space. To achieve this layout, flexbox should be used instead of columns. As the container container-pc is already a flex container, the column classes can be removed. Columns are designed for fixed proportions and do not allow flexibility or overlapping. Therefore, utilizing flex properties is more appropriate.

In the case where you adjust the ratio from 8:4 to 9:3, the banner may overflow due to mismatches in sizing against the column structure.

I advise referring to the Bootstrap documentation to leverage Bootstrap effectively without redundant stylesheets and JavaScript files. Additionally, ensuring responsiveness on narrow screens involves using relative sizes for images rather than fixed dimensions.


The examples below demonstrate:

  • Utilizing a 9:3 ratio for columns
  • Omitting column classes in favor of flexbox within the container
  • Applying Bootstrap's d-flex class to the container
  • Adding d-md-flex class at the medium breakpoint along with object-fit-cover property for background image in the card div
    • (d-md-flex applies display: flex starting from the medium breakpoint)
    • (object-fit-cover maintains image aspect ratio by clipping edges instead of distorting the image as container shrinks)

@media (min-width: 992px) {
  .container-pc {
    display: flex;
    max-width: 1080px;
    margin-left: auto;
    margin-right: auto;
  }
}

@media (min-width: 992px) {
  .container-primapagina {
    padding-right: 20px;
  }
}

.banner {
  text-align: right;
}

.height1 {
  height: 450px;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8bab7b7acabacaab9a898edf6ebf6e8">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
  <title>Bootstrap Example</title>


</head>

<body>

<p class="text-center mt-4 mb-1">>d-md-flex &amp; object-fit-cover classes</p>
  <div class="container-pc d-md-flex">
    <div class="container-primapagina">
      <div class="card text-bg-dark">
        <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="card-img height1 object-fit-cover" alt="...">
        <div class="card-img-overlay">
          <h5 class="card-title">Card title</h5>
        </div>
      </div>
    </div>
    <div class="banner">
      <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
    </div>
  </div>

<p class="text-center mt-4 mb-1">>d-flex class<&/lt;&p>
  <div class="container-pc d-flex">
   <div class="container-primapagina">
     <div class="card text-bg-dark">
       <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="card-img height1" alt="...">
       <div class="card-img-overlay">
         <h5 class="card-title">Card title</h5>
      </div>
     </div>
   </div>
   <div class="banner">>
      <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
   </div>
  </div>

 <p class="text-center mt-4 mb-1">Without column classes</p>
    <div class="container-pc">>
      <div class="container-primapagina">>
        <div class="card text-bg-dark">
         <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="card-img height1" alt="...">>
           <div class="card-img-overlay">>
              <h5 class="card-title">>Card title</pre>
      		</div>
      	</div>
		<div class="banner">
			<img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
		</div>
	</div>
</div>

<p class="text-center mt-4 mb-1">Columns 9:3</p>
<div class="container-pc">
	<div class="container-primapagina col-12 col-md-9 col-lg-9">
		<div class="card text-bg-dark">
			<img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="card-img height1" alt="...">
				<div class="card-img-overlay">
					<h5 class="card-title">Card title</h5>
				</div>
		</div>
	</div>
	<div class="banner col-lg-3">
		<img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
	</div>
</div>

<div class="container-pc">
	<div class="col-lg-8" style="background-color: aqua;">Col 8 (Utility only for measuring)</div>
	<div class="col-lg-4" style="background-color: rgb(81, 255, 0);">Col 4 (Utility only for measuring)</div>
</div>

</body>

</html>

Answer №2

Based on the code snippet and accompanying comments, it appears that this solution aligns with your needs. By setting the image to cover in the background of the card, it creates a cleaner look that complements the grid columns.

@media (min-width: 992px) {
  .container-pc {
    display: flex;
    max-width: 1080px;
    margin-left: auto;
    margin-right: auto;
  }
}

.image-bground {
  background-image: url("https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg");
  background-attachment: fixed;
  margin-right:20px;
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71131e1e05020503100131445f435f42">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-pc">

  <div class="container-primapagina col-12 col-md-8 col-lg-8 image-bground">
    <div class="card text-bg-dark">
      <div class="card-img-overlay">
        <h5 class="card-title">Card title</h5>
      </div>
    </div>
  </div>

  <div class="banner col-lg-4">
    <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
  </div>
</div>

<div class="container-pc">
  <div class="col-lg-8" style="background-color: aqua;" ;>Col 8 (Utility only for measuring)</div>
  <div class="col-lg-4" style="background-color: rgb(81, 255, 0);" ;>Col 4 (Utility only for measuring)</div>
</div>

Answer №3

Consider utilizing col and col-auto classes for the division. Refer to the code snippet below

@media (min-width: 992px) {
  .container-pc {
    display: flex;
    max-width: 1080px;
    margin-left: auto;
    margin-right: auto;
  }
}

@media (min-width: 992px) {
  .container-primapagina {
    padding-right: 20px;
  }
}

.banner {
  text-align: right;
}

.height1 {
  height: 450px;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1>
  <link href=""https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="20424f4f54535452415060150e130e10">[email protected]</a>" rel="stylesheet>
  <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet>
  <title>Bootstrap Example</title>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="73111c1c07000701120333465d405d43">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

  <link href="style.css" rel="stylesheet>

</head>

<body>

  <div class="container-pc">

    <div class="container-primapagina col">
      <div class="card text-bg-dark">
        <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="card-img height1" alt="...">
        <div class="card-img-overlay">
          <h5 class="card-title">Card title</h5>
        </div>
      </div>
    </div>

    <div class="banner col-12 col-md-auto">
      <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
    </div>
  </div>

  <div class="container-pc">
    <div class="col" style="background-color: aqua;" ;>Col (Utility only for measuring)</div>
    <div class="col-12 col-md-auto" style="background-color: rgb(81, 255, 0);" ;>Col-md-auto (Utility only for measuring) [will take 100% width for devices smaller than `md-width(&lt;740px)`]
    </div>

</body>

</html>

Answer №4

@media (min-width: 992px) {
  .container-pc {
    display: flex;
    max-width: 1080px;
    margin-left: auto;
    margin-right: auto;
  }
}

@media (min-width: 992px) {
  .container-primapagina {
    padding-right: 20px;
  }
}



.height1 {
  height: 450px;
}
<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e6c61617a7d7a7c"><i>example</i></a>/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
  <title>Bootstrap Example</title>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f99b96968d8a8d8b88"><i>example</i></a>/dist/js/bootstrap.bundle.min.js"></script>

  <link href="style.css" rel="stylesheet">

</head>

<body>

  <div class="container-pc">

    <div class="container-primapagina col-12 col-md-8 col-lg-8">
      <div class="card text-bg-dark">
        <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="card-img height1" alt="...">
        <div class="card-img-overlay">
          <h5 class="card-title">Card title</h5>
        </div>
      </div>
    </div>

    <div class="banner col-lg-4">
      <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner">
    </div>
  </div>

  <div class="container-pc">
    <div class="col-lg-8" style="background-color: aqua;" ;>Col 8 (Utility only for measuring)</div>
    <div class="col-lg-4" style="background-color: rgb(81, 255, 0);" ;>Col 4 (Utility only for measuring)</div>
  </div>

</body>

</html>

Answer №5

VIEW IN FULL SCREEN FOR OUTPUT

  • Honestly, your goal can be achieved using bootstrap alone.

  • Remember that bootstrap operates on a 12-grid system.

  • You are directly utilizing col-, it's recommended to adhere to the following code structure.

<div class="row">
   <div class="col"></div>
   <div class="col"></div>
</div>
  • You'll notice in my code, I do not use any additional CSS. If you need to adjust the container width, modify it within the .container class or swap it with a new class.

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c2a0adadb6b1b6b0a3b282f7ecf1ecf2">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://getbootstrap.com/docs/5.3/assets/css/docs.css" rel="stylesheet">
  <title>Bootstrap Example</title>
  <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="65070a0a11161117041525504b564b55">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

  <link href="style.css" rel="stylesheet">

</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-md-9 ps-0">
        <div class="card text-bg-dark">
          <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" class="img-fluid" alt="...">
          <div class="card-img-overlay">
            <h5 class="card-title">Card title</h5>
          </div>
        </div>
      </div>
      <div class="col-md-3 pe-0">
        <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner" class="img-fluid h-100">
      </div>
    </div>
    <div class="row">
      <div class="col-md-9" style="background-color: aqua;" ;="">Col 9 (Utility only for measuring)</div>
      <div class="col-md-3" style="background-color: rgb(81, 255, 0);" ;="">Col 3 (Utility only for measuring)</div>
    </div>
  </div>
</body>

</html>

PURE HTML CSS METHOD

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  max-width: 1140px;
  margin-inline: auto;
}

.card {
  display: flex;
  gap: 15px;
}

.card .card-left {
  position: relative;
}

.card .card-left .card-title {
  position: absolute;
  top: 15px;
  left: 15px;
  color: white;
  font-size: 32px;
}

.card .card-left img {
  height: 453px;
  width: 100%;
  object-fit: cover;
}
<body>
  <div class="container">
    <div class="card">
      <div class="card-left">
        <img src="https://siviaggia.it/wp-content/uploads/sites/2/2021/02/grattacieli-new-york.jpg" alt="...">
        <h5 class="card-title">Card title</h5>
      </div>
      <div class="card-right">
        <img src="https://i.ibb.co/tmCY8gW/ffffffffffff.png" alt="provabanner" class="img-fluid h-100">
      </div>
    </div>
  </div>
</body>

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

Simulating a mobile device screen size using an embedded iframe

Imagine this scenario: What if instead of adjusting the browser window size to showcase a responsive web design, we could load the site into an IFRAME with the dimensions of a mobile screen. Could this concept actually work? Picture having an IFRAME like ...

Deploying static files with Django in a production environment

My Django application is functioning properly on Ubuntu 14.04 with nginx 1.10, Django 1.10.2, and uWSGI 2.0.14. It is able to load static files such as JavaScript, CSS, and images, but the CSS files are not being applied to my website. Below is the configu ...

How can I showcase the Junit HTML report in Karate DSL, as outlined in the official Karate documentation?

After following the Karate Doc and pasting the Junit HTML Report into my browser, I was surprised to see that the output was in plain text and did not look anything like what was described in the tutorial video. I attempted to view it on both Firefox 57.0. ...

Struggling to synchronize animation timing between elements using jquery

When you navigate to an album (for example, Nature because I'm still working on the others) and select one of the images, they all gradually fade out while the selected image appears on the screen. What's actually happening is that the selected i ...

Struggling to eliminate the underlines from an HTML hyperlink

Can someone help me figure out how to remove the annoying underline link that pops up when hovering over an a element in HTML? I attempted adding a style="text-decoration: none" attribute to the a link, but the underline persists when I hover over the text ...

Is it possible for me to traverse a CSS stylesheet using code?

Exploring the depths of jQuery, one can effortlessly traverse the DOM. But what if we could also navigate through a stylesheet, accessing and modifying attributes for the specified styles? Sample Stylesheet div { background: #FF0000; display: blo ...

How to align a div in the center while another div is floated to the right?

Shown below is an example: <div id="mainContainer"> <div id="itemIWantToCenter"></div> <div id="itemIwantFloatedRight"></div> </div> The mainContainerwidth width is set to 100%. The itemIwantFloatedRight widt ...

Issue with flex-grow property not functioning as expected in Internet Explorer 11

Hello! I'm running into an issue with flexbox in Internet Explorer: http://jsfiddle.net/EvvBH/ I've noticed that the #two element has flex: auto, which is meant to make it expand to fill the container, even if there's limited content. Ho ...

The issue with Angular 2's Parameterised router link component not fully refreshing

I'm trying to figure out how to show a set of images when I click on a specific menu item. The menu structure looks like this: <ul id="demo23" class="collapse"> <li> <a [routerLink]="['image-gallery','Picasso ...

Incorporate a platform-specific button in a user interface with React Native

I need to display a button that is only shown on iOS, not on android. I have tried using the following style, but it doesn't seem to be working. ...Platform.select({ android: { display:'none', }, The code for my button is: <Bu ...

Modifying the image height in a column using Bootstrap and JSON data

My webpage is dynamically generating images from a JSON file through a JavaScript file. However, the images are displaying at different heights, and I want each column to adjust to the height of the image to eliminate any gaps. Particularly, data with the ...

Is it usual for the container to overflow with div content?

Is this common CSS behavior? How can we make the container wrap around the button? http://jsfiddle.net/afrontrow/yepw7oLw/ CSS: .button { background: grey; padding: 10px 20px; } .container { border: 1px solid black; } HTML: <div class= ...

The AngularJS change event is not being activated

I am a beginner with angular js and I have implemented a bootstrap calendar in my application. However, I am facing an issue where the change event is not being triggered when the month changes, no matter where I place it within the code. Here is the snip ...

Utilizing data attributes over classes for styling in CSS

Programming Languages <span data-bar> ... </span> JavaScript span[data-bar]{ ... } Do you agree with this coding practice? Are there any potential pitfalls? I believe implementing the data- attribute is beneficial when dealing with a large ...

Difficulty in retrieving list elements from Flask template variables in JavaScript

Within my JavaScript code, I have a function: function functioncalled(){ var result = 1; var activityID = {{ mynames[result][8] }}; console.log(activityID); } The variable mynames represents a list of lists in my Flask template. However, the code above ...

Access Information within live tabs

When attempting to click on any tabs in order to retrieve their respective data, I am successfully able to do so. However, I'm only able to retrieve the data of one row at a time. Can anyone provide assistance with this issue? <div class="col-lg-1 ...

Does anyone know of a tool that allows you to save HTML as a standalone page?

Looking for a solution to easily send standalone .html files with no external dependencies to clients on a regular basis. The original pages are built using node.js and express, and feature libraries like High Charts. Up until now, I have been manually pre ...

Tips for maintaining the border radius of a container while changing the background color on hover

I am encountering an issue with a drop-down list where the bottom corners have a border-radius applied, and I would like to change the hover color of the li items. However, when hovering over the last item, the background-color covers up the border radiu ...

The issue of undefined database columns arises when attempting to transmit data from an HTML form to a MySQL database via Express

My primary objective is to develop a RestAPI using Node.js and test it in a small HTML application. With the guidance of my instructor, I successfully created the RestAPI based on an example and customized it to work with my own MySQL database. Testing ea ...

Disabling the .hover function temporarily

I am currently exploring how to temporarily disable the .hover function in jQuery based on a specific event that occurs on the page. You can view my current jsfiddle here: http://jsfiddle.net/4vhajam3/3/ (please note that I have omitted some code for simp ...