CSS下拉菜单padding(内边距)间隙问题咨询与解决
下拉菜单内边距对齐问题解决
问题描述
为一级列表项(LINK1、LINK2、LINK3)添加左右内边距实现文字居中后,下拉菜单显示时左侧出现间隙,无法与父菜单项对齐,如图所示:

相关代码如下:
HTML结构
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dexeus Rep</title> <link rel="stylesheet" href="./resources/styles/reset.css"> </head> <body> <nav class="nav-bar"> <div class="nav-items"> <ul> <li><a href="#">LINK 1</a> <ul class="dropdown"> <li><a href="#">Link 1.1</a></li> <li><a href="#">Link 1.2</a></li> <li><a href="#">Link 1.3</a></li> <li><a href="#">Link 1.4</a></li> <li><a href="#">Link 1.5</a></li> <li><a href="#">Link 1.6</a></li> <li><a href="#">Link 1.7</a></li> </ul> </li> <li><a href="#">LINK 2</a></li> <li><a href="#">LINK 3</a></li> </ul> </div> </nav> </body>
自定义CSS
.nav-bar { background-color: black; max-width: 100%; display: flex; justify-content: space-between; align-items: center; position: fixed; top: 0; width: 100%; height: 3.325em; } .nav-items { display: flex; align-items: center; justify-content: center; } .nav-items ul { display: flex; color: orange; } .nav-items ul li { color: white; display: flex; align-items: center; font-size: 1em; font-weight: 600; padding-left: 1em; padding-right: 1em; height: 3.325em; background-color: black; } ul li:hover { background-color: orange; } ul li ul { visibility: hidden; display: none; position: absolute; margin-top: 1px; top: 3.325em; /* same as nav-bar height */ /* height: 18em; */ } .dropdown li{ position: relative; } ul li:hover > ul, ul li ul:hover { visibility: visible; display: flex; flex-direction: column; height: 18.5em; }
重置样式(reset.css)
@charset "UTF-8"; /* Con este reset vamos a resolver: 👉 Unificar el uso de Custom Properties 👉 Problemas de box-model más generales 👉 Problemas con imagenes, vídeos e iconos svg 👉 Problemas con tipografías y etiquetas input en formularios 👉 Unificar la tipografía de todas las etiquetas de una web */ /* Aquí definimos las Custom properties */ :root { --negro: #393939; /* Tipografía */ --tipo-principal: Helvetica, Arial, sans-serif; --tipo-secundaria: Verdana; } /* Las adaptamos al modo oscuro */ @media (prefers-color-scheme: dark) { :root { --negro: #ececec; } } /* Opcional */ /* Configuramos si un usuario ha activado el modo alto contraste. (WD) */ /* Opcional */ /* Desactivamos los animations en el caso de que el usuario haya configurado el modo sin animation */ @media (prefers-reduced-motion: reduce) { * { -webkit-animation: none !important; animation: none !important; -webkit-transition: none !important; transition: none !important; } } /* Reseteamos los margin y paddings de todas las etiquetas */ * { margin: 0; padding: 0; border: 0; -webkit-box-sizing: border-box; box-sizing: border-box; vertical-align: baseline; } /* Evitamos problemas con las imagenes */ img, picture, video, iframe, figure { max-width: 100%; width: 100%; display: block; /* opcional */ -o-object-fit: cover; object-fit: cover; /* opcional */ -o-object-position: center center; object-position: center center; } /* Reseteamos los enlaces para funcionar como cajas... */ a { display: block; text-decoration: none; color: inherit; font-size: inherit; } /* ... excepto los que se encuentran en párrafos */ p a { display: inline; } /* Quitamos los puntos de los <li> */ li { list-style-type: none; } /* Configuramos anclas suaves */ html { scroll-behavior: smooth; } /* Desactivamos estilos por defecto de las principales etiquetas de texto */ h1, h2, h3, h4, h5, h6, p, span, a, strong, blockquote, i, b, u, em { font-size: 1em; font-weight: inherit; font-style: inherit; text-decoration: none; color: inherit; } /* Evitamos problemas con los pseudoelementos de quotes */ blockquote:before, blockquote:after, q:before, q:after { content: ""; content: none; } /* Configuramos el texto que seleccionamos */ ::-moz-selection { background-color: var(--negro); color: var(--blanco); } ::selection { background-color: var(--negro); color: var(--blanco); } /* Nivelamos problemas de tipografías y colocación de formularios */ form, input, textarea, select, button, label { font-family: inherit; font-size: inherit; -webkit-hyphens: auto; -ms-hyphens: auto; hyphens: auto; background-color: transparent; color: inherit; display: block; /* opcional */ -webkit-appearance: none; -moz-appearance: none; appearance: none; } /* Reseteamos las tablas */ table, tr, td { border-collapse: collapse; border-spacing: 0; } /* Evitamos problemas con los SVG */ svg { width: 100%; display: block; fill: currentColor; } /* (Probándolo, no usar en producción) En el caso de añadir una */ /* p svg{ display: inline; width: initial; } */ /* Configuramos la tipografía para toda la web */ body { min-height: 100vh; font-size: 100%; font-family: var(--tipo-principal); color: var(--negro); /* opcional */ line-height: 1.4em; /* opcional */ -webkit-hyphens: auto; -ms-hyphens: auto; hyphens: auto; /* opcional */ font-smooth: always; /* opcional */ -webkit-font-smoothing: antialiased; /* opcional */ -moz-osx-font-smoothing: grayscale; }
问题原因
下拉菜单使用position: absolute后,默认会相对于最近的已定位祖先元素的内容区(content box)定位。当前父级<li>未设置position: relative,且本身带有padding-left: 1em,导致下拉菜单左边缘对齐父元素内容区左边缘,从而和父元素外边缘产生padding大小的间隙。
解决方法
方法一:给父级
- 给一级列表项添加
position: relative,让下拉菜单以父元素外边缘为定位基准 - 给下拉菜单设置
left: 0和width: 100%,确保宽度与父项匹配
修改后的关键CSS:
.nav-items ul li { color: white; display: flex; align-items: center; font-size: 1em; font-weight: 600; padding-left: 1em; padding-right: 1em; height: 3.325em; background-color: black; position: relative; /* 添加相对定位 */ } ul li ul { visibility: hidden; display: none; position: absolute; margin-top: 1px; top: 3.325em; left: 0; /* 强制左对齐父元素外边缘 */ width: 100%; /* 下拉宽度匹配父项 */ }
方法二:将内边距从
把一级菜单的左右padding转移到<a>标签上,让<li>本身无内边距,下拉菜单定位不受padding影响,同时更符合点击区域的语义化:
修改后的关键CSS:
.nav-items ul li { color: white; display: flex; align-items: center; font-size: 1em; font-weight: 600; height: 3.325em; background-color: black; position: relative; /* 添加相对定位 */ } .nav-items ul li a { padding: 0 1em; /* 把内边距移到链接上 */ height: 100%; display: flex; align-items: center; /* 保持文字垂直居中 */ } ul li ul { visibility: hidden; display: none; position: absolute; margin-top: 1px; top: 3.325em; left: 0; width: 100%; }
两种方法均可解决对齐问题,方法二更贴合交互逻辑,推荐使用。
内容的提问来源于stack exchange,提问作者Gonzalo Molina
相关产品推荐
相关产品推荐

