Inline vs. block-level elements: a demonstration
Inline vs. block-level elements: a demonstration
HTML (Hypertext Markup Language) elements historically were categorized as either "block-level" elements or "inline-level" elements. Since this is a presentational characteristic it is nowadays specified by CSS in the Flow Layout. Inline elements are those which only occupy the space bounded by the tags defining the element, instead of breaking the flow of the content. In this article, we'll examine HTML inline-level elements and how they differ from block-level elements.
An inline element does not start on a new line and only takes up as much width as necessary.
https://jsfiddle.net/ud25tGNwMddCP7JI/x4juzmed/15/
This is most easily demonstrated with a simple example. First, some simple CSS that we'll be using:
1 2 3 | .highlight { background-color : #ee3 ; } |
Inline
Let's look at the following example which demonstrates an inline element:
1 2 3 | < div >The following span is an < span class="highlight">inline element</ span >; its background has been colored to display both the beginning and end of the inline element's influence.</ div > |
In this example, the <div>
block-level element contains some text. Within that text is a <span>
element, which is an inline element. Because the <span>
element is inline, the paragraph correctly renders as a single, unbroken text flow, like this:
Block-level
Now let's change that <span>
into a block-level element, such as <p>
:
1 2 3 | < div >The following paragraph is a < p class="highlight">block-level element;</ p > its background has been colored to display both the beginning and end of the block-level element's influence.</ div > |
Rendered using the same CSS as before, we get:
See the difference? The <p>
element totally changes the layout of the text, splitting it into three segments: the text before the <p>
, then the <p>
's text, and finally the text following the <p>
.
Changing element levels
You can change the visual presentation of an element using the CSS display
property. For example, by changing the value of display
from "inline"
to "block"
, you can tell the browser to render the inline element in a block box rather than an inline box, and vice versa. However, doing this will not change the category and the content model of the element. For example, even if the display
of the span
element is changed to "block"
, it still would not allow to nest a div
element inside it.
Conceptual differences
In brief, here are the basic conceptual differences between inline and block-level elements:
- Content model
- Generally, inline elements may contain only data and other inline elements. You can't put block elements inside inline elements.
- Formatting
- By default, inline elements do not force a new line to begin in the document flow. Block elements, on the other hand, typically cause a line break to occur (although, as usual, this can be changed using CSS).
List of "inline" elements
The following elements are inline by default (although block and inline elements are no longer defined in HTML 5, use content categories instead):
<a>
<abbr>
<acronym>
<audio>
(if it has visible controls)<b>
<bdi>
<bdo>
<big>
<br>
<button>
<canvas>
<cite>
<code>
<data>
<datalist>
<del>
<dfn>
<em>
<embed>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<label>
<map>
<mark>
<meter>
<noscript>
<object>
<output>
<picture>
<progress>
<q>
<ruby>
<s>
<samp>
<script>
<select>
<slot>
<small>
<span>
<strong>
<sub>
<sup>
<svg>
<template>
<textarea>
<time>
<u>
<tt>
<var>
<video>
<wbr>
See also
https://jsfiddle.net/ud25tGNwMddCP7JI/x4juzmed/15/
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-01-13 How can I manually create a authentication cookie instead of the default method?
2019-01-13 CodeWars上的JavaScript技巧积累
2019-01-13 What's the difference between using “let” and “var” to declare a variable in JavaScript?
2015-01-13 Covariance and Contravariance (C#)
2015-01-13 Excel导出失败的提示