尝试将 p 元素和图像垂直居中。我知道我可以调整边距和填充,但我想确保无论用户使用什么设备进入网站,元素都是对齐的。因此,我正在寻找另一种选择。
html, body {
font-size: 100%;
height: 100%;
font-family: 'Lato', sans-serif;
font-weight: 200;
}
/*Menu*/
.menu {
height: auto;
background-color: #fff;
width: 100%;
position: fixed;
z-index: 1;
-moz-box-shadow: 0 2px 0 0 #6ed3cf;
-webkit-box-shadow: 0 2px 0 0 #6ed3cf;
box-shadow: 0 2px 0 0 #6ed3cf;
}
.menu .logo {
padding-left: 2em;
}
.menu ul li a {
color: #3b3a36;
}
.menu .dropdown {
float: right;
right: 2em;
z-index: 20;
}
.menu .dropdown-menu {
background: #fff;
border: none;
}
.dropdown img {
height: 2.8em;
width: 2.8em;
-webkit-transition: width .8s, height .8s, -webkit-transform .8s; /* Safari */
transition: width .8s, height .8s, transform .8s;
}
.dropdown ul {
top: 3em;
left: -6.5em;
width: auto;
position: absolute;
color: #3DD0AC;
}
.rotate {
-webkit-transform: rotate(180deg); /* Safari */
transform: rotate(180deg);
}
.logo p {
font-size: 100%;
color: black;
font-family: 'Lato', sans-serif;
font-weight: bolder;
letter-spacing: 0.5em;
margin: 0;
}
<div class="menu">
<div class="logo">
<p><⁄BY_John Doe></p>
</div>
<div class="dropdown"> <img src="img/menu.jpg">
<ul class="dropdown-menu">
<li><a href="#">Find me on Linkedin</a>
<li>
<li><a href="#">Send me an email</a></li>
<li><a href="/docs/Resume.pdf" download="Resume">Download my resume</a></li>
</ul>
</div>
</div>
最佳答案
Flex box 可以为您实现这一目标。只需将此添加到您的 .menu
的 css 中:
display:flex;
justify-content:space-between;
align-items: center
这里有更多关于 flexbox 的设置:https://css-tricks.com/snippets/css/a-guide-to-flexbox/
html,
body {
font-size: 100%;
height: 100%;
font-family: 'Lato', sans-serif;
font-weight: 200;
}
/*Menu*/
.menu {
height: auto;
background-color: #fff;
width: 100%;
position: fixed;
z-index: 1;
-moz-box-shadow: 0 2px 0 0 #6ed3cf;
-webkit-box-shadow: 0 2px 0 0 #6ed3cf;
box-shadow: 0 2px 0 0 #6ed3cf;
display:flex;
justify-content:space-between;
align-items: center
}
.menu .logo {
padding-left: 2em;
}
.menu ul li a {
color: #3b3a36;
}
.menu .dropdown {
float: right;
right: 2em;
z-index: 20;
}
.menu .dropdown-menu {
background: #fff;
border: none;
}
.dropdown img {
height: 2.8em;
width: 2.8em;
-webkit-transition: width .8s, height .8s, -webkit-transform .8s;
/* Safari */
transition: width .8s, height .8s, transform .8s;
}
.dropdown ul {
top: 3em;
left: -6.5em;
width: auto;
position: absolute;
color: #3DD0AC;
}
.rotate {
-webkit-transform: rotate(180deg);
/* Safari */
transform: rotate(180deg);
}
.logo p {
font-size: 100%;
color: black;
font-family: 'Lato', sans-serif;
font-weight: bolder;
letter-spacing: 0.5em;
margin: 0;
}
<div class="menu">
<div class="logo">
<p><⁄BY_John Doe></p>
</div>
<div class="dropdown">
<img src="img/menu.jpg">
<ul class="dropdown-menu">
<li><a href="#">Find me on Linkedin</a>
<li>
<li><a href="#">Send me an email</a></li>
<li><a href="/docs/Resume.pdf" download="Resume">Download my resume</a></li>
</ul>
</div>
</div>
关于html - 在菜单栏中居中 p 元素和 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41181133/