Home Web Front-end CSS Tutorial The difference between display:inline-block and float in css when arranging elements in a row

The difference between display:inline-block and float in css when arranging elements in a row

Sep 18, 2017 am 09:13 AM
display float inline-block

During layout, many designs need to arrange elements in a row. There are many ways to arrange elements in a row. From the perspective of compatibility and ease of use, we will first introduce display:inline-block (display within block) and float (float).

float: Float, you will know its meaning after hearing its name: making elements float and break away from the document flow, so as to achieve the purpose of arranging multiple elements in a row. What are the characteristics of floating elements? As shown below:

  1. supports width and height;

  2. ## is displayed in one line;

  3. When the width is not set, the width is supported by the content;

  4. will move in the specified order until it encounters the float The boundary of the element or parent stops;

  5. After the element is floated, the height of the parent of the floating element will collapse;

  6. It breaks away from the document flow;

  7. After the element floats, the upper and lower margins no longer overlap;

  8. triggers BFC.

inline-block: Inline block, which has both the characteristics of inline elements and the characteristics of block-level elements. As shown below:

  1. supports width and height;

  2. is displayed in one line;

  3. code wraps Will be parsed into spaces;

  4. When the width is not set, the width will be stretched by the content;

  5. Inline-block type elements will have a by default at the bottom Gap;

  6. The upper and lower margins of inline-b type elements do not overlap;

  7. triggers BFC.

It can be seen from the characteristics of these two styles that they are the same in that: 1. Support width and height; 2. Display in one line; 3. When the content is not set, the width It is supported by the content; 7. The upper and lower margins do not overlap and 8. BFC is triggered. Displaying this feature in a row determines the two elements that can be arranged in a row. So when using differences, you have to start analyzing them from their different characteristics:

  1. Arrangement direction limitation. The fourth characteristic of float determines that it can determine the order of elements. float:left: elements are arranged from left to right, float:right: elements are arranged from right to left. And display:inline-block can only be used from left to right.

  2. Whether to break away from the document flow. Floated elements will break out of the document flow, but display:inline-block will not. So there will be a phenomenon: when the first floating element or inline-block element already occupies a row, and add margin-left:-100% to the second element, the second floating element will cover the second floating element. On top of the element, the second inline-block will be displayed in a new line and moved according to the margin-left;

  3. The floating element will collapse to the height of the parent. In order to avoid the height collapse of the parent element, this feature must be processed when using float: set the height of the parent or clear the float;

  4. inline-block There is a gap at the bottom of the element by default. When making web pages, you need to set the vertical alignment: vertical-align to eliminate gaps;

  5. Inline-block type elements will generate spaces when the HTML code wraps. In order to eliminate the impact of spaces , need: write the inline element as one line in the structure or set the font-size of the parent of the inline element to 0 in the style;

From the above difference analysis, it can be seen that Advantages and disadvantages of float and inline-block: After float, in most cases you need to clear the float or set the height. display:inline-block can only be used without changing the default arrangement (from left to right), and the bottom gap and the spaces on the left and right caused by code wrapping need to be cleared.

The above is the detailed content of The difference between display:inline-block and float in css when arranging elements in a row. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Fujifilm X-M5 price expectations rise as leaked selfie display adds complexity to the formerly affordable camera line Sep 07, 2024 am 09:34 AM

Fujifilm fans were recently very excited at the prospect of the X-T50, since it presented a relaunch of the budget-oriented Fujifilm X-T30 II that had become quite popular in the sub-$1,000 APS-C category. Unfortunately, as the Fujifilm X-T50's launc

What is the maximum value of float? What is the maximum value of float? Oct 11, 2023 pm 05:54 PM

Maximum value of float: 1. In C language, the maximum value of float is 3.40282347e+38. According to the IEEE 754 standard, the maximum exponent of the float type is 127, and the number of digits of the mantissa is 23. In this way, the maximum floating point number is 3.40282347 e+38; 2. In the Java language, the maximum float value is 3.4028235E+38; 3. In the Python language, the maximum float value is 1.7976931348623157e+308.

Apple Studio Display's power cord is detachable, but requires special tools Apple Studio Display's power cord is detachable, but requires special tools May 17, 2023 pm 03:05 PM

Apple Studio Display is now officially available in stores, and many customers around the world have purchased the product. Unlike the ProDisplayXDR, the StudioDisplay has a unique power connector that appears to be non-removable. It turns out that the cable is removable, but you'll need a special tool to remove it. Apple says on its website that the Studio Display's power cord is not detachable -- and many users think so. That's because removing the cable with your hands seems impossible, but luckily the cable can be detached from the monitor. , Apple has a special tool for extracting information from its new StudioDispl

Comparison: Apple Studio Display vs Samsung Smart Monitor M8 Comparison: Apple Studio Display vs Samsung Smart Monitor M8 May 11, 2023 pm 10:46 PM

Samsung Smart Display M8 vs. Apple Studio Display: Design and Size Since its launch, the Apple Studio Display has been compared to the iMac, consisting of a relatively thin panel on a relatively simple L-shaped stand. It's a well-known and well-loved aesthetic, and Samsung seems to have borrowed it for its presentation. The Samsung SmartMonitor M8 uses the same idea of ​​a thin screen on a stand that looks very similar. Some minor elements are different, such as the small section in the lower left corner that sticks out a bit and the Samsung's chin is very thin, but they seem to be close in terms of basic design. Samsung seems to have taken a lot of inspiration from the 24-inch iMac. Apple's display is smaller than Samsung's

Samsung: New, large display based on E Ink technology displays colors and communicates wirelessly Samsung: New, large display based on E Ink technology displays colors and communicates wirelessly Jun 19, 2024 pm 03:37 PM

We frequently report on devices based on displays with electronic ink, such as e-readers. The technology offers a number of advantages: it can be read in bright environments without a backlight, and it only requires power when switching without light

Comparison: Apple Studio Display vs LG UltraFine 5K Display: Which one is better? Comparison: Apple Studio Display vs LG UltraFine 5K Display: Which one is better? Apr 16, 2023 pm 08:25 PM

StudioDisplay and LG UltraFine5KDisplay occupy similar positions in the market, but Apple's monitor is $300 more expensive. Here's everything you need to know about how these monitors compare. Six years is a long time in the tech world, and it's also the time since Apple has sold a branded monitor that costs less than $5,000. During this time, Apple partnered with LG to sell the LG UltraFine series, which catered specifically to Mac users. In 2019, Apple stopped selling these LG monitors in favor of ProDisplayXDR, an affordable Mac-friendly display

Boot Camp updated to support Apple Studio Display Boot Camp updated to support Apple Studio Display May 20, 2023 pm 11:34 PM

IntelMac users running Windows on a Mac can now update their drivers in BootCamp to support Apple's StudioDisplay. Apple regularly updates BootCamp to introduce support for new hardware, as well as typical compatibility and performance improvements. In the March software update, Apple has enabled BootCamp to work with the new StudioDisplay. The update that brings BootCamp to version 6.1.17 introduces two key support elements. First, it adds compatibility with StudioDisplay, ensuring

What values ​​does display have? What values ​​does display have? Nov 20, 2023 pm 05:28 PM

The values ​​of display include block, inline, none, inline-block, flex, grid, table, inline-table and list-item. Detailed introduction: 1. block, which renders elements into block-level elements. Block-level elements form a block on the page and occupy one line alone; 2. inline, which renders elements into inline elements. Inline elements will not occupy a line by themselves and can be side by side with other elements; 3. none, this value specifies that the element will not be on the page, etc.

See all articles