Home Web Front-end HTML Tutorial Detailed tutorial on converting between inline and block-level elements

Detailed tutorial on converting between inline and block-level elements

May 06, 2017 pm 02:04 PM

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 src 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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tutorial on how to use Dewu Tutorial on how to use Dewu Mar 21, 2024 pm 01:40 PM

Dewu APP is currently a very popular brand shopping software, but most users do not know how to use the functions in Dewu APP. The most detailed usage tutorial guide is compiled below. Next is the Dewuduo that the editor brings to users. A summary of function usage tutorials. Interested users can come and take a look! Tutorial on how to use Dewu [2024-03-20] How to use Dewu installment purchase [2024-03-20] How to obtain Dewu coupons [2024-03-20] How to find Dewu manual customer service [2024-03-20] How to check the pickup code of Dewu [2024-03-20] Where to find Dewu purchase [2024-03-20] How to open Dewu VIP [2024-03-20] How to apply for return or exchange of Dewu

Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

In summer, you must try shooting a rainbow In summer, you must try shooting a rainbow Jul 21, 2024 pm 05:16 PM

After rain in summer, you can often see a beautiful and magical special weather scene - rainbow. This is also a rare scene that can be encountered in photography, and it is very photogenic. There are several conditions for a rainbow to appear: first, there are enough water droplets in the air, and second, the sun shines at a low angle. Therefore, it is easiest to see a rainbow in the afternoon after the rain has cleared up. However, the formation of a rainbow is greatly affected by weather, light and other conditions, so it generally only lasts for a short period of time, and the best viewing and shooting time is even shorter. So when you encounter a rainbow, how can you properly record it and photograph it with quality? 1. Look for rainbows. In addition to the conditions mentioned above, rainbows usually appear in the direction of sunlight, that is, if the sun shines from west to east, rainbows are more likely to appear in the east.

What software is photoshopcs5? -photoshopcs5 usage tutorial What software is photoshopcs5? -photoshopcs5 usage tutorial Mar 19, 2024 am 09:04 AM

PhotoshopCS is the abbreviation of Photoshop Creative Suite. It is a software produced by Adobe and is widely used in graphic design and image processing. As a novice learning PS, let me explain to you today what software photoshopcs5 is and how to use photoshopcs5. 1. What software is photoshop cs5? Adobe Photoshop CS5 Extended is ideal for professionals in film, video and multimedia fields, graphic and web designers who use 3D and animation, and professionals in engineering and scientific fields. Render a 3D image and merge it into a 2D composite image. Edit videos easily

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

Tutorial on how to turn off the payment sound on WeChat Tutorial on how to turn off the payment sound on WeChat Mar 26, 2024 am 08:30 AM

1. First open WeChat. 2. Click [+] in the upper right corner. 3. Click the QR code to collect payment. 4. Click the three small dots in the upper right corner. 5. Click to close the voice reminder for payment arrival.

How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone How to convert qq music to mp3 format Convert qq music to mp3 format on mobile phone Mar 21, 2024 pm 01:21 PM

QQ Music allows everyone to enjoy watching movies and relieve boredom. You can use this software every day to easily satisfy your needs. A large number of high-quality songs are available for everyone to listen to. You can also download and save them. The next time you listen to them, you don’t need an Internet connection. The songs downloaded here are not in MP3 format and cannot be used on other platforms. After the membership songs expire, there is no way to listen to them again. Therefore, many friends want to convert the songs into MP3 format. Here, the editor explains You provide methods so that everyone can use them! 1. Open QQ Music on your computer, click the [Main Menu] button in the upper right corner, click [Audio Transcoding], select the [Add Song] option, and add the songs that need to be converted; 2. After adding the songs, click to select Convert to [mp3]

How to convert full-width English letters into half-width letters How to convert full-width English letters into half-width letters Mar 25, 2024 pm 02:45 PM

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

See all articles