Creating evenly spaced PHP-generated divs without utilizing flexbox

My goal is to display images randomly from my image file using PHP, which is working fine. However, I am facing an issue with spacing the images evenly to fill the width of my site.

This is how it currently appears:

https://i.stack.imgur.com/AzKTK.png

I have marked a red line on the right side where I want the div to extend to.

I attempted to use flex method for spacing but found that it compresses all divs when the page is resized, which is not ideal for a responsive design.

I also tried the span stretch method but it did not work as expected.

HTML:

<div id="tile_wrapper">
    <?php
        $res = $conn->query("SELECT * FROM randomimg ORDER BY RAND() LIMIT 16");
        while($row=$res->fetch_array())
        {
            include 'includes/tile.php'; 
        } 
    ?>
    <span class="stretch"></span>
</div>

PHP:

<div id="tile_image_wrapper">
    <a href="storepage.php?name=<?php echo $row['storename']; ?>" />
        <?php 
            echo 
                '<div id="tile_inner_wrapper">' .
                    '<img src="images/stores/' . $row['storename'] . '.png">' .
                '</div>';
        ?>
    </a>
</div> 

CSS:

#tile_wrapper {
    width:100%;
    margin-left: auto; 
    margin-right: auto;
     text-align: justify;
    -ms-text-justify: distribute-all-lines;
    text-justify: distribute-all-lines;
}


#tile_image_wrapper {
    display: inline-block;
    width:130px;
    border:1px solid #ccc;
    height:50px;
    border-radius: 3px;
    background-color:#fff;
    margin:3px;
}

#tile_inner_wrapper {
    height:50px;
    width:130px;
    vertical-align: top;
    display: inline-block;
    *display: inline;
    zoom: 1;
}

#tile_wrapper img {
    max-width:100px;
    max-height:40px;
    top: 50%;
    transform: translateY(-50%);
    display: block;
    position: relative;
    margin: 0 auto;
}

#tile_image_wrapper:hover {
    border:1px solid #aaa;
}

.stretch {
    width: 100%;
    display: inline-block;
    font-size: 0;
    line-height: 0
}

Any assistance would be greatly appreciated!

Answer №1

There appears to be an issue with how you are constructing the DOM in your PHP files, as you are closing the anchor tag around your image tiles prematurely and then attempting to close it again after defining the tile. Consider updating the code snippet from:

<a href="storepage.php?name=<?php echo $row['storename']; ?>" />

to:

<a href="storepage.php?name=<?php echo $row['storename']; ?>">

A demonstration of this correction can be viewed on jsfiddle: https://jsfiddle.net/1x7wa59t/2/

Additionally, there are a few typographical errors in your CSS (such as *display:inline and a missing semicolon in .stretch).

It is important to note that the CSS3 property text-justify is currently only fully supported in Internet Explorer 5.5+ (http://www.w3schools.com/cssref/css3_pr_text-justify.asp).

Answer №2

tile_wrapper id ought to be activated

text-align:center;

attribute.

Answer №3

Implement

margin:0 auto;
width:960px; //or any other value you prefer

within the tile_wrapper id

Answer №4

To achieve the desired effect of centering images inside divs, you can set the display property to inline and use text-align: center on the parent div. This will help create a layout that is similar to what you are looking for and should meet your needs.

If you require the widths of the divs to adjust dynamically to always fill the available space on window resize, you may need to utilize JavaScript. This script can calculate the width of the container and resize each div accordingly. Another option could be using media queries with percentages to determine where to insert a break in the layout. Just a suggestion to consider for achieving the desired result.

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

What is causing Puppeteer to not wait?

It's my understanding that in the code await Promise.all(...), the sequence of events should be: First console.log is printed 9-second delay occurs Last console.log is printed How can I adjust the timing of the 3rd print statement to be displayed af ...

Tips for adjusting the size of grid tiles according to the dimensions of the window within a specific range

I am currently exploring how to replicate the effect found on this webpage: When the window size is adjusted, javascript dynamically resizes the grid tiles between 200 and 240px based on the optimal fit for the screen. Is there a ready-made JavaScript/jQ ...

Troubleshooting the "Failed to mount component" error in Vue: fixing template or render function definition issues

Struggling with writing a Vue component, encountering the issue: Failed to mount component: template or render function not defined. Tried various fixes like adding default when importing the component, but none of them seem to work. My component code is i ...

Selenium RC: Utilizing the CSS :contains pseudo-class for Element Selection

I have a task to verify that the data in a table row matches my expectations for two different tables. Let's look at an example using HTML: <table> <tr> <th>Table 1</th> </tr> <tr> <t ...

Utilize the same variable within a React functional component across multiple components

Within a React functional component, I am utilizing the following constant: const superHeroPowers = [ { name: 'Superman', status: superManPowerList }, { name: 'Batman', status: batmanPowerList }, { name: 'Wonder Woman&a ...

What is the procedure for selecting an element based on its child containing specifically filtered text?

Imagine a webpage with the following elements: <div data-search="type1"> any HTML <!-- .click is a child of any level --> <span class="click" data-source="page1.html">blue</span> <!-- let's call it "click1 ...

The reason the CSS direct descendant selector does not affect Angular components

We are working with a basic main.html. <app> <sidebar></sidebar> <main-content> <router-outlet></router-outlet> </main-content> </app> After loading a component through routing (changing the ...

Guide for positioning all navbar items except one to the left, beginning at the left edge, utilizing a set minimum and maximum width, while moving the final item to the right edge

Is there a way to align all items in a navbar except for one to the left, with a fixed combined minimum and maximum width of 300px-400px, while sending the last item to the right margin? I want the final result to resemble this: https://i.stack.imgur.com ...

Exploring the transparency of material lab autocomplete drop-down text for enabling multiple selections

Check out this demo for checkboxes and tags in Material UI The code below demonstrates an autocomplete component that functions correctly. However, the drop-down text appears transparent. Is there a way to fix this issue without modifying any libraries? ...

Display drop-down item when selected for the first time and hide it when selected for the same item for the second time

Is there a way to display form input when "C" is selected and hide it when "A", "B", or "D" are selected? If "C" is selected again after initially showing the form input, should it be hidden? For example, if "C" is selected for the first time, the form inp ...

Is it possible to customize the color of icons in react's Material-table?

I'm currently utilizing the material-table library and I'm struggling to individually change the color of each icon. Could you assist me with this? I attempted custom CSS, but it's affecting all icons at once instead of specific ones. Here i ...

What is the method for determining the data type of a column in an Excel sheet that has been

Currently, I am utilizing the XLSX npm library to convert an Excel sheet into JSON format. However, all of the retrieved data is currently being returned as strings. To see a demo of the XLSX read process, you can visit this Stackblitz demo Is there a w ...

What is the best way to start tiny-slider automatically once the video has ended?

I am currently using the tns-slider plugin and have a setup with 3 slides (2 photos and 1 video). <div class='tiny-slider'> <div class='slide slide1'> <div class='video-slide'> <video id=&qu ...

Is there a restriction on the maximum size of a CSS file?

Currently, I am working on building a site using ASP.NET MVC. The CSS file that I have been working on has now grown to 88KB in size and contains over 5,000 lines of code. Recently, I have noticed that some styles that I added towards the end of the file a ...

Comparing Doctrine's Data Insertion Methods: Repository vs Entity

I have come to realize the distinction between a Doctrine repository and a Doctrine entity. While attempting to carry out basic CRUD operations on a table, I mistakenly injected a default Doctrine repository into my controller without injecting an entity. ...

Customize the columns of your Angular Material 2 table by defining them using TemplateRef and ngTemplateOutlet

Looking to create a flexible material table with the ability to utilize TemplateRef along with ngTemplateOutlet for generating columns. Check out this demo where I have employed the cards component within my custom material-table component. Inside the card ...

When attempting to download a PDF file from Flask to the client, the file appears to be

I am encountering an issue with my Flask server that sends a PDF file using the send_file function. When testing this route on Postman, I am able to view and download the PDF successfully. However, when attempting to download it through my React frontend, ...

What could be causing the error "Unexpected identifier 'trytoCatch' while trying to minify?

I recently updated my script.js and now I'm looking to use "minify" in Node.js to compress it. When I type the command minify script.js > script.min.js into the terminal, I get an error message that says: /node_modules/bin/minify.js:3 import "tryToCat ...

What is causing the alt or title tags to not function properly in emails?

I am currently facing an issue with e-mail templates that include images. The problem arises from the fact that these images need to be downloaded before they can be displayed in the email. To work around this, I always use ALT and TITLE tags to provide de ...

ajax duplicator and reset form tool

Hello everyone, I have a website where users can add their experiences. While adding an experience, they can dynamically add and remove more fields. One of the input fields is for a date, but when the data is submitted, the same date appears for all entrie ...