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
Div height doesn't adjust to fit content
提问by Ptheitroad
How can I center a div horizontally and vertically and adjust height to fit content?
如何水平和垂直居中 div 并调整高度以适应内容?
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:
这就是我要的:
回答by Pradip Borde
Edit .content class to have following css and remove position absolute
编辑 .content 类以具有以下 css 并删除绝对位置
height:auto;
overflow:visible;
回答by NoobEditor
In your .content
class, remove
position: absolute;
and add
margin-top:24%;
to align it in vertically middle while taking the height of the contentabsolute
positioned 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 relative
content
class....
诀窍是absolute
<span>
在relative
content
课堂上对齐 a ....
回答by SRC SRC
replace your css
with 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;
}