父元素高度为auto:子元素使用top:-50%没有效果的问题

本文探讨了CSS中当父元素高度设为auto时,子元素使用百分比定位(top: -50%)失效的原因,并通过实例对比展示了不同设置下的布局效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当为父元素设定了height:auto的时候,子元素设置position:relative top:50%没有效果的问题

代码如下:

<!DOCTPYE html>
<html>
<head>
    <meta content="text/html" charset="utf-8">
    <style type="text/css">
     html, body
      {
          width:100%;
          height:100%;
      }
      #one
      {
          position: absolute;
          top:50%;
          left:50%;
         <span style="color:#ff0000;"><strong> height:auto;</strong></span>
          width:auto;
      }
      .pa
      {
          position: relative;
          height:400px;
          width:200px;
          <strong><span style="color:#ff0000;">top:-50%;</span></strong>
          left:-50%;
          border:1px solid black;
          background-color:greenyellow ;

      }

    </style>
</head>

<body>
<div class="pr" id="one">

    <div class="pa pa1">#one的子元素pa1,相对#one绝对定位,#one是它的父元素,与.pa2为同级兄弟元素</div>

</div>

</body>
</html>
其实这段代码是想让 .pa 水平垂直居中的,可是并没有效果,top:-50%没有效果,如下图


正常情况下父元素设置height:auto应用获得子元素最大高度,在浏览器中也显示了父元素的模型高度是子元素高度400px,但是使用top:-50%并没有效果,这是因为

w3c的文档说

<percentage>
The offset is a percentage of the containing block's width (for 'left' or 'right') or height (for 'top' and 'bottom'). For 'top' and 'bottom', if the height of the containing block is not specified explicitly (i.e., it depends on content height), the percentage value is interpreted like 'auto'.
如果父元素的高度没有明确指定,top 的值跟设定为auto的效果是一样的 。


当我们把height高度设定为固定值的时候:比如height:400px,代码如下,这时候子元素使用top:-50%就有效果了

<!DOCTPYE html>
<html>
<head>
    <meta content="text/html" charset="utf-8">
    <style type="text/css">
     html, body
      {
          width:100%;
          height:100%;
      }
      #one
      {
          position: absolute;
          top:50%;
          left:50%;
      <strong><span style="color:#cc0000;">  </span><span style="color:#ff0000;">  height:400px;</span></strong>
          width:auto;
      }
      .pa
      {
          position: relative;
          height:400px;
          width:200px;
      <strong>  <span style="color:#ff0000;">  top:-50%;</span></strong>
          left:-50%;
          border:1px solid black;
          background-color:greenyellow ;

      }

    </style>
</head>

<body>
<div class="pr" id="one">

    <div class="pa pa1">#one的子元素pa1,相对#one绝对定位,#one是它的父元素,与.pa2为同级兄弟元素</div>

</div>

</body>
</html>
结果如下图



所以,当父元素height设定为:auto的时候,子元素position:relative top:-50%,top:百分比值并不起作用,用px可以,比如:top:-(子元素高度的一半)


} 一个最长的用法就是:让弹出的元素始终位于页面的中间位置。首先position: fixed; top: 50%; left: 50%;让这个图片的左上角处在屏幕的正中间。然后让他上下位移transform:translateX(-50%) translateY(-50%);就是相对于这个图片的宽高的50%,也就是一半。移回来。 .center-vertical {    position: fixed;    top: 50%;    left:50%;      -webkit-transform: translate(-50%-50%);    -ms-transform: translate(-50%,-50%);    -moz-transform: translate(-50%,-50%);    -o-transform: translate(-50%,-50%);    transform: translate(-50%-50%);}肖肖肖丽珠关注120分享专栏目录CSS实现垂直居中的4种思路详解09-24CSS实现垂直居中是网页布局中的常见需求,本文将详细讲解四种不同的方法,帮助开发者更好地理解和运用这些技巧。 1. **行高line-height实现单行文本垂直居中** 单行文本的垂直居中可以通过设置`line-height`来实现...CSS进阶之形变与动画 (一):transform、垂直居中总结、transition动画、animation动画、vertical-alignGood Good Study,Day Day Up! 1万+水平居中的方案行内级元素:设置父元素的 text-align:center块级元素:设置当前块级元素(要有宽度) margin:0 auto绝对定位:元素有宽度的情况下,设置left0、right0、margin:0 auto;flex布局:通过设置justify-content: center;垂直居中的方案绝对定位: 元素要有高度的情况下,设置top0、bottom0、margin:auto 0;flex布局 通过设置align-CSS 盒子垂直居中的方法_css transform居中3-15margin-top: 50%; // 向下移动父盒子的一半 transform: translateY(-50%); // 向上移动自身盒子的一半 } /* 通过 定位来移动*/ .father { width: 500px; height: 500px; background-color: skyblue; border: 1px solid #000; margin: 0 auto; position: relative; } .son { width: 200px; height...CSS进阶之形变与动画 (一):transform、垂直居中总结、transition动画、a...3-13translate的水平垂直居中 水平居中:使用left/translate,其中需要知道left是基于包含块的宽度,translate是基于自身的大小。 垂直居中: 使用top/translate,其中需要知道top是基于包含块的高度,translate是基于自身的大小。 .shuipin{ position: relative; left: 50%; transform: translateX(-50%); } .chuizhi{ position...transform 垂直居中weixin_33948416的博客 9212019独角兽企业重金招聘Python工程师标准>>> ...css水平垂直居中方案最新发布m0_73351190的博客 187本篇文章介绍比较常见的三种方法。...的元素水平垂直居中的总结_绝对定位位水平垂直居中 transform...3-15DOCTYPEhtml>元素的垂直居中.elem1{position:relative;width:500px;height:500px;border:1px solid red; } .elem2{position:absolute;width:200px;height:200px;border:1px solid#9521de;left:50%;top:50%;transform:translate(-50%,-50%); } 效果如下: 不过transform是css3里面的内容,所以存在兼容性问题;IE9...实现div元素在整个屏幕的的垂直居中之translateY(-50%)的利用_tran...3-14本文介绍了如何使用CSS的transform: translateY(-50%)实现div元素在整个屏幕的垂直居中。通过设置position: absolute,top: 50%,right: 20px,再结合transform: translateY(-50%),即使元素高度不确定,也
03-19
body { padding: 0; margin: 0 } #unity-container { position: absolute } #unity-container.unity-desktop { left: 50%; top: 50%; transform: translate(-50%, -50%) } #unity-container.unity-mobile { position: fixed; width: 100%; height: 100% } #unity-canvas { background: {{{ BACKGROUND_COLOR }}} } .unity-mobile #unity-canvas { width: 100%; height: 100% } #unity-loading-bar { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); display: none } #unity-logo { width: 154px; height: 130px; background: url('webgl-logo.png') no-repeat center } #unity-progress-bar-empty { width: 1000px; height: 18px; margin-top: 10px; margin-left: 6.5px; background: url('frame_gaojing_nomal@2x.png') no-repeat center } #unity-progress-bar-full { width: 0%; height: 18px; margin-top: 10px; background: url('progress-bar-full-dark.png') no-repeat center } #unity-footer { position: relative } .unity-mobile #unity-footer { display: none } #unity-build-title { float: right; margin-right: 10px; line-height: 38px; font-family: arial; font-size: 18px } #unity-fullscreen-button { cursor:pointer; float: right; width: 38px; height: 38px; background: url('fullscreen-button.png') no-repeat center } #unity-warning { position: absolute; left: 50%; top: 5%; transform: translate(-50%); background: white; padding: 10px; display: none }我需要#unity-logo { width: 154px; height: 130px; background: url('webgl-logo.png') no-repeat center }这个logo在应用中间 然后#unity-progress-bar-full { width: 0%; height: 18px; margin-top: 10px; background: url('progress-bar-full-dark.png') no-repeat center }这个进度会从0到100加载 不是一整条过去显示
最新发布
08-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值