html - 问题居中元素

标签 html css

我正在编写菜单代码,这次使用 CSS 制作动画,并使用 js 将类从显示更改为隐藏和半隐藏。 (js还没入手)

有些问题我不知道如何解决(压力很大)。

  • 为什么上边距不是 0?
  • 为什么在 div 之间的左侧和顶部有一些 padding/margin/wtv?
  • 如何使链接垂直居中? (我认为我需要继续使用 100% 并固定位置并 overflow hidden ,因为我需要将 div 移出屏幕)

html,
body {
  background: #E6E6E6;
}
.menu {
  overflow: hidden;
  position: fixed;
  height: 100%;
  width: 100%;
  -webkit-transition: all ease .65s;
  -moz-transition: all ease .65s;
  -ms-transition: all ease .65s;
  -o-transition: all ease .65s;
  transition: all ease .65s;
}
.left {
  right: 0;
  margin-right: 0;
}
.left.bar {
  right: 100%;
  margin-right: -60px;
}
.left.hidden {
  right: 100%;
  margin-right: 0;
}
.menu > .links {
  color: white;
  font-family: sans-serif;
  font-size: 2.5em;
  font-weight: 500;
  text-align: center;
  text-transform: uppercase;
}
.menu li {
  color: rgba(255, 255, 255, .75);
  cursor: pointer;
  list-style: none;
  padding: 15px;
  margin: 5px;
  -webkit-transition: all ease 1s;
  -moz-transition: all ease 1s;
  -ms-transition: all ease 1s;
  -o-transition: all ease 1s;
  transition: all ease 1s;
}
.menu li:hover {
  color: rgba(255, 255, 255, 1);
  background: rgba(0, 0, 0, .1);
}
.m1 {
  background: orange;
  z-index: 8;
}
<div class="menu left m1">
  <div class="links l1">
    <ul>
      <li data-open=".m2">Architecture</li>
      <li data-open=".m3">Coding</li>
      <li data-open=".m4">Thesis</li>
    </ul>
  </div>
</div>

Screenshot

最佳答案

要删除左侧和顶部的空间,您必须覆盖 ul 的默认样式:

ul{
    padding: 0;
    margin: 0;
}

为了垂直居中,我会这样做:

.links ul{
    position: relative;
    top: 50%;
    -webkit-transform: translateY(-50%);
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);
}

.links{
    height: 100%;   
}

在这里查看:JSFiddle

关于html - 问题居中元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26593652/

相关文章:

Javascript 表单验证不适用于 2 个输入字段

javascript - HTML5 : Input field with the type of 'number' will still accept higher value than its 'max' using keyboard inputs

html - 使文本适合 div。 Django 模板

javascript - 如何找出 contenteditable 属性不起作用的原因?

javascript - 从 contentEditable 中删除元素后出现奇怪的行为

css - 隐藏的标题标签是否具有语义值(value)?

html - 可访问的 HTML 表单输入类型 - "submit"与 "image"

html - 如何在前面显示模态?

javascript - 窗口大小改变时如何防止div重叠?

css - 基于父页面的自定义元素 Polymer 对话框大小/位置