Guide to aligning an image in the center of varying heights and widths with the use of HTML and CSS

I am trying to achieve a layout where all the images are centered, with five images in the first row and the rest in the second row, also centered. Below is the code I have attempted:

<section id="Section">
    <h1 class="sectionTitle text-center">Images</h1>
    <div class="row center" id="Logo">
      <!-- All logo from firestore -->
    </div>
</section>

For fetching data from Firestore, here is my JavaScript code:

db.collection('images').orderBy('image').onSnapshot((snapshot) => {
    //insertHtml("#main-content", response);
    snapshot.docs.forEach(doc => {

       var brand = '<div class="column">'
                       + '<img src="images/'+ doc.data().image + '"></div>';
       $("#Logo").append(brand);
        
    });
});

And here's my CSS code for styling:

#Section .center {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 80%;
}

#Section .row .column {
  float: left;
  width: 20%;
}

#Section .row .column img{
  max-height: 115px;
  max-width: 210px;
}

Currently, the first row displays correctly with 5 images, but the second row is not centered. How can I resolve this issue? https://i.sstatic.net/GjXYZ.jpg

Answer №1

To create a responsive layout, utilize the display flex property and adjust the flex-basis according to the number of elements in the container:

#Section .center {
  display: flex;
  margin-left: auto;
  margin-right: auto;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
  column-gap: 15px;
  row-gap: 15px;
  width: 80%;
}
.logos {
  text-align: center;
  flex-basis: calc(20% - 15px);
  background-color: red;
}
<section id="Section">
    <h1 class="sectionTitle text-center">Images</h1>
    <div class="row center" id="Logo">
      <div class="logos"> 1 </div>
      <div class="logos"> 2 </div>
      <div class="logos"> 3 </div>
      <div class="logos"> 4 </div>
      <div class="logos"> 5 </div>
      <div class="logos"> 6 </div>
      <div class="logos"> 7 </div>
      <div class="logos"> 8 </div>
      <div class="logos"> 9 </div>
    </div>
</section>

Answer №2

You have the option to utilize

For arranging all elements in a horizontal line

<div class="row justify-content-center" id="Logo">
<div class="col-md-*"> 1 logo </div> 
<div class="col-md-*"> 2 logo </div> 
<div class="col-md-*"> 3 logo </div> 
<div class="col-md-*"> 4 logo </div> 
<div class="col-md-*"> 5 logo </div> 
</div>

<div class="row justify-content-center" id="Logo">
<div class="col-md-4"> 1 logo </div> 
<div class="col-md-4"> 2 logo </div>
<div class="col-md-4"> 3 logo </div>
</div>
 

Answer №3

Is this the information you are seeking?

[example

.wrapper {
  display: flex;
  flex: 1;
  flex-wrap: wrap;
}

.item {
  width: 200px;
  height: 50px;
  background: pink;
  margin: 20px;
  display: flex;
  justify-content: center;
}

img {
  width: 40px;
  background: teal;
}
<div class="wrapper">
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
  <div class="item">
    <img src="" alt="1" />
  </div>
</div>

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

How to apply jQuery addClass to only the following target element and not all elements on the entire webpage

Currently, I am using jQuery to create an animation effect on a hidden element (which is an image description) so that it becomes visible when hovering over an image. Although the code snippet I found does work, it has a downside - all the hidden image de ...

Incorporating unique HTML5/iframe widgets into your Facebook Timeline and Tab Pages

I have a unique widget that users can easily integrate into their websites using the HTML/iframe code generated by my app. Now, I am looking for a way to allow users to also add this widget to their Facebook Company Pages. The widgets are accessible th ...

What is the best way to create a shape using CSS?

In my chat application, I have a functionality where replies on a chat are initially hidden and represented by a count in a red box. When the user clicks on the red button, the replies from other users are revealed along with an input box for writing a r ...

Modifying the HTML5 data attribute according to the URL hash

Within my div element, I have this code: <div id="nav-aj" data-options='{"default":'t1","animation": {"duration": 500, "effects": "fade"}}'> </div> I am looking to dynamically adjust the value of the default option based on th ...

The carousel top position in Bootstrap 4 is not functioning properly

I am struggling to get the teal rectangle to move to the top of the carousel. I have set my carousel to have a position relative and my teal rectangle to have a position absolute with top="0", but it doesn't seem to be working properly. Instead of mov ...

Effortlessly apply CSS styles to multiple cached jQuery variables without redundancy

Hey there, I've got this piece of code written in Javascript for a classic ASP page. Since there are no CSS classes defined, everything is handled within the Javascript itself. var $a= $('#a'), $v= $('#v'), $s= $('#s'), ...

Adjusting the appearance of dropdown option borders in HTML/CSS - customize or eliminate borders as

select { width: 300px; font-family: inherit; font-size: 18px; padding-top: 10px; padding-bottom: 10px; padding-right: 30px; display:block; color: #999; border: none; border-bottom: 1px solid #757575; } *:focus { outline: #757575; } ...

"Making changes to the HTML data attribute as desired, however, the updates are not reflecting on

I have some HTML that includes an input field like this: <input type="text" id="query" data-source="whatever"> Recently, I wrote some jQuery code to modify the value of the data attribute from "whatever" to "test". Here is the snippet: $(function( ...

Incorporating PlayN games into an HTML/JS application

I've been considering creating a game using PlayN. While it's great for the game itself, I would rather handle menus, navigation, high-score lists, etc in HTML (possibly with GWT). If I develop a game using PlayN, are there ways to integrate it ...

Utilize HTML5 Application Cache solely for storing and managing dependencies

My goal is to selectively cache certain files like JavaScript, CSS, fonts, and image sprites. Should I create a manifest file for these specific files or rely on the browser's caching mechanism? If using a manifest is preferred, can I still prevent ...

What is the best way to ensure bidirectional text appears correctly when two conflicting languages are combined, ensuring explicit directionality is set?

As I work on localization implementation, I encounter an issue with the directionality of mixed characters on the page. The text content is stored in a json file and inserted into the DOM using a Vue.js template. While individual characters display corre ...

How can jQuery be used to adjust the width and height of an iframe?

After researching a solution on a website, I attempted to implement it but encountered issues. It seems like there may be another script dictating the styling, so I need to add some code directly to the page to override it. This is what I tried: <scri ...

Is it possible to bind HTML within an AngularJS partial but encountering an inexplicable issue?

I'm facing an issue with my ng-repeat that involves using a partial HTML file to loop through and display items on a page. The variables within the partial are not displaying, but interestingly enough, the variable {{rule.name}} outside of the partial ...

I'm having trouble adding two Owl carousels in two separate rows

I am trying to incorporate two owl-carousel sliders on a single HTML page. For example, I want owl-carousel1 in row 1 and owl-carosoul2 in row 2. <div class="item"> <!-- Item Image --> <div cl ...

Slideshow plugin glitch on Safari and Opera caused by jQuery implementation

My current project involves utilizing a fantastic plugin called Sequence.js for creating animations and transitions with CSS3 based on jQuery. I have set up a simple responsive fadeIn-fadeOut slideshow using this plugin. While everything works smoothly an ...

There seems to be an issue with the functionality of the JavaScript Quiz as it is

While working on my JS quiz, I encountered an issue where some answers were not displaying due to quotes and the need to escape HTML characters. Additionally, I am facing difficulty in accurately awarding points or deductions based on user responses. Curre ...

Differences in color rendering between XAML WPF and HTML

After migrating my app from WPF to HTML, CSS, and JavaScript, I noticed a discrepancy in color representation. Despite using the same hex value, the HTML color appears lighter than in WPF. See the comparison image here: https://i.sstatic.net/vB7rR.png In ...

There is a significant spacing issue between the header and main category when viewed on a mobile device

Experiencing issues with element distances on different screen sizes? While the website looks fine on normal screens, there's a noticeable gap between the header and main sections on mobile devices. See below for the provided HTML and CSS code. ...

Tips for Sending <Div> Data to a Servlet

I attempted to pass the content of an entire div in a form action URL using JavaScript. However, when I try to retrieve this request parameter as a String on the servlet side, it is returning [object Object]. Below is my code for the form and JavaScript: ...

Beautiful ExpressionChangedAfterItHasBeenCheckedError

I need your help Input field where I can enter a Student email or IAM, which will be added to a string array List displaying all the students I have added, using a for loop as shown below Delete button to remove a student from the array The list has a sp ...