Tips for aligning Picture boxes using CSS

As a newbie, I recently received training in HTML, CSS, and a bit of JavaScript. Currently, I am involved in a project where I display the latest news from an API. The technical aspects of fetching data from the API using JavaScript are handled by a senior colleague, while my task is to create an appealing user interface.

After referring to w3schools, I was able to design the UI as envisioned. However, I encountered a problem where the layout does not adapt well on certain screen sizes, resulting in empty spaces and misalignment issues. Can anyone offer suggestions to help me rectify this dilemma?

Attached below is an image showcasing my progress thus far along with the corresponding code. Any assistance provided would be greatly appreciated. Thank you!

Link to Image

div.gallery {
border: 1px solid #ccc;
}
div.gallery:hover {
    border: 1px solid #777;
  }
  
  div.gallery img {
    width: 100%;
    height: auto; 
  }
  
  div.desc {
    padding: 15px;
    text-align: center;
  }
  
  * {
    box-sizing: border-box;
  }
  
  .responsive {
    padding: 0 6px;
    float: left;
    width: 24.99999%;
  }
  
  @media only screen and (max-width: 700px) {
    .responsive {
      width: 49.99999%;
      margin: 6px 0;
    }
  }
  
  @media only screen and (max-width: 500px) {
    .responsive {
      width: 100%;
    }
  }
  
  .clearfix:after {
    content: "";
    display: table;
    clear: both;
  }

The HTML code snippet below illustrates the structure utilized within a JavaScript loop to showcase each news item:

<div class="responsive">
    <div class="gallery">
       <img src={Image link here} alt="Link broken" width="600" height="400" />
           <div class="desc">
               {Post details here}
           </div>
     </div>
</div>

Answer №1

Give this a shot,

To achieve alignment, I have utilized the grid method along with media queries for responsiveness on specific devices.

.responsive {
  padding: 10px 6px;
  width:100%;
  display: grid;
  grid-template-columns: auto auto auto auto;
}
@media only screen and (max-width: 1200px){
  .responsive {grid-template-columns: auto auto auto;}
}
@media only screen and (max-width: 768px){
  .responsive {grid-template-columns: auto auto;}
}
@media only screen and (max-width: 576px) {
  .responsive {grid-template-columns: auto;}
}

div.gallery {
  border: 1px solid #ccc;
  margin:10px;
}
div.gallery:hover {
   border: 1px solid #777;
}
div.gallery img {
  width: 100%;
  height: auto; 
}
div.desc {
  padding: 15px;
  text-align: center;
}
.responsive {
  padding: 10px 6px;
  width:100%;
  display: grid;
  grid-template-columns: auto auto auto auto;
}
@media only screen and (max-width: 1200px){
  .responsive {grid-template-columns: auto auto auto;}
}
@media only screen and (max-width: 768px){
  .responsive {grid-template-columns: auto auto;}
}
@media only screen and (max-width: 576px) {
  .responsive {grid-template-columns: auto;}
}
<div class="responsive">
  <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">1</div>
   </div>
   <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">2</div>
   </div>
   <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">3</div>
   </div>
   <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">4</div>
   </div>
   <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">5</div>
   </div>
   <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">6</div>
   </div>
   <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">7</div>
   </div>
   <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">8</div>
   </div>
        <div class="gallery">
     <img src=https://i.sstatic.net/NnPLf.png alt="Link broken" width="600" height="400" />
       <div class="desc">9</div>
   </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

Unexpected issue encountered for identifiers beginning with a numerical digit

Is it possible to select an element from a page with an id that begins with a number? $('#3|assets_main|ast_module|start-iso-date') Upon attempting to do so, the following error occurs: Uncaught Error: Syntax error, unrecognized expression: ...

Restore checkbox to default setting

Is it possible to reset checkboxes in a form back to their initial status using javascript, PHP, jQuery, or any other method? Here is the code I am currently using: <form method="POST> <input type="text" name="name" id="name" value="default val ...

jquery-enhanced tabs with versatile functionality

HTML: <ul class="tabs"> <li><a href="#tab-one" class="current">Residential</a></li> <li><a href="#tab-two">Commercial</a></li> <li><a href="#tab-three">Agricultural</a></li> < ...

Sharing the input value with a service in Angular 4

I am a beginner when it comes to Angular 4. I currently have a variable named "md_id" which is connected to the view in the following way. HTML: <tr *ngFor="let item of driverData"> <td class="align-ri ...

Tracking changes in real time and calculating the sum with AJAX, PHP, and MySQL for efficient processing

Initially, I kindly request you to read this until the end. I am in dire need of assistance with my problem as I have searched for solutions but still remain clueless. https://i.sstatic.net/DAb1q.png Referring to the image provided, the first 'Produ ...

How to use jQuery to set a background image using CSS

I've been working on setting backgrounds dynamically with a jQuery script, but it seems like the .css function is not working as expected. Here's the code snippet: $(document).ready(function () { $(".VociMenuSportG").each(function () { ...

Here is an example of how to transfer a value from PHP to a jQuery function in the code snippet provided

This is an example of my code. It is functioning properly even without passing a value. function displayMessage(text) { alert(text); } <button type="button" id="button" class="btn btn-success" onclick="displayMessage("Hello");"> Click Me </ ...

How to Implement Flexbox Display Across Various Browsers Using jQuery?

Running into an issue here. I'm trying to switch from display: none to display: flex, display: flex-box, and display: -ms-flexbox but for some reason it's causing errors in my code.. Here's what I've been working on: $(elem).css({&apos ...

Change SVG path data into a range of 0 to 1 in order to utilize it as a clip path with objectBoundingBox

My SVG shape, exported from Illustrator as a clipping path, is quite complex. The issue I'm facing is that objectBoundingBox restricts path data to the 0-1 range, but my path data exceeds this range. Here is the current code I'm using: <svg& ...

Invisible boundary line within the column

My task involves creating a table using div elements. The layout of the table includes two columns structured as follows: <div> <div class = "columnborder"><span>Some text for column 1</span></div> <div><span>Some ...

What is the best solution for adjusting spacing in justified text columns within bootstrap?

As I work on creating a div with two columns, I am facing an issue with maintaining justified alignment. It looks great with two divs using col-lg6- in Bootstrap, but when I switch to col-md-6 or col-sm-6, there are excessive spaces between words to keep t ...

div is consistently correct across all web browsers

After creating the following CSS code for a div to always be positioned on the right: #button > .right { background-position: -268px 1415px; height: 180px; position: fixed; right: -90px; top: 40%; width: 263px; -webkit-transform ...

Button will be disabled unless a value is selected in the dropdown menu

I am currently dealing with a code issue where the button is disabled on page load when the dropdown value is empty. However, even after selecting a value from the populated database dropdown, the button remains disabled. Jquery: <script> $(doc ...

What is the best way to completely eliminate a div from a webpage

I've created a new div element using jQuery: $(document.body).append('<div id="test"></div>'); Then, I display a success message and remove the div after 10 seconds: $('test').html('SUCCESS!'); setT ...

Flask Pagination : UnboundLocalError occurs when trying to reference a local variable 'res' before it has been assigned

I'm currently troubleshooting an issue with flask pagination. Whenever I attempt to implement it, I encounter the UnboundLocalError. Even after removing all variables, the pagination feature still fails to function as intended. @app.route('/&apos ...

By employing the pseudo selector ::after, one can enhance the design

Trying to align 2 images next to each other, I am floating one left and the other right with the intention of clearing the float using the ::after pseudo selector. The surrounding text is expected to flow in between the images. I often struggle with maki ...

Skrollr's transformation effect makes the text tremble

Using skrollr.js, I am implementing transition effects on an inner div nested inside another div. The inner div is positioned relatively. Here are my codes: HTML Structure <div class="outer"> <div class="inner-div" style="transition:transform ...

Ways to turn off the dragging animation for cdkDrag?

I am facing an issue with cdkDrag where a small preview of the item being dragged shows up. I want to disable this feature and remove any CSS classes related to the dragging state. Can anyone guide me on how to achieve this? https://i.sstatic.net/nYU07.pn ...

What is the best way to vertically center text within a selection box? (This is not referring to a select box or a div box)

While assigning a line-height to text elements like div, span, or p and dragging them in the browser, an unsightly empty space appears within the selection box: I am seeking a way to elevate the actual text content within the selection box to enhance its ...

Stop the div from collapsing when hiding or deleting all child elements to maintain its structure

I have recently developed a Vuetify application that manages card items. To ensure security and accessibility, I have added a feature where the actions/buttons are displayed based on the User's permissions. In case of missing permissions, these button ...