gpt4 book ai didi

html - 如何在保持垂直居中的同时将元素右对齐?

转载 作者:行者123 更新时间:2023-11-28 15:21:56 25 4
gpt4 key购买 nike

我希望“William Marchesi”和“Contact”文本与标题右侧的“Contact”按钮垂直居中。当我尝试 float: right 时,联系人文本会移动,使其高于“William Marchesi”文本。

html {
height: 100%;
background-repeat: no-repeat;
background-color: green;
}

body {
font-size: 87.5%;
/* Base font size on 14px */
font-family: sans-serif;
line-height: 1.5;
text-align: justify;
margin: 0 auto;
padding: 0;
}

a {
text-decoration: none;
color: inherit;
}

.header div {
margin: 0 auto;
width: 70%;
clear: both;
padding: 0;
}

.header {
background-color: #FCFCFC;
width: 100%;
padding: 0;
margin: 0 auto;
/*height: 5em;*/
}

.heading,
.contactButton {
display: inline-block;
padding: 0;
margin: 0;
vertical-align: middle;
line-height: 3em;
height: 3em;
font-family: 'Nunito', sans-serif;
}

.heading {
font-size: 3em;
padding: 0;
margin: 0;
}

.contactButton {
font-size: 2em;
text-align: right;
padding: 0;
margin: 0;
float: right;
}
<body class="body">
<div class="header">
<div>
<h1 class="heading">William Marchesi</h1>
<a class="contactButton" href="#contact">Contact</a>
</div>
</div>
</body>

最佳答案

这是许多可能的解决方案之一,在联系按钮上使用 position:absolute

html {
height: 100%;
background-repeat: no-repeat;
background-color: green;
}

body {
font-size: 87.5%; /* Base font size on 14px */
font-family: sans-serif;
line-height: 1.5;
text-align: justify;
margin: 0 auto;
padding: 0;
}

a {
text-decoration: none;
color: inherit;
}

.header div {
margin: 0 auto;
padding: 0;
text-align:center;
}
.header {
background-color: #FCFCFC;
padding: 0;
}

.heading, .contactButton {
padding: 0;
margin: 0;
vertical-align: middle;
line-height: 3em;
font-family: 'Nunito', sans-serif;
}
.heading {
font-size: 3em;
padding: 0;
margin: 0;
}

.contactButton {
font-size: 2em;
text-align: right;
padding: 0;
margin: 0;
position:absolute;
top:0.8em;
right:1em;
}
<div class="header">
<div>
<h1 class="heading">William Marchesi</h1>
<a class="contactButton" href="#contact">Contact</a>
</div>
</div>

关于html - 如何在保持垂直居中的同时将元素右对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991684/

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