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>
Facing issues? Have Questions? Post them here! I am happy to answer!
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
- How to create a circular Image using pure CSS Code
- Horizontally Center Align tag in HTML using CSS
- Add scroll to div element in HTML Css
- Change CSS Background Opacity with Examples
- How to make text bold using CSS
- How to place two div elements next to each other
- Vertically Center Text in a DIV Element
- align image at middle of div element
- Tailwind CSS Hello World Example
- How to make div or text in html unselectable using CSS
- Only Chessboard using HTML and CSS Code Example
- Simple CSS Grid Example
- How to set CSS background-Image Property
- CSS Background Opacity with Examples
- reCAPTCHA Implementation Tutorial
- List of 32 CSS cursors web developers must know about
- How to Vertically Center Align Text in a Div using CSS Code Example
- 10 Must Know CSS Border Styles with Examples
- Tutorial : Simple Lightweight Pure CSS based Vertical Navigation Menu
- CSS: Apply opacity only for div background and not text
- Set Falling Show on Website for Christmas using Pure CSS Code
- How to make jsfiddle bootstrap ready
- Elegant CSS Box Shadows Ideas
- Responsive Web Design with CSS Media Queries: A Beginner's Tutorial [Updated for 2023]
- How to Center Align Image in Bootstrap
More Posts:- java: unclosed string literal [Error] - Java
- How to remove password from pdf file - HowTos
- Parsing Data for android-21 failed unsupported major.minor version 51.0 - Android
- How to add Multiple Rulers in Sublime Text - Sublime-Text
- How to know the version of Ubuntu - Ubuntu
- Fix MySQL ERROR 1045 (28000): Access denied for user root@localhost (using password: YES) - MySQL
- Backup your Mac before you update to macOS Ventura using Time Machine - MacOS
- Fix - zsh: command not found: conda [macOS] - zsh