gpt4 book ai didi

html - 使用多种字体大小时如何对齐 div 和按钮?

转载 作者:行者123 更新时间:2023-12-04 04:11:04 25 4
gpt4 key购买 nike

我需要一个激活下拉菜单的按钮和一个包含链接的 div,在导航栏中,按钮和链接下面有一个名称和一小行文本。我希望小文本行是较小的字体。如果我将文本行放在 p 标签中并指定较小的字体,我将无法使文本在其中整齐地对齐。

我可以在链接上使用填充来将它下推,但是当我开始使用媒体查询来缩小链接时,它们就会失去对齐。我也尝试过使用行高但有类似的问题。我可以通过所有媒体查询稍微调整填充/行高以获得对齐,但显然这不能解决问题,只会创建一个困惑的解决方案。

如果我删除小文本行上的字体大小,它们会正确对齐。有人可以帮助我理解为什么使用不同的字体大小会导致按钮和 div 中的文本对齐方式发生不同变化的原因,并提出一个解决方案,以便在我调整其他属性(例如两者的高度)时它们可以轻松一致地对齐。谢谢

.main-menu-button{ 
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
float:left;
position:relative;
font-weight: 600;
font-size: 16pt;
height:50px;
top:25px;
line-height: 3pt;
z-index:10;
background-color: white;
border: 1px solid green;
}

.main-menu-link{
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
float: left;
position:relative;
font-weight: 600;
font-size: 16pt;

height:50px;
width:120px;
top:25px;

line-height: 3pt;
z-index:10;
text-align: center;
border:solid 1px black;
}

/* if you remove font size in the p tag, the text aligns, but I want this below*/

p {
font-size:8pt;
}
<html>
<button class="main-menu-button">My button<p>bit of text</p></button>
<div class="main-menu-link"><a>My link</a><p>bit of text</p></div>
</html>

最佳答案

  1. 删除了两者的行高
  2. 从 p 中删除边距
  3. 为按钮的 div 添加边距*

如果您仅使用 div 或仅使用按钮,则第 3 步会更好。

.main-menu-button{ 
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
float:left;
position:relative;
font-weight: 600;
font-size: 16pt;
height:50px;
top:25px;
z-index:10;
background-color: white;
border: 1px solid green;
}

.main-menu-link{
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
float: left;
position:relative;
font-weight: 600;
font-size: 16pt;
padding-top: 5px;
height:50px;
width:120px;
top:25px;
z-index:10;
text-align: center;
border:solid 1px black;
}

/* if you remove font size in the p tag, the text aligns, but I want this below*/

p {
font-size:8pt;
margin: 0;
}
<html>
<button class="main-menu-button"><span>My button</span><p>bit of text</p></button>
<div class="main-menu-link"><a>My link</a><p>bit of text</p></div>
</html>

关于html - 使用多种字体大小时如何对齐 div 和按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61725392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com