


display attribute in CSS: the difference between block, inline and inline-block_html/css_WEB-ITnose
I recently encountered a problem when writing HTML: I want to set the width and height of span, but it always doesn't work under IE9 and chrome. The code and renderings are as follows:
<head> <style> span{ background-color:#43be60; width:100px; height:50px; margin-top:20px; margin-left:20px; } </style></head><body> <div style="background-color:#ededed;width:400px;height:400px;"> <span>1</span> <span>10</span> <span>100</span> <span>1000</span> </div></body>
You can see that the width and height set by css have no effect, and the actual span Display width and height are determined by its content. Why is this happening? This involves the difference between block-level elements (block) and row-level elements (inline) in HTML. For example, div is a common block-level element, and span is a common row-level element. We can use the display attribute of CSS to set whether an element is a block-level or row-level element. display:block sets the element to block level, and display:inline sets the element to line level.
display:block features:
1. The block element will occupy its own line, and multiple block elements will each start a new line. By default, the width of a block element automatically fills the width of its parent element.
2. The block element can set width and height attributes. Even if the width of a block-level element is set, it still occupies an exclusive line.
3. The block element can set margin and padding attributes.
display:inline features:
1. Inline elements will not occupy one line, multiple adjacent inline elements will Arranged in the same row, until one row cannot fit, a new row will be added, and its width will change with the content of the element.
2. The width and height attributes set for inline elements are invalid.
3. The margin and padding attributes of inline elements, the horizontal padding-left, padding-right, margin-left, and margin-right all produce margin effects; but the vertical padding-top, padding-bottom, margin-top, margin-bottom will not produce margin effects.
Since span is a row-level element, its width and height cannot be set; if span is set to block, it will automatically wrap. How to display multiple spans on the same line and have a fixed width? This requires the use of display:inline-block. The code and renderings are as follows:
<head> <style> span{ background-color:#43be60; width:100px; height:50px; margin-top:20px; margin-left:20px; display:inline-block; } </style></head><body> <div style="background-color:#ededed;width:400px;height:400px;"> <span>1</span> <span>10</span> <span>100</span> <span>1000</span> </div></body>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex
