Gaps between div and img

Source: Internet
Author: User

Gaps between div and img

In this project, the customer said that there should be no gaps between the upper and lower sides of the banner diagram. After a closer look, we found that there were gaps between the upper and lower sides. The review element finds that all div, img padding and margin are both 0, and the gap is true if the cause is not found.

I checked it online and found many similar problems.

There are four solutions:

1. Set img to block;

This basically solves the gap between img and div.

2. Set the vertical alignment of img

V-align: bottom;

3. Set the font-size of the parent div to 0.

4. Set line-height: 0 for the outer div

The first method is recommended.

However, the strange thing about this project is that there are gaps between the upper and lower sides. At the beginning, the gap above can be solved through margin-top:-10px. However, this method won't work after the code structure is changed. I have found a lot online and there is no solution.

Therefore, the following solutions are implemented through positioning:

Set the position of the parent element to relative.

The position of the sub-element img is absolute; the top is set to 0, which perfectly solves this problem.

To avoid affecting other la S, set the div height to the img height.

The cause of the problem is still not found, but the problem is solved. There are many ways to solve the problem, but the depressing thing is that the problem is often solved, but I don't know how it happened, which is not conducive to avoiding these problems in the code writing process in the future.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.