Skip to content

Learn how to center anything in CSS — text, images, divs, and elements — explained step by step with simple examples.

Notifications You must be signed in to change notification settings

Sourov-Chandra/css-centering-techniques

Repository files navigation

🎯 Hack the Ways to Center Anything in CSS

CSS এ alignment সেট করা অনেক সময় একটু tricky মনে হতে পারে 🥲
এই README তে আমরা CSS centering-এর সবচেয়ে practical ও commonly-used techniques শিখব — step by step, real examples সহ।

💡 Key idea
text-align: center শুধু inline/inline-block content (text, image) এর জন্য কাজ করে।
কিন্তু width দেওয়া block-level element কে center করতে হলে margin: auto লাগবেই ❌


📌 What You’ll Learn

এই tutorial এ আমরা ৩টি part এ centering শিখব:

  • Part 1: Horizontally centering (text / image / element)
  • Part 2: Div এর ভেতরে vertically, horizontally & both centering
  • Part 3: Viewport (vh/vw) অনুযায়ী full screen centering

=====================================

🔹 Part‑1: Horizontally Centering

=====================================

1️⃣ Center align text

<div class="center">
  <p>This is Sourov</p>
</div>
.center {
  text-align: center;
  border: 4px solid purple;
}

2️⃣ Center align a block element (with width)

.center {
  margin: auto;
  width: 50%;
  border: 4px solid purple;
  padding: 10px;
}

Rule:
Block-level element + width ⇒ margin: 0 auto


3️⃣ Center an image horizontally

<img src="./web-pic.jpg" alt="photo">
img {
  display: block;
  margin-left: auto;
  margin-right: auto;
}

=====================================

🔹 Part‑2: Centering Inside a Div

=====================================

🧠 Common Methods

  • CSS Position
  • Flexbox ✅ (Recommended)
  • CSS Grid

1️⃣ Using CSS Position

Vertical centering only

<div class="container">
  <div class="centered-element">
    <p>Hello, This is Sourov</p>
  </div>
</div>
.container {
  position: relative;
  height: 350px;
  border: 3px solid #006100;
}

.centered-element {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

Horizontal + Vertical centering

.centered-element {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Centering an image using position

<div class="container">
  <div class="centered-element">
    <img src="./web-pic.jpg" alt="photo">
  </div>
</div>
.container {
  position: relative;
  height: 350px;
  border: 4px solid purple;
}

.centered-element {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

👉 Horizontal centering: left: 50% + translateX(-50%)


2️⃣ Using Flexbox (🔥 Best & Cleanest)

Vertical centering

.container {
  display: flex;
  align-items: center;
  height: 350px;
  border: 4px solid purple;
}

Vertical + Horizontal centering

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 350px;
  border: 4px solid purple;
}

Centering an image

<div class="container">
  <img src="./web-pic.jpg" alt="photo">
</div>
.container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 350px;
  border: 4px solid purple;
}

=====================================

🔹 Part‑3: Full Viewport Centering (vh + vw)

=====================================

1️⃣ Center text on full screen

<h1 class="content">Hello!!! This is me !! Sourov!</h1>
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

2️⃣ Center an image on screen

<div class="content">
  <img src="./web-pic.jpg" alt="photo">
</div>
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
}

3️⃣ Center any element

<div class="content">
  <div style="background-color: tomato; width: 200px; height: 200px;"></div>
</div>
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

📝 Final Notes

  • একবারে সব clear না হলে বারবার পড়ুন
  • Code নিজে নিজে লিখে practice করুন
  • Flexbox আপনার best friend ❤️

🚀 Happy Coding!

**~ Let’s_code_your_career ~**

About

Learn how to center anything in CSS — text, images, divs, and elements — explained step by step with simple examples.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors