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 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to change your WordPress database prefix?WordPressAdd animation to your skills bar using CSSCSSWhat does it mean by Continuous Integration, Continuous Delivery and Continuous Deployment?MiscellaneousThe unique operator($) in Envision BasicEnvision BasicCREATE DATABASE in PostgreSQLPostgresHow to modify navigation menus in WordPress?WordPressHow to check if the page is single post page in WordPress?WordPressHow to compress images with gulp in WordPress?WordPressUse inline if to make a shorter conditional syntax in ReactReactHow to create an empty array in Swift?SwiftHow to create a simple stopwatch using JavaScript?JavaScriptHow to CREATE TABLE in SQL with and without using Another Table?SQL/MySQL