HTML Div

The generic container for grouping content.

✍️ 3 exercises + code challenge in this lesson

The <div> element is a block-level container with no special meaning. It is used to group other elements so you can style or position them together.

Centering and side-by-side divs

  • margin: auto with a fixed width centres a div.
  • display: flex on a parent places child divs side by side.
Before reaching for a div, check whether a semantic element (<header>, <section>, <article>...) describes the content better.

Key Takeaways: HTML Div

Mastering HTML Div helps you write clean and reliable HTML code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

HTML Div Code Example

HTML
<style>
  .card { width: 280px; margin: 0 auto 16px; padding: 16px; border-radius: 10px; background: #071A2F; color: white; }
  .row  { display: flex; gap: 10px; }
  .row div { flex: 1; padding: 20px; background: #2563EB; color: white; text-align: center; border-radius: 8px; }
</style>

<div class="card">A centred div card</div>
<div class="row">
  <div>One</div>
  <div>Two</div>
  <div>Three</div>
</div>
Free sign-in required · Google login available

HTML Div Practice Exercises

Check what you learned in “HTML Div”.

/ 3 solved
Fill in the blank

Use the block-level container element.

< class="card">
  <h2>Title</h2>
</>
✓ Correct! Not quite. Try again.
Multiple choice

What is the <div> element mainly used for?

✓ Correct! Not quite. Try again.
Code challenge

Group the heading and paragraph inside a <div> with the class "box".

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services