Detailed explanation of the spacing problem between img tags

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has four elements:

(1) src: image path (2) width:
(3) height:
(4) alt:

The role of alert:

When an error occurs in an image, use the function in alt to indicate the error content for easy search engine crawling (convenient for search engine crawling and SEO rendering)

Note:

(1) The width and height set for img must be the original size of the image. (2) Generally, inline elements cannot set width and height. Although img is also an inline element, it is a replaced element and can be set through src.

The difference between inline elements and inline elements.

How to solve the spacing problem between inline elements:

The reasons for the upper and lower gaps in the inline elements in the figure below are:

img is an inline element => inline elements will generate line breaks in the element code by default (also called whitespace => can also be used as a text string by default)

The reason for the vertical gap between inline elements in the following figure:

The default baseline alignment of inline elements causes empty nodes to appear (vertical-align: baseline;)
What is baseline alignment?
Acts in vertical and horizontal ways. What is a blank node?
The cause of blank nodes in the vertical direction: because the img inline element may be treated as a string in the browser, it may be affected by the front-size font size, and line-height will also affect the blank node => When adding an image, there will be an extra blank line below, which is the role of the blank node

Solution 1:

Because inline elements are treated as strings, you can add a font-size: 0 to the parent; the alignment becomes: vertical-align: middle; but this may cause problems with the text style in the parent

Solution 2:

Use display to change from an inline element to a block-level element:

Note: vertical-align and line-height are the two most difficult knowledge points to understand in CSS: Why can font-size = 0 be changed? Can the blank node of img be made to disappear?

Text and image example: Use span tags with images

The alignment of the two inline elements is vertical-align: baseline; it is obvious that the text exceeds the baseline of the image.


In the following figure, the text size is changed, and the vertical gap between the images also increases.

Causes:

The text will have a default line height, which causes vertical spacing between the text. The larger the text, the larger the spacing. => When "vertical-align: miiddle;" is used, the text position will rise and no spacing will be generated as shown below

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  iframe src assignment problem (server side)

>>:  Vue implements the sample code of associating the side navigation bar with the Tab page

Recommend

Tutorial on logging into MySQL after installing Mysql 5.7.17

The installation of mysql-5.7.17 is introduced be...

Detailed explanation of Vue component reuse and expansion

Table of contents Overview Is the extension neces...

Let you understand the deep copy of js

Table of contents js deep copy Data storage metho...

Detailed explanation of mkdir command in Linux learning

Table of contents Preface 1. Basic knowledge of f...

Detailed explanation of how to easily switch CSS themes

I recently added a very simple color scheme (them...

How to display percentage and the first few percent in MySQL

Table of contents Require Implementation Code dat...

How to build Git service based on http protocol on VMware+centOS 8

Table of contents 1. Cause 2. Equipment Informati...

How to understand Vue front-end and back-end data interaction and display

Table of contents 1. Technical Overview 2. Techni...

MySQL daily statistics report fills in 0 if there is no data on that day

1. Problem reproduction: Count the total number o...

The connection between JavaScript constructors and prototypes

Table of contents 1. Constructors and prototypes ...

CSS sample code with search navigation bar

This article shows you how to use CSS to create a...