How to solve the gap between the top and bottom of the img tag
This article mainly introduces you to the method of solving the gap between the top and bottom of the img tag. Because I encountered this problem at work recently, I wanted to record it. For your convenience or for friends in need, let’s take a look below.
In our normal development process, we often need to use multiple pictures, and when using multiple pictures, we usually use a list to host our img.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"> img{ height: 200px; margin: 0; padding: 0; border-bottom: 1px solid red; } ul{ border: 1px solid blue; list-style: none; padding: 0; margin: 0; } </style> </head> <body> <ul> <li> <img src="lipeng.png"> </li> </ul> </body> </html>
But this time we discovered a problem, why is there an extra line under my picture?
what is going on?
Have I not cleared all the outer margins and inner margins of the img?
Actually, this is actually caused by the inline element.
Any visible element that is not a block-level element is an inline element, and its performance characteristics are in the form of "row layout". ----"CSS Definitive Guide"
What does it mean?
means that in fact, inline elements such as pictures and text, their default alignment is aligned with the baseline of its parent, but what you align is the baseline, and it is the element that expands the height. The overall height (bottom line) will definitely cause a certain gap, which is the problem we had above.
Now that we know the cause of this problem, it will be much simpler to solve it.
1. The first solution
Since this problem only occurs with inline elements, we can naturally solve this problem simply and crudely, then Isn't it enough to "change personality" to our elements and change them from inline to block?
<style type="text/css"> img{ height: 200px; margin: 0; padding: 0; border-bottom: 1px solid red; display: block; } ul{ border: 1px solid blue; list-style: none; padding: 0; margin: 0; } </style>
2. The second solution
This is too rough, change the gender, and return it later How fun it is to play, so we have to try curve rescue. We can modify its vertical alignment. Is this okay?
<style type="text/css"> img{ height: 200px; margin: 0; padding: 0; border-bottom: 1px solid red; vertical-align: middle; } ul{ border: 1px solid blue; list-style: none; padding: 0; margin: 0; } </style>
It can be seen that this can also achieve the desired effect.
The reason is that the default attribute of vertical-align is baseline. As long as we set an attribute different from baseline, we can avoid this problem.
3. The third solution
#But the alignment is modified, which may also cause problems. , can we make this element float? Since you are no longer in the current document flow, you will naturally not refer to this text for alignment when laying out.
We can use floats.
<style type="text/css"> img{ height: 200px; margin: 0; padding: 0; border-bottom: 1px solid red; float: left; } ul li { overflow: hidden; } ul{ border: 1px solid blue; list-style: none; padding: 0; margin: 0; } </style>
This can also solve this problem, but please note that "floating is good, but don't be greedy."
You must be able to correctly solve the impact of floating, and if you originally planned to do a text wrapping effect, then using floating must be your best choice.
4. The fourth solution
If none of the above solutions can solve your problem, then the only option is to use my killer weapon .
You can set the text size to 0 for your parent element.
<style type="text/css"> img{ height: 200px; margin: 0; padding: 0; border-bottom: 1px solid red; } ul li { font-size: 0px; } ul{ border: 1px solid blue; list-style: none; padding: 0; margin: 0; } </style>
Since you are aligning according to the baseline of the text, I will directly set the text to you so that you can't position it, but this method, It is only recommended to use it when you are "too anxious to get it out".
The above is the detailed content of How to solve the gap between the top and bottom of the img tag. For more information, please follow other related articles on the PHP Chinese website!

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

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

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











Methods to open img files include using virtual optical drive software, using compression software, and using special tools. Detailed introduction: 1. Use virtual optical drive software to open, download and install a virtual optical drive software, right-click the img file, select "Open with" or "Associated Program", select the installed virtual optical drive software in the pop-up dialog box, virtual The optical drive software will automatically load the img file and use it as a disc image in the virtual optical drive. Double-click the disc icon in the virtual optical drive to open the img file and access its contents, etc.

How to open the img file: 1. Confirm the img file path; 2. Use the img file opener; 3. Select the opening method; 4. View the picture; 5. Save the picture. The img file is a commonly used image file format, usually used to store picture data.

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

img is a file compression format, mainly used to create image files of floppy disks. It can be used to compress the contents of an entire floppy disk or an entire CD; files with the extension ".IMG" are created using this file format. ; The img file includes 3 basic nodes, namely "Ehfa_HeaderTag", "Ehfa_File" and "Ehfa_Entry".

When browsing Douyin works, we often see a clock icon behind the tag. So, what exactly is this clock? This article will focus on the discussion of "What is the clock behind the Douyin label", hoping to provide some useful reference for your use of Douyin. 1. What is the clock behind the Douyin label? Douyin will launch some hot topic challenges. When users participate, they will see a clock icon after the tag, which means that the work is participating in the topic challenge and displays the remaining time of the challenge. For some time-sensitive content, such as holidays, special events, etc., Douyin will attach a clock icon after the label to remind users of the validity period of the content. 3. Popular tags: When a tag becomes popular, Douyin will add a clock icon after the tag to indicate that the tag is

As a popular short video social platform, Douyin has a huge user base. For Douyin creators, using tags to attract traffic is an effective way to increase the exposure of content and attract attention. So, how does Douyin use tags to attract traffic? This article will answer this question in detail for you and introduce related techniques. 1. How to add tags on Douyin to attract traffic? When posting a video, make sure to choose tags that are relevant to the content. These tags should cover the topic and keywords of your video to make it easier for users to find your video through tags. Leveraging popular hashtags is an effective way to increase your video’s exposure. Research current popular tags and trends and incorporate them into your video descriptions and tags. These popular tags usually have higher visibility and can attract the attention of more viewers. 3. Label

Detailed explanation of the video tag in HTML The video tag in HTML5 is a tag used to play videos on web pages. It can render videos using different formats, such as MP4, WebM, Ogg, and more. In this article, we will introduce the use of video tag in detail and provide specific code examples. Basic Structure The following is the basic structure of the video tag:

Differences: 1. The head tag is used to define the head of the document, which is a container for all head elements, and the header tag is used to define the header (introduction information) of the document; 2. All browsers support the head tag, and older versions of browsers None of the browsers support the header tag, and browsers such as IE9+ and above are required to support the header tag.
