Achieving uniform space between divs using flex

This is the layout setup I am aiming for using display:flex. My goal is to have text1 displayed at the top, text3 at the bottom, and text2 in the middle.

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

However, the actual outcome looks like this:

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

I thought that by using align-self, I could position the top and bottom divs accordingly.

What did I overlook?

Here is the link to my HTML markup and CSS stylesheet.

HTML:

<div class=container>

  <div class='logo-container hidden-xs'>
    <span>LOGO on the side</span>
  </div>
  <div class='text-container' > 
    <div class='visible-xs'>
      <span>LOGO</span>
    </div>
    <div class=text1>
      Text1
    </div>
    <div class=text2>
      Text 2
    </div>
    <div class=text3>
      Text 3
    </div>
  </div>  

</div>

CSS:

.container {
  display: flex;
  /* flex-direction: row;  no effect */
  border: 1px;
  border-style: solid;
  height: 300px;
}

.container span {
  padding: 5px;
  border: 1px;
  border-style: solid;
  border-color: blue;
}

.text-container {
  flex-direction: column;
  border: 1px;
  border-style: solid;
  border-color: red;
}

.do-i-need-this {
  display: flex;
  flex-direction: row;
} 

.text1 {
  align-self: flex-start;  
  border: 1px;
  border-style: solid;
  border-color: green;

}

.text2 {

  border: 1px;
  border-style: solid;
  border-color: green;

}


.text3 {
  align-self: flex-end;
  border: 1px;
  border-style: solid;
  border-color: green;
}

Answer №1

Include the following in your text-container style

display: flex;
justify-content: space-between;

You no longer need to use align-self

Answer №2

The response from @EdDogan is accurate. I will further elaborate on the solution provided with additional details.

One of the main challenges you are encountering in achieving your desired layout is the absence of a flex container for the text elements. To address this issue, .text-container should have the property display: flex applied to enable the use of flex properties on its child elements.

To align the flex items at opposite ends with one centered, utilize justify-content: space-between, which is ideal for this scenario (especially when flex-direction is set to column for vertical alignment).

.text-container {
    display: flex; /* new */
    justify-content: space-between; /* new */
    flex-direction: column;
}

Revised Codepen Link

A few additional points to consider:

  • If the middle item simply needs to be positioned between the others, using justify-content: space-between (or

    space-around</code) will suffice.</p>
    
    <p>However, if precise centering of the middle item within the container is required, take note that the <code>justify-content
    method is effective only when all items share the same dimensions. Any discrepancies in height (or width for flex-direction: row) among the items can lead to misalignment issues.

    For more insights, a demonstration, and a potential workaround, reference Box #76 here:

  • In relation to:

    .container {   /* flex-direction: row;  no effect */ }
    

    Upon establishing a flex container (by applying either display: flex or display: inline-flex to an element), default rules like flex-direction: row come into play automatically. Therefore, your explicit declaration of this rule is redundant and will not alter the layout.

  • In relation to:

    .text1 { align-self: flex-start; }
    
    .text3 { align-self: flex-end; }
    

    The primary reason these directives do not function as intended is because the parent element of these items lacks the display: flex attribute. As a result, any flex-related properties on the child elements remain disregarded.

    Addtionally, even with display: flex enabled, being in flex-direction: column implies that the cross-axis is horizontal, causing align- properties to influence the left/right positioning rather than up/down alignment.

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

Firebase monitors the authentication status of a user

Trying to maintain a global state in my application based on whether the user is logged in or not has me puzzled. Should I only have a single onAuthStateChanged() in my JavaScript file to monitor state changes and manipulate HTML elements like account deta ...

Having trouble getting your Bootstrap v4 carousel to function properly?

Currently, I have implemented the carousel feature from Bootstrap v4 in a Vue web application. I am following the same structure as shown in the sample example provided by Bootstrap, but unfortunately, it is not functioning correctly on my local setup and ...

Leveraging Enum with sec:authorize="hasRole(...)" for user authentication in a Spring Boot and Thymeleaf application

Trying to make a change: <div sec:authorize="hasRole('ADMIN')"></div> to: <div sec:authorize="hasRole(${T(com.mypackage.Role).ADMIN.getText()})"></div> However, the above modification is not working. ...

Unexpectedly, the digit '1' has mysteriously appeared on my website without any explanation

I have noticed that the number "1" is appearing on my website without me having implemented it myself. Currently, I am using a template.php file to manage page creation. I simply import the content code into a $the_content variable and then print it. To ...

Interactive div containing elements that cannot be clicked

http://codepen.io/anon/pen/zxoYBN To demonstrate my issue, I created a small example where there is a link button within a div that toggles another div when clicked. However, I want the link button to not trigger the toggle event and be excluded from the ...

Using the CSS trick of First-of-type and Last-of-type can result in shortened rounded edges when viewed in IE11

When using the radio+label trick to style radio buttons, a peculiar issue arises in Internet Explorer 11 (IE11). The first and last buttons in each set appear to have their bottom parts cut off or shortened, depending on the background displayed. Removing ...

Is it possible to create two header columns for the same column within a Material UI table design?

In my Material UI table, I am looking to create a unique header setup. The last column's header will actually share the same space as the previous one. Picture it like this: there are 4 headers displayed at the top, but only 3 distinct columns undern ...

What could be causing the constant issue of receiving an empty $_FILE when attempting to upload a file using ajax and php

I am currently utilizing ajax to send form data and upload files from the client to the server (php). An issue arises when attempting to access the file content on the server side - for reasons unclear, the $_FILES variable appears empty regardless of my ...

What is the process for setting up a template to save my HTML document while developing a web application using Python's Flask Framework in the PyCharm IDE?

I am currently working on a tutorial with FreeCodeCamp that involves using Python's Flask Framework to develop a web application in PyCharm. However, I have hit a roadblock at a part that mentions 'Flask searches for HTML files in a directory nam ...

Is it feasible to animate a JQuery slider?

Is there a way to animate the slider using jQuery? I want to be able to press a button and have the inner part of the slider move slower (e.g. 300ms) without reacting to mouse clicks and slides. Here is the code: http://jsfiddle.net/gm4tG/5/ HTML: <d ...

Image background for the table

I am attempting to set a background image for table cells that will be displayed on mobile phones. Since the image is quite large, I need to ensure it responds well to different screen sizes (iPhone, Android, etc). To achieve this, I have used the followin ...

Adjust the parent div's background when hovering over it

Here is the code snippet I am working with: <div class="thumb_image_holder_orange"> <img src="http://dummyimage.com/114x64/000/fff.png" /> </div> I want to change the background of the div when hovering over the image, rather than j ...

Styling text in table cells using only CSS, without the use of scripting, based on the values within those cells

I have scoured the forum and came across several older threads that suggest it is not achievable with just CSS (the system I am utilizing does not support scripts). Is there a possibility that this has changed or if someone else has an alternative idea? W ...

React Application Appearing Distinct in Localhost and gh-pages Environment

Hello, I am new to React and currently working on building a portfolio. I encountered an issue while using the react-mdl library for the projects page. Specifically, when viewing my project on a mobile interface (tested on FireFox, Chrome dev tools, and On ...

Having trouble with the contact form sending emails

My contact form box seems to be having trouble directing the mail to my email. When I hit the send message button, it just refreshes the page in a continuous loop. <div class="wow fadeInUp col-md-6 col-sm-12" data-wow-delay="1.6s"> <h1>I ...

The BottomNavigation component in MUI has a minimum size limit of 400px when it displays 5 items

My issue involves a bottom navigation bar with 5 elements. When the window is resized to less than 400px, the bottom navigation does not shrink, instead maintaining a minimum width of 400px and causing a scrollbar to appear on the x-axis. You can view a m ...

Alignment of nav-item with superimposed right position

Can someone help me align my navbar elements in Bootstrap 4? I want all elements aligned to the right, except for one that needs to be centered: <ul class="nav navbar-nav"> <li class="nav-item float-xs-right"> <a class="nav-link" ...

Tips on creating a View with flexbox in a React Native component

https://i.sstatic.net/nWdac.png Here is the layout I'm aiming for, with a focus on the products. This is what I tried: I created a component called "AllProducts" and set its flexDirection to row, then bound my Product Array to it. However, this was ...

Illuminated Box with Text Beneath Image Overlay

Is there a way to customize the text displayed under each zoomed-in image even further, using images, etc. instead of just relying on the alt text? An ideal solution would involve displaying the text in a div. You can find the codepen here: // Looking ...

Modifying the color of drawings on a Javascript canvas

I am currently working on developing a drawing board using HTML and JavaScript (Node.js on the server side). One challenge I'm facing is implementing a color picker that allows users to change the paint color dynamically. While I could hard code the c ...