html 中 包含img标签的父容器元素p、div、a等类型的高度多出一块 且css查询不到margin和padding的原因及解决

12 篇文章 0 订阅

问题

  当p、div、a这些作为父元素容器且包含img子元素标签时,父元素的总体高度会比img图片的高度多出一些px

<div class="content">
<img src="https://www.baidu.com/images/asfwqdr1fgh44.png">
<div>

F12排查了父元素是否存在margin或者padding,但是没有找到对应的间距设置,又排查了html文本,看看是否是空格或者换行符导致的。仍不行,排除html空格的问题

原因

根据查询的资料了解到,在html5中,img标签是一种类似text文本的标签元素,在结束的时候,会在末尾加上一个空白符(匿名文本),这个文本外有一个匿名行级盒子,它默认的vertical-alignbaseline的,而且往往因为上文(父容器那继承到的样式)line-height的影响,使它有个line-height,从而使其有了高度,因为baseline对齐的原因,这个匿名盒子就会下沉,往下撑开一些距离,所以把父元素撑高了一些px

解决

  • 消除掉匿名盒子的高度,给父元素设置line-height: 0; 或者 font-size: 0;
  • 给两者vertical-align:top(实际上,只需要设置父元素,因为子元素默认继承父元素该样式),让其top对齐,而不是baseline对齐 
  • 给img以display:block,让它和匿名行级盒子不在一个布局上下文中,也就不存在行级盒的对齐问题【推荐】
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
好的,我可以为您提供一个基本的外卖订餐网站首页,并列出需要的元素及其相应的HTMLCSS代码。 需要的元素包括: 1. 网站Logo 2. 导航栏菜单 3. 轮播图 4. 推荐菜品列表 5. 订单下单表单 6. 底部版权信息 以下是HTML代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>外卖订餐网站首页</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <div class="logo"> <img src="logo.png" alt="网站Logo"> </div> <nav> <ul> <li><a href="#">首页</a></li> <li><a href="#">菜单</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">联系我们</a></li> </ul> </nav> </header> <div class="banner"> <img src="banner.jpg" alt="轮播图"> </div> <section class="featured-dishes"> <h2>推荐菜品</h2> <ul> <li> <img src="dish1.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥50</span> </li> <li> <img src="dish2.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥60</span> </li> <li> <img src="dish3.jpg" alt="菜品图片"> <h3>菜品名称</h3> <p>菜品描述</p> <span class="price">¥70</span> </li> </ul> </section> <section class="order-form"> <h2>下单</h2> <form> <label for="name">姓名</label> <input type="text" id="name"> <label for="phone">电话</label> <input type="tel" id="phone"> <label for="address">地址</label> <input type="text" id="address"> <label for="dish">菜品</label> <select id="dish"> <option value="1">菜品1</option> <option value="2">菜品2</option> <option value="3">菜品3</option> </select> <button type="submit">提交订单</button> </form> </section> <footer> <p>©2021 外卖订餐网站. All Rights Reserved.</p> </footer> </body> </html> ``` 以下是相应的CSS代码: ```css * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: 'Helvetica Neue', sans-serif; font-size: 16px; line-height: 1.5; color: #333; } header { display: flex; justify-content: space-between; align-items: center; padding: 20px; background-color: #fff; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } .logo img { height: 60px; } nav ul { display: flex; } nav li { margin: 0 10px; } nav a { display: block; padding: 10px; text-decoration: none; color: #333; } nav a:hover { background-color: #ffcc00; color: #fff; } .banner img { width: 100%; height: 400px; object-fit: cover; } .featured-dishes { padding: 60px 20px; text-align: center; } .featured-dishes h2 { margin-bottom: 30px; font-size: 36px; font-weight: bold; } .featured-dishes ul { display: flex; justify-content: center; list-style: none; } .featured-dishes li { margin: 0 20px; text-align: center; } .featured-dishes img { width: 100%; max-width: 300px; height: 200px; object-fit: cover; border-radius: 10px; } .featured-dishes h3 { margin-top: 20px; font-size: 24px; font-weight: bold; } .featured-dishes p { margin-top: 10px; font-size: 16px; color: #777; } .featured-dishes .price { display: block; margin-top: 10px; font-size: 20px; font-weight: bold; color: #ffcc00; } .order-form { padding: 60px 20px; text-align: center; background-color: #f2f2f2; } .order-form h2 { margin-bottom: 30px; font-size: 36px; font-weight: bold; } .order-form form { display: flex; flex-direction: column; align-items: center; } .order-form label { display: block; margin-top: 20px; font-size: 20px; font-weight: bold; } .order-form input[type="text"], .order-form input[type="tel"], .order-form select { width: 100%; max-width: 400px; padding: 10px; margin-top: 10px; font-size: 16px; border-radius: 5px; border: none; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } .order-form button { display: block; margin-top: 20px; padding: 10px 20px; font-size: 20px; font-weight: bold; color: #fff; background-color: #ffcc00; border: none; border-radius: 5px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); cursor: pointer; } .order-form button:hover { background-color: #ff9900; } footer { padding: 20px; text-align: center; background-color: #fff; box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1); } footer p { font-size: 14px; color: #777; } ``` 希望这个例子能对您有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hzxOnlineOk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值