Align elements using CSS Flexbox

Currently delving into the world of HTML and CSS, I am pondering how to display this.

The HTML Markup

<body>
  <div class="container">
    <div id=“one"> 1</div>
    <div id=“two"> 2</div>
    <div id="three">3</div>
    <div id=“four"> 4</div>
    <div id=“five"> 5</div>
  </div>
</body>

The CSS Styling

.container {
  width: 240px;
  height: 200px;
  border: 1px solid gray;
}
.container > div {
  width: 80px;
  height: 50px;
  border: 1px solid red;
}

Aiming to utilize flexbox for this layout adjustment, I attempted some modifications in the CSS code. However, the outcome was not as expected. You can view the result here.

.container {
  width: 240px;
  height: 200px;
  border: 1px solid gray;
  display: inline-flex;
  flex-direction: column-reverse;
  flex-wrap: wrap;
  align-content: flex-start;
}
.container > div {
  width: 80px;
  height: 50px;
  border: 1px solid red;
}

Answer №1

Make sure to include

flex-wrap: wrap-reverse; align-content: flex-start;
in your CSS

.box {
  width: 240px;
  height: 200px;
  border: 1px solid gray;
  display: flex;
  flex-wrap: wrap-reverse;
  align-content: flex-start;
}

.box>div {
  width: 80px;
  height: 50px;
  border: 1px solid red;
  box-sizing: border-box; /* crucial */
}
<div class="box">
  <div id="one"> 1</div>
  <div id="two"> 2</div>
  <div id="three">3</div>
  <div id="four"> 4</div>
  <div id="five"> 5</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

Central logo navigation with paired links on each side

I am currently working on creating a navigation bar that features a centered logo with two links positioned on each side. My goal is to have the side links placed closer to the logo than the edges of the page. Initially, I attempted to achieve this using v ...

What is the best way to adjust the CSS height of an HTML select element to 100% within a table cell in Internet Explorer

Is it possible to make a select (dropdown list) element fill out 100% of the table cell height? I was successful in getting it to work in Firefox and Chrome, but unfortunately not in IE (Internet Explorer). Here is the test HTML code: <table> & ...

An HTML form is unable to send data to a JavaScript function via

Creating a web form in HTML for user input and submission, encountering an error when trying to load the submitted data from submitted.html. Error: Cannot POST /api/submitted The code within my index.html file is as shown below: <form action="/a ...

Using Jquery to dynamically adjust the size of a div based on the width and height of the browser window

How can I dynamically change the height of a .test div based on the browser width and height? I want the value to update whenever the browser is resized. $(document).ready( function() { window.onresize = function(event) { resizeDiv(); ...

Connecting to another page based on data list selection

I have created a datalist with multiple options and now I want to redirect to another page when one of the options is selected. Here is my initial attempt where I tried to directly add a hyperlink to the option label, but it didn't work as expected. ...

Ways to customize the default CSS styles of angularJS for elements like search box and more

Looking for a way to customize the appearance of an AngularJs search box? <div ng-controller="PersonListCtrl"> <div class="bar">Search: <input ng-model="query"> </div> <ul cl ...

Steps for eliminating a column from a table

By including the detailPanel option, a new column with an icon button will be automatically added. https://i.sstatic.net/RoNue.png Is there a way to eliminate this additional column that is generated? Appreciate your help! ...

Displaying a continuous loop of full-screen images using the chocolat.js library

I need some help creating a fullscreen slideshow that loops images using Chocolat JS. According to the documentation, I should start with: <div class="chocolat-parent" data-chocolat-title="set title"> <a class="chocolat-image" href="series/1 ...

When working with basic shapes such as "rect" or "circle," the inline SVG may not be displayed

This is the code we are using to style our checkboxes: .checkbox-default { display: inline-block; *display: inline; vertical-align: middle; margin: 0; padding: 0; width: 22px; height: 22px; background: url('data:image/ ...

Is your CSS not reflecting changes in Node.js?

I am having trouble getting the CSS to update in the browser using Browser Sync via Gulp on Node.js. What could be the issue? Below is my Gulp.js file: var themename = 'playtest haha 1'; var gulp = require('gulp'), // Prepare ...

Utilize html5 to drag and drop numerous items effortlessly

Recently, I created a basic HTML5 drag and drop feature using JavaScript. However, I encountered an issue. function allowDrop(ev) { ev.preventDefault(); } function drag(ev) { ev.dataTransfer.setData("text", ev.target.id); } function drop(ev) { ...

Even if the width is not set to 100%, the div still breaks into a new row

Having an issue here. I'm working with 2 divs that should be displayed side by side. Both are enclosed in a div with max-width : n. I set width: 70% to the first div and width: 30% to the second div, expecting them to fully occupy the parent div. H ...

Eliminating the gaps between a vertical, asymmetrical, and intertwining stack of cards

I'm currently working on adjusting the layout of 12 vertical stacks of cards to remove excess empty space between them. The cards are already stacking nicely by rank, but there seems to be unwanted horizontal space between each pile, and I can't ...

Is there a way to handle specific email format designs with PHP?

When trying to send an email with specific shipping information and tracking numbers, the formatting appears strange. Here is the desired format: Dear xyz , Per your request, this email is to no ...

"Taking cues from Instagram's web/desktop design, we have created

When you view someone's instagram page on a desktop or pc, the search bar is designed to float left when clicked, allowing text to be entered for searching. If no text is entered in the search field, the search icon and placeholder return to their ori ...

Unable to retrieve a value after deactivating a Div element

I have created a web page using MVC Razor. On this page, I included a div element with the following structure: <div class="row" id="DivDisableForView"> <div class="col-md-4"> <label for="" class="control-label" ...

Setting margins for individual cells in a CSS table

I am looking to create a table with a left floated cell, a right floated cell, and the middle cell to remain centered. For example: https://i.sstatic.net/xXo59.png Issue: As the content within the middle cell increases, it shifts further to the left, red ...

How come my columns are stacking on top of each other instead of being positioned side by side?

I am having an issue with my products displaying below each other instead of next to each other in their own column. How can I adjust the layout so that they appear side by side? View image <tr> <th>Month</th> <div *ngFor="let p ...

Highcharts stretching beyond parent container width in Chrome exclusive

On my webpage, I have multiple charts displayed in individual bordered boxes. However, upon the initial page load, all the charts exceed the width of their parent container. An interesting observation is that if I slightly resize the page, everything adju ...

The YQL Query is currently not returning any results

Can someone provide the YQL Query for extracting movie data from the IMDB website? I am specifically looking to retrieve cast information from the title page, using the input URL provided. Example: Input URL : ...