html - 顶部 :-50% not working?

标签 html css

根据这个 tutorial 处理我的示例为了为我的网站制作独特的 CSS,我在 code 中发现了一个问题:

HTML

<div class="items">
  <div class="outerContainer">
      <div class="innerContainer">
          <div class="element"><a href="#">Finally over four lines, all the code is the same for each list</a></div>
      </div>
  </div>
</div>

CSS

.items .outerContainer
{
    height: 180px;
    width: 200px;
    background-color: #EBEBEB;
    position: relative;    
}

.items .outerContainer .innerContainer
{
    width: 100%;
    text-align: center;
    position: absolute;
    top: 50%;
    background-color:red;    
}

.items .outerContainer .innerContainer .element
{
    position: relative;
    top: -50%;
}

为什么 top: -50%; of .items .outerContainer .innerContainer .element 没有将元素“移动”到顶部?例如,如果我写 top: -20px 效果很好,但我想要 %.

为什么?我该如何解决?

奇怪,它只适用于 IE7 :)

最佳答案

按百分比移动元素要求包含元素具有高度属性。所以如果你把 height:90px; (我收集到的是正确的,因为该元素高 180 像素,内容器应距离顶部 50%)它应该有效。

关于html - 顶部 :-50% not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12603727/

相关文章:

css - HTML/CSS 之谜?不知道在哪里调出尺寸

html - 将包含多个图像的 div 居中放置在另一个 div 中

javascript - 这段代码中的 jQuery focus() 方法在做什么?

html - 删除 <h1> 和 <p> 之间的间隙?

html - 带有图像背景的无序列表,如果 1 或 2 行,则使其高度相同

css - 在 2 列中堆叠可变高度的 div,例如 Facebook 时间轴

html - 不同缩放级别的元素之间的间隙

javascript - Blur() 事件不会在 webkit 浏览器中触发选择表单

html - 在 drupal 7 中将图像内容类型显示为网格?

html - IFrame 内容在 IE7 和 IE=EmulateIE7 (Sharepoint) 中显示为白色