CSS Tutorial for Beginners Part 24 - Horizontal & Vertical Align | Learn Web Technologies

Опубликовано: 12 Июнь 2026
на канале: Learn Web Technologies
62
4

CSS Playlist
   • CSS Tutorial for Beginners  

HTML Playlist
   • Html tutorial for beginners  

Sign up for my newsletter to receive weekly updates
http://bit.ly/2RRFktj

Join our facebook page
bit.ly/2OKQYV7

CSS Layout - Horizontal & Vertical Align

Center Align Elements
To horizontally center a block element (like div), use margin: auto;

Setting the width of the element will prevent it from stretching out to the edges of its container.

Note: Center aligning has no effect if the width property is not set
(or set to 100%).

Center Align Text
To just center the text inside an element, use text-align: center;

Center an Image
To center an image, set left and right margin to auto and make it into a block element:

Left and Right Align - Using position
One method for aligning elements is to use position: absolute;

Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

Left and Right Align - Using float
Another method for aligning elements is to use the float property:

Note: If an element is taller than the element containing it, and it is floated, it will overflow outside of its container. You can use the "clearfix" hack to fix this (see example below).

for clearfix hack video :    • CSS Tutorial for Beginners Part 22 - CSS f...  

Center Vertically - Using padding
There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding:
To center both vertically and horizontally, use padding and text-align: center:

Center Vertically - Using line-height
Another trick is to use the line-height property with a value that is equal to the height property.

Center Vertically - Using position & transform
If padding and line-height are not options, a third solution is to use positioning and the transform property:

----------------------------------------------------------------------------------------------------------------

#LearnWebTechnologies #CSS #CSSTutorialForBeginners