Yogesh Chauhan's Blog

How to vertically and horizontally align text and image block (without flex or grid) in CSS?

in CSS on February 5, 2021

If we want to align just texts in the center then we could easily use text-align property. But sometime we just want to align the whole text block in the center and it doesn’t work out that simply.

The trick to align the block of texts in center horizontally is to set the margin property correctly and by correctly, I mean set it as ‘auto‘.

Horizontal alignment of a text block

It works the same way for an image block too. Be careful though, it won’t work without the ‘display:block‘ property.

Horizontal alignment of an image block

This is actually CSS2 way of aligning the text block vertically. In CSS3, we can just use grid or text or even just a bit more advanced properties that are not available in CSS2.

We need to add an outer container to the paragraph tag. Set the display as a table cell and then just use vertical-align property.

Vertical alignment of a text block

We can align an image block vertically the exact same way as we did for text above.

Vertical alignment of an image block


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to modify navigation menus in WordPress?WordPressHow to add a Line Chart in Angular App?AngularContainers 101: What are containers?MiscellaneousReview an intentionally vulnerable plugin in WordPressWordPressOpen Source Security Tools for Defense SecurityMiscellaneousA Quick Comparison of JOIN and Subquery in SQLSQL/MySQLList of all Pseudo Elements in CSSCSSCasting in PostgreSQLPostgresWhat’s new in Constructor in PHP 8?PHPHow to Create a Backup From Any Database in SQL?SQL/MySQLShould we ever delete data from a database?MiscellaneousIntroduction to Angular modules Part 1: NgModule metadataAngular