1. Home
HTML

Learn HTML: A Comprehensive Tutorial for Beginners | Step-by-Step Guide

Learn HTML from scratch! Our tutorial covers basics to advanced concepts. Start coding websites today with step-by-step guidance.

  • 49
  • 12 Hours
right-top-arrow
32

Master HTML Layouts With Block-Level And Inline Elements

Updated on 16/08/2024445 Views

Have you ever noted that specific HTML components appear to start a new section or line while other elements remain on the same line? The more web design experience I've had, the more I realize how crucial it is to comprehend block-level and inline elements in HTML. In this tutorial, I'll highlight these components' distinctive qualities and describe their applications. 

What are Block Elements?

Consider block-level elements the basic components of your HTML layout. They provide your web page with structure and shape. When added, a block-level element in HTML expands to fill the width of its parent container, creating a new line.

Let's describe this using a few typical block-level tags in HTML and their functionalities:

<div> (Division)

Visualize a box that can accommodate more components, such as blocks inside a bigger block. This tag works well for simultaneously applying styles to multiple items, organizing content, and building layouts. 

Do you require a sidebar? Make use of a <div>

Developing a section on content? Make use of a <div>

It's among HTML's most adaptable tools.

Code:

<html>

<head>

<title>Using div tag</title>

<style>

.mydiv {

            border: 5px outset rgb(255, 0, 0);

            background-color: rgb(223, 213, 31);    

            text-align: center;

}

</style>

</head>

<body>

    <h1>The div Element</h1>

<div class ="mydiv">

    <h2>This is a heading in a</h2>

    <p>This is the text inside the div tag</p>

</div>

<p>This is the text outside the div tag</p>

</body>

</html>

<p> (Paragraph)

Use this tag whenever you want to format text into paragraphs. A new line with some built-in gap above and below is created when you add a <p>. It's ideal for dividing lengthy passages of text into manageable portions. I’d recommend using it to arrange content when you’re writing an article or blog post.

Code:

<!DOCTYPE html>

<html>

<body>

<h2>Welcome User</h2>

<!-- Use of <p> tag -->

<p>This a paragraph</p>

</body>

</html>

From <h1> to <h6> Headings

<h1> is the largest and most significant headers, while <h6> is the smallest. Make use of them to arrange your information in a hierarchy. Your primary title is usually <h1>, and subheadings are <h2> through <h6>. Users will find it easier to explore and understand the information flow with this framework in place.

Code:

<!DOCTYPE html>

<html>

<head>

    <title>Different Headings in HTML</title>

</head>

<body>

<h1>This is a heading 1</h1>

<h2>This is a heading 2</h1>

<h3>This is a heading 3</h1>

<h4>This is a heading 4</h4>

<h5>This is a heading 5</h5>

<h6>This is a heading 6</h6>

</body>

</html>

<ul> (Unordered List)

Use this tag if you require a list formatted with bullet points. Use it for features, grocery lists, or tasks that don't have to be done in a particular order. It's an easy method for arranging data in a way that's convenient for users.

Syntax:

<ul>

<l1> Item 1</li>

<li>Item2</li>

.

.

</ul>

Code:

<!DOCTYPE html>

<html>

<head>

    <title>Unordered List in HTML</title>

</head>

<body>

<h2>Hello User</h1>

<h3>This is a HTML Unordered list</h1>

<h4>Coding Languages</h4>

    <ul>

        <li>C</li>

        <li>C++</li>

        <li>Java</li>

        <li>HTML & CSS</li>

    </ul>

</body>

</html>

These block elements in HTML span the entire width of their container, beginning on a new line. This feature provides the framework of a webpage and facilitates the use of CSS styling. Make a neat, orderly, and visually appealing website by knowing when and how to apply these block level elements. To start creating your website, pick up your preferred code editor and experiment with these building elements!

What are Inline Elements?

The purpose of inline elements in HTML is to remain inside the current text or content line. This implies that they don't result in unplanned pauses or excessive spacing. Like parts, they fit in naturally. Typical inline tags in HTML and their applications are as follows:

<span> (Span)

Consider this as a flexible container. To wrap words or text portions without breaking the line, use <span>. It is one of the inline elements in HTML that is really helpful for altering material or applying particular styles without breaking the flow. For instance, you can use CSS styling and a <span> to make a word red inside a sentence.

Syntax:

<span class="">Some Text</span>

Code:

<!DOCTYPE html>  

<html>  

<head>  

 <title>Span Tag</title>  

 </head>  

<body>  

  <h2>Span tag in HTML</h2>  

  <p>My favourite colors are 

    <span style="color: red;">red</span>,   

    <span style="color: blue;">blue</span>, and  

    <span style="color: green;">green</span>  

  </p>  

</body>  

</html>  

<a> (Link/Anchor)

This is one of the inline elements in the HTML tag used to create hyperlinks. You can include interactive links in text that point to different websites, email addresses, or parts of the same page. Links can be readily inserted into paragraphs because they are inline, maintaining the flow of the text.

Syntax:

<a href = "..........."> Link Text </a>

Code:

<!DOCTYPE html>  

<html>  

<head>  

    <title>Anchor Tag in HTML</title>  

</head>  

<body>

    <h1>Welcome to upGarad</h1>

<p>Click on <a href = "https://www.upgrad.com /" target = "_blank" > this-link </a > to go to the homepage of upGrad.</p>  

</body>  

</html> 

<em> (Highlight)

You can use this tag to emphasize text. However, it usually results in italicized text. You can use <em> to emphasize a word or phrase. I usually insert it in a phrase without starting a new line because it fits in the line.

Syntax:

<em>Write some text here.....</em>  

Code:

<!DOCTYPE html>  

<html>  

<head>  

<title>Em Tag</title>  

    <style>  

          h2{  

                color:#f40505;  

                          }  

 </style>  

 </head>  

 <body>  

 <h2>HTML em Tag</h2>  

 <p>This is <em> HTML em tag</em> and it will emphasis the <em> important text</em> of the sentence</p>  

</body>  

</html>  

<img> (Image)

Inline images allow you to add images to text without disrupting the layout. To keep things cohesive, you can include small visuals or icons within a line of text using the <img> tag.

Syntax:

<img src="" alt="" width="" height="">

Code:

<!DOCTYPE html>  

<html>  

<head>  

    <title>HTML img Tag</title>  

<head>

<body>

<h2>HTML Image Example</h2>  

<img src="img3.jpg" width =300px height =200px alt="Secure your data"/>  

</body>

Moreover, to add background images in HTML, keep reading for a step-by-step guide explaining everything you need.

Try It Out Now on Your Own

Now that you know what an inline element in HTML is, let's put it into effect. Try using some inline elements in HTML, such as <span>, <a>, and <em>, in an introductory paragraph you write. Try adjusting the styles or adding links and see how these components maintain the layout without deviating. This practical technique will help you better understand how HTML inline components function.

What are the Impact of Block and Inline Elements on Web Page Layout? 

Let’s talk about the differences in behavior between inline and block elements in HTML here. This distinction greatly influences the layout and organization of web pages. Determining them is essential to creating functional and aesthetically pleasing websites.

Think of a standard web page with a header, body, and footer. Block components such as <header>, <main>, and <footer> will show up on separate lines and will help you distinguish between different sections. This structure makes it simpler for users to navigate the page and offers a logical flow.

On the other hand, inline elements in HTML do not result in line breaks. They merely take up the necessary space and blend in with the text. This behavior is ideal for adding content without changing the layout that is already in place. For instance, you can apply particular styles or interactions without adding additional lines by using inline components like <span>, <a>, or <em>.

Combining inline block elements in HTML will allow you to make visually appealing and highly functioning websites.

How Do You Convert Between Block and Inline Elements?

Altering between multiple styles of content arrangement on a webpage is analogous to converting between block and inline elements in HTML. The show attribute in CSS allows for this change. 

Converting a Block to Inline Elements in HTML

A block element becomes inline when it is changed from being a new line to flowing with the content. This works well for adding minor components, such as icons or inline links, without breaking the flow.

Converting Incline to Block Elements in HTML

The opposite is true when converting an inline to a block; you want the element to fill the entire width and begin on a new line. This is handy when you wish inline elements in HTML like as or to behave like blocks for layout purposes.

So, as we saw, block-level and inline elements in HTML allow you to insert more minor elements without disrupting the flow. Together, they help create a well-structured and flexible webpage. By mastering the structure of an HTML document, you can use these elements to craft a seamless user experience.

In Summary

To sum up, inline and block elements are crucial resources for building structured and aesthetically beautiful websites. While inline elements in HTML like <span>, <a>, and <em> allow you to add style and interaction without detracting from the text's natural flow, block elements like <div>, <p>, and <h1> provide the framework for defining discrete parts. 

As you practice, you'll become increasingly adept at detecting how these components affect the user experience. 

Lastly, go to upGrad for detailed explanations of various HTML topics. It provides many short-term tutorials and courses targeted at professionals and students who want to hone and pick up new skills.  Have fun with coding!

Frequently Asked Questions

1. What is block and inline in HTML? 

In HTML block and inline are elements that are used to facilitate different kinds of designs on a webpage.  

2. What is the difference between block and inline elements? 

Block and inline elements, being quite fundamental to HTML, have their own unique use cases. Block elements like <p>, <div>, <h1> are used as a vertical stack, beginning on a new line. They occupy the entire width of the container and thus can be used for major sections of a webpage. Inline elements on the other hand are used to flow along with the surrounding content. They occupy only the space allocated to them and thus can be used for adjustments within the texts.

3. How do you use inline in HTML? 

You use inline in HTML to insert content without causing line breaks, allowing text or other elements to flow naturally.

4. Can inline elements contain block elements? 

No, inline elements in HTML should not contain block elements, which can lead to unexpected behavior and layout issues.

5. When should I use block elements vs. inline elements? 

Use block elements when you need structure and separation in the layout, and use inline elements in HTML to maintain text flow and embed more minor elements without breaking lines.

6. How do I change blocks to inline in HTML? 

Change block elements to inline in HTML by setting their CSS `display` property to `inline` or `inline-block.`

7. Are there any HTML elements that blur the line between block and inline? 

Yes, elements like `<img>,` `<input>,` and `<button>` can behave as inline elements in HTML but also exhibit block-like characteristics, depending on their styling or context.

Rohan Vats

Rohan Vats

Software Engineering Manager @ upGrad. Assionate about building large scale web apps with delightful experiences. In pursuit of transforming engi…Read More

Get Free Career Counselling
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...