html - HTML 中 <a> 标签上的文本垂直居中

标签 html css

我用我的 HTML 设置了一些按钮,通过使用此 CSS 和 HTML,它们的高度都相同

ul {
    	display: table;
    	padding: 0;
    	width: 90%;
    	margin: 5% auto;
    	list-style-type: none;
    	vertical-align: middle;
    	height: 20vh;
    }
    li {
    	display: block;
    	float: left;
    	vertical-align: middle;
    	text-align: center;
    	width: 50%;
    	height: 100%;
    	margin-top: 15px;
    	margin-right: 0;
    }
    li.third {
    	vertical-align: middle;
    	text-align: center;
    	margin: auto;
    	width: 50%;
    }
    .ghost-button {
    	color: rgb(9, 82, 85);
    	background: rgba(255, 255, 255, 0.01);
    	border: 1px solid rgb(9, 82, 85);
    	padding: 5px 5px;
    	margin: auto;
    	display: inline-block;
    	text-decoration: none;
    	width: 100%;
    	height: 100%;
    	vertical-align: middle;
    } 
    
    .third a {
    	display: block;
    	margin-left: 50%;
    	vertical-align: middle;
    }
    
    .ghost-button:hover, .ghost-button:active {
    	color: #FFF;
    	background-color: rgb(9, 82, 85);
    	text-decoration: none;
    }
    
    a {
    	text-decoration: none;
    	vertical-align: middle;
    	color: rgb(9, 82, 85);
    }
<ul>
<li><a class="ghost-button" class="ghost-button" href="S1A.php" class="ghost-button" >Check the newspaper classifieds
	for affordable pieces </a></li>
<li><a class="ghost-button" class="ghost-button" href="S1B.php" class="ghost-button" >Check for garage sales within
	walking distance </a></li>
<li class="third"><a class="ghost-button" class="ghost-button" href="S1C.php" class="ghost-button" >Do nothing</a></li>
</ul>

在我的页面上,按钮没有重叠,这是我无法弄清楚的代码片段的问题。该代码段还必须整页查看,否则问题不会发生。对于我第一次使用代码片段的问题,我们深表歉意。

我想让文本垂直对齐,如您所见,除非我使用错误,否则 vertical-align: middle 不起作用。

最佳答案

您可以将 .ghost-button 上的 display:inline-block 更改为 display:inline-flex 并更改 vertical-align:middlealign-items:center

align-items 是 flex 盒子的垂直居中。 justify-content 将处理水平对齐。

ul {
    	display: table;
    	padding: 0;
    	width: 90%;
    	margin: 5% auto;
    	list-style-type: none;
    	vertical-align: middle;
    	height: 20vh;
    }
    li {
    	display: block;
    	float: left;
    	vertical-align: middle;
    	text-align: center;
    	width: 50%;
    	height: 100%;
    	margin-top: 15px;
    	margin-right: 0;
    }
    li.third {
    	vertical-align: middle;
    	text-align: center;
    	margin: auto;
    	width: 50%;
    }
    .ghost-button {
    	color: rgb(9, 82, 85);
    	background: rgba(255, 255, 255, 0.01);
    	border: 1px solid rgb(9, 82, 85);
    	padding: 5px 5px;
    	margin: auto;
    	display: inline-flex;
    	text-decoration: none;
    	width: 100%;
    	height: 100%;
    	align-items:center;
    } 
    
    .third a {
    	display: block;
    	margin-left: 50%;
    	vertical-align: middle;
    }
    
    .ghost-button:hover, .ghost-button:active {
    	color: #FFF;
    	background-color: rgb(9, 82, 85);
    	text-decoration: none;
    }
    
    a {
    	text-decoration: none;
    	vertical-align: middle;
    	color: rgb(9, 82, 85);
    }
<ul>
<li><a class="ghost-button" href="S1A.php" >Check the newspaper classifieds
	for affordable pieces </a></li>
<li><a class="ghost-button"  href="S1B.php">Check for garage sales within
	walking distance </a></li>
<li class="third"><a class="ghost-button" href="S1C.php" >Do nothing</a></li>
</ul>

关于html - HTML 中 <a> 标签上的文本垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43402823/

相关文章:

html - 高度 0 不隐藏 DOM

CSS。如何为不同的窗口宽度编写(代码)css?

javascript - 取消固定后输入位置

css - 我想让边界的一部分与其他部分不同

html - 外部 CSS 在 Chrome 中不起作用

html - css 同时左右对齐

javascript - 如何检测仍然具有焦点的 HTML 选择的所有更改/单击事件?

html - 在页面之间导航时,导航栏和链接闪烁/跳转/调整大小,如何解决?

javascript - 提交时不进入验证功能的表单

jquery - 查找相对于父滚动 div 而不是窗口的偏移量