Div 高度不会调整以适应内容

2023-11-24

如何使 div 水平和垂直居中并调整高度以适应内容?

fiddle

这是我的 html 代码:

 <div class="sprite">
  </div>

 <div class="content">
     <span>close</span>
     <div class="centered">

         lorem lipsum.....

     </div>

 </div>

And css:

.sprite{
    position: fixed;
    left: 0px;
    top: 0px;
    z-index: 20;
    width: 100%;
    height: 100%;
    background-color: gray;
    opacity: 0.6;
}

.content{
     border:1px solid red; 
     z-index:21; 
     position: absolute; 
     margin:auto; 
     padding:10px;
     left: 0px;
     top: 0px; 
     bottom:0px; 
     right:0px;
     height:30%; 
     width:30%;  
     text-align:center; 
}
.content span{
     position:absolute; 
     top:0px; 
     right:0px;}

.centered{
     height:100%; 
/* Internet Explorer 10 */
     display:-ms-flexbox;
     -ms-flex-pack:center;
     -ms-flex-align:center;

/* Firefox */
    display:-moz-box;
    -moz-box-pack:center;
    -moz-box-align:center;

/* Safari, Opera, and Chrome */
    display:-webkit-box;
    -webkit-box-pack:center;
    -webkit-box-align:center;

/* W3C */
   display:box;
   box-pack:center;
   box-align:center;
}

这就是我要的:

enter image description here


编辑 .content 类以具有以下 css 并删除绝对位置

height:auto;
overflow:visible;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Div 高度不会调整以适应内容 的相关文章

随机推荐