Achieving Dynamic Alignment in CSS: How to Responsively Align a Div to the Width of its Parent Using Padding and Margins

My dilemma could not be clearer - I am struggling to properly align 4 divs within a width of 1150px (the maximum width of the content div). When resizing the screen, I want them to adjust accordingly: 4 in a row when possible, then 3 in the center, and so on.

For screens larger than 1150px, the desired layout can be seen here: https://i.sstatic.net/i9XxP.png. Currently, my closest attempt looks like this: https://i.sstatic.net/mlqsL.png. Adjusting the left margin for the first child helps, but the issue persists with multiple rows. Creating new rows as separate divs is problematic as it disrupts the layout when resized to show only 3 or 1 item per row.

The goal is for the alignment to center smoothly upon resizing, maintaining even margins on all sides. The current outcome does not meet these expectations, as shown here: https://i.sstatic.net/rst3B.png.

At present, my code is limited to this snippet due to lack of alternative solutions:

div.project-container {
    float: left;
    margin: 0 8px 30px 8px;
    position: relative;
    width: 270px;
}

I believe Javascript may hold the key to resolving this challenge, and I am open to suggestions or examples found online. Any guidance will be greatly appreciated. Thank you.

Answer №1

Derived from a historical response :

Web Design

<div id="wrapper">
    <div class="element">1</div>
    <div class="element">2</div>
    <div class="element">3</div>
    <div class="element">4</div>
    <div class="element">5</div>
    <div class="element">6</div>
    <div class="element">7</div>
    <div class="element push"></div>
    <div class="element push"></div>
    <div class="endpush"></div>
</div>

Cascading Style Sheet

#wrapper
{
    max-width: 980px;
    background-color: lavender;
    display: inline-block;
    text-align: justify;
}

.element
{
    width: 180px;
    height: 180px;
    background-color: lightgreen;
    display: inline-block;
    margin-bottom: 20px;
}
.element.push {
    height: 0px
}
.endpush {
    width: 100%;
    height: 0px;
    display: inline-block;
}

trial

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 the Bootstrap accordion to not display content when clicked on?

As a beginner in web development, I am currently working on my very first website. One issue I'm facing is with incorporating an accordion-style card in my webpage. Although it allows me to click on different titles, the collapsed ones fail to expand ...

Issue with :hover pseudo-class in IE8

If you're unsure about my explanation, check out the video for a visual demonstration. Pay close attention to the cursor movements while I attempt to optimize my website for IE8. Hopefully there is a solution to this issue. Thank you in advance! http ...

Align these buttons in a vertical position

I'm trying to place a Twitter button and a LinkedIn profile button next to each other, using the recommended embed code from each service. However, they are not aligning vertically as I would like. I want both buttons to be centered vertically within ...

Having trouble locating the Nativescript-theme-core file for your Nativescript application?

I'm currently working on a basic barcode scanner app and encountering an unusual error once the app is deployed to a Genymotion emulator. It appears to be searching for the core theme in the incorrect location. Any thoughts on why this issue is occurr ...

Enclosing floated elements within a parent div

Looking for a way to make a div wrap around a changing number of child elements without specifying a width for the parent? I've managed to get the content wrapping by floating both the child and parent elements, but I'm facing an issue when the ...

What is the best way to seamlessly transition from responsive design to mobile design?

While developing a single-page app, I encountered an issue with the layout when the screen width reaches a specific point. On narrow screens, I prefer to utilize the full width, but for wider screens, I want to limit the width for better readability. The l ...

What is preventing Kohana from reading my CSS?

Recently, I launched my website kipclip.com on the Kohana platform and added a new rental page. However, the CSS and JS files are not being applied to this page. I've looked into how these files are included in Kohana but haven't had any luck so ...

ng2-table ways to customize the appearance of a particular row

Hey there, I'm currently new to Angular 2 and working with ng2-table. I've successfully added a table like the one shown here to my website. Now, I'm trying to figure out how to add color to specific rows within the table. Following the ste ...

Unfortunate Outcome: CSS Request Results in 404 Error

Recently, I made a transition from having a single-page website to using an express server for my website. During this process, I had to modify the file paths. However, I am encountering difficulties in loading my css and js files. Upon inspecting the dev ...

Optimizing Your Click-to-Call Phone Number for Your Website

When building a responsive webpage using Twitter Bootstrap, I noticed that certain elements are only visible on smaller resolutions like tablets and phones. On an iPhone with OS7, I found that leaving phone numbers as plain text without a link allows the ...

Issues with displaying iframes on iOS devices, particularly iPhones

There is a strange issue I am encountering with iframes not displaying on certain phones while working perfectly on others. (They show up fine on all android devices) (However, they do not work on iphone 6 and 7 but surprisingly work on 7 plus, 7S, and a ...

How to style tags between select options in HTML using React JS?

Is there a way to customize the appearance of a span element that is nested within an option tag? <select> <option value="option1">Active Wallet <span style={{fontWeight:'bold!important'}}>mBTC</span></o ...

Troubleshooting: CSS background opacity issue unresolved

I am having trouble getting a parent div to fill the screen with a transparent background. The code I'm using only results in a solid color background for the parent div. Here is the code snippet I have been working with: .outer { position: rela ...

What is the source of these additional pixels that appear when percentages are used for defining height?

I have been working on implementing a sticky footer that I've successfully used in the past for various websites. You can find the link to the original method here: However, this time I am facing a challenge as I want to make the footer more responsi ...

Create a styled-components component that renders a cross inside a recognizable object surrounded by borders

Currently developing an Object Recognition app and aiming to add a border around the object along with a cross that indicates the center. The border has been successfully implemented, but having trouble creating the cross. The plan is to add two more boxes ...

Steps for configuring a switch to display a color at random

Looking for a way to modify colors of a basic switch <body> <label class="toggle"> <input type="checkbox"> <span class="slider"></span> </label> </body> .toggle { --width: 80px; ...

Issue "Invalid UTF-8" encountered while compiling sass using @import

Here is the structure of my project: project assets scripts styles app.scss bower_components node_modules public css app.css js bower.json gulpfile.js package.json I am using gulp-sass to compile app.scss into ap ...

Hover over the hidden DIV

I have a div containing an image that is overlapping multiple other divs. It looks something like this: <div style='width:100%;height:100%'><img src='someImage.png'></div> <div id='covered'>I'm un ...

Button for toggling the mobile navbar in Bootstrap

Is there a way to adjust the position of the navbar-toggle button http://prntscr.com/5sldgb within the navbar after changing its height? Here is the HTML code: <div class="navbar navbar-default"> <div class="container"> ...

CSS :contains selector after adding a script through Ajax append operation

Is there a way to change the text color in $('td:contains("text")').css('color','red') after an Ajax load script? Here is the main code snippet <div id="datatable"></div> <script src="https://code.jquery.com/j ...