<div> and <span> elements
The <div> and <span> elements although simple, are the most widely used HTML elements. Lets get familiar with them.
Course content
71 lessons- 1Tools you need for the HTML/CSS course1:59
- 2Overview of the VS Code Text Editor4:57
- 3Code your first webpage3:59
- 4What is HTML Really?3:16
- 5What does HTML look like?2:46
- 6HTML Elements7:20
- 7HTML Elements - Continued3:36
- 8Common HTML Elements (Cheatsheet included)3:26
- 9HTML is a forgiving language3:47
- 10Basic Markup of HTML5:18
- 11The <h1>...<h6>, <i> and <strong> elements5:18
- 12Learn to link webpages via the anchor <a> element4:04
- 13Relative vs Absolute Paths - linking continued5:50
- 14<div> and <span> elements6:36
- 15Making lists in HTML via <ul>, <ol> and <li> elements5:36
- 16Making nested lists + Homework4:31
- 17Adding special characters in your webpage5:50
- 18Emmet shortcuts and code comments3:37
- 19<meta> elements and what they are about3:19
- 20<meta> description and keyword elements for SEO3:33
- 21<blockquote> and <pre> elements for special text formatting5:43
- 22Adding images with the <img> element5:11
- 23Using <video> and <audio> elements5:56
- 24Semantic Elements + Homework4:26
- 25Intro to CSS - Making the webpages beautiful4:22
- 26Intro to CSS continued4:10
- 27<style> element & using CSS selectors5:38
- 28Organize your CSS is its own file4:55
- 29CSS id selector (# hash sign)4:20
- 30CSS class selector (. dot sign)3:52
- 31Select all elements with asterisk (*) selector0:58
- 32Using multiple selectors together (using comma and more)4:40
- 33CSS Combinators selectors (~ + > etc.)7:50
- 34Pseudo Selectors (:visited: :hover :active :focus)6:04
- 35Chrome Developer Tools - Intro6:43
- 36Colors in CSS3:19
- 37Apply multiple CSS classes + write notes with code comments2:59
- 38Fonts: Change & add custom fonts6:20
- 39Background CSS Property. Change color, images and more7:39
- 40Common CSS Units - (px, em, rem, %)7:01
- 41Common CSS Units - (rem)2:39
- 42CSS Margin and Padding9:49
- 43CSS border property4:00
- 44CSS border style2:05
- 45Size elements with 'width' and 'height' property3:21
- 46Use 'box-sizing' for more control on element sizing3:43
- 47'border-radius' Curve borders and make custom shapes.7:58
- 48<div> vs <span> the real difference4:36
- 49Using 'display' property of CSS & 'inline-block'3:19
- 50'float' & 'clear' property - Moving elements around.4:38
- 51Hide content with `overflow`, `visibility` and `display` property3:36
- 52Clearfix: Hack to fix webpage spacing issue3:51
- 53Project: Blog Design - Create beautiful looking website5:20
- 54Project: Blog Design (Part 2/7) - Layout the HTML9:09
- 55Project: Blog Design (Part 3/7) - Styling the header4:01
- 56Project: Blog Design (Part 4/7) - Styling main and footer area4:02
- 57Project: Blog Design (Part 5/7) - Styling sidebar3:10
- 58Project: Blog Design (Part 6/7) - Styling articles2:17
- 59Project: Blog Design (Part 7/7) - Additional Pages5:13
- 60'position' - Control element position within your page8:10
- 61Project: Coupon (using `absolute` & `relative` position)5:21
- 62CSS Flexbox & CSS Grid - More layout methods3:02
- 63CSS Priority Order6:04
- 64Additional Resources - Codepen + HTML Cheatsheet5:18
- 65<table> Making tables in HTML5:32
- 66<table> Making tables in HTML continued2:41
- 67<iframe> Embedding other webpages in your webpage4:16
- 68Creating forms and understanding how they work11:17
- 69Creating form - continued6:41
- 70Using external CSS libraries in your projects6:42
- 71Conclusion and next steps3:28
HTML Cheatsheet
- Download HTML Cheatsheet (PDF)
- Star HTML Cheatsheet on Github
- Share HTML/CSS Cheatsheet Link: https://ilovecoding.org/blog/htmlcss-cheatsheet
CSS Cheatsheet
- Download CSS Cheatsheet (PDF)
- Star CSS Cheatsheet on Github
- Share HTML/CSS Cheatsheet Link: https://ilovecoding.org/blog/htmlcss-cheatsheet
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:
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.
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").
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.
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.
So just to prove the point there can be more text over here, I can just put some more text after it.
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.
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.