YogeshChauhan.com
How to vertically and horizontally align text and image block (without flex or grid) in CSS?
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

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript Mar 2 How to zoom an element on hover using CSS? Mar 2 the box-sizing property in CSS

You might also like these

How to Skip or Exclude a Specific URL or File From .htaccess Rewrite Rule?PHPHow to add a Bar Chart in Angular App?Angular3 JavaScript methods to use when you want to go back and forth in historyJavaScriptSorting Object Arrays in JavaScriptJavaScriptHow to solve “framework not found” error in Swift?SwiftReading Multiple Inputs in SwiftSwiftFlash of Invisible Text and Mitt Romney Web Font ProblemCSSHow to add a ribbon inside a container using CSS?CSSSocial Media Colors: Sass VariablesMiscHow to remove N/A from Radio Button list in Drupal?DrupalRIGHT JOIN in PostgresPostgres5 Key Principles Of Good Website UsabilityUI/UX