Starting Now: iLoveCoding courses are completely FREE. No login or signup required.

<div> and <span> elements

The <div> and <span> elements although simple, are the most widely used HTML elements. Lets get familiar with them.

FREELevel: Beginner6:36 mins
Course content
Lessons #1: Tools you need for the HTML/CSS course FREE
1:59 mins
Lessons #2: Overview of the VS Code Text Editor FREE
4:57 mins
Lessons #3: Code your first webpage FREE
3:59 mins
Lessons #4: What is HTML Really? FREE
3:16 mins
Lessons #5: What does HTML look like? FREE
2:46 mins
Lessons #6: HTML Elements FREE
7:20 mins
Lessons #7: HTML Elements - Continued FREE
3:36 mins
Lessons #8: Common HTML Elements (Cheatsheet included) FREE
3:26 mins
Lessons #9: HTML is a forgiving language FREE
3:47 mins
Lessons #10: Basic Markup of HTML FREE
5:18 mins
Lessons #11: The <h1>...<h6>, <i> and <strong> elements FREE
5:18 mins
Lessons #12: Learn to link webpages via the anchor <a> element FREE
4:04 mins
Lessons #13: Relative vs Absolute Paths - linking continued FREE
5:50 mins
Lessons #14: <div> and <span> elements FREE
6:36 mins
Lessons #15: Making lists in HTML via <ul>, <ol> and <li> elements FREE
5:36 mins
Lessons #16: Making nested lists + Homework FREE
4:31 mins
Lessons #17: Adding special characters in your webpage FREE
5:50 mins
Lessons #18: Emmet shortcuts and code comments FREE
3:37 mins
Lessons #19: <meta> elements and what they are about FREE
3:19 mins
Lessons #20: <meta> description and keyword elements for SEO FREE
3:33 mins
Lessons #21: <blockquote> and <pre> elements for special text formatting FREE
5:43 mins
Lessons #22: Adding images with the <img> element FREE
5:11 mins
Lessons #23: Using <video> and <audio> elements FREE
5:56 mins
Lessons #24: Semantic Elements + Homework FREE
4:26 mins
Lessons #25: Intro to CSS - Making the webpages beautiful FREE
4:22 mins
Lessons #26: Intro to CSS continued FREE
4:10 mins
Lessons #27: <style> element & using CSS selectors FREE
5:38 mins
Lessons #28: Organize your CSS is its own file FREE
4:55 mins
Lessons #29: CSS id selector (# hash sign) FREE
4:20 mins
Lessons #30: CSS class selector (. dot sign) FREE
3:52 mins
Lessons #31: Select all elements with asterisk (*) selector FREE
0:58 mins
Lessons #32: Using multiple selectors together (using comma and more) FREE
4:40 mins
Lessons #33: CSS Combinators selectors (~ + > etc.) FREE
7:50 mins
Lessons #34: Pseudo Selectors (:visited: :hover :active :focus) FREE
6:04 mins
Lessons #35: Chrome Developer Tools - Intro FREE
6:43 mins
Lessons #36: Colors in CSS FREE
3:19 mins
Lessons #37: Apply multiple CSS classes + write notes with code comments FREE
2:59 mins
Lessons #38: Fonts: Change & add custom fonts FREE
6:20 mins
Lessons #39: Background CSS Property. Change color, images and more FREE
7:39 mins
Lessons #40: Common CSS Units - (px, em, rem, %) FREE
7:01 mins
Lessons #41: Common CSS Units - (rem) FREE
2:39 mins
Lessons #42: CSS Margin and Padding FREE
9:49 mins
Lessons #43: CSS border property FREE
4:00 mins
Lessons #44: CSS border style FREE
2:05 mins
Lessons #45: Size elements with 'width' and 'height' property FREE
3:21 mins
Lessons #46: Use 'box-sizing' for more control on element sizing FREE
3:43 mins
Lessons #47: 'border-radius' Curve borders and make custom shapes. FREE
7:58 mins
Lessons #48: <div> vs <span> the real difference FREE
4:36 mins
Lessons #49: Using 'display' property of CSS & 'inline-block' FREE
3:19 mins
Lessons #50: 'float' & 'clear' property - Moving elements around. FREE
4:38 mins
Lessons #51: Hide content with `overflow`, `visibility` and `display` property FREE
3:36 mins
Lessons #52: Clearfix: Hack to fix webpage spacing issue FREE
3:51 mins
Lessons #53: Project: Blog Design - Create beautiful looking website FREE
5:20 mins
Lessons #54: Project: Blog Design (Part 2/7) - Layout the HTML FREE
9:09 mins
Lessons #55: Project: Blog Design (Part 3/7) - Styling the header FREE
4:01 mins
Lessons #56: Project: Blog Design (Part 4/7) - Styling main and footer area FREE
4:02 mins
Lessons #57: Project: Blog Design (Part 5/7) - Styling sidebar FREE
3:10 mins
Lessons #58: Project: Blog Design (Part 6/7) - Styling articles FREE
2:17 mins
Lessons #59: Project: Blog Design (Part 7/7) - Additional Pages FREE
5:13 mins
Lessons #60: 'position' - Control element position within your page FREE
8:10 mins
Lessons #61: Project: Coupon (using `absolute` & `relative` position) FREE
5:21 mins
Lessons #62: CSS Flexbox & CSS Grid - More layout methods FREE
3:02 mins
Lessons #63: CSS Priority Order FREE
6:04 mins
Lessons #64: Additional Resources - Codepen + HTML Cheatsheet FREE
5:18 mins
Lessons #65: <table> Making tables in HTML FREE
5:32 mins
Lessons #66: <table> Making tables in HTML continued FREE
2:41 mins
Lessons #67: <iframe> Embedding other webpages in your webpage FREE
4:16 mins
Lessons #68: Creating forms and understanding how they work FREE
11:17 mins
Lessons #69: Creating form - continued FREE
6:41 mins
Lessons #70: Using external CSS libraries in your projects FREE
6:42 mins
Lessons #71: Conclusion and next steps FREE
3:28 mins
πŸ˜‡Important: This course is FREE and always will be πŸ™Œ. Feel free to share it with friends. 🎁

HTML Cheatsheet

CSS Cheatsheet

HTML CSS Cheatsheet thumb

The <div> Element

One of the most commonly used elements, is actually the <div> element.

The "div" element stands for "division", which basically means that you organize your different pieces of your HTML inside it's own container.

So "division" or <div> is simply a container, it's a generic container element. It has no special properties but one. Let's take a look at it right now.

So we are in our web page, and let's say I am making a web page for myself, and I want to organize this web page in different sections.

So let's say I have a section for my background, one for my professional life, and one section for my personal life and underneath each of these sections I may have multiple paragraphs, and some text about each of these sections.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>
      This is my first page
    </title>
  </head>
  <body>
    <h1>Hi My name is Aziz</h1>
    <h2>Background</h2>
    <p>sadjalsdjlkds</p>
    <p>skdjalsdkjasldj</p>
    <h2>Professional</h2>
    <p>sdjalsdjlkjds</p>
    <p>skdjalsdkjasldj</p>
    <h2>Personal</h2>
    <p>sdjalsdjlkjds</p>
    <p>skdjalsdkjasldj</p>
  </body>
</html>

When I save and refresh, I see this:

creating multiple sections without div

Now if you look at the code, the code is not easy to read. It's not organized, so we need to make organize each of these sections separately.

To do that, we are going to put each of sections inside their own <div> element, so here's how you do that. I am going to take the following code:

    <h2>Background</h2>
    <p>sadjalsdjlkds</p>
    <p>skdjalsdkjasldj</p> 

The above section (the background and the paragraphs) and wrap it inside a <div> element. Like such:

    <div>
      <h2>Background</h2>
      <p>sadjalsdjlkds</p>
      <p>skdjalsdkjasldj</p>
    </div>

So now the two benefits are that it's easier to read because I know this is one section, and I am going to do the same thing over here for the second and third sections as well:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>
      This is my first page
    </title>
  </head>
  <body>
    <h1>Hi My name is Aziz</h1>

    <div>
      <h2>Background</h2>
      <p>sadjalsdjlkds</p>
      <p>skdjalsdkjasldj</p>
    </div>

    <div>
      <h2>Professional</h2>
      <p>sadjalsdjlkds</p>
      <p>skdjalsdkjasldj</p>
    </div>

    <div>
      <h2>Personal</h2>
      <p>sadjalsdjlkds</p>
      <p>skdjalsdkjasldj</p>
    </div>
  </body>
</html>

Each one of these are now individual sections.

The significance of organizing this in a section, is not for readability; it's actually when we get to the CSS section of our website or of this course, we are going to see that when we have these sections we can actually use the CSS techniques to actually have advanced layouts.

So if I save this and refresh, it's going to have no difference because <div> is a generic element, I have already told you it's just a generic container.

results before we inspect the <h2> element

Just to show you what is happening in the background, when I am right-click on the word "Background" and click "Inspect", it's actually going to open the "Developer tools" and highlight this particular element (The element of the word "Background").

results after we inspect the <h2> element

You will see the background element is highlighted, and all of it's different spacing has been highlighted as well when I hover right there.

So if I click over here, you will see that this entire blue area is highlighted, because this div element has all of these three things inside.

Inspecting the <div> element

The Special Property of <div>

Now one more thing, is that the special property of a div element, is that it takes up all the horizontal real estate that it can find, so it takes up all of this space.

It does not let the second "Professional" <div> element to be on the right hand side; it's actually blocking the entire right hand side.

So that's why it's actually containing all of it's information, and taking up all the horizontal real estate.

So that is the property of a <div> element; not just organizing things in a container, but also taking up all the horizontal real estate.

Now this will become again very powerful and helpful when we get to the CSS section, where we can do advanced layout.

So until then wait along and just think of the <div> element as just a generic container.

The <span> Element

Now a close brother to the div element, is actually the <span> element.

The <span> is also used as a generic container, but the difference is that where the <div> element takes up all the horizontal real estate, the <span> element only takes up the space that it needs.

The <span> element is typically used within a paragraph. Let's say I have a big paragraph over here in the background section.

    <div>
      <h2>Background</h2>
      <p>
        sadjalsdjlkds sadjalsdjlkds
        sadjalsdjlkds sadjalsdjlkds
        sadjalsdjlkds sadjalsdjlkds
        sadjalsdjlkds sadjalsdjlkds

        special text
      </p>
      <p>skdjalsdkjasldj</p>
    </div>

Let's say there is some "special text" over here, and we want to give it some some special attention.

So what are we going to do is we are going to wrap in this particular "special text" inside the <span> element like such:

    <div>
      <h2>Background</h2>
      <p>
        sadjalsdjlkds sadjalsdjlkds
        sadjalsdjlkds sadjalsdjlkds
        sadjalsdjlkds sadjalsdjlkds
        sadjalsdjlkds sadjalsdjlkds
        <span>special text</span>
      </p>
      <p>skdjalsdkjasldj</p>
    </div>

Save, refresh, and you see all of this text is here, but this "special text" is like within the same line, It's actually not taking up all the horizontal space.

showing the special text within the paragraph

So just to prove the point there can be more text over here, I can just put some more text after it.

showing some text after the special text

And you see the rest of the text is over here, and the "special text" just does not expand it's limits, it only takes up the space it needs.

And just to prove that point, I can right-click and inspect, and you see the <span> element gets highlighted just to show that this element is taking up this much space.

Inspecting the <span> element

Now the significance of the <span> element will also become very evident to you when we get to the CSS section.

Until then you just know that <div> and <span> elements are generic containers; <div> takes up all the horizontal speed real-estate that it has, and <span> only takes up space inline within the line space that it needs.

Until then, I will talk to you in the next lesson.

Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login