HTML Tutorial

HTML Div

div is a generic box. Use it for layout groups when no more specific tag exists.

A generic box

div stands for division. It is a block element with no built-in meaning. Browsers do not treat it as a heading, a paragraph, a list, or a landmark. They treat it as a box that groups whatever you put inside.

That makes div useful for layout: a card, a row of columns, a toolbar, a wrapper around two related blocks. It also makes div easy to overuse. If a real tag already names the region, use that tag first.

Grouping related content

Put elements that belong together in one div. You can then move, hide, or style the group as a unit instead of touching each child separately.

Example

<div>
  <h2>Lunch special</h2>
  <p>Tomato soup and sourdough.</p>
  <p>Available until 14:00.</p>
</div>

<div>
  <h2>Hours</h2>
  <p>Tuesday to Sunday, 8–16.</p>
</div>

Each div is one group. CSS can give the special a border and the hours a different background without changing the HTML of the headings and paragraphs.

CSS treats divs as boxes

Layout in CSS is a game of boxes. A div is the default box: block-level, as wide as its parent, as tall as its content. Padding, border, margin, flex, and grid all apply to it.

Example

<style>
  .card {
    border: 1px solid #cbd5e1;
    padding: 1rem;
    max-width: 20rem;
  }
</style>

<div class="card">
  <h2>Espresso</h2>
  <p>Short, hot, 2.50.</p>
</div>

Without CSS, the div is invisible. With a class and a few rules, it becomes a card. Open this in/html/try and change the padding to see the box grow.

Prefer header, main, and article when they fit

A div does not tell assistive technology or search engines what the region is. Landmark tags do:

  • header — the banner of the page or of an article
  • nav — a list of links used to move around
  • main — the unique content of this page (one per page)
  • article — a self-contained piece, such as a post or a product
  • aside — related extras, such as a tip or a related list
  • footer — the closing strip: credits, contact, legal

If the group is “the site header”, write <header>, not <div>. The layout chapter later builds a full page with those tags. Use div for the leftover boxes that have no name of their own: a two-column wrap, a button row, a card shell.

A useful test: if you can replace div with header, main, orarticle and the sentence still makes sense, use the specific tag.

Nested divs

Boxes nest. An outer div can hold a row; inner divs can hold each column. Keep the nesting shallow. Three levels is already a lot for a beginner page.

Example

<div class="row">
  <div class="col">
    <h2>Eat in</h2>
    <p>Find a table. Order at the counter.</p>
  </div>
  <div class="col">
    <h2>Takeaway</h2>
    <p>Ready in about ten minutes.</p>
  </div>
</div>

Later, CSS grid or flexbox on .row can place the two .col boxes side by side. The HTML only says: these two groups sit inside one parent.

When a div is the right choice

You needPrefer
The page bannerheader
The main unique contentmain
A self-contained story or card of contentarticle
A paragraph of textp
A list of itemsul or ol
A word inside a sentencespan
A layout wrapper with no extra meaningdiv

Name the box with a class

Bare divs are hard to style later. Add a class that describes the role of the box:card, row, toolbar. Do not use the class as a substitute for a semantic tag. The next chapter is how classes work, including one element with several class names.

Nested divs are not a layout system by themselves. They only group. CSS (flex, grid, width) does the placing. Do not use HTML tables to fake columns — that is covered in the layout chapter.

Worked examples

The short listings above show the tag in isolation. These pages use the same markup on documents you would actually publish: a lab report, a clinic form, a timetable, a weather card.

HTML does not calculate. It names the pieces so a browser, a screen reader, and a search engine can tell a heading from a paragraph. The numbers below are classroom values — the same Ohm, pH, and pulse figures as the C track — now sitting in real page structure.

Preview them in the HTML editor at /html/try. Change a heading or a number and watch the page, not a print log.

Biology

A vitals card when no better tag exists

div is a generic block. Use it when you need a box and no semantic tag fits. A group of readings can be a div until you learn article or section.

Prefer header, main, nav, article when they match. div is the leftover, not the default for every region.

Example

<div style="background:#fef3c7;padding:0.8rem">
  <h2>Vitals</h2>
  <p>36.8 °C · 72 bpm</p>
</div>

Maths

Two formula tiles

Two divs side by side need CSS (flex or grid). Alone, each div stacks. That stack is still better than a table used as a layout grid.

Example

<div style="background:#e0e7ff;padding:0.6rem;margin-bottom:0.4rem">A = 12 m²</div>
<div style="background:#e0e7ff;padding:0.6rem">c = 5 m</div>

FAQ: HTML Div

Common questions about this page.

What is the StudyGrid HTML tutorial?

The StudyGrid HTML tutorial is a full beginner track: tags, headings, links, images, tables, layout, forms, and media. Each chapter has copy-and-run examples.

Should I run html div examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn html div in this HTML HTML lesson (HTML Div).

Is the HTML editor the same as Try Python?

No. Try HTML is a live page preview at /html/try. Try Python stays at /try and runs Python. HTML lessons never open the Python editor.

Do I need to install anything to learn HTML?

No. Open a chapter, click Try it in HTML, and the page preview updates in the browser. You can also download a .html file and open it locally.

Where should I start the HTML tutorial?

Start at HTML Intro, then Basic, Elements, and Attributes. After the first document, continue to headings, links, and forms. Use Next at the bottom of each chapter.

Is the HTML tutorial free?

Yes. The HTML studio on StudyGrid (studygrid.in) is free: dashboard, chapters, and the live preview editor.