Utilize flexbox to achieve center left alignment of elements within a DIV container

I've encountered a puzzling issue with my inherited markup. The width is defined on the outermost div and then inherited by each child div, creating a dynamic width scenario.

The challenge I face is aligning the pink wrapper div to match the exact width of the row of blue boxes in the first row, without knowing the number or widths of the blue boxes beforehand.

My aim is to center the pink wrapper within the red/green divs and have the blue boxes left-aligned. Despite two attempts, I haven't been successful. Any assistance would be greatly appreciated.

You can view the desired outcome in this image: https://i.sstatic.net/xtOxA.png

.outer {
  width: 275px;
  border: 4px solid red;
}

.parent-wrap {
  border: 3px solid green;
}

.wrapper {
  background: pink;
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
}

.box {
  width: 50px;
  height: 50px;
  background: blue;
  display: inline-block;
  margin: 5px;
}

/* Attempt 2*/

.outer2 {
  width: 275px;
  border: 4px solid red;
}

.parent-wrap2 {
  border: 3px solid green;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.wrapper2 {
  background: pink;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.box2 {
  width: 50px;
  height: 50px;
  background: blue;
  display: inline-block;
  margin: 5px;
}
<h2>Attempt #1</h2>
<div class="outer">
  <div class="parent-wrap">
    <div class="wrapper">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </div>
</div>

<h2>Attempt #2</h2>
<div class="outer2">
  <div class="parent-wrap2">
    <div class="wrapper2">
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
    </div>
  </div>
</div>

Answer №1

To align content in the center using flexbox, you need to apply the following CSS properties: set the container to display: flex and justify-content: center. The child elements can then be aligned to the left using justify-content: left.

.outer {
  width: 400px; /* Increasing width for better visualization of centering */
  border: 4px solid red;
}

.parent-wrap {
  border: 3px solid green;
  justify-content: center; /* CENTER */
  display: flex; /* APPLYING FLEXBOX */
}

.wrapper {
  background: pink;
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
}

.box {
  width: 50px;
  height: 50px;
  background: blue;
  display: inline-block;
  margin: 2%;
  flex: 0 0 21%; /* Restricting number of boxes per line using flex property */
}
<h2>Attempt #1</h2>
<div class="outer">
  <div class="parent-wrap">
    <div class="wrapper">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </div>
</div>

Answer №2

Adjust the width property for both .wrapper and wrapper2.

.outer {
  width: 275px;
  border: 4px solid red;
}

.parent-wrap {
  border: 3px solid green;
}

.wrapper {
  background: pink;
  display: flex;
  flex-wrap: wrap;
  justify-content: left;
}

.box {
  width: 50px;
  height: 50px;
  background: blue;
  display: inline-block;
  margin: 5px;
}

/* Attempt 2*/

.outer2 {
  width: 275px;
  border: 4px solid red;
}

.parent-wrap2 {
  border: 3px solid green;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.wrapper2 {
  background: pink;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.box2 {
  width: 50px;
  height: 50px;
  background: blue;
  display: inline-block;
  margin: 5px;
}

.wrapper, .wrapper2 {
  width: 90%;
}
<h2>Attempt #1</h2>
<div class="outer">
  <div class="parent-wrap">
    <div class="wrapper">
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
      <div class="box"></div>
    </div>
  </div>
</div>

<h2>Attempt #2</h2>
<div class="outer2">
  <div class="parent-wrap2">
    <div class="wrapper2">
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
      <div class="box2"></div>
    </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

What is the process for daily web scraping of news articles using Python?

As I work on developing an application, I require daily news feeds from multiple websites. Utilizing Python's BeautifulSoup library seems like a viable option, but it may not be suitable for websites that display news across various pages which requir ...

Including Vuetify in my project has triggered a SecurityError, specifically relating to the CSSStyleSheet.cssRules getter being restricted from accessing cross-origin stylesheets

Looking to create a Vue application that opens a CKEditor component in a separate window, I managed to do so following the method outlined in this helpful post. I have even set up a Codesandbox demonstration to showcase the functioning example. However, u ...

Find the ID of the displayed div using jQuery

Here is the snippet of code I am working with: <div class="sequence-container"> <div id="1"></div> <div id="2"></div> <div id="3"></div> </div> Along with my jQuery script: $(window).load(function() ...

Create a spectrum of vibrant colors depending on the numerical value

I'm attempting to create a function that generates rainbow colors based on a numerical value. var max = 10000; var min = 0; var val = 8890; function getColor(min, max, val) { // code to return color between red and black } Possible Colors: Re ...

How to stop parent event propagation in jQuery

I am facing a frustrating issue with my code. Every time I toggle a checkbox, the parent element's click event also triggers. I have experimented with various solutions like: event.stopImmediatePropagation(); event.stopPropagation(); event.preventD ...

Adjusting Chart.js line graph alignment to the left

I'm curious about why my chart is not aligned to the left and how I can fix this issue. Here is the code snippet I am working with: var data = { labels: ["ID"] , datasets: [ { label: "Sensor 1" , data: [{ ...

What is the correct way to call upon Bootstrap's CSS that was obtained through a Gradle dependency?

I'm having trouble referencing the Bootstrap library from the 'External Libraries' section in my project. In my build.gradle file, I included: compile group: 'org.webjars', name: 'bootstrap', version: '3.3.7' ...

Are there any jQuery Context Menu plugins clever enough to handle window borders seamlessly?

After reviewing UIkit, as well as some other jQuery Context Menu plugins, I have noticed that they all tend to exhibit a similar behavior: The actual menu div renders outside the window, causing valuable content to be hidden from view. Is there a way to ...

Dynamically injecting a JavaScript script within a Vue.js application

Is there a way to dynamically load a JavaScript script within a Vue.js application? One approach is as follows: <script async v-bind:src="srcUrl"></script> <!--<script async src="https://cse.google.com/cse.js?cx=007968012720720263530:10 ...

Showing data from the POST request in a dialog box

Greetings to all! I am in the process of building a webpage to test my API. The goal is to send a form to my nodejs backend and receive a JSON response back. So far, sending the request is functioning properly and I can track its progress on my VPS conso ...

Extract the HTML table from Gmail and transfer it to Google Sheets with the help of Google Apps Script

Just dipping my toes into google apps script. On my to-do list is creating a script that can extract data from a table in Gmail and transfer it to Google Sheets. Here's an example of what the email body looks like: CONFIRMATION CODE GUEST&apo ...

Dreamweaver's JSON stringify works perfectly in preview mode, but fails to function when accessed from the .AIR file after application creation

For a while now, I've been working on a basic application using Adobe AIR, specifically the HTML and JavaScript version. What this application does is submit a form to an online URL, with the form values being converted to JSON strings. In order to ...

Trouble retrieving key value in HTML with Angular and Firebase

When trying to access the key of an object in my database to navigate to a URL based on it, I am finding that the p.$key value is always undefined. service: getAll(){ return this.db.list('/products').valueChanges(); } component: product ...

Guide to highlighting a particular letter in a string variable using JavaScript and AngularJS

Looking to highlight specific letters within a string variable. I have the string stored as an AngularJS variable in a table like this: <td>{{variable}}<td> In my JavaScript file, I am passing a variable for Angular that contains the string & ...

Unusual dark streak beneath the Wordpress emblem

I recently created my website using Wordpress and decided to use a PNG image (140*82 px) as the logo instead of the default "site-title". However, I encountered an unusual issue where a black line appeared just below the image. After checking my "header.ph ...

How can I modify the Twitter Bootstrap carousel's background?

Is there a way to customize the background of Twitter Bootstrap 3 carousel? I am looking for a solution where I can have a plain red background and then overlay each carousel image on top of it. I have tried modifying the CSS of the <body> tag to a ...

Update the variables upon a click event following the completion of an AJAX request

Despite my efforts, I am unable to find a solution to the issue I am currently facing. To address this problem, I have created a script using PHP and jQuery that enables users to promote their "listings" on my website. When users visit a specific page, the ...

tips for accessing data attributes in ajax request

I am struggling to retrieve data-id from an anchor tag when clicked using AJAX, but it keeps returning undefined. Below is my code: $image_html .= '<a class="float-left " onclick="modal()" data-toggle="modal" data-targ ...

Tips for creating two divs next to each other with inline-block?

Is there a way to align the divs side by side and have the 'contentwrapper' div responsive to the resizing of the browser window? HTML <div id="maincontainer"> <div id="leftcolumn">&nbsp;</div> <div id="contentwrapper ...

Tips for maintaining video height consistency while adjusting its attributes

When you click on a button, the video's poster and src attributes change. However, after clicking, the video height briefly becomes 0, causing an unsightly effect on the entire page. How can this be avoided? Please note - lorem.mp4 and ipsum.mp4 hav ...