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 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
PHP arrays for US states’ full names and abbreviationsPHPObject destructuring in JavaScript: Unpacking fields from objects passed as function parameterJavaScriptSequence generator (range) using JavaScript Array.from()JavaScriptHow to add Go Back button in Swift 5?SwiftList of WordPress directories functionsWordPressIntroduction to Angular modules Part 3: NgModules vs JavaScript modules and Angular librariesAngular12 URLSearchParams methods in JavaScriptJavaScriptHow to list all PHP variables to debug the script?PHPHow to check if the element exists using JavaScript and jQuery?JavaScriptArbitrary Arguments in SCSS functionsSCSSContent types in DrupalDrupalHow to add recaptcha version 3 to PHP website?PHP