How to vertically align text inside a flexbox using CSS? - GeeksforGeeks

12 Apr, 2019
CSS flexbox: The flex property in CSS is the combination of flex-grow, flex-shrink, and flex-basis property. It is used to set the length of flexible items. The flex property is much responsive and mobile friendly. It is easy to positioning child elements and the main container. The margin doesn’t collapse with the content margins. Order of any element can be easily changed without editing the HTML section. The flexbox was added to the CSS standards a few years ago to manage space distribution and element alignment. Basically it is one-dimensional layout syntax.
Vertical align to center: The flexbox property is used to set the content to vertical align. The text content can be aligned vertically by setting the following display properties:
align-items
justify-content
flex-direction
align-items and justify-content are the important properties to absolutely center text horizontally and vertically. Horizontally centering is managed by the justify-content property and vertical centering by align-items property.
Example 1: This example set the text content vertically align to center.
<!DOCTYPE html><html>  <head>    <title>        Vertically align text content    </title>          <!-- CSS property to vertical align text        content using flex-box -->    <style>        .GFG {            display: flex;            align-items: center;            justify-content: center;            flex-direction: column;            width: 100%;            text-align: center;            min-height: 400px;            background-color: green;            align-items: center;        }    </style></head>  <body>    <div class="GFG">        <h1>GeeksforGeeks</h1>        <h2>Vertically align text using Flexbox</h2>    </div></body>  </html>
Output:
Example 2: This example illustrates how to set left align text vertically in a flexbox using CSS.
<!DOCTYPE html><html>  <head>    <title>        Vertically align text content    </title>          <!-- CSS property to vertical align text        content using flex-box -->    <style>        .GFG {            display: flex;            align-items: left;            justify-content: left;            flex-direction: column;            text-align: left;            margin: 13% 0;            min-height: 300px;            background-color:green;            align-items: left;        }    </style></head>  <body>    <div class="GFG">        <h1>GeeksforGeeks</h1>              <img src="https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-6.png"         style="display: inline;">    </div></body>  </html>                    
Output:
CSS-Misc
Picked
CSS
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to create footer to stay at the bottom of a Web page?
Types of CSS (Cascading Style Sheet)
Design a web page using HTML and CSS
How to position a div at the bottom of its container using CSS?
Create a Responsive Navbar using ReactJS
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Convert a string to an integer in JavaScript
How to calculate the number of days between two dates in javascript?