#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Align

Aligning things in CSS means placing text or boxes horizontally and vertically inside their container. There is no single property that does it all; the right tool depends on whether you are aligning inline text, a block element, or items inside a flex or grid container. Knowing which situation you are in is most of the battle.

Aligning text and inline content

text-align controls how inline content (text, links, images, inline-block elements) sits within a block. Values include left, right, center and justify. It is set on the parent, not the item. A common confusion is applying text-align: center to a div and expecting the div itself to move; it centres the content inside, while the div keeps its full width.

For vertical alignment of inline elements relative to the text line, vertical-align accepts values such as middle, top and baseline. It is handy for lining up an icon with text, and it also works in table cells. It does nothing on ordinary block elements.

Centring a block horizontally

A block element with a set width can be centred with automatic side margins: margin: 0 auto;. The browser splits the leftover space equally on both sides. Without a width (or max-width), the block fills the whole line and there is no space left to share, so nothing appears to happen.

Flexbox: alignment on two axes

Flexbox made alignment far easier. Once the parent has display: flex, two properties do most of the work:

  • justify-content aligns items along the main axis (horizontal by default): flex-start, center, space-between, space-around, space-evenly.
  • align-items aligns items on the cross axis (vertical by default): stretch, center, flex-start, flex-end, baseline.
/* Perfect centring with flexbox */
.hero {
  display: flex;
  justify-content: center;  /* horizontal */
  align-items: center;      /* vertical */
  min-height: 60vh;
}

/* Navbar: logo left, links right */
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* Grid shortcut: centre a single child */
.badge-box {
  display: grid;
  place-items: center;
}

If you switch to flex-direction: column, the axes swap. Now justify-content works vertically and align-items works horizontally. Forgetting this swap is a classic source of "why is my centring not working" moments.

Grid alignment

CSS Grid uses the same property names plus a few extras. justify-items and align-items place items inside their grid cells, and place-items: center sets both at once. For a single item that needs to break the rule, use justify-self or align-self on that child.

Why vertical centring seems to fail

The number one mistake is centring vertically inside a container with no height. A div is only as tall as its content by default, so the content is already "centred" in a box that hugs it exactly. Give the container a height or min-height first, as in the .hero example above.

Older tricks such as position: absolute with top: 50% and transform: translate(-50%, -50%) still work and are useful for overlays, but for normal layout flexbox or grid is simpler and less fragile.

Takeaway: use text-align for inline content, margin: 0 auto for fixed-width blocks, and flexbox or grid for anything that needs vertical centring, making sure the container actually has height.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course