HTML
HTML
Block elements take up the full width of their container and always start on a new line. They
create a “block” of content. These elements usually have a line break before and after them.
Key Characteristics:
They take up the entire width available (unless you specify a width).
They start on a new line and push other content below them.
They can contain other block elements or inline elements.
Example:
html
Copy code
<div>
<h1>Welcome to My Website</h1>
<p>This is a paragraph of text.</p>
</div>
In this example, the <div>, <h1>, and <p> are all block elements. Each will appear on a new line.
Inline Elements
Inline elements do not start on a new line. They only take up as much width as necessary,
meaning they fit within the flow of the surrounding content. They do not cause line breaks
before or after them.
Key Characteristics:
They do not force line breaks; they flow inline with other elements.
They only take up as much space as they need.
They cannot contain block elements (but can contain other inline elements).
<span>: A generic inline container (used for styling or grouping small pieces of content).
<a>: Links.
<img>: Images.
<strong>: Text that is emphasized (usually bold).
<em>: Text that is emphasized (usually italicized).
Example:
html
Copy code
<p>This is a <strong>bold</strong> word in a paragraph.</p>
In this example, <strong> is an inline element. It doesn't cause a line break and only takes up as
much space as the bolded text.
You can also combine block and inline elements within the same page. For example, you can
have a block element like a <div> that contains several inline elements like <span> or <a>.
Example:
html
Copy code
<div>
<p>This is a paragraph with <span>inline text</span> inside.</p>
<a href="https://example.com">Click here</a>
</div>
In this example, the <div> is a block element, but the <p>, <span>, and <a> inside it are inline
elements.
What is <div>?
The <div> tag in HTML stands for “division”. It’s a block-level container used to group other
elements together so you can style them or organize your webpage.
Think of <div> like a box you can use to hold other HTML elements. It doesn’t have any default
styling or visible borders—you have to add styles with CSS if you want it to look a certain way.
It’s especially useful when you want to control the layout and design of a webpage.
👉 In this example, both the <h1> and <p> are inside the <div>.
👉 They are grouped together so you could style them together with CSS.
css
Copy code
.box {
background-color: lightblue;
padding: 20px;
border: 2px solid black;
}
👉 Now, the <div> will have a light blue background, padding, and a border.
👉 You can style everything inside the <div> by styling the container.
But if you just need a general container with no specific meaning, <div> is perfect!
🎯 In short:
✅ Key characteristics:
Inline element → it stays inside the line of text.
Used for styling a part of text or inline content.
Does not have any default style or appearance.
✅ Basic example:
html
Copy code
<p>This is a <span style="color: red;">red word</span> in a sentence.</p>
👉 Only the word “red word” will appear in red, because we wrapped it in <span> and styled it.
👉 The text inside <span class="highlight"> will have a yellow background and be bold, but
the rest of the sentence stays normal.
🎯 In short:
✅ Use <span> to style or target small parts inside a line of text without breaking the line.
✅ It's like a container for inline content.
✅ It won’t do anything visible until you style it with CSS.