Achieving Centered Items with CSS Flexbox and Positioning

Struggling to arrange 3 elements using flexbox while keeping them centered both horizontally and vertically.

Here is the desired layout:

https://i.sstatic.net/Uo6WS.png

This is my current attempt, but it's not working as expected. Can anyone spot the mistake?

.container {
  width: 100%;
  max-width: 800px;
  height: auto;
  margin: 0 auto;
  background: pink;
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;

  .first { 
    background: green; 
    height: 50px;
    align-self: stretch;
  }

  .second { 
    background: blue; 
    height: 100px;
  }

  .third { 
    background: crimson; 
    height: 100px;
  }
}
<div class="container">
  <div class="first"></div>
  <div class="second"></div>
  <div class="third"></div>
</div>

Check out this demo on Fiddle: https://jsfiddle.net/u21uqs7q/

Answer №1

To enhance your code, simply add the snippet below:

.container {
width: 100%;
max-width: 800px;
height: 100vh;
margin: 0 auto;
background: pink;
display: flex;
  align-items: center;
  justify-content: center;

}
.left  .first{ 
background: green; 
height: 50px;
}

.right .second { 
background: blue; 
height: 100px;
}

.right .third { 
background: crimson; 
height: 100px;
}
<div class="container">
<div class="left">
  <div class="first">111</div>
  </div>
  
<div class="right">
  <div class="second">222</div>
<div class="third">333</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 steps should I follow to generate a table using Ajax and Javascript when a button is clicked?

After spending hours following various tutorials and exploring previously asked questions, I am struggling to make this work. My HTML page looks like this: <!DOCTYPE html> <html> <head> <link type="text/css" rel="styleshee ...

Alter the app's entire background color in React.js with a button click

I am facing an issue with changing the background color of my entire React App when a button is clicked. Currently, I am able to change the color of the button itself but not the background. I have imported the App.css file into my project and I am looking ...

Unable to maintain the height of a div at 100% as it keeps reverting back to 0px

I'm having trouble setting the height of the individual parent columns (and their children) to 100% because for some reason, the div height keeps resetting to 0. The first column contains two child boxes that should each take up 50% of the page heigh ...

Is it possible to use a while loop to draw and populate a table?

Despite my efforts, this method is not producing the desired outcome. $row2 = 0; while ($row = mysql_fetch_assoc($query)) { echo "<table border='1'>"; if($row2 == 0){ echo "<tr>"; } elseif($row2 < 5){ ...

Getting all "li" elements from the "ul" class using Selenium WebDriver is a required task for web scraping or automated testing

As a beginner in Selenium webdriver, I recently encountered a task where I need to automate clicking on all links within a specific section. Could someone provide me with the Java code for this? I have attached an image displaying the Firebug properties of ...

The text in the Bootstrap 4 card spills over the card-text boundary

I've encountered an issue when trying to insert text into a card-text paragraph. Here's what happens: https://i.sstatic.net/CWGh2.png I've been attempting to resolve the problem without success. I'm completely stumped on how to fix it ...

Collecting data from an HTML form filled out by users

I need a way to capture user input from an HTML form and display it later on my website. I want to show all the information users provide along with their pizza selections. I've been struggling for hours trying to make this work without success :( Spe ...

Increasing the margin-left automatically in Bootstrap 3.0.0

I am looking to automatically generate margin-left in the ".card" class "style" element every time a post is entered on a page. My jQuery version is 1.12.4 This is my idea: If the .card CSS style has a margin-left of 0 and width of 479px, set position to ...

Achieving a transparent background in WebGLRender: A guide

I've been experimenting with placing objects in front of CSS3DObjects using the THREE.NoBlending hack. However, in the latest revisions (tested on r65 and r66), I only see the black plane without the CSS3DObject. Here is a small example I created: in ...

The HTML source code in the browser varies from the output obtained through Python requests

I've noticed a discrepancy between the HTML source code displayed in my browser and the code I receive when using Python's requests.get function. You can see the image depicting this issue at the following link: Any thoughts on why this might be ...

Attempting to dynamically update the image source from an array when a click event occurs in a React component

Has anyone successfully implemented a function in react.js to change the image source based on the direction of an arrow click? For instance, I have an array set up where clicking the right arrow should move to the next image and clicking the left arrow s ...

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

Issue with mouseover in the jQuery area

As I navigate through a WordPress website, I am attempting to create a mandala using dynamic images. Utilizing jQuery and CSS areas, I aim to display the corresponding image when hovering over a specific section. However, I am facing an issue where there ...

Looking to showcase a .tif image in your Angular project?

This code is functioning properly for .png images. getNextImage(imageObj:{imageName:string,cityImageId:number,imgNumber:number}):void{ this.imgNumber= imageObj.imgNumber; this.imagePath=`assets/images/${imageObj.imageName}.png`; this.cityIma ...

Tips for Customizing the Active Class in Bootstrap 3

After spending the last 3 days searching online and trying various examples, I still can't figure out how to style the active class properly. Below is the HTML code snippet: <div class="navbar navbar-inverse navbar-fixed-top" id="mainHeader" ...

Insert a div element into the JavaScript file

I have a JavaScript code snippet that needs to be inserted after the "Artwork" section. Here is the code: <div class="image-upload"> <label for="files"> <img src="ohtupload.jpg"> </label> </di ...

Retrieving data from websites through variable access

I'm currently facing a challenge in scraping a specific website (URL provided in the code below). When I extract the section of HTML that contains the information I need, all I get are the variable names rather than the actual values. While manually i ...

JQuery restricts input to only allow numbers with a set number of digits, ensuring uniqueness

Can someone assist me in creating a jQuery script that only allows unique numbers with exactly 4 digits in a numeric field? Here is the HTML code: <input type="text" id="registration_number" class="input-sm" name="registration_number" maxlength="6" re ...

Restricting CSS Background Image Width to 960 Pixels

My CSS style is causing my background image to span across the whole screen instead of repeating within the 960px width of the body. body { width:960px; margin: 10px auto 10px auto; background-image:url(logo.png),url(backgroundimage.jpg); ...

IE Browser Exposes Flawed Design in JSP Coding

I have a pair of JSP files. The first one features a table, while the second one contains rows (<tr>) that are meant to be added to the table. Within the second JSP file, I initialize the same action, followed by the <tr> element that should b ...