Displaying two images side by side in HTML using Bootstrap

As a beginner in HTML, I am faced with the challenge of placing two images of different sizes side by side on the same row using HTML and Bootstrap. Below is my current code:

<div class="row">
  <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6">
    <img src="https://x1" alt="logo"
         id="logo"> 
    </div>
    <div class="col-lg-6 col-md-6 col-xs-6 col-sm-6">
    <img src="https://x2" alt="ex" id="img2">
    </div>
</div>

Although they appear side by side, the images are of different sizes. How can I resize them to fit as two square images next to each other? Here is the CSS affecting the images:

img{
      margin-top:25px;
      width:100%;
}

I would greatly appreciate any help I could get!

Answer №1

Here is a CSS solution for making two elements the same size. Simply set the width to 100% and then assign a fixed height to achieve this effect. Take a look at the example in the link below.

Code Example - https://jsfiddle.net/su16pzqc/

img{
      margin-top:25px;
      width:100%;
      height: 400px;
    }

Answer №2

To ensure equal height for elements, consider maintaining the same aspect ratio between their width and height. This way, even if the widths differ, they will at least have uniform heights in a row. Implement the following CSS code to achieve this:

img {
  height:100%;
  width: auto;
}

Keep in mind that the parent element must specify a height for this method to be effective.

Answer №3

Check out this JSFiddle demo I created using your code:

https://jsfiddle.net/onjaL3bu/2/

The HTML structure is as follows:

<div class="row">
  <div class="col-xs-6">
    <img src="http://lorempixel.com/400/200" alt="logo" id="logo"> 
  </div>
  <div class="col-xs-6">
    <img src="http://lorempixel.com/600/400" alt="ex" id="img2">
  </div>
</div>

As for CSS, it's minimal at the moment:

img{

}

No need to overcomplicate things with excessive CSS styling for what you're trying to achieve.

Consider simplifying the classes you use in your code - less is often more effective.

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

Retrieving binary content from an HTML5 Canvas using base64 encoding (readAsBinaryString)

Is it possible to extract binary data from an HTML Canvas? Currently, I have the following HTML code displaying an input file and a canvas below it: <p><button id="myButton" type="button">Get Image Content</button></p> <p>In ...

What is the best way to shorten text in React/CSS or Material UI based on the line height multiples?

I'm facing an issue with creating a Material UI card that contains text. My goal is to set a fixed height for the card and truncate the text if it exceeds 3 lines. Can anyone suggest the best approach to achieve this? Here's the code snippet I&a ...

How to retrieve the index upon clicking in Javascript

In my 3d art gallery project, I am utilizing plain JavaScript. The task at hand involves populating certain columns with images by pulling from an array of image sources. On click, I need to retrieve the index of the clicked image so that I can extract add ...

Issue with Angular JS: Unable to remove uploaded files

I'm currently learning about AngularJS and I've been working on uploading and deleting files. However, I've come across an issue with deleting files. Below is the HTML code: <div> <ul> <li ng-repeat="files in table.proce ...

Switch up the functionality of Bootstrap tooltips: show all the content and only hide it when hovered over

I have a page containing multiple tooltips that I would like to show all at once. All of the tooltips share a class called "tooltipWarn." <span class='warning tooltipWarn'>test</span> Using JQuery: $('.tooltipWarn').toolti ...

How to design a bell curve using CSS styling

Exploring CSS shape design, I am aiming to create a classic bell shape reminiscent of the holiday season. While the top and bottom balls are not essential to my design, here is the basic outline I'm striving for: This is what I have been able to achi ...

Challenges when implementing tooltip in bootstrap using Angular and TypeScript

Looking to enhance my website with a tooltip using Bootstrap in Angular. Reference link: https://getbootstrap.com/docs/4.0/components/tooltips/ The issue at hand: Current tooltip appearance: https://i.sstatic.net/MJlCg.png Desired tooltip outcome: ht ...

Communication between Laravel and controller using AJAX for exchanging information

I have a specific AJAX function being called from a view: function gatherProductData() { var productIds = []; $('#compare-widget tbody tr').each(function(i, ele) { productIds[i] = $(ele).data('product-id'); }); ...

The userscript will keep the window open as long as it remains inactive

Hello everyone, I'm excited to join the StackOverflow community and dive into userscripts for the first time! Putting aside any unnecessary details, I'm encountering a small issue with a script I recently created. (function () { $("#enbut"). ...

What is the best way to format dates in jQuery AJAX requests?

Utilizing jquery to interact with a REST API in (ASP.net Web API). I've constructed a basic object and then utilized jquery to 'PUT' it on the server: var myObject = { name: 'just a name', createDate: new Date() } $.ajax({ ...

Inject information into an HTML element

Currently, I am fetching data from MySQL and the task at hand is to display the result in a specific class. To achieve this, I have employed jQuery to update every 10 seconds successfully. However, the challenge lies in transferring that data into the desi ...

Using jQuery UI to make elements sortable and draggable, while also retrieving the index of the dragged element

I encountered a specific issue while using jQuery UI sortable and draggable simultaneously. My goal is to determine the index or placement of the newly dragged element within the list. I am able to accomplish this when rearranging elements within the list. ...

Is it possible to modify the font color of a specific MenuItem in material-ui?

I've scoured every corner of the internet, but I'm still stumped by this question: How can I tweak the text color (not the background) for a selected or hovered MenuItem? I know the solution lies within useStyles, but my attempts thus far have be ...

Is there a way to shade the entire webpage background in HTML?

Instead of affecting other DOM classes, I tried using another div but it doesn't darken the whole page. This means I have to manually modify each DOM class. Is there a way to overlay the entire document with a semi-transparent gray plane? ...

Understanding the relationship between controller and view in AngularJS is crucial for building dynamic

My AngularJS script with MVC architecture is causing some issues. I suspect that there might be errors in the connection between the model, view, and controller layers. JS: var myApp = angular.module("myApp", []); //App.js myApp.controller('MainCon ...

Troublesome Suckerfish CSS Navigation Menus failing to function properly on IE7

Feeling completely exasperated, I am encountering issues with aligning the drop downs under the parent item in IE7. Despite functioning properly in all other browsers, including IE8, the drop down menu is not lining up correctly in IE7. In IE7, the drop d ...

The function is not defined, even though it is located within the same file

I've encountered an issue with my web page where I'm trying to read data from an HTML data table and update it on a PHP file using AJAX queries in JavaScript. Everything was working fine until recently, even though I didn't make any signific ...

Learn the method to duplicate Outer HTML with the use of jQuery or Pure JavaScript

I have successfully copied the value of an input into the clipboard using the first part of the solution provided. However, I am now trying to figure out how to copy HTML content like an entire <p> tag. Currently, when attempting this, I am encounter ...

Having trouble getting CSS Animation to work in a React project?

I am currently developing a Next.js application using React. const partyButton = useRef(null) const handleParty = () => { setTimeout(() => { partyButton.current.classList.add('party-time'); console.log(party ...

Transforming nested tables into JSON format

I have a table that is dynamically generated and I am looking to convert it into JSON format. Despite trying several methods, I haven't been successful. Can someone help me by providing guidance on how to achieve this using jQuery? For your reference ...