
Let us take a look at how to vertically center text inside a <div> element using CSS,
Step 1: Let's create a div element with some text
<div>This is some text I want to center<div>
Step 2: Let's add some border and background
Adding border and background just to see if we are able to achieve the center alignment of the text within the div
.center_text {
border:1px solid #444;
background:#22929266
}
<div class="center_text">This is some text I want to center<div>
Step 3: Let's add width and height to the div
Again! This is just for the demo purpose, you may change the dimensions according to your needs.
.center_text {
border:1px solid #444;
background:#22929266;
width:400px;
height:200px
}
<div class="center_text">This is some text I want to center<div>
Step 4: Add property line-height
Make sure to add line-height exactly the same as that you have set for your div element. In our case, it is 200px
.center_text {
border:1px solid #444;
background:#22929266;
width:400px;
height:200px
line-height:200px
}
<div class="center_text">This is some text I want to center<div>
You will see that the text will be vertically aligned to the center of your div element.
Step 5: Add propertytext-align
Adding the property text-align:center will make the text-align at the center of the element both vertically and horizontally.
.center_text {
border:1px solid #444;
background:#22929266;
width:400px;
height:200px
line-height:200px
text-align: center;
}
<div class="center_text">This is some text I want to center<div>
Have Questions? Post them here!
- How to make jsfiddle bootstrap ready
- How to place two div elements next to each other
- List of 32 CSS cursors web developers must know about
- How to create a circular Image using pure CSS Code
- How to set CSS background-Image Property
- Tutorial : Simple Lightweight Pure CSS based Vertical Navigation Menu
- Only Chessboard using HTML and CSS Code Example
- Set Falling Show on Website for Christmas using Pure CSS Code
- Horizontally Center Align
- How to Center Align Image in Bootstrap
- align image at middle of div element
- CSS Media Query Tutorial for Responsive Mobile First Web Design
- reCAPTCHA Implementation Tutorial
- Add scroll to div element in HTML Css
- CSS: Apply opacity only for div background and not text
- How to make div or text in html unselectable using CSS
- How to list all tables using Java JDBC - Java
- Installing and using unzip Command to unzip a zip file using Terminal - Linux
- Xcode 13 - unknown error compiling for iOS 15.0 but module has a minimum deployment target of iOS 15.2 - iOS
- How to check your installed version of Git - Git
- How to open terminal on Mac to run commands - MacOS
- Fix Microsoft Teams Admin Center error - The Security zone setting isn't configured correctly - Teams
- How to change Chrome Spell Check from UK English to US English - Chrome
- Fix: TypeError: can only concatenate str (not int) to str in Python - Python