Html Block and Inline element
Html Block and Inline element
HTML
BLOCK AND INLINE ELEMENT IN HTML
❑ A block-level element always starts on a new line, and the browsers
automatically add some space (a margin) before and after the element.
❑ A block-level element always takes up the full width available (stretches out
to the left and right as far as it can).
❑ Two commonly used block elements are: <p> and <div>.
❑ The <p> element defines a paragraph in an HTML document.
❑ The <div> element defines a division or a section in an HTML document.
Inline Elements
❑ An inline element does not start on a new line.
❑ An inline element only takes up as much width as necessary.
❑ This is a <span> element inside a paragraph.
THE <DIV> ELEMENT
❑ The <div> element is by default a block element, meaning that it takes all
available width, and comes with line breaks before and after
❑ The <div> element is used as a container for other HTML elements.
❑ A <div> element takes up all available width
<div> as a container
❑ The <div> element is often used to group sections of a web page together.
Example
<div>
<h2>London</h2>
<p>London is the capital city of England.</p>
<p>London has over 13 million inhabitants.</p>
</div>
<div>
<h2>Oslo</h2>
<p>Oslo is the capital city of Norway.</p>
<p>Oslo has over 600.000 inhabitants.</p>
</div>
<div>
<h2>Rome</h2>
<p>Rome is the capital city of Italy.</p>
<p>Rome has almost 3 million inhabitants.</p>
</div>
Aligning <div> elements side by side
❑ When building web pages, you often want to have two or more <div> elements
side by side, like this
There are different methods for aligning elements side by side, all include some CSS styling.
Float
❑ The CSS float property was not originally meant to align <div> elements side-by-
side, but has been used for this purpose for many years.
❑ The CSS float property is used for positioning and formatting content and allow
elements float next to each other instead of on top of each other.
<style>
div {
width: 30%;
display: inline-block;
}
Flex </style>
❑ The CSS Flexbox Layout Module was introduced to make it easier to design flexible
responsive layout structure without using float or positioning.
❑ To make the CSS flex method work, surround the <div> elements with
another <div> element and give it the status as a flex container.