Align image in center and middle within div
I have following div
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png">
</div>
How to align the image so as to be located in the middle and center of div ?
html css
add a comment |
I have following div
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png">
</div>
How to align the image so as to be located in the middle and center of div ?
html css
10
Duplicate asked 2 minutes ago: CSS: image middle
– Pekka 웃
Feb 3 '11 at 15:43
1
Similar topic here: stackoverflow.com/questions/18516317/…
– Hashem Qolami
Jan 20 '14 at 23:38
Consider selecting one answer as correct.
– McSonk
Apr 20 '17 at 14:59
Possible duplicate of How to position image in the center/middle both vertically and horizontally
– gbjbaanb
Oct 3 '17 at 15:06
add a comment |
I have following div
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png">
</div>
How to align the image so as to be located in the middle and center of div ?
html css
I have following div
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png">
</div>
How to align the image so as to be located in the middle and center of div ?
html css
html css
edited Jul 19 '14 at 0:11
simple_human
9,09344667
9,09344667
asked Feb 3 '11 at 15:42
Dro1n2Dro1n2
1,3352117
1,3352117
10
Duplicate asked 2 minutes ago: CSS: image middle
– Pekka 웃
Feb 3 '11 at 15:43
1
Similar topic here: stackoverflow.com/questions/18516317/…
– Hashem Qolami
Jan 20 '14 at 23:38
Consider selecting one answer as correct.
– McSonk
Apr 20 '17 at 14:59
Possible duplicate of How to position image in the center/middle both vertically and horizontally
– gbjbaanb
Oct 3 '17 at 15:06
add a comment |
10
Duplicate asked 2 minutes ago: CSS: image middle
– Pekka 웃
Feb 3 '11 at 15:43
1
Similar topic here: stackoverflow.com/questions/18516317/…
– Hashem Qolami
Jan 20 '14 at 23:38
Consider selecting one answer as correct.
– McSonk
Apr 20 '17 at 14:59
Possible duplicate of How to position image in the center/middle both vertically and horizontally
– gbjbaanb
Oct 3 '17 at 15:06
10
10
Duplicate asked 2 minutes ago: CSS: image middle
– Pekka 웃
Feb 3 '11 at 15:43
Duplicate asked 2 minutes ago: CSS: image middle
– Pekka 웃
Feb 3 '11 at 15:43
1
1
Similar topic here: stackoverflow.com/questions/18516317/…
– Hashem Qolami
Jan 20 '14 at 23:38
Similar topic here: stackoverflow.com/questions/18516317/…
– Hashem Qolami
Jan 20 '14 at 23:38
Consider selecting one answer as correct.
– McSonk
Apr 20 '17 at 14:59
Consider selecting one answer as correct.
– McSonk
Apr 20 '17 at 14:59
Possible duplicate of How to position image in the center/middle both vertically and horizontally
– gbjbaanb
Oct 3 '17 at 15:06
Possible duplicate of How to position image in the center/middle both vertically and horizontally
– gbjbaanb
Oct 3 '17 at 15:06
add a comment |
29 Answers
29
active
oldest
votes
JSFiddle
body {
margin: 0;
}
#over img {
margin-left: auto;
margin-right: auto;
display: block;
}
<div id="over" style="position:absolute; width:100%; height:100%">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
8
doesnt work, this will work if the element is not floating on any direction...
– vikas devde
Apr 21 '13 at 12:26
4
display: block;
was my pitfall. TnX
– Ujjwal Singh
May 9 '13 at 8:23
2
Following doesn't work. What's the mistake i am doing. <html> <head> <style> #over img { display: block; margin-left: auto; margin-right: auto; } </style> </head> <body> <div id="over" style="position:absolute; width:100%; height:100%"> <img src="img8a.flixcart.com/image/tablet/f/k/t/…"> </div> </body> </html>
– nizam.sp
Oct 21 '13 at 11:00
8
This doesn't align vertically
– alpadev
May 11 '17 at 14:32
13
How can an answer that do not attend the question get more than 300 votes???
– Rodrigo
Jun 9 '17 at 13:13
|
show 4 more comments
<div style="display:table-cell; vertical-align:middle; text-align:center">
<img src="img.png">
</div>
4
top answer, very helpful!
– Ilian Andreev
Jun 2 '13 at 19:58
1
Very short and easy solution, but it appears to only be working with a fixed, non-percentage width and height. It does work with floats though so +1 for that. — jsfiddle.net/2s2nY/2
– magnetronnie
May 26 '14 at 10:10
1
but this is only doing the vertical-align but not the horizontal right?
– V-SHY
Mar 25 '15 at 3:38
1
It won't work if image's width larger than div's width.
– Davuz
May 26 '15 at 7:02
4
If we remove display:table-cell; it works perfactly.
– Ahesanali Momin
May 18 '17 at 7:39
|
show 2 more comments
Seems to me that you also wanted that image to be vertically centered within the container. (I didn't see any answer that provided that)
Working Fiddle:
- Pure CSS solution
- Not breaking the document flow (no floats or absolute positioning)
- Cross browser compatibility (even IE6)
- Completely responsive.
HTML
<div id="over">
<span class="Centerer"></span>
<img class="Centered" src="http://th07.deviantart.net/fs71/200H/f/2013/236/d/b/bw_avlonas_a5_beach_isles_wallpaper_image_by_lemnosexplorer-d6jh3i7.jpg" />
</div>
CSS
*
{
padding: 0;
margin: 0;
}
#over
{
position:absolute;
width:100%;
height:100%;
text-align: center; /*handles the horizontal centering*/
}
/*handles the vertical centering*/
.Centerer
{
display: inline-block;
height: 100%;
vertical-align: middle;
}
.Centered
{
display: inline-block;
vertical-align: middle;
}
Note: this solution is good to align any element within any element.
for IE7, when applying the .Centered
class on block elements, you will have to use another trick to get the inline-block
working. (that because IE6/IE7 dont play well with inline-block on block elements)
10
Only this one is working (vertical & horizontal)
– Bartek Kosa
Nov 13 '13 at 13:01
1
This is superb...Magic!!! Worked awesomly well! Thanks so much for this...
– Nitin Bansal
Jul 1 '14 at 10:55
2
excellent.. this should the answer to this question.
– user3790264
Sep 30 '14 at 10:59
2
Should be the accepted answer +1
– Lehan Coetzee
Feb 2 '16 at 11:17
2
Works perfectly!
– wotaskd
Jun 8 '16 at 14:28
|
show 15 more comments
This can also be done using the Flexbox layout:
STATIC SIZE
.parent {
display: flex;
height: 300px; /* Or whatever */
background-color: #000;
}
.child {
width: 100px; /* Or whatever */
height: 100px; /* Or whatever */
margin: auto; /* Magic! */
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
DYNAMIC SIZE
html, body {
width: 100%;
height: 100%;
display: flex;
background-color: #999;
}
* {
margin: 0;
padding: 0;
}
.parent {
margin: auto;
background-color: #000;
display: flex;
height: 80%;
width: 80%;
}
.child {
margin: auto; /* Magic! */
max-width: 100%;
max-height: 100%;
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
I found the example in this article, which does a great job explaining the how to use layout.
flex is state of the art - just works without tricks
– Bernhard Zürn
Jan 17 '17 at 20:02
In the Static Size, there's no need for width and height for the child (at least in my version of Firefox).
– Rodrigo
Jun 9 '17 at 13:23
add a comment |
img.centered {
display: block;
margin: auto auto;
}
1
this is very similar to this answer
– simple_human
Jul 19 '14 at 0:29
add a comment |
#over {position:relative; text-align:center;
width:100%; height:100%; background:#CCC;}
#over img{
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
Best solution that actually works.
– Burjua
Jun 15 '17 at 13:10
This is the right answer, that actually centers both vertically and horizontally.
– Alexander Kim
Jun 7 '18 at 8:44
Best answer so far.
– kiran puppala
Jan 31 at 9:02
add a comment |
You can do this easily by using display:flex css property
#over {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
add a comment |
I still had some issues with other solution presented here. Finally this worked best for me:
<div class="parent">
<img class="child" src="image.png"/>
</div>
css3:
.child {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%); /* Safari and Chrome */
-moz-transform: translate(-50%, -50%); /* Firefox */
-ms-transform: translate(-50%, -50%); /* IE 9 */
-o-transform: translate(-50%, -50%); /* Opera */
// I suppose you may like those too:
// max-width: 80%;
// max-height: 80%;
}
You can read more about that approach at this page.
I had to also add parent css but your code worked perfectly! position: relative; width: 100%; float: left; overflow: hidden; min-height: 189px;
– user2593040
Dec 9 '16 at 9:12
add a comment |
Basically, setting right and left margin to auto will cause the image to center align.
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png" style="display: block; margin: 0 auto;">
</div>
add a comment |
This would be a simpler approach
#over > img{
display: block;
margin:0 auto;
}
add a comment |
setting the img to display:inline-block while having set the superior div to text-align:center will do the job too
EDIT: to those folks who are playing with display:inline-block >>> don't forget that e.g. two divs like
<div>Div1 content</div>NOSPACEHERE<div>Div2 content</div>
really have no spaces between them (as seen here).
Just basic to avoid these (inline block inherent) gaps between them. These gaps can be seen between every two words I'm writing right now! :-) So .. hope this helps some of you.
add a comment |
SIMPLE. 2018. FlexBox. To Check Browser Support - Can I Use
Minimal Solution:
div#over {
display: flex;
justify-content: center;
align-items: center;
}
To get the widest browser support possible:
div#over {
display: -webkit-flex;
display: -ms-flex;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
}
add a comment |
I've tried many approaches but only this one works for multiple inline elements inside a container div. Here is code to align everything in div at middle.
CSS
.divContainer
{
vertical-align: middle;
text-align: center; <!-- If you want horizontal center alignment -->
}
.divContainer > *
{
vertical-align: middle;
}
HTML
<div class="divContainer">
<span>Middle Text</span>
<img src="test.png"/>
</div>
Sample code is here: https://jsfiddle.net/yogendrasinh/2vq0c68m/
add a comment |
CSS File
.over {
display : block;
margin : 0px auto;
add a comment |
Try this minimal code:
<div class="outer">
<img src="image.png"/>
</div>
And CSS:
.outer{
text-align: center;
}
.outer img{
display: inline-block;
}
add a comment |
many answer suggests to use margin:0 auto
but this works only when the element you trying to make centered is not floating on left or right, that is float
css attribute isn't set. In order to do this apply display
attribute to table-cell
and then apply margin of left and right to auto so your style will look like style="display:table-cell;margin:0 auto;"
add a comment |
<div>
<p style="text-align:center; margin-top:0px; margin-bottom:0px; padding:0px;">
<img src="image.jpg" alt="image"/>
</p>
</div>
add a comment |
HTML:
<div id="over">
<img src="img.png">
</div>
CSS:
#over {
text-align: center;
}
#over img {
vertical-align: middle;
}
It's best if you add some commentary and not just toss code out there.
– Parkash Kumar
Oct 26 '15 at 10:43
add a comment |
For center horizontally Just put
#over img {
display: block;
margin: 0 auto;
clear:both;
}
Another method:
#over img {
display: inline-block;
text-align: center;
}
For center vertically Just put:
#over img {
vertical-align: middle;
}
add a comment |
This worked for me:
#image-id {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
width: auto;
margin: 0 auto;
}
add a comment |
Well, we are in 2016... why not use flexbox?
It's also responsive. Enjoy.
#over{
display:flex;
align-items:center;
justify-content:center;
}
<div id="over">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
1
Love css3! Thank you. However, its good to note that this will only work in modern browsers (except IE). All other browsers will have no effect.
– Neel
Jan 9 '17 at 16:44
add a comment |
this did the trick for me.
<div class="CenterImage">
<asp:Image ID="BrandImage" runat="server" />
</div>
'Note: do not have a css class assocaited to 'BrandImage' in this case
CSS:
.CenterImage {
position:absolute;
width:100%;
height:100%
}
.CenterImage img {
margin: 0 auto;
display: block;
}
add a comment |
Use positioning. The following worked for me...
With zoom to the center of the image (image fills the div):
div{
display:block;
overflow:hidden;
width: 70px;
height: 70px;
position: relative;
}
div img{
min-width: 70px;
min-height: 70px;
max-width: 250%;
max-height: 250%;
top: -50%;
left: -50%;
bottom: -50%;
right: -50%;
position: absolute;
}
Without zoom to the center of the image (image does not fill the div):
div{
display:block;
overflow:hidden;
width: 100px;
height: 100px;
position: relative;
}
div img{
width: 70px;
height: 70px;
top: 50%;
left: 50%;
bottom: 50%;
right: 50%;
position: absolute;
}
add a comment |
You can take a look on this solution:
Centering horizontally and vertically an image in a box
<style type="text/css">
.wraptocenter {
display: table-cell;
text-align: center;
vertical-align: middle;
width: ...;
height: ...;
}
.wraptocenter * {
vertical-align: middle;
}
.wraptocenter {
display: block;
}
.wraptocenter span {
display: inline-block;
height: 100%;
width: 1px;
}
</style>
<!--[if lt IE 8]-->
<style>
.wraptocenter span {
display: inline-block;
height: 100%;
}
</style>
<!--[endif]-->
<div class="wraptocenter"><span></span><img src="..." alt="..."></div>
add a comment |
A simple way would be creating separate styles for both the div and the image and then position them independently. Say if I want to set my image position to 50%, then I would have code which looks like the following....
#over{
position:absolute;
width:100%;
height:100%;
}
#img{
position:absolute;
left:50%;
right:50%;
}
<div id="over">
<img src="img.png" id="img">
</div>
Just tell me if it works with all kinds of browsers, even though this is a basic thing that every browser must support (otherwise don't call it a browser)
– KSJ10
Nov 21 '16 at 17:00
add a comment |
#over {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 100px;
}
Modify height value for your need.
add a comment |
This should work.
IMPORTANT FOR TEST: To Run code snippet click left button RUN code snippet, then right link Full page
#fader{
position:fixed;z-index: 10;
top:0;right:0;bottom:0;left:0;
opacity: 0.8;background: black;
width:100%;height:100%;
text-align: center;
}
.faders{display:inline-block;height:100%;vertical-align:middle;}
.faderi{display:inline-block;vertical-align:middle;}
<div id="fader">
<span class="faders"></span>
<img class="faderi" src="https://i.stack.imgur.com/qHF2K.png" />
</div>
add a comment |
Use bootstraps align-items
and justify-content
. See example below:
<div class="well" style="align-items:center;justify-content:center;">
<img src="img_source" height="50px" width="50px"/>
</div>
add a comment |
I add some more properties to the CSS. Like so:
div#over {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
-ms-align-items: center;
display: -webkit-flex;
display: -ms-flex;
display: flex;
}
add a comment |
protected by Tushar Gupta Oct 15 '14 at 17:37
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
29 Answers
29
active
oldest
votes
29 Answers
29
active
oldest
votes
active
oldest
votes
active
oldest
votes
JSFiddle
body {
margin: 0;
}
#over img {
margin-left: auto;
margin-right: auto;
display: block;
}
<div id="over" style="position:absolute; width:100%; height:100%">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
8
doesnt work, this will work if the element is not floating on any direction...
– vikas devde
Apr 21 '13 at 12:26
4
display: block;
was my pitfall. TnX
– Ujjwal Singh
May 9 '13 at 8:23
2
Following doesn't work. What's the mistake i am doing. <html> <head> <style> #over img { display: block; margin-left: auto; margin-right: auto; } </style> </head> <body> <div id="over" style="position:absolute; width:100%; height:100%"> <img src="img8a.flixcart.com/image/tablet/f/k/t/…"> </div> </body> </html>
– nizam.sp
Oct 21 '13 at 11:00
8
This doesn't align vertically
– alpadev
May 11 '17 at 14:32
13
How can an answer that do not attend the question get more than 300 votes???
– Rodrigo
Jun 9 '17 at 13:13
|
show 4 more comments
JSFiddle
body {
margin: 0;
}
#over img {
margin-left: auto;
margin-right: auto;
display: block;
}
<div id="over" style="position:absolute; width:100%; height:100%">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
8
doesnt work, this will work if the element is not floating on any direction...
– vikas devde
Apr 21 '13 at 12:26
4
display: block;
was my pitfall. TnX
– Ujjwal Singh
May 9 '13 at 8:23
2
Following doesn't work. What's the mistake i am doing. <html> <head> <style> #over img { display: block; margin-left: auto; margin-right: auto; } </style> </head> <body> <div id="over" style="position:absolute; width:100%; height:100%"> <img src="img8a.flixcart.com/image/tablet/f/k/t/…"> </div> </body> </html>
– nizam.sp
Oct 21 '13 at 11:00
8
This doesn't align vertically
– alpadev
May 11 '17 at 14:32
13
How can an answer that do not attend the question get more than 300 votes???
– Rodrigo
Jun 9 '17 at 13:13
|
show 4 more comments
JSFiddle
body {
margin: 0;
}
#over img {
margin-left: auto;
margin-right: auto;
display: block;
}
<div id="over" style="position:absolute; width:100%; height:100%">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
JSFiddle
body {
margin: 0;
}
#over img {
margin-left: auto;
margin-right: auto;
display: block;
}
<div id="over" style="position:absolute; width:100%; height:100%">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
body {
margin: 0;
}
#over img {
margin-left: auto;
margin-right: auto;
display: block;
}
<div id="over" style="position:absolute; width:100%; height:100%">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
body {
margin: 0;
}
#over img {
margin-left: auto;
margin-right: auto;
display: block;
}
<div id="over" style="position:absolute; width:100%; height:100%">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
edited Jan 21 '17 at 8:41
H. Pauwelyn
5,931184988
5,931184988
answered Sep 5 '12 at 13:10
Gurpreet SinghGurpreet Singh
3,9881910
3,9881910
8
doesnt work, this will work if the element is not floating on any direction...
– vikas devde
Apr 21 '13 at 12:26
4
display: block;
was my pitfall. TnX
– Ujjwal Singh
May 9 '13 at 8:23
2
Following doesn't work. What's the mistake i am doing. <html> <head> <style> #over img { display: block; margin-left: auto; margin-right: auto; } </style> </head> <body> <div id="over" style="position:absolute; width:100%; height:100%"> <img src="img8a.flixcart.com/image/tablet/f/k/t/…"> </div> </body> </html>
– nizam.sp
Oct 21 '13 at 11:00
8
This doesn't align vertically
– alpadev
May 11 '17 at 14:32
13
How can an answer that do not attend the question get more than 300 votes???
– Rodrigo
Jun 9 '17 at 13:13
|
show 4 more comments
8
doesnt work, this will work if the element is not floating on any direction...
– vikas devde
Apr 21 '13 at 12:26
4
display: block;
was my pitfall. TnX
– Ujjwal Singh
May 9 '13 at 8:23
2
Following doesn't work. What's the mistake i am doing. <html> <head> <style> #over img { display: block; margin-left: auto; margin-right: auto; } </style> </head> <body> <div id="over" style="position:absolute; width:100%; height:100%"> <img src="img8a.flixcart.com/image/tablet/f/k/t/…"> </div> </body> </html>
– nizam.sp
Oct 21 '13 at 11:00
8
This doesn't align vertically
– alpadev
May 11 '17 at 14:32
13
How can an answer that do not attend the question get more than 300 votes???
– Rodrigo
Jun 9 '17 at 13:13
8
8
doesnt work, this will work if the element is not floating on any direction...
– vikas devde
Apr 21 '13 at 12:26
doesnt work, this will work if the element is not floating on any direction...
– vikas devde
Apr 21 '13 at 12:26
4
4
display: block;
was my pitfall. TnX– Ujjwal Singh
May 9 '13 at 8:23
display: block;
was my pitfall. TnX– Ujjwal Singh
May 9 '13 at 8:23
2
2
Following doesn't work. What's the mistake i am doing. <html> <head> <style> #over img { display: block; margin-left: auto; margin-right: auto; } </style> </head> <body> <div id="over" style="position:absolute; width:100%; height:100%"> <img src="img8a.flixcart.com/image/tablet/f/k/t/…"> </div> </body> </html>
– nizam.sp
Oct 21 '13 at 11:00
Following doesn't work. What's the mistake i am doing. <html> <head> <style> #over img { display: block; margin-left: auto; margin-right: auto; } </style> </head> <body> <div id="over" style="position:absolute; width:100%; height:100%"> <img src="img8a.flixcart.com/image/tablet/f/k/t/…"> </div> </body> </html>
– nizam.sp
Oct 21 '13 at 11:00
8
8
This doesn't align vertically
– alpadev
May 11 '17 at 14:32
This doesn't align vertically
– alpadev
May 11 '17 at 14:32
13
13
How can an answer that do not attend the question get more than 300 votes???
– Rodrigo
Jun 9 '17 at 13:13
How can an answer that do not attend the question get more than 300 votes???
– Rodrigo
Jun 9 '17 at 13:13
|
show 4 more comments
<div style="display:table-cell; vertical-align:middle; text-align:center">
<img src="img.png">
</div>
4
top answer, very helpful!
– Ilian Andreev
Jun 2 '13 at 19:58
1
Very short and easy solution, but it appears to only be working with a fixed, non-percentage width and height. It does work with floats though so +1 for that. — jsfiddle.net/2s2nY/2
– magnetronnie
May 26 '14 at 10:10
1
but this is only doing the vertical-align but not the horizontal right?
– V-SHY
Mar 25 '15 at 3:38
1
It won't work if image's width larger than div's width.
– Davuz
May 26 '15 at 7:02
4
If we remove display:table-cell; it works perfactly.
– Ahesanali Momin
May 18 '17 at 7:39
|
show 2 more comments
<div style="display:table-cell; vertical-align:middle; text-align:center">
<img src="img.png">
</div>
4
top answer, very helpful!
– Ilian Andreev
Jun 2 '13 at 19:58
1
Very short and easy solution, but it appears to only be working with a fixed, non-percentage width and height. It does work with floats though so +1 for that. — jsfiddle.net/2s2nY/2
– magnetronnie
May 26 '14 at 10:10
1
but this is only doing the vertical-align but not the horizontal right?
– V-SHY
Mar 25 '15 at 3:38
1
It won't work if image's width larger than div's width.
– Davuz
May 26 '15 at 7:02
4
If we remove display:table-cell; it works perfactly.
– Ahesanali Momin
May 18 '17 at 7:39
|
show 2 more comments
<div style="display:table-cell; vertical-align:middle; text-align:center">
<img src="img.png">
</div>
<div style="display:table-cell; vertical-align:middle; text-align:center">
<img src="img.png">
</div>
edited Feb 3 '11 at 15:53
answered Feb 3 '11 at 15:48
John K.John K.
4,60711620
4,60711620
4
top answer, very helpful!
– Ilian Andreev
Jun 2 '13 at 19:58
1
Very short and easy solution, but it appears to only be working with a fixed, non-percentage width and height. It does work with floats though so +1 for that. — jsfiddle.net/2s2nY/2
– magnetronnie
May 26 '14 at 10:10
1
but this is only doing the vertical-align but not the horizontal right?
– V-SHY
Mar 25 '15 at 3:38
1
It won't work if image's width larger than div's width.
– Davuz
May 26 '15 at 7:02
4
If we remove display:table-cell; it works perfactly.
– Ahesanali Momin
May 18 '17 at 7:39
|
show 2 more comments
4
top answer, very helpful!
– Ilian Andreev
Jun 2 '13 at 19:58
1
Very short and easy solution, but it appears to only be working with a fixed, non-percentage width and height. It does work with floats though so +1 for that. — jsfiddle.net/2s2nY/2
– magnetronnie
May 26 '14 at 10:10
1
but this is only doing the vertical-align but not the horizontal right?
– V-SHY
Mar 25 '15 at 3:38
1
It won't work if image's width larger than div's width.
– Davuz
May 26 '15 at 7:02
4
If we remove display:table-cell; it works perfactly.
– Ahesanali Momin
May 18 '17 at 7:39
4
4
top answer, very helpful!
– Ilian Andreev
Jun 2 '13 at 19:58
top answer, very helpful!
– Ilian Andreev
Jun 2 '13 at 19:58
1
1
Very short and easy solution, but it appears to only be working with a fixed, non-percentage width and height. It does work with floats though so +1 for that. — jsfiddle.net/2s2nY/2
– magnetronnie
May 26 '14 at 10:10
Very short and easy solution, but it appears to only be working with a fixed, non-percentage width and height. It does work with floats though so +1 for that. — jsfiddle.net/2s2nY/2
– magnetronnie
May 26 '14 at 10:10
1
1
but this is only doing the vertical-align but not the horizontal right?
– V-SHY
Mar 25 '15 at 3:38
but this is only doing the vertical-align but not the horizontal right?
– V-SHY
Mar 25 '15 at 3:38
1
1
It won't work if image's width larger than div's width.
– Davuz
May 26 '15 at 7:02
It won't work if image's width larger than div's width.
– Davuz
May 26 '15 at 7:02
4
4
If we remove display:table-cell; it works perfactly.
– Ahesanali Momin
May 18 '17 at 7:39
If we remove display:table-cell; it works perfactly.
– Ahesanali Momin
May 18 '17 at 7:39
|
show 2 more comments
Seems to me that you also wanted that image to be vertically centered within the container. (I didn't see any answer that provided that)
Working Fiddle:
- Pure CSS solution
- Not breaking the document flow (no floats or absolute positioning)
- Cross browser compatibility (even IE6)
- Completely responsive.
HTML
<div id="over">
<span class="Centerer"></span>
<img class="Centered" src="http://th07.deviantart.net/fs71/200H/f/2013/236/d/b/bw_avlonas_a5_beach_isles_wallpaper_image_by_lemnosexplorer-d6jh3i7.jpg" />
</div>
CSS
*
{
padding: 0;
margin: 0;
}
#over
{
position:absolute;
width:100%;
height:100%;
text-align: center; /*handles the horizontal centering*/
}
/*handles the vertical centering*/
.Centerer
{
display: inline-block;
height: 100%;
vertical-align: middle;
}
.Centered
{
display: inline-block;
vertical-align: middle;
}
Note: this solution is good to align any element within any element.
for IE7, when applying the .Centered
class on block elements, you will have to use another trick to get the inline-block
working. (that because IE6/IE7 dont play well with inline-block on block elements)
10
Only this one is working (vertical & horizontal)
– Bartek Kosa
Nov 13 '13 at 13:01
1
This is superb...Magic!!! Worked awesomly well! Thanks so much for this...
– Nitin Bansal
Jul 1 '14 at 10:55
2
excellent.. this should the answer to this question.
– user3790264
Sep 30 '14 at 10:59
2
Should be the accepted answer +1
– Lehan Coetzee
Feb 2 '16 at 11:17
2
Works perfectly!
– wotaskd
Jun 8 '16 at 14:28
|
show 15 more comments
Seems to me that you also wanted that image to be vertically centered within the container. (I didn't see any answer that provided that)
Working Fiddle:
- Pure CSS solution
- Not breaking the document flow (no floats or absolute positioning)
- Cross browser compatibility (even IE6)
- Completely responsive.
HTML
<div id="over">
<span class="Centerer"></span>
<img class="Centered" src="http://th07.deviantart.net/fs71/200H/f/2013/236/d/b/bw_avlonas_a5_beach_isles_wallpaper_image_by_lemnosexplorer-d6jh3i7.jpg" />
</div>
CSS
*
{
padding: 0;
margin: 0;
}
#over
{
position:absolute;
width:100%;
height:100%;
text-align: center; /*handles the horizontal centering*/
}
/*handles the vertical centering*/
.Centerer
{
display: inline-block;
height: 100%;
vertical-align: middle;
}
.Centered
{
display: inline-block;
vertical-align: middle;
}
Note: this solution is good to align any element within any element.
for IE7, when applying the .Centered
class on block elements, you will have to use another trick to get the inline-block
working. (that because IE6/IE7 dont play well with inline-block on block elements)
10
Only this one is working (vertical & horizontal)
– Bartek Kosa
Nov 13 '13 at 13:01
1
This is superb...Magic!!! Worked awesomly well! Thanks so much for this...
– Nitin Bansal
Jul 1 '14 at 10:55
2
excellent.. this should the answer to this question.
– user3790264
Sep 30 '14 at 10:59
2
Should be the accepted answer +1
– Lehan Coetzee
Feb 2 '16 at 11:17
2
Works perfectly!
– wotaskd
Jun 8 '16 at 14:28
|
show 15 more comments
Seems to me that you also wanted that image to be vertically centered within the container. (I didn't see any answer that provided that)
Working Fiddle:
- Pure CSS solution
- Not breaking the document flow (no floats or absolute positioning)
- Cross browser compatibility (even IE6)
- Completely responsive.
HTML
<div id="over">
<span class="Centerer"></span>
<img class="Centered" src="http://th07.deviantart.net/fs71/200H/f/2013/236/d/b/bw_avlonas_a5_beach_isles_wallpaper_image_by_lemnosexplorer-d6jh3i7.jpg" />
</div>
CSS
*
{
padding: 0;
margin: 0;
}
#over
{
position:absolute;
width:100%;
height:100%;
text-align: center; /*handles the horizontal centering*/
}
/*handles the vertical centering*/
.Centerer
{
display: inline-block;
height: 100%;
vertical-align: middle;
}
.Centered
{
display: inline-block;
vertical-align: middle;
}
Note: this solution is good to align any element within any element.
for IE7, when applying the .Centered
class on block elements, you will have to use another trick to get the inline-block
working. (that because IE6/IE7 dont play well with inline-block on block elements)
Seems to me that you also wanted that image to be vertically centered within the container. (I didn't see any answer that provided that)
Working Fiddle:
- Pure CSS solution
- Not breaking the document flow (no floats or absolute positioning)
- Cross browser compatibility (even IE6)
- Completely responsive.
HTML
<div id="over">
<span class="Centerer"></span>
<img class="Centered" src="http://th07.deviantart.net/fs71/200H/f/2013/236/d/b/bw_avlonas_a5_beach_isles_wallpaper_image_by_lemnosexplorer-d6jh3i7.jpg" />
</div>
CSS
*
{
padding: 0;
margin: 0;
}
#over
{
position:absolute;
width:100%;
height:100%;
text-align: center; /*handles the horizontal centering*/
}
/*handles the vertical centering*/
.Centerer
{
display: inline-block;
height: 100%;
vertical-align: middle;
}
.Centered
{
display: inline-block;
vertical-align: middle;
}
Note: this solution is good to align any element within any element.
for IE7, when applying the .Centered
class on block elements, you will have to use another trick to get the inline-block
working. (that because IE6/IE7 dont play well with inline-block on block elements)
edited Feb 19 '14 at 9:46
Tushar Gupta
42.7k188391
42.7k188391
answered Sep 16 '13 at 14:24
avrahamcoolavrahamcool
11.2k23855
11.2k23855
10
Only this one is working (vertical & horizontal)
– Bartek Kosa
Nov 13 '13 at 13:01
1
This is superb...Magic!!! Worked awesomly well! Thanks so much for this...
– Nitin Bansal
Jul 1 '14 at 10:55
2
excellent.. this should the answer to this question.
– user3790264
Sep 30 '14 at 10:59
2
Should be the accepted answer +1
– Lehan Coetzee
Feb 2 '16 at 11:17
2
Works perfectly!
– wotaskd
Jun 8 '16 at 14:28
|
show 15 more comments
10
Only this one is working (vertical & horizontal)
– Bartek Kosa
Nov 13 '13 at 13:01
1
This is superb...Magic!!! Worked awesomly well! Thanks so much for this...
– Nitin Bansal
Jul 1 '14 at 10:55
2
excellent.. this should the answer to this question.
– user3790264
Sep 30 '14 at 10:59
2
Should be the accepted answer +1
– Lehan Coetzee
Feb 2 '16 at 11:17
2
Works perfectly!
– wotaskd
Jun 8 '16 at 14:28
10
10
Only this one is working (vertical & horizontal)
– Bartek Kosa
Nov 13 '13 at 13:01
Only this one is working (vertical & horizontal)
– Bartek Kosa
Nov 13 '13 at 13:01
1
1
This is superb...Magic!!! Worked awesomly well! Thanks so much for this...
– Nitin Bansal
Jul 1 '14 at 10:55
This is superb...Magic!!! Worked awesomly well! Thanks so much for this...
– Nitin Bansal
Jul 1 '14 at 10:55
2
2
excellent.. this should the answer to this question.
– user3790264
Sep 30 '14 at 10:59
excellent.. this should the answer to this question.
– user3790264
Sep 30 '14 at 10:59
2
2
Should be the accepted answer +1
– Lehan Coetzee
Feb 2 '16 at 11:17
Should be the accepted answer +1
– Lehan Coetzee
Feb 2 '16 at 11:17
2
2
Works perfectly!
– wotaskd
Jun 8 '16 at 14:28
Works perfectly!
– wotaskd
Jun 8 '16 at 14:28
|
show 15 more comments
This can also be done using the Flexbox layout:
STATIC SIZE
.parent {
display: flex;
height: 300px; /* Or whatever */
background-color: #000;
}
.child {
width: 100px; /* Or whatever */
height: 100px; /* Or whatever */
margin: auto; /* Magic! */
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
DYNAMIC SIZE
html, body {
width: 100%;
height: 100%;
display: flex;
background-color: #999;
}
* {
margin: 0;
padding: 0;
}
.parent {
margin: auto;
background-color: #000;
display: flex;
height: 80%;
width: 80%;
}
.child {
margin: auto; /* Magic! */
max-width: 100%;
max-height: 100%;
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
I found the example in this article, which does a great job explaining the how to use layout.
flex is state of the art - just works without tricks
– Bernhard Zürn
Jan 17 '17 at 20:02
In the Static Size, there's no need for width and height for the child (at least in my version of Firefox).
– Rodrigo
Jun 9 '17 at 13:23
add a comment |
This can also be done using the Flexbox layout:
STATIC SIZE
.parent {
display: flex;
height: 300px; /* Or whatever */
background-color: #000;
}
.child {
width: 100px; /* Or whatever */
height: 100px; /* Or whatever */
margin: auto; /* Magic! */
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
DYNAMIC SIZE
html, body {
width: 100%;
height: 100%;
display: flex;
background-color: #999;
}
* {
margin: 0;
padding: 0;
}
.parent {
margin: auto;
background-color: #000;
display: flex;
height: 80%;
width: 80%;
}
.child {
margin: auto; /* Magic! */
max-width: 100%;
max-height: 100%;
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
I found the example in this article, which does a great job explaining the how to use layout.
flex is state of the art - just works without tricks
– Bernhard Zürn
Jan 17 '17 at 20:02
In the Static Size, there's no need for width and height for the child (at least in my version of Firefox).
– Rodrigo
Jun 9 '17 at 13:23
add a comment |
This can also be done using the Flexbox layout:
STATIC SIZE
.parent {
display: flex;
height: 300px; /* Or whatever */
background-color: #000;
}
.child {
width: 100px; /* Or whatever */
height: 100px; /* Or whatever */
margin: auto; /* Magic! */
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
DYNAMIC SIZE
html, body {
width: 100%;
height: 100%;
display: flex;
background-color: #999;
}
* {
margin: 0;
padding: 0;
}
.parent {
margin: auto;
background-color: #000;
display: flex;
height: 80%;
width: 80%;
}
.child {
margin: auto; /* Magic! */
max-width: 100%;
max-height: 100%;
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
I found the example in this article, which does a great job explaining the how to use layout.
This can also be done using the Flexbox layout:
STATIC SIZE
.parent {
display: flex;
height: 300px; /* Or whatever */
background-color: #000;
}
.child {
width: 100px; /* Or whatever */
height: 100px; /* Or whatever */
margin: auto; /* Magic! */
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
DYNAMIC SIZE
html, body {
width: 100%;
height: 100%;
display: flex;
background-color: #999;
}
* {
margin: 0;
padding: 0;
}
.parent {
margin: auto;
background-color: #000;
display: flex;
height: 80%;
width: 80%;
}
.child {
margin: auto; /* Magic! */
max-width: 100%;
max-height: 100%;
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
I found the example in this article, which does a great job explaining the how to use layout.
.parent {
display: flex;
height: 300px; /* Or whatever */
background-color: #000;
}
.child {
width: 100px; /* Or whatever */
height: 100px; /* Or whatever */
margin: auto; /* Magic! */
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
.parent {
display: flex;
height: 300px; /* Or whatever */
background-color: #000;
}
.child {
width: 100px; /* Or whatever */
height: 100px; /* Or whatever */
margin: auto; /* Magic! */
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
html, body {
width: 100%;
height: 100%;
display: flex;
background-color: #999;
}
* {
margin: 0;
padding: 0;
}
.parent {
margin: auto;
background-color: #000;
display: flex;
height: 80%;
width: 80%;
}
.child {
margin: auto; /* Magic! */
max-width: 100%;
max-height: 100%;
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
html, body {
width: 100%;
height: 100%;
display: flex;
background-color: #999;
}
* {
margin: 0;
padding: 0;
}
.parent {
margin: auto;
background-color: #000;
display: flex;
height: 80%;
width: 80%;
}
.child {
margin: auto; /* Magic! */
max-width: 100%;
max-height: 100%;
}
<div class="parent">
<img class="child" src="https://i.vimeocdn.com/portrait/58832_300x300"/>
</div>
edited Feb 27 '17 at 4:39
Manatax
1,89231937
1,89231937
answered May 6 '14 at 20:04
aerdmanaerdman
1,00473
1,00473
flex is state of the art - just works without tricks
– Bernhard Zürn
Jan 17 '17 at 20:02
In the Static Size, there's no need for width and height for the child (at least in my version of Firefox).
– Rodrigo
Jun 9 '17 at 13:23
add a comment |
flex is state of the art - just works without tricks
– Bernhard Zürn
Jan 17 '17 at 20:02
In the Static Size, there's no need for width and height for the child (at least in my version of Firefox).
– Rodrigo
Jun 9 '17 at 13:23
flex is state of the art - just works without tricks
– Bernhard Zürn
Jan 17 '17 at 20:02
flex is state of the art - just works without tricks
– Bernhard Zürn
Jan 17 '17 at 20:02
In the Static Size, there's no need for width and height for the child (at least in my version of Firefox).
– Rodrigo
Jun 9 '17 at 13:23
In the Static Size, there's no need for width and height for the child (at least in my version of Firefox).
– Rodrigo
Jun 9 '17 at 13:23
add a comment |
img.centered {
display: block;
margin: auto auto;
}
1
this is very similar to this answer
– simple_human
Jul 19 '14 at 0:29
add a comment |
img.centered {
display: block;
margin: auto auto;
}
1
this is very similar to this answer
– simple_human
Jul 19 '14 at 0:29
add a comment |
img.centered {
display: block;
margin: auto auto;
}
img.centered {
display: block;
margin: auto auto;
}
edited May 26 '18 at 20:30
Ronan Boiteau
5,96451838
5,96451838
answered Jan 27 '14 at 7:38
NitinNitin
59144
59144
1
this is very similar to this answer
– simple_human
Jul 19 '14 at 0:29
add a comment |
1
this is very similar to this answer
– simple_human
Jul 19 '14 at 0:29
1
1
this is very similar to this answer
– simple_human
Jul 19 '14 at 0:29
this is very similar to this answer
– simple_human
Jul 19 '14 at 0:29
add a comment |
#over {position:relative; text-align:center;
width:100%; height:100%; background:#CCC;}
#over img{
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
Best solution that actually works.
– Burjua
Jun 15 '17 at 13:10
This is the right answer, that actually centers both vertically and horizontally.
– Alexander Kim
Jun 7 '18 at 8:44
Best answer so far.
– kiran puppala
Jan 31 at 9:02
add a comment |
#over {position:relative; text-align:center;
width:100%; height:100%; background:#CCC;}
#over img{
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
Best solution that actually works.
– Burjua
Jun 15 '17 at 13:10
This is the right answer, that actually centers both vertically and horizontally.
– Alexander Kim
Jun 7 '18 at 8:44
Best answer so far.
– kiran puppala
Jan 31 at 9:02
add a comment |
#over {position:relative; text-align:center;
width:100%; height:100%; background:#CCC;}
#over img{
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
#over {position:relative; text-align:center;
width:100%; height:100%; background:#CCC;}
#over img{
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
edited Feb 19 '14 at 9:46
Tushar Gupta
42.7k188391
42.7k188391
answered May 11 '13 at 17:27
dhirdhir
27133
27133
Best solution that actually works.
– Burjua
Jun 15 '17 at 13:10
This is the right answer, that actually centers both vertically and horizontally.
– Alexander Kim
Jun 7 '18 at 8:44
Best answer so far.
– kiran puppala
Jan 31 at 9:02
add a comment |
Best solution that actually works.
– Burjua
Jun 15 '17 at 13:10
This is the right answer, that actually centers both vertically and horizontally.
– Alexander Kim
Jun 7 '18 at 8:44
Best answer so far.
– kiran puppala
Jan 31 at 9:02
Best solution that actually works.
– Burjua
Jun 15 '17 at 13:10
Best solution that actually works.
– Burjua
Jun 15 '17 at 13:10
This is the right answer, that actually centers both vertically and horizontally.
– Alexander Kim
Jun 7 '18 at 8:44
This is the right answer, that actually centers both vertically and horizontally.
– Alexander Kim
Jun 7 '18 at 8:44
Best answer so far.
– kiran puppala
Jan 31 at 9:02
Best answer so far.
– kiran puppala
Jan 31 at 9:02
add a comment |
You can do this easily by using display:flex css property
#over {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
add a comment |
You can do this easily by using display:flex css property
#over {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
add a comment |
You can do this easily by using display:flex css property
#over {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
You can do this easily by using display:flex css property
#over {
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
}
edited Jan 11 at 8:37
crmpicco
8,5581788158
8,5581788158
answered Nov 14 '17 at 15:41
tanveer ahmad dartanveer ahmad dar
2,2541725
2,2541725
add a comment |
add a comment |
I still had some issues with other solution presented here. Finally this worked best for me:
<div class="parent">
<img class="child" src="image.png"/>
</div>
css3:
.child {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%); /* Safari and Chrome */
-moz-transform: translate(-50%, -50%); /* Firefox */
-ms-transform: translate(-50%, -50%); /* IE 9 */
-o-transform: translate(-50%, -50%); /* Opera */
// I suppose you may like those too:
// max-width: 80%;
// max-height: 80%;
}
You can read more about that approach at this page.
I had to also add parent css but your code worked perfectly! position: relative; width: 100%; float: left; overflow: hidden; min-height: 189px;
– user2593040
Dec 9 '16 at 9:12
add a comment |
I still had some issues with other solution presented here. Finally this worked best for me:
<div class="parent">
<img class="child" src="image.png"/>
</div>
css3:
.child {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%); /* Safari and Chrome */
-moz-transform: translate(-50%, -50%); /* Firefox */
-ms-transform: translate(-50%, -50%); /* IE 9 */
-o-transform: translate(-50%, -50%); /* Opera */
// I suppose you may like those too:
// max-width: 80%;
// max-height: 80%;
}
You can read more about that approach at this page.
I had to also add parent css but your code worked perfectly! position: relative; width: 100%; float: left; overflow: hidden; min-height: 189px;
– user2593040
Dec 9 '16 at 9:12
add a comment |
I still had some issues with other solution presented here. Finally this worked best for me:
<div class="parent">
<img class="child" src="image.png"/>
</div>
css3:
.child {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%); /* Safari and Chrome */
-moz-transform: translate(-50%, -50%); /* Firefox */
-ms-transform: translate(-50%, -50%); /* IE 9 */
-o-transform: translate(-50%, -50%); /* Opera */
// I suppose you may like those too:
// max-width: 80%;
// max-height: 80%;
}
You can read more about that approach at this page.
I still had some issues with other solution presented here. Finally this worked best for me:
<div class="parent">
<img class="child" src="image.png"/>
</div>
css3:
.child {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%); /* Safari and Chrome */
-moz-transform: translate(-50%, -50%); /* Firefox */
-ms-transform: translate(-50%, -50%); /* IE 9 */
-o-transform: translate(-50%, -50%); /* Opera */
// I suppose you may like those too:
// max-width: 80%;
// max-height: 80%;
}
You can read more about that approach at this page.
answered Jan 14 '15 at 18:42
pawel7318pawel7318
1,9971734
1,9971734
I had to also add parent css but your code worked perfectly! position: relative; width: 100%; float: left; overflow: hidden; min-height: 189px;
– user2593040
Dec 9 '16 at 9:12
add a comment |
I had to also add parent css but your code worked perfectly! position: relative; width: 100%; float: left; overflow: hidden; min-height: 189px;
– user2593040
Dec 9 '16 at 9:12
I had to also add parent css but your code worked perfectly! position: relative; width: 100%; float: left; overflow: hidden; min-height: 189px;
– user2593040
Dec 9 '16 at 9:12
I had to also add parent css but your code worked perfectly! position: relative; width: 100%; float: left; overflow: hidden; min-height: 189px;
– user2593040
Dec 9 '16 at 9:12
add a comment |
Basically, setting right and left margin to auto will cause the image to center align.
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png" style="display: block; margin: 0 auto;">
</div>
add a comment |
Basically, setting right and left margin to auto will cause the image to center align.
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png" style="display: block; margin: 0 auto;">
</div>
add a comment |
Basically, setting right and left margin to auto will cause the image to center align.
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png" style="display: block; margin: 0 auto;">
</div>
Basically, setting right and left margin to auto will cause the image to center align.
<div id="over" style="position:absolute; width:100%; height:100%>
<img src="img.png" style="display: block; margin: 0 auto;">
</div>
edited Feb 7 '13 at 22:57
Reine Johansson
14814
14814
answered Dec 8 '12 at 5:29
GarconisGarconis
575518
575518
add a comment |
add a comment |
This would be a simpler approach
#over > img{
display: block;
margin:0 auto;
}
add a comment |
This would be a simpler approach
#over > img{
display: block;
margin:0 auto;
}
add a comment |
This would be a simpler approach
#over > img{
display: block;
margin:0 auto;
}
This would be a simpler approach
#over > img{
display: block;
margin:0 auto;
}
edited Jun 10 '15 at 5:53
Subedi Kishor
4,68752849
4,68752849
answered Jun 10 '15 at 5:21
Sujay sreedharSujay sreedhar
3,26321625
3,26321625
add a comment |
add a comment |
setting the img to display:inline-block while having set the superior div to text-align:center will do the job too
EDIT: to those folks who are playing with display:inline-block >>> don't forget that e.g. two divs like
<div>Div1 content</div>NOSPACEHERE<div>Div2 content</div>
really have no spaces between them (as seen here).
Just basic to avoid these (inline block inherent) gaps between them. These gaps can be seen between every two words I'm writing right now! :-) So .. hope this helps some of you.
add a comment |
setting the img to display:inline-block while having set the superior div to text-align:center will do the job too
EDIT: to those folks who are playing with display:inline-block >>> don't forget that e.g. two divs like
<div>Div1 content</div>NOSPACEHERE<div>Div2 content</div>
really have no spaces between them (as seen here).
Just basic to avoid these (inline block inherent) gaps between them. These gaps can be seen between every two words I'm writing right now! :-) So .. hope this helps some of you.
add a comment |
setting the img to display:inline-block while having set the superior div to text-align:center will do the job too
EDIT: to those folks who are playing with display:inline-block >>> don't forget that e.g. two divs like
<div>Div1 content</div>NOSPACEHERE<div>Div2 content</div>
really have no spaces between them (as seen here).
Just basic to avoid these (inline block inherent) gaps between them. These gaps can be seen between every two words I'm writing right now! :-) So .. hope this helps some of you.
setting the img to display:inline-block while having set the superior div to text-align:center will do the job too
EDIT: to those folks who are playing with display:inline-block >>> don't forget that e.g. two divs like
<div>Div1 content</div>NOSPACEHERE<div>Div2 content</div>
really have no spaces between them (as seen here).
Just basic to avoid these (inline block inherent) gaps between them. These gaps can be seen between every two words I'm writing right now! :-) So .. hope this helps some of you.
edited Oct 25 '15 at 2:44
answered Apr 12 '13 at 17:16
sashasasha
496617
496617
add a comment |
add a comment |
SIMPLE. 2018. FlexBox. To Check Browser Support - Can I Use
Minimal Solution:
div#over {
display: flex;
justify-content: center;
align-items: center;
}
To get the widest browser support possible:
div#over {
display: -webkit-flex;
display: -ms-flex;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
}
add a comment |
SIMPLE. 2018. FlexBox. To Check Browser Support - Can I Use
Minimal Solution:
div#over {
display: flex;
justify-content: center;
align-items: center;
}
To get the widest browser support possible:
div#over {
display: -webkit-flex;
display: -ms-flex;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
}
add a comment |
SIMPLE. 2018. FlexBox. To Check Browser Support - Can I Use
Minimal Solution:
div#over {
display: flex;
justify-content: center;
align-items: center;
}
To get the widest browser support possible:
div#over {
display: -webkit-flex;
display: -ms-flex;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
}
SIMPLE. 2018. FlexBox. To Check Browser Support - Can I Use
Minimal Solution:
div#over {
display: flex;
justify-content: center;
align-items: center;
}
To get the widest browser support possible:
div#over {
display: -webkit-flex;
display: -ms-flex;
display: flex;
justify-content: center;
-ms-align-items: center;
align-items: center;
}
edited Jun 26 '18 at 13:01
Calvin Nunes
3,71831135
3,71831135
answered May 30 '18 at 10:05
NadavNadav
338310
338310
add a comment |
add a comment |
I've tried many approaches but only this one works for multiple inline elements inside a container div. Here is code to align everything in div at middle.
CSS
.divContainer
{
vertical-align: middle;
text-align: center; <!-- If you want horizontal center alignment -->
}
.divContainer > *
{
vertical-align: middle;
}
HTML
<div class="divContainer">
<span>Middle Text</span>
<img src="test.png"/>
</div>
Sample code is here: https://jsfiddle.net/yogendrasinh/2vq0c68m/
add a comment |
I've tried many approaches but only this one works for multiple inline elements inside a container div. Here is code to align everything in div at middle.
CSS
.divContainer
{
vertical-align: middle;
text-align: center; <!-- If you want horizontal center alignment -->
}
.divContainer > *
{
vertical-align: middle;
}
HTML
<div class="divContainer">
<span>Middle Text</span>
<img src="test.png"/>
</div>
Sample code is here: https://jsfiddle.net/yogendrasinh/2vq0c68m/
add a comment |
I've tried many approaches but only this one works for multiple inline elements inside a container div. Here is code to align everything in div at middle.
CSS
.divContainer
{
vertical-align: middle;
text-align: center; <!-- If you want horizontal center alignment -->
}
.divContainer > *
{
vertical-align: middle;
}
HTML
<div class="divContainer">
<span>Middle Text</span>
<img src="test.png"/>
</div>
Sample code is here: https://jsfiddle.net/yogendrasinh/2vq0c68m/
I've tried many approaches but only this one works for multiple inline elements inside a container div. Here is code to align everything in div at middle.
CSS
.divContainer
{
vertical-align: middle;
text-align: center; <!-- If you want horizontal center alignment -->
}
.divContainer > *
{
vertical-align: middle;
}
HTML
<div class="divContainer">
<span>Middle Text</span>
<img src="test.png"/>
</div>
Sample code is here: https://jsfiddle.net/yogendrasinh/2vq0c68m/
answered Aug 16 '16 at 22:52
YogeeYogee
1,0961018
1,0961018
add a comment |
add a comment |
CSS File
.over {
display : block;
margin : 0px auto;
add a comment |
CSS File
.over {
display : block;
margin : 0px auto;
add a comment |
CSS File
.over {
display : block;
margin : 0px auto;
CSS File
.over {
display : block;
margin : 0px auto;
edited Oct 3 '17 at 12:57
lapinkoira
3,49532245
3,49532245
answered Oct 3 '17 at 11:49
Sabarish RSabarish R
6112
6112
add a comment |
add a comment |
Try this minimal code:
<div class="outer">
<img src="image.png"/>
</div>
And CSS:
.outer{
text-align: center;
}
.outer img{
display: inline-block;
}
add a comment |
Try this minimal code:
<div class="outer">
<img src="image.png"/>
</div>
And CSS:
.outer{
text-align: center;
}
.outer img{
display: inline-block;
}
add a comment |
Try this minimal code:
<div class="outer">
<img src="image.png"/>
</div>
And CSS:
.outer{
text-align: center;
}
.outer img{
display: inline-block;
}
Try this minimal code:
<div class="outer">
<img src="image.png"/>
</div>
And CSS:
.outer{
text-align: center;
}
.outer img{
display: inline-block;
}
edited Feb 19 '14 at 9:45
Tushar Gupta
42.7k188391
42.7k188391
answered Feb 7 '14 at 0:08
luchopintadoluchopintado
697711
697711
add a comment |
add a comment |
many answer suggests to use margin:0 auto
but this works only when the element you trying to make centered is not floating on left or right, that is float
css attribute isn't set. In order to do this apply display
attribute to table-cell
and then apply margin of left and right to auto so your style will look like style="display:table-cell;margin:0 auto;"
add a comment |
many answer suggests to use margin:0 auto
but this works only when the element you trying to make centered is not floating on left or right, that is float
css attribute isn't set. In order to do this apply display
attribute to table-cell
and then apply margin of left and right to auto so your style will look like style="display:table-cell;margin:0 auto;"
add a comment |
many answer suggests to use margin:0 auto
but this works only when the element you trying to make centered is not floating on left or right, that is float
css attribute isn't set. In order to do this apply display
attribute to table-cell
and then apply margin of left and right to auto so your style will look like style="display:table-cell;margin:0 auto;"
many answer suggests to use margin:0 auto
but this works only when the element you trying to make centered is not floating on left or right, that is float
css attribute isn't set. In order to do this apply display
attribute to table-cell
and then apply margin of left and right to auto so your style will look like style="display:table-cell;margin:0 auto;"
answered Apr 21 '13 at 12:34
vikas devdevikas devde
8,08882939
8,08882939
add a comment |
add a comment |
<div>
<p style="text-align:center; margin-top:0px; margin-bottom:0px; padding:0px;">
<img src="image.jpg" alt="image"/>
</p>
</div>
add a comment |
<div>
<p style="text-align:center; margin-top:0px; margin-bottom:0px; padding:0px;">
<img src="image.jpg" alt="image"/>
</p>
</div>
add a comment |
<div>
<p style="text-align:center; margin-top:0px; margin-bottom:0px; padding:0px;">
<img src="image.jpg" alt="image"/>
</p>
</div>
<div>
<p style="text-align:center; margin-top:0px; margin-bottom:0px; padding:0px;">
<img src="image.jpg" alt="image"/>
</p>
</div>
answered Sep 5 '13 at 8:49
betty.88betty.88
995
995
add a comment |
add a comment |
HTML:
<div id="over">
<img src="img.png">
</div>
CSS:
#over {
text-align: center;
}
#over img {
vertical-align: middle;
}
It's best if you add some commentary and not just toss code out there.
– Parkash Kumar
Oct 26 '15 at 10:43
add a comment |
HTML:
<div id="over">
<img src="img.png">
</div>
CSS:
#over {
text-align: center;
}
#over img {
vertical-align: middle;
}
It's best if you add some commentary and not just toss code out there.
– Parkash Kumar
Oct 26 '15 at 10:43
add a comment |
HTML:
<div id="over">
<img src="img.png">
</div>
CSS:
#over {
text-align: center;
}
#over img {
vertical-align: middle;
}
HTML:
<div id="over">
<img src="img.png">
</div>
CSS:
#over {
text-align: center;
}
#over img {
vertical-align: middle;
}
edited Oct 26 '15 at 10:44
Parkash Kumar
4,26131937
4,26131937
answered Oct 25 '15 at 4:08
LahoriLahori
409212
409212
It's best if you add some commentary and not just toss code out there.
– Parkash Kumar
Oct 26 '15 at 10:43
add a comment |
It's best if you add some commentary and not just toss code out there.
– Parkash Kumar
Oct 26 '15 at 10:43
It's best if you add some commentary and not just toss code out there.
– Parkash Kumar
Oct 26 '15 at 10:43
It's best if you add some commentary and not just toss code out there.
– Parkash Kumar
Oct 26 '15 at 10:43
add a comment |
For center horizontally Just put
#over img {
display: block;
margin: 0 auto;
clear:both;
}
Another method:
#over img {
display: inline-block;
text-align: center;
}
For center vertically Just put:
#over img {
vertical-align: middle;
}
add a comment |
For center horizontally Just put
#over img {
display: block;
margin: 0 auto;
clear:both;
}
Another method:
#over img {
display: inline-block;
text-align: center;
}
For center vertically Just put:
#over img {
vertical-align: middle;
}
add a comment |
For center horizontally Just put
#over img {
display: block;
margin: 0 auto;
clear:both;
}
Another method:
#over img {
display: inline-block;
text-align: center;
}
For center vertically Just put:
#over img {
vertical-align: middle;
}
For center horizontally Just put
#over img {
display: block;
margin: 0 auto;
clear:both;
}
Another method:
#over img {
display: inline-block;
text-align: center;
}
For center vertically Just put:
#over img {
vertical-align: middle;
}
edited Jul 14 '16 at 6:28
answered Feb 16 '16 at 10:37
Sanjib DebnathSanjib Debnath
1,1241212
1,1241212
add a comment |
add a comment |
This worked for me:
#image-id {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
width: auto;
margin: 0 auto;
}
add a comment |
This worked for me:
#image-id {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
width: auto;
margin: 0 auto;
}
add a comment |
This worked for me:
#image-id {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
width: auto;
margin: 0 auto;
}
This worked for me:
#image-id {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
width: auto;
margin: 0 auto;
}
answered Jul 28 '16 at 1:53
SileriaSileria
11.8k43624
11.8k43624
add a comment |
add a comment |
Well, we are in 2016... why not use flexbox?
It's also responsive. Enjoy.
#over{
display:flex;
align-items:center;
justify-content:center;
}
<div id="over">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
1
Love css3! Thank you. However, its good to note that this will only work in modern browsers (except IE). All other browsers will have no effect.
– Neel
Jan 9 '17 at 16:44
add a comment |
Well, we are in 2016... why not use flexbox?
It's also responsive. Enjoy.
#over{
display:flex;
align-items:center;
justify-content:center;
}
<div id="over">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
1
Love css3! Thank you. However, its good to note that this will only work in modern browsers (except IE). All other browsers will have no effect.
– Neel
Jan 9 '17 at 16:44
add a comment |
Well, we are in 2016... why not use flexbox?
It's also responsive. Enjoy.
#over{
display:flex;
align-items:center;
justify-content:center;
}
<div id="over">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
Well, we are in 2016... why not use flexbox?
It's also responsive. Enjoy.
#over{
display:flex;
align-items:center;
justify-content:center;
}
<div id="over">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
#over{
display:flex;
align-items:center;
justify-content:center;
}
<div id="over">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
#over{
display:flex;
align-items:center;
justify-content:center;
}
<div id="over">
<img src="http://www.garcard.com/images/garcard_symbol.png">
</div>
answered Nov 17 '16 at 14:00
GabMicGabMic
844722
844722
1
Love css3! Thank you. However, its good to note that this will only work in modern browsers (except IE). All other browsers will have no effect.
– Neel
Jan 9 '17 at 16:44
add a comment |
1
Love css3! Thank you. However, its good to note that this will only work in modern browsers (except IE). All other browsers will have no effect.
– Neel
Jan 9 '17 at 16:44
1
1
Love css3! Thank you. However, its good to note that this will only work in modern browsers (except IE). All other browsers will have no effect.
– Neel
Jan 9 '17 at 16:44
Love css3! Thank you. However, its good to note that this will only work in modern browsers (except IE). All other browsers will have no effect.
– Neel
Jan 9 '17 at 16:44
add a comment |
this did the trick for me.
<div class="CenterImage">
<asp:Image ID="BrandImage" runat="server" />
</div>
'Note: do not have a css class assocaited to 'BrandImage' in this case
CSS:
.CenterImage {
position:absolute;
width:100%;
height:100%
}
.CenterImage img {
margin: 0 auto;
display: block;
}
add a comment |
this did the trick for me.
<div class="CenterImage">
<asp:Image ID="BrandImage" runat="server" />
</div>
'Note: do not have a css class assocaited to 'BrandImage' in this case
CSS:
.CenterImage {
position:absolute;
width:100%;
height:100%
}
.CenterImage img {
margin: 0 auto;
display: block;
}
add a comment |
this did the trick for me.
<div class="CenterImage">
<asp:Image ID="BrandImage" runat="server" />
</div>
'Note: do not have a css class assocaited to 'BrandImage' in this case
CSS:
.CenterImage {
position:absolute;
width:100%;
height:100%
}
.CenterImage img {
margin: 0 auto;
display: block;
}
this did the trick for me.
<div class="CenterImage">
<asp:Image ID="BrandImage" runat="server" />
</div>
'Note: do not have a css class assocaited to 'BrandImage' in this case
CSS:
.CenterImage {
position:absolute;
width:100%;
height:100%
}
.CenterImage img {
margin: 0 auto;
display: block;
}
answered Jun 22 '17 at 9:34
julian9876julian9876
4118
4118
add a comment |
add a comment |
Use positioning. The following worked for me...
With zoom to the center of the image (image fills the div):
div{
display:block;
overflow:hidden;
width: 70px;
height: 70px;
position: relative;
}
div img{
min-width: 70px;
min-height: 70px;
max-width: 250%;
max-height: 250%;
top: -50%;
left: -50%;
bottom: -50%;
right: -50%;
position: absolute;
}
Without zoom to the center of the image (image does not fill the div):
div{
display:block;
overflow:hidden;
width: 100px;
height: 100px;
position: relative;
}
div img{
width: 70px;
height: 70px;
top: 50%;
left: 50%;
bottom: 50%;
right: 50%;
position: absolute;
}
add a comment |
Use positioning. The following worked for me...
With zoom to the center of the image (image fills the div):
div{
display:block;
overflow:hidden;
width: 70px;
height: 70px;
position: relative;
}
div img{
min-width: 70px;
min-height: 70px;
max-width: 250%;
max-height: 250%;
top: -50%;
left: -50%;
bottom: -50%;
right: -50%;
position: absolute;
}
Without zoom to the center of the image (image does not fill the div):
div{
display:block;
overflow:hidden;
width: 100px;
height: 100px;
position: relative;
}
div img{
width: 70px;
height: 70px;
top: 50%;
left: 50%;
bottom: 50%;
right: 50%;
position: absolute;
}
add a comment |
Use positioning. The following worked for me...
With zoom to the center of the image (image fills the div):
div{
display:block;
overflow:hidden;
width: 70px;
height: 70px;
position: relative;
}
div img{
min-width: 70px;
min-height: 70px;
max-width: 250%;
max-height: 250%;
top: -50%;
left: -50%;
bottom: -50%;
right: -50%;
position: absolute;
}
Without zoom to the center of the image (image does not fill the div):
div{
display:block;
overflow:hidden;
width: 100px;
height: 100px;
position: relative;
}
div img{
width: 70px;
height: 70px;
top: 50%;
left: 50%;
bottom: 50%;
right: 50%;
position: absolute;
}
Use positioning. The following worked for me...
With zoom to the center of the image (image fills the div):
div{
display:block;
overflow:hidden;
width: 70px;
height: 70px;
position: relative;
}
div img{
min-width: 70px;
min-height: 70px;
max-width: 250%;
max-height: 250%;
top: -50%;
left: -50%;
bottom: -50%;
right: -50%;
position: absolute;
}
Without zoom to the center of the image (image does not fill the div):
div{
display:block;
overflow:hidden;
width: 100px;
height: 100px;
position: relative;
}
div img{
width: 70px;
height: 70px;
top: 50%;
left: 50%;
bottom: 50%;
right: 50%;
position: absolute;
}
answered Mar 24 '16 at 8:38
FinkAvenueFinkAvenue
624419
624419
add a comment |
add a comment |
You can take a look on this solution:
Centering horizontally and vertically an image in a box
<style type="text/css">
.wraptocenter {
display: table-cell;
text-align: center;
vertical-align: middle;
width: ...;
height: ...;
}
.wraptocenter * {
vertical-align: middle;
}
.wraptocenter {
display: block;
}
.wraptocenter span {
display: inline-block;
height: 100%;
width: 1px;
}
</style>
<!--[if lt IE 8]-->
<style>
.wraptocenter span {
display: inline-block;
height: 100%;
}
</style>
<!--[endif]-->
<div class="wraptocenter"><span></span><img src="..." alt="..."></div>
add a comment |
You can take a look on this solution:
Centering horizontally and vertically an image in a box
<style type="text/css">
.wraptocenter {
display: table-cell;
text-align: center;
vertical-align: middle;
width: ...;
height: ...;
}
.wraptocenter * {
vertical-align: middle;
}
.wraptocenter {
display: block;
}
.wraptocenter span {
display: inline-block;
height: 100%;
width: 1px;
}
</style>
<!--[if lt IE 8]-->
<style>
.wraptocenter span {
display: inline-block;
height: 100%;
}
</style>
<!--[endif]-->
<div class="wraptocenter"><span></span><img src="..." alt="..."></div>
add a comment |
You can take a look on this solution:
Centering horizontally and vertically an image in a box
<style type="text/css">
.wraptocenter {
display: table-cell;
text-align: center;
vertical-align: middle;
width: ...;
height: ...;
}
.wraptocenter * {
vertical-align: middle;
}
.wraptocenter {
display: block;
}
.wraptocenter span {
display: inline-block;
height: 100%;
width: 1px;
}
</style>
<!--[if lt IE 8]-->
<style>
.wraptocenter span {
display: inline-block;
height: 100%;
}
</style>
<!--[endif]-->
<div class="wraptocenter"><span></span><img src="..." alt="..."></div>
You can take a look on this solution:
Centering horizontally and vertically an image in a box
<style type="text/css">
.wraptocenter {
display: table-cell;
text-align: center;
vertical-align: middle;
width: ...;
height: ...;
}
.wraptocenter * {
vertical-align: middle;
}
.wraptocenter {
display: block;
}
.wraptocenter span {
display: inline-block;
height: 100%;
width: 1px;
}
</style>
<!--[if lt IE 8]-->
<style>
.wraptocenter span {
display: inline-block;
height: 100%;
}
</style>
<!--[endif]-->
<div class="wraptocenter"><span></span><img src="..." alt="..."></div>
edited Jul 19 '16 at 19:05
Mowzer
5,573743109
5,573743109
answered Jul 19 '16 at 19:02
fpauerfpauer
32339
32339
add a comment |
add a comment |
A simple way would be creating separate styles for both the div and the image and then position them independently. Say if I want to set my image position to 50%, then I would have code which looks like the following....
#over{
position:absolute;
width:100%;
height:100%;
}
#img{
position:absolute;
left:50%;
right:50%;
}
<div id="over">
<img src="img.png" id="img">
</div>
Just tell me if it works with all kinds of browsers, even though this is a basic thing that every browser must support (otherwise don't call it a browser)
– KSJ10
Nov 21 '16 at 17:00
add a comment |
A simple way would be creating separate styles for both the div and the image and then position them independently. Say if I want to set my image position to 50%, then I would have code which looks like the following....
#over{
position:absolute;
width:100%;
height:100%;
}
#img{
position:absolute;
left:50%;
right:50%;
}
<div id="over">
<img src="img.png" id="img">
</div>
Just tell me if it works with all kinds of browsers, even though this is a basic thing that every browser must support (otherwise don't call it a browser)
– KSJ10
Nov 21 '16 at 17:00
add a comment |
A simple way would be creating separate styles for both the div and the image and then position them independently. Say if I want to set my image position to 50%, then I would have code which looks like the following....
#over{
position:absolute;
width:100%;
height:100%;
}
#img{
position:absolute;
left:50%;
right:50%;
}
<div id="over">
<img src="img.png" id="img">
</div>
A simple way would be creating separate styles for both the div and the image and then position them independently. Say if I want to set my image position to 50%, then I would have code which looks like the following....
#over{
position:absolute;
width:100%;
height:100%;
}
#img{
position:absolute;
left:50%;
right:50%;
}
<div id="over">
<img src="img.png" id="img">
</div>
#over{
position:absolute;
width:100%;
height:100%;
}
#img{
position:absolute;
left:50%;
right:50%;
}
<div id="over">
<img src="img.png" id="img">
</div>
#over{
position:absolute;
width:100%;
height:100%;
}
#img{
position:absolute;
left:50%;
right:50%;
}
<div id="over">
<img src="img.png" id="img">
</div>
edited Dec 4 '16 at 8:57
answered Nov 21 '16 at 16:54
KSJ10KSJ10
438
438
Just tell me if it works with all kinds of browsers, even though this is a basic thing that every browser must support (otherwise don't call it a browser)
– KSJ10
Nov 21 '16 at 17:00
add a comment |
Just tell me if it works with all kinds of browsers, even though this is a basic thing that every browser must support (otherwise don't call it a browser)
– KSJ10
Nov 21 '16 at 17:00
Just tell me if it works with all kinds of browsers, even though this is a basic thing that every browser must support (otherwise don't call it a browser)
– KSJ10
Nov 21 '16 at 17:00
Just tell me if it works with all kinds of browsers, even though this is a basic thing that every browser must support (otherwise don't call it a browser)
– KSJ10
Nov 21 '16 at 17:00
add a comment |
#over {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 100px;
}
Modify height value for your need.
add a comment |
#over {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 100px;
}
Modify height value for your need.
add a comment |
#over {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 100px;
}
Modify height value for your need.
#over {
display: table-cell;
vertical-align: middle;
text-align: center;
height: 100px;
}
Modify height value for your need.
answered Jul 24 '17 at 2:33
Terry LinTerry Lin
1,5361319
1,5361319
add a comment |
add a comment |
This should work.
IMPORTANT FOR TEST: To Run code snippet click left button RUN code snippet, then right link Full page
#fader{
position:fixed;z-index: 10;
top:0;right:0;bottom:0;left:0;
opacity: 0.8;background: black;
width:100%;height:100%;
text-align: center;
}
.faders{display:inline-block;height:100%;vertical-align:middle;}
.faderi{display:inline-block;vertical-align:middle;}
<div id="fader">
<span class="faders"></span>
<img class="faderi" src="https://i.stack.imgur.com/qHF2K.png" />
</div>
add a comment |
This should work.
IMPORTANT FOR TEST: To Run code snippet click left button RUN code snippet, then right link Full page
#fader{
position:fixed;z-index: 10;
top:0;right:0;bottom:0;left:0;
opacity: 0.8;background: black;
width:100%;height:100%;
text-align: center;
}
.faders{display:inline-block;height:100%;vertical-align:middle;}
.faderi{display:inline-block;vertical-align:middle;}
<div id="fader">
<span class="faders"></span>
<img class="faderi" src="https://i.stack.imgur.com/qHF2K.png" />
</div>
add a comment |
This should work.
IMPORTANT FOR TEST: To Run code snippet click left button RUN code snippet, then right link Full page
#fader{
position:fixed;z-index: 10;
top:0;right:0;bottom:0;left:0;
opacity: 0.8;background: black;
width:100%;height:100%;
text-align: center;
}
.faders{display:inline-block;height:100%;vertical-align:middle;}
.faderi{display:inline-block;vertical-align:middle;}
<div id="fader">
<span class="faders"></span>
<img class="faderi" src="https://i.stack.imgur.com/qHF2K.png" />
</div>
This should work.
IMPORTANT FOR TEST: To Run code snippet click left button RUN code snippet, then right link Full page
#fader{
position:fixed;z-index: 10;
top:0;right:0;bottom:0;left:0;
opacity: 0.8;background: black;
width:100%;height:100%;
text-align: center;
}
.faders{display:inline-block;height:100%;vertical-align:middle;}
.faderi{display:inline-block;vertical-align:middle;}
<div id="fader">
<span class="faders"></span>
<img class="faderi" src="https://i.stack.imgur.com/qHF2K.png" />
</div>
#fader{
position:fixed;z-index: 10;
top:0;right:0;bottom:0;left:0;
opacity: 0.8;background: black;
width:100%;height:100%;
text-align: center;
}
.faders{display:inline-block;height:100%;vertical-align:middle;}
.faderi{display:inline-block;vertical-align:middle;}
<div id="fader">
<span class="faders"></span>
<img class="faderi" src="https://i.stack.imgur.com/qHF2K.png" />
</div>
#fader{
position:fixed;z-index: 10;
top:0;right:0;bottom:0;left:0;
opacity: 0.8;background: black;
width:100%;height:100%;
text-align: center;
}
.faders{display:inline-block;height:100%;vertical-align:middle;}
.faderi{display:inline-block;vertical-align:middle;}
<div id="fader">
<span class="faders"></span>
<img class="faderi" src="https://i.stack.imgur.com/qHF2K.png" />
</div>
edited Oct 24 '17 at 22:59
answered Oct 24 '17 at 22:53
IntactoIntacto
1843
1843
add a comment |
add a comment |
Use bootstraps align-items
and justify-content
. See example below:
<div class="well" style="align-items:center;justify-content:center;">
<img src="img_source" height="50px" width="50px"/>
</div>
add a comment |
Use bootstraps align-items
and justify-content
. See example below:
<div class="well" style="align-items:center;justify-content:center;">
<img src="img_source" height="50px" width="50px"/>
</div>
add a comment |
Use bootstraps align-items
and justify-content
. See example below:
<div class="well" style="align-items:center;justify-content:center;">
<img src="img_source" height="50px" width="50px"/>
</div>
Use bootstraps align-items
and justify-content
. See example below:
<div class="well" style="align-items:center;justify-content:center;">
<img src="img_source" height="50px" width="50px"/>
</div>
answered Feb 20 '18 at 14:18
MwizakMwizak
1,79321116
1,79321116
add a comment |
add a comment |
I add some more properties to the CSS. Like so:
div#over {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
-ms-align-items: center;
display: -webkit-flex;
display: -ms-flex;
display: flex;
}
add a comment |
I add some more properties to the CSS. Like so:
div#over {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
-ms-align-items: center;
display: -webkit-flex;
display: -ms-flex;
display: flex;
}
add a comment |
I add some more properties to the CSS. Like so:
div#over {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
-ms-align-items: center;
display: -webkit-flex;
display: -ms-flex;
display: flex;
}
I add some more properties to the CSS. Like so:
div#over {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
-ms-align-items: center;
display: -webkit-flex;
display: -ms-flex;
display: flex;
}
answered Nov 23 '18 at 10:35
KramerKramer
191114
191114
add a comment |
add a comment |
protected by Tushar Gupta Oct 15 '14 at 17:37
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
10
Duplicate asked 2 minutes ago: CSS: image middle
– Pekka 웃
Feb 3 '11 at 15:43
1
Similar topic here: stackoverflow.com/questions/18516317/…
– Hashem Qolami
Jan 20 '14 at 23:38
Consider selecting one answer as correct.
– McSonk
Apr 20 '17 at 14:59
Possible duplicate of How to position image in the center/middle both vertically and horizontally
– gbjbaanb
Oct 3 '17 at 15:06