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

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 invalidate all existing cookies using Security Keys in WordPress?WordPressWhat does JSX do in React?ReactHow to create a flip pricing table using CSS and JavaScript?CSSWordPress: How to print ACF repeater field values?WordPressHow to control file extensions with an .htaccess file?MiscellaneousKilling A Project Part 3: How can an organization ensure that a doomed project is killed as early as possible?MiscellaneousSelf-Driving and Intelligent NetworksMiscellaneousJSON.stringify() in JavaScriptJavaScriptWhat’s the difference between variables in CSS and SCSS (Sass)?CSSMicroservices vs Monolithic ArchitectureMiscellaneousWordPress: How to change a blog address (URL)?WordPressA simple example on grid ‘auto-fill’ vs ‘auto-fit’CSS