问题
How can I center a div horizontally and vertically and adjust height to fit content?
fiddle
Here is my html code:
<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;
}
This is what I want:

回答1:
Edit .content class to have following css and remove position absolute
height:auto;
overflow:visible;
回答2:
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!
Fiddle : http://jsfiddle.net/logintomyk/Xxfhn/
EDIT
Here you go mate
CSS to ammend :
.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/
Trick was to align a absolute
<span>
in relative
content
class....
回答3:
replace your css
with mine...
.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;
}
回答4:
.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
回答5:
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;
}
来源:https://stackoverflow.com/questions/20267675/div-height-doesnt-adjust-to-fit-content