在菜单栏中居中p元素和div

时间:2016-12-16 09:30:11

标签: html css

尝试垂直居中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>&lt;&frasl;BY&#95;John Doe&gt;</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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

Flex盒子可以为您实现这一目标。只需将此添加到.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>&lt;&frasl;BY&#95;John Doe&gt;</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>