我需要在窗口中心放置一个div
(带有position:absolute;
)元素。但是我这样做有问题,因为宽度未知。
我试过了 但是由于宽度是响应性的,因此需要对其进行调整。
.center {
left: 50%;
bottom:5px;
}
有任何想法吗?
我需要在窗口中心放置一个div
(带有position:absolute;
)元素。但是我这样做有问题,因为宽度未知。
我试过了 但是由于宽度是响应性的,因此需要对其进行调整。
.center {
left: 50%;
bottom:5px;
}
有任何想法吗?
You can place the image in a div and add a div id and have the CSS for that div
have a text-align:center
HTML:
<div id="intro_img">
<img src="???" alt="???">
</div>
CSS :
#intro_img {
text-align:center;
}
.center {
position: absolute
left: 50%;
bottom: 5px;
}
.center:before {
content: '';
display: inline-block;
margin-left: -50%;
}
HTML:
<div class="wrapper">
<div class="inner">
content
</div>
</div>
CSS:
.wrapper {
position: relative;
width: 200px;
height: 200px;
background: #ddd;
}
.inner {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
margin: auto;
width: 100px;
height: 100px;
background: #ccc;
}
This and more examples here
This solution works if the element has width
and height
.wrapper {
width: 300px;
height: 200px;
background-color: tomato;
position: relative;
}
.content {
width: 100px;
height: 100px;
background-color: deepskyblue;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
<div class="wrapper">
<div class="content"></div>
</div>
#container
{
position: relative;
width: 100%;
float:left
}
#container .item
{
width: 50%;
position: absolute;
margin: auto;
left: 0;
right: 0;
}
sass/compass version of Responsive Solution above:
#content {
position: absolute;
left: 50%;
top: 50%;
@include vendor(transform, translate(-50%, -50%));
}
My preferred centering method:
position: absolute;
margin: auto;
width: x%
JSFiddle here
This worked for me :
<div class="container><p>My text</p></div>
.container{
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
Flex can be used to center absolute positioned div.
display:flex;
align-items:center;
justify-content:center;
.relative {
width: 275px;
height: 200px;
background: royalblue;
color: white;
margin: auto;
position: relative;
}
.absolute-block {
position: absolute;
height: 36px;
background: orange;
padding: 0px 10px;
bottom: -5%;
border: 1px solid black;
}
.center-text {
display: flex;
justify-content: center;
align-items: center;
box-shadow: 1px 2px 10px 2px rgba(0, 0, 0, 0.3);
}
<div class="relative center-text">
Relative Block
<div class="absolute-block center-text">Absolute Block</div>
</div>
** RESPONSIVE SOLUTION **
Assuming the element in the div, is another div...
this solution works fine
<div class="container">
<div class="center"></div>
</div>
the container can be any size(must be position relative)
.container {
position: relative;/*important*/
width: 200px;/*any width*/
height: 200px;/*any height*/
background: red;
}
The element(div) can also be any size(must be smaller than container)
.center {
position: absolute;/*important*/
top: 50%;/*position Y halfway in*/
left: 50%;/*position X halfway in*/
transform: translate(-50%,-50%);/*move it halfway back(x,y)*/
width: 100px;/*any width*/
height: 100px;/*any height*/
background: blue;
}
The result will look like this. Run the code snippet
.container {
position: relative;
width: 200px;
height: 200px;
background: red;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="center"></div>
</div>
I found it very helpfull
This is a mix of other answers, which worked for us:
.el {
position: absolute;
top: 50%;
margin: auto;
transform: translate(-50%, -50%);
}
Works on any random unknown width of the absolute positioned element you want to have in the centre of your container element.
<div class="container">
<div class="box">
<img src="https://picsum.photos/200/300/?random" alt="">
</div>
</div>
.container {
position: relative;
width: 100%;
}
.box {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
}
If you need to center horizontally and vertically too:
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
Searching for an solution I got answers above and could make content centered with Matthias Weiler answer but using text-align.
#content{
position:absolute;
left:0;
right:0;
text-align: center;
}
Worked with chrome and Firefox.
this work for vertical and horizontal
#myContent{
position: absolute;
left: 0;
right: 0;
top:0;
bottom:0;
margin: auto;
}
and if you want make element center of parent, set position of parent relative
#parentElement{
position:relative
}
edit:
for vertical center align set height to your element. thanks to @Raul
if you want make element center of parent, set position of parent to relative
Absolute Centre
HTML :
<div class="parent">
<div class="child">
<!-- content -->
</div>
</div>
CSS :
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
Demo: http://jsbin.com/rexuk/2/
Tested in Google Chrome, Firefox, and IE8
Hope this helps :)
Really nice post.. Just wanted to add if someone wants to do it with single div tag then here the way out:
Taking width
as 900px
.
#styleName {
position: absolute;
left: 50%;
width: 900px;
margin-left: -450px;
}
In this case one should know the width
beforehand.
<body>
<div style="position: absolute; left: 50%;">
<div style="position: relative; left: -50%; border: dotted red 1px;">
I am some centered shrink-to-fit content! <br />
tum te tum
</div>
</div>
</body>
A simple approach that worked for me to horizontally center a block of unknown width:
A text-align property may be added to the #block ruleset to align its content independently of the alignment of the block.
This worked on recent versions of Firefox, Chrome, IE, Edge and Safari.