YogeshChauhan . com

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

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to set opacity or transparency using CSS?CSSKanban vs Scrum: The two frameworks of agile principlesMiscWHERE Clause in PostgresPostgresThe Difference Between isNaN() Method And isNaN() Function In JavaScriptJavaScriptHow to render lists inside a component in React?Reactinclude, include_once, require, require_once in PHPPHPList of WordPress directories functionsWordPressHow to create a dynamic countdown using HTML and JavaScript?HTMLWordPress: How to query all posts from custom post type and display them in a list?WordPressHow to remove WordPress TinyMCE Editor buttons?WordPressWhat’s a Log File and What are Log File Monitors?MiscPHP Login System using PDO Part 1: Create User Registration PagePHP