Is there a way to align several divs in the center of a page and then adjust them to be aligned to the left when they move to a

I am working on a webpage layout that consists of multiple modules, each made up of individual <div> elements measuring 159 pixels in width and 160 pixels in height. These modules are supposed to be arranged in a row at the center of the page. The containing <div> is currently set to 70% width.

However, I'm facing an issue when the window size is adjusted, causing some modules to drop down to the next row. At this point, they are centered again instead of aligning to the left as I desire. This results in uneven distribution where, for example, four modules are displayed above with one module centered below them.

Currently, using text-align: center is not an option due to the undesired centering behavior upon resizing. While it does work perfectly for initial alignment, it fails to readjust when modules move to the next row. Is there a way to address this effectively through CSS alone? If not, I am open to exploring jQuery solutions.

#modulesAreaBlock { /*Outer container DIV*/
    margin-top: 50px;
    width:70%;
    margin-left: auto;
    margin-right: auto;
    text-align: center;
}

#modulesCenter { /*DIV for centering the modules within the outer DIV*/
    display: inline-block;
    text-align: left;
}

.moduleBlock { /*Individual "module" DIVs*/
    text-align:center;
    height: 160px;
    width: 159px;
    margin-bottom: 2px;
    display: inline-block;
    zoom: 1;
    *display: inline;
    _height: 160px;
}

Answer №1

Have you attempted to float the modules to the left and change their display to block instead of relying on inline-block? Inline-block may not be as compatible across different browsers, especially in cases involving IE.

Answer №2

Thanks to Scott's suggestion of using placeholders, I successfully implemented a solution utilizing jQuery and CSS. You can view the result here: http://jsfiddle.net/9Qgsm/20/

Relying solely on placeholders posed challenges as the content would lose center alignment when the window size changed. With the help of jQuery, placeholders can now adapt their visibility based on the width of the parent div. The minimum width of the div is dynamically adjusted according to the number of modules present (which will be handled by ColdFusion).

jQuery:

$(document).ready(function(){ 
    //If the div spans multiple rows, insert dummy divs
    if ($('#modulesAreaBlock').width()<625){
        $('.dummy').css('display','inline-block');
    }
});

$(window).resize(function(){
    //If the div creates more than one row of modules, add dummy divs
    if ($('#modulesAreaBlock').width()<625){
        $('.dummy').css('display','inline-block');        
    }
    //If the width of containing div contains all modules, hide the dummies
        if ($('#modulesAreaBlock').width()>=625){
        $('.dummy').css('display','none');
    }
});

CSS:

#modulesAreaBlock {
    margin-top: 50px;
    width:70%;
    margin-left: auto;
    margin-right: auto;
    position: relative; 
    text-align: center;
}

/*Displays the module blocks*/
.moduleBlock {
    text-align:center;
    height: 100px;
    width: 100px;
    margin-bottom: 2px;
    display: inline-block;
    zoom: 1;
    *display: inline;
    _height: 160px;
}

.dummy{
    display:none;
    width:100px; 
}

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

Strategizing - positioning columns inside a container

Currently, I am a university student working on a project where I am trying to design a layout with multiple images on the left and text along with a button on the right. Despite using Bootstrap classes, I'm facing an issue with aligning these element ...

Tips for adding a child div to encase the inner content

Can someone help me figure out how to use pure JS (not jquery) to add a <del> inside the <div> that contains the text 'abc'? For practice purposes, I am not allowed to use innerHTML method, inline CSS, or jquery. Just plain JS. <h ...

Using Font Awesome icons with CSS rotation causes compatibility issues

Having trouble getting the icons to rotate on hover? The transition effect is working smoothly, but it seems like the rotation functionality is not kicking in. Take a look at the code below and see if there are any mistakes: @import 'https://maxcdn ...

Utilizing the dynamic capabilities of Ajax in tandem with the popular Jquery and Code

tag, I have a scenario where a controller is sending data from two functions in a model to a view. This involves retrieving the results of 'waiting' and 'beingseen' functions and passing them to the 'studentqueue/studentqueue' ...

Distinct headings break up two-columned lists

Is there a way to ensure that the h2 header always stays with its list items, even if the number of list items varies? Currently, the header appears in one column and the lists appear in another column, causing them to be separated when the list count chan ...

What causes certain webpages to experience delays? (Advertisement impact)

Whenever I visit low-quality websites filled with ads, I often experience a delay where part of the site loads and then stalls for seconds before loading the rest. However, when I use AdBlock, the site loads much faster. What exactly is causing this stal ...

Is the script making a break for it?

Here is how my JavaScript function, UpdateLevelRemove(), is set up - function UpdateLevelRemove() { //Do something var ajaxCall = $.ajax({ data: { Svc: cntnrRoot, Cmd: 'updateLEvel', updatePrivacyAction: 'Remove' ...

Having trouble aligning image and expanding video to fit the screen in a NextJS application

I need help with: centering the image above the waitlist sign up scaling the background video in a relative way to fill the screen regardless of browser size This is the relevant part of index.js: import Head from 'next/head' import Image from ...

What is the best way to eliminate the border surrounding an image that appears after clicking on it in Google Chrome?

Currently, I am utilizing SpryTabs plugin for menu navigation. I have incorporated two background images to indicate the active and inactive tabs. The tabs are highlighted on hover, activating them, and deactivating the previously selected tab when another ...

Maintaining image ratio on the entire screen

I am in search of a way to create a webpage that includes the following elements from top to bottom: Full-screen width image/background-image (maintaining aspect ratio) Navigation bar (aligned at the top right of the image) Some text Another full-screen ...

Exploring Angular 2 Tabs: Navigating Through Child Components

Recently, I've been experimenting with trying to access the HTML elements within tabs components using an example from the Angular 2 docs. You can view the example here. Here is a snippet of my implementation: import {Component, ElementRef, Inj ...

How to align a div in the center of a cell with Bootstrap

I am currently working with Bootstrap 3 and I have a specific requirement to center a div within a cell in the container row. Most resources I found only mention how to center a div within the entire container, which is not what I am looking for. My goal i ...

What's the best way to position menu items vertically in a navbar?

I've been struggling to vertically center the menu items "TEST 1","TEST 2" and "BRAND" within the navigation bar without any luck. I've experimented with vertical-align, margin-top, and bottom properties. What I'm aiming for is to have them ...

Do not transfer any files during the migration process from NGINX to Apache

I have a specific URL structure: http://www.example.com/folder/index.php?dir=dir1 I want to make it accessible from: http://www.example.com/folder/dir1 To achieve this, I set up rules in my .htaccess file located in the 'folder' directory: O ...

How can I ensure that my columnar div box is responsive on smaller devices with CSS or should I consider using Bootstrap?

I have a container with a row that contains multiple div boxes. Currently, I am using CSS to spread these div boxes into 3 columns using the column-count property. I have a few questions: 1) How can I make this responsive for smaller devices? 2) Can I ...

Switching from using $.AJAX to using $.POST is a

Could someone assist me in changing this code to use $.post instead? I've tried replacing some of the code but it's not working properly. Thank you for your help. $.post({ url: "http://192.168.254.107/webs/main/ajax/validateLogin.php", ...

Using $.load to load a local HTML file

When I open my app, the local HTML file from the SD card is loaded. Below is the code snippet: String extPath = getExternalFilesDir(null).toString(); String html = getHtml(extPath+File.separator+fileName); //this reads the HTML file and returns its conten ...

jQuery AJAX encountering parsing error when retrieving data from Laravel controller

I'm attempting to retrieve data from a laravel controller using 'jquery' ajax, but I keep encountering the 'parsererror'. According to the laravel documentation, it automatically sets the header Content-Type= 'application/json ...

Having trouble implementing a range slider with text in jQuery?

Currently, I am utilizing jquery and looking for a way to adjust the size of a "text" element whenever we increase or decrease the "slider range". How can this be achieved? I have attempted the following code without success. Your assistance is much appr ...

repeated execution of a javascript function

I have created a tag picker that generates checkbox inputs based on the "group" selected. After selecting the desired tags and pressing the done button, it should return a string to set the value of a text input. Below are some related code snippets. The ...