search
HomeWeb Front-endHTML TutorialDetailed tutorial on converting between inline and block-level elements

On January 5, 2015, I went to an Internet startup company in Beijing for an interview. I felt the atmosphere of young people working together there. The recruitment column said that they wanted to have several years of web front-end work experience. I relied on my little ability to go ahead without overestimating my abilities. As a result, the interviewer asked me many questions that I had been exposed to. However, unfortunately, I only had a general understanding of those knowledge points, but I knew very little about the actual application of these knowledge points. During the interview, I was embarrassed because I always felt like I was wasting people's time.

This question was when the interviewer asked me boxmodel. Since I need to talk about the top, bottom, left and right margins, in order to explain it better, I gave an example of inline Elements and block-level elements, but people still ask "What methods can be used to convert inline elements and block-level elements?" What a shame! I only answered <a href="http://www.php.cn/wiki/927.html" target="_blank">display</a>, so I looked for relevant books, read the source codes of several websites, and then summarized it.


  • Block-level elements and inline elements

    • Block-level elements will expand to the same width as the element.

    • Inline elements "shrink wrap" their contents and wrap them as tightly as possible.
      (The above two summaries are from the book "CSS Design Guide")

  • The default css style

    • The height of a block-level element is the height of its content, and its width will expand to the same width as the parent element. As a result, the block-level element will occupy its own line and cannot accommodate other block-level elements and inline elements behind it.

    • The height of an inline element is the height of its content, and its width will shrink and wrap its content. A strange point: There is a certain distance between inline elements. When viewing it using the console, this distance is not counted as a box model. If I use <a href="http://www.php.cn/wiki/931.html" target="_blank">margin</a>:0;padding:0 it will not clear this distance,? ? Please tell me!

  • css modification

    • Replacement element: Replacement element is the browser based on the element's tag and attribute, to determine the specific display content of the element. For example, the browser will read the picture information and display it based on the value of the Detailed tutorial on converting between inline and block-level elementssrc attribute. However, if you view the (X)HTML code, you will not be able to see the actual image. Content; Another example is that the type attribute of <input> determines whether to display an input box or a radio button, etc. Quoted from here

    • Non-replacement elements: such as p a span p these, that is Its content is displayed directly to the user (such as a browser).
      So inline elements are divided into inline replacement elements img input, and inline non-replacement elements span a...For both There is a difference in css modification.

    • Inline non-replaced elements: their width and height settings are invalid. You can set the inner and outer margins and borders, but the vertical settings will not affect the document flow layout, so set the vertical inner and outer margins. Margins have no effect. Vertical borders are visible to us, but they do not occupy the document flow. Therefore, the above inline element sets a border with a width of 10px, which will not cause the lower element to move downward.

    • Inline replacement elements, in addition to the characteristics of juxtaposing inline elements, can set the inner and outer margins, width and height that affect the document flow layout. And setting the height is equivalent to setting the line height, which will be vertically centered

    • line-height Although inline non-replacement elements do not have width and height, they can use line -height to set the height, but the strange thing is that although the line height can affect the document flow layout of inline elements, its border will still wrap its content, ignoring the upper and lower white space caused by the line height.

    • margin The margin of inline elements, which will not be merged (replacement, non-replacement). Unlike block-level elements, top and bottom, left and right margins will be combined.

    • Block-level elements can set their height and width, and they can also set their borders, inner and outer margins, and the setting results affect the document flow layout effect.
      Note that modifying width will not cause the inline elements below it to move upward.

    • Inline elements, can the height of inline elements be set? Of course not! This answer is of course too absolute.
      Elements are divided into replacement elements and non-replacement elements. Almost all replaced elements are inline elements.

  • Conversion between inline elements and block-level elements

    • display:none; The element will not be displayed, nor will the document flow position originally occupied by the element be retained.

    • display:block;Convert to block-level elements.

    • display:inline;Convert to inline elements.

    • display:inline-block;Convert to inline block-level elements.
      The above four are the ones I commonly use. In fact, there are many more

      The conversion between the two types of element realization forms can be easily achieved through display:block; and display:inline; .
      However, there are many times when we need to use display:inline-block; to convert inline elements or block-level elements. However, this element is a new attribute of css2. This attribute is not supported for versions below IE7, so some compatible methods are needed. I will write an article soon

    • ##displayBlock-level elements default to display:block; inline non-replacement elements (a, span) default to display:inline;; Inline replacement element (input) defaults to display:inline-block;

    • float<a href="http://www.php.cn/wiki/919.html" target="_blank"></a>When the inline After the element is set to float
      :left/right, the display attribute of the element in the row will be assigned the block value and has the float feature. The inexplicable white space between inline elements is removed.

    • position<a href="http://www.php.cn/wiki/902.html" target="_blank"></a>When positioning inline elements, position:absolute, with position:fixed. Will make the original inline elements become block-level elements.

  • [Note] Through the above settings, you can set the width, height, and inner and outer margins for inline non-replaced elements. However, when replacing, you need to pay attention to the fact that converting to block-level elements is only a side effect of float and position. Their own effects will also interfere with the layout effect.

    When we introduced block-level elements before, we said that the width of block-level elements will
    inheritits parent elements. However, this effect will only occur if display:block; is set for inline elements. Other conversions will not bring this effect by default.

    It is recommended that friends type and compare by themselves in

    Editor, and then they will be impressed.

【Related recommendations】

1.

Free html online video tutorial

2.

html Development Manual

3.

php.cn Original HTML5 Video Tutorial

The above is the detailed content of Detailed tutorial on converting between inline and block-level elements. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is the purpose of HTML tags?What is the purpose of HTML tags?Apr 28, 2025 am 12:02 AM

HTMLtagsareessentialforstructuringwebpages,enhancingaccessibility,SEO,andperformance.1)Theyareenclosedinanglebracketsandusedinpairstocreateahierarchicalstructure.2)SemantictagslikeandimproveuserexperienceandSEO.3)Creativetagslikeenabledynamicgraphics

What are self-closing tags? Give an example.What are self-closing tags? Give an example.Apr 27, 2025 am 12:04 AM

Self-closingtagsinHTMLandXMLaretagsthatclosethemselveswithoutneedingaseparateclosingtag,simplifyingmarkupstructureandenhancingcodingefficiency.1)TheyareessentialinXMLforelementswithoutcontent,ensuringwell-formeddocuments.2)InHTML5,usageisflexiblebutr

Beyond HTML: Essential Technologies for Web DevelopmentBeyond HTML: Essential Technologies for Web DevelopmentApr 26, 2025 am 12:04 AM

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)