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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20267675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 01:08:59  来源:igfitidea点击:

Div height doesn't adjust to fit content

cssheightcentervertical-alignment

提问by Ptheitroad

How can I center a div horizontally and vertically and adjust height to fit content?

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

fiddle

小提琴

Here is my html code:

这是我的 html 代码:

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

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

         lorem lipsum.....

     </div>

 </div>

And css:

和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;
}

This is what I want:

这就是我要的:

enter image description here

在此处输入图片说明

回答by Pradip Borde

Edit .content class to have following css and remove position absolute

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

height:auto;
overflow:visible;

回答by NoobEditor

In your .contentclass, remove

                         position: absolute;

                          and add

                          margin-top:24%;

to align it in vertically middle while taking the height of the content


absolutepositioned attrib are rather difficult to style!

在您的.content班级中,删除

                         position: absolute;

                          和添加

                          margin-top:24%;

以将其垂直居中对齐,同时取内容的高度


absolute定位 attrib 很难设置样式!

Fiddle: http://jsfiddle.net/logintomyk/Xxfhn/

小提琴http: //jsfiddle.net/logintomyk/Xxfhn/

EDIT

编辑

Here you go mate

来了,伙计

CSS to ammend :

CSS 修改:

  .sprite{
    position: fixed;
    left: 0px;
    top: 0px;
    z-index: 20;
     width: 100%;
    /* height: 100%; */
    background-color: gray;
    opacity: 0.6;
    border:1px solid #FFF;
    text-align:center;
}

.content{
     border:1px solid red; 
     position:relative;
     z-index: 21; /* change this to less than 20 to overlay under sprite on scroll*/
     margin:auto;
     padding:10px;
     width:30%;  
     margin-top:24%;
     text-align:center; 
}

.content span{position:absolute;right:0;top:0;text-align:right; border:1px solid #F00000}

Fiddle: http://jsfiddle.net/logintomyk/Xxfhn/2/

小提琴http: //jsfiddle.net/logintomyk/Xxfhn/2/

Trick was to align a absolute<span>in relativecontentclass....

诀窍是absolute<span>relativecontent课堂上对齐 a ....

回答by SRC SRC

replace your csswith mine...

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;

     width:30%;  

     text-align:center; 
}

.content span{position:absolute; top:0px; right:0px;}

.centered{

/* 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;
}

回答by SRC SRC

.content{
     border:1px solid red; 
     position:relative;

     margin:auto;
     padding:10px;
     width:30%;  
     margin-top:4%;
     text-align:center; 
}

.content span{position:absolute;right:0;top:0;text-align:right;}

.sprite{
    position: fixed;
    left: 0px;
    top: 0px;

     width: 100%;

    background-color: gray;
    opacity: 0.6;
    border:1px solid #FFF;
    text-align:center;
}

does that help

这有帮助吗

回答by CodingSince007

You are making this hard on yourself, just change the Position to be Relative in the class content, see below;

您对自己太苛刻了,只需将课程内容中的位置更改为相对,见下文;

 .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; 
 }