Updated June 16, 2023
Introduction to HTML Inline-Block
HTML has another type of element called Inline Block Elements. It is only the space occupied and bounded by each and every tag called by defined elements instead of breaking the HTML contents flow. The block-level elements feature mainly we consider as <div> tags same thing we have an Inline Elements tag called <span> primarily focuses on the inline-block elements; there is a slight difference between inline and inline-block elements it allows the user to set width, the height of the Html elements also we have customized the top, bottom and display the content will be inline-block and also paddings will be initialized if the requirements needed.
Syntax of HTML Inline-Block
The basic syntax is followed below:
Syntax:
<html>
<body>
<p><span> ---some html codes ---</span> </p>
</body>
</html>
The above codes are basic syntax for writing the inline-block elements in html. We have used some sets of pre-defined inline-block elements below.
- <a>
- <abbr>
- <acronym>
- <b>
- <bdo>
- <big>
- <br>
- <button>
- <cite>
- <code>
- <dfn>
- <em>
- <i>
- <img>
- <input>
- <kbd>
- <label>
- <map>
- <object>
- <output>
- <q>
- <samp>
- <script>
- <select>
- <small>
- <span>
- <strong>
- <sub>
- <sup>
- <textarea>
- <time>
- <tt>
- <var>
The above tags are pre-defined html inline elements based on the user requirements in the html mainly; we will focus on <span> tag in html inline-block elements. We all know block-level elements always start a new line and take up with full width for given variables, but inline elements do not start a new line. Also, it takes less width when compared to block-level elements, but it has necessary to declare width in the html inline elements. The inline elements will be declared on the inside of the paragraph elements. The <span> element is often used in the container as some text and has no specific required attributes, but the given css style, class, and ids are common when the <span> element is used together with css in some style parts of the text.
How to Create HTML Inline Block?
- As discussed in earlier articles, we have created the HTML inline-block elements in the block-level elements. The HTML elements only take the user to allot the given necessary spaces and appear next to each other on the same line of the HTML tags. We can’t control the height and width of the HTML elements; it is the properties of the inline elements.
- The CSS style attributes called display: inline disregards the padding and margin settings when they need; it will allow the padding and margin values in the HTML. It contains only inline elements and makes block elements appear in a single line with the help of settings like display: inline. We can use CSS styles to set div tags at 50% width if you don’t want to minimize the HTML codes.it is a CSS solution for inline-block elements.
- Sometimes we can use white space:no-wrap is the parent container; it will allow the <div> tag to display in each div inline-block as we expected without chaining the divs together is also referred to as a nested div tag. We have discussed some inline-block elements that will be used for creating layouts with the help of CSS styles.
- Suppose we use two divs tags in HTML displayed with the inline-block elements settings in CSS styles. We can set the width of each tag as 50%, but the second div tag falls below the first div tag.
Code:
<html>
<head>
section {
background: green;
box-sizing: border-box;
padding: 150px;
}
div {
box-sizing: border-box;
display: inline-block;
height: 100px;
padding: 54px;
text-align: center;
width: 53%;
}
.green {
background: lightgreen;
}
.black {
background: black;
}
</head>
<body>
<span style="border: 2px lightgreen">
</span>
<section>
<div class="green">Width: 40%</div>
<div class="black">Width: 60%</div>
</section>
</body>
</html>
Explanation of the above code: In the above codes, we have set the width for two div tags; each will be 50%, and the display attributes will be inline-block. The expected output will vary because the two div tags’ width is 50%, so will change any of the tag values should be 51% or 49%. Still, it is not a good practice and also not enough for the HTML element space; it needs at least 50% because inline elements respect with word spacing between the two div tags in HTML.
We can also use some borders and padding styles in the HTML div tags; it will highlight the web pages more effectively. We use <section> tag called block-level elements for the above codes. We use the <span> tag as the inline element tag; we will use some contents inside the tags that will be required on the web page.
Examples to Implement in Inline-Block
We will discuss the below examples.
Example #1
Code:
<html>
<body>
<p>Sample <span style="border:3px green">Welcome</span>To my domain</p>
<p>Welcome to My Domain</p>
</body>
</html>
Output:
Example #2
Code:
<html>
<head>
<style>
span.first {
display: inline;
width: 150px;
height: 120px;
padding: 8px;
border: 3px blue;
background-color: green;
}
span.second {
display: inline-block;
width: 140px;
height: 110px;
padding: 7px;
border: 3px blue;
background-color: green;
}
span.third {
display: block;
width: 103px;
height: 110px;
padding: 6px;
border: 2px yellow;
background-color: black;
}
</style>
</head>
<body>
<div> Welcome to My Domain
<span class="first">Welcome</span>
<div>
<div> Same Same
<span class="second">Same Same</span>
<div>
<div> Welcome to My Domain
<span class="third">Welcome</span>
<div>
</body>
</html>
Output:
Example #3
Code:
<html>
<head>
<style>
.first {
float:center;
display: inline;
width: 150px;
height: 120px;
padding: 8px;
border: 3px blue;
background-color: green;
}
.1{
clear:center;
}
</style>
</head>
<body>
<div class="first"> <marquee> Welcome to My Domain</marquee>
</div>
</body>
</html>
Output:
Explanation of the above code: The first example will discuss the essential inline element called <span> tag that will use for the web page; the second example will use span in the CSS style, and the same will be called in the <span> tag in body sections each of the CSS styles will be defined in each span third example we will use some text values in the box-limit areas in the inline-block element features.
Conclusion
In Conclusion, partly based on the above topics, we have some ideas about inline-block elements in Html. We use both tag elements for their dependencies and features in user areas. However, IE and other browser versions may or may not support some tags.
Recommended Articles
This is a guide to HTML Inline-Block. Here we discuss basic syntax for writing the inline-block elements and how it works. Along with examples to implement. You can also go through our other related articles to learn more –