Center image using text-align center?
Is the property text-align: center; a good way to center an image using CSS?
img {
text-align: center;
}
html css
add a comment |
Is the property text-align: center; a good way to center an image using CSS?
img {
text-align: center;
}
html css
add a comment |
Is the property text-align: center; a good way to center an image using CSS?
img {
text-align: center;
}
html css
Is the property text-align: center; a good way to center an image using CSS?
img {
text-align: center;
}
html css
html css
edited Aug 15 '13 at 1:44
GEOCHET
18.5k156691
18.5k156691
asked Aug 14 '11 at 6:21
Web_Designer
34.4k74180239
34.4k74180239
add a comment |
add a comment |
23 Answers
23
active
oldest
votes
That will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. See the W3C spec.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
5
i tried this method and it works! but when I tried 2 images, it didn't work, it just stack on top of each other like a totem, any ideas how to align 2 images on the same line in the middle?
– PatrickGamboa
Feb 28 '13 at 4:20
2
@PatrickSerrano: See this answer: stackoverflow.com/a/11819439/244353
– Mrchief
Mar 8 '13 at 5:44
1
How is it not supported by the W3C? Can you provide with links to back that claim?
– Madara Uchiha♦
Apr 7 '15 at 14:44
4
@Mrchief Images are inline elements, not blocks.text-alignworks just as well on them.
– Madara Uchiha♦
Apr 7 '15 at 14:50
4
That explanation is kind of weird isn't it? You say 'text-align' applies to blocks and not inlines, which I believe, then you literally change it to a block element yet avoid using text-align. I mean your code works, but that paragraph needs to be completely reworded, imo.
– Octopus
Aug 18 '16 at 4:19
|
show 8 more comments
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
13
When does that "not work"?
– Madara Uchiha♦
Apr 7 '15 at 15:02
add a comment |
Came across this post and it worked for me:
img {
position: absolute;
top: 0; bottom:0; left: 0; right:0;
margin: auto;
}
(Vertical and horizontal alignment)
20
Only works if parent div is position:relative;
– Ulad Kasach
Apr 8 '15 at 22:50
add a comment |
Another way of doing would be centering an enclosing paragraph:
<p style="text-align:center"><img src="..."/></p>
19
I would disagree, I think this does answer the question. The OP asked whether or not the propertytext-align: centeris a good way to center an image, and did not specify that the property had to be a part of the img tag. This answer uses the property in question in an effort to provide a solution (that does work).
– MandM
Mar 19 '13 at 22:19
2
This worked for me when display:block, etc. would not.
– matthewsheets
Aug 27 '15 at 20:35
add a comment |
You can do:
<center><img src="..." /></center>
9
unfortunately,<center>is not supported in html5, but damn, it works.
– Ayrat
Oct 11 '15 at 12:58
add a comment |
Actually, the only problem with your code is that the text-align attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span tag around the image and set its style to text-align: center, as so:
span.centerImage {
text-align: center;
}<span class="centerImage"><img src="http://placehold.it/60/60" /></span>The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span (or div).
2
Aspanelement isdisplay: inline;by default, so this runs into the same problem as placingtext-align: center;on theimgitself. You must set thespantodisplay: block;or replace it with adivfor this to work.
– Web_Designer
May 22 '17 at 20:05
add a comment |
Only if you need to support ancient IE browsers.
The modern approach is to do margin: 0 auto in your CSS.
Example here: http://jsfiddle.net/bKRMY/
HTML:
<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>
CSS:
p.pic {
width: 48px;
margin: 0 auto;
}
Only issue ihere is that the width of the paragraph must be the same as the width of the image. If you don't put a width on the paragraph, it will not work, because it will assume 100% and your image will be aligned left, unless of course you use text-align:center.
Try out the fiddle and experiment with it if you like.
add a comment |
There are three methods for centering an element that I can suggest.
using
text-alignproperty
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
using
marginproperty
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
using
positionproperty
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
The first and second methods only work if the parent is at least as wide as the image. When the image is wider than its parent, the image will not stay centered!!!
But:
Third method is a good way for that!
Here's an example:
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>
#3 is so precise that I've been able to center animginside a justifieddivin aWebViewwith CSS injection. Thank you!
– JorgeAmVF
Nov 16 '18 at 22:19
add a comment |
if you are using a class with image then the following will do
class{
display: block;
margin: 0 auto;
}
if it is only an image in a specific calss that you want to center align then following will do
class img {
display: block;
margin: 0 auto;
}
I'd change the bottom one to beimg.classor add animg.classversion too. Thx for this.
– Chuck Savage
Dec 15 '15 at 19:32
add a comment |
img{
display: block;
margin-right: auto;
margin-left: auto;
}
dream hunter, if you are proposing an alternative way of centering an image using css, then you need to expand your question a bit. You could explain how and why this proposed alternative would be a better way to achieve the questioner's goal, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.
– Vince Bowdren
Aug 19 '16 at 15:59
add a comment |
simply change parent align :)
Try this one on parent properties text-align:center
add a comment |
you can use text-align: center on the parent and change the img to display: inline-block ->it therefore behaves like a text-element and is will be centered if the parent has a width!
img {
display: inline-block
}
add a comment |
On the container holding image You can use css3 flex box to perfectly center the image inside, both vertically and horizontally.
Let assume You have as image holder:
then as css You have to use
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
And this will make all your content inside this div perfectly centered.
whate in the justify-content can use for set align in <p>??
– teran teshara
Sep 8 '18 at 9:54
add a comment |
If you want to set the image as background, I've got solution:
.image{
background-image: url(yourimage.jpg);
background-position: center;
}
add a comment |
To center a non background image depends on whether you want to display the image as an inline (default behavior) or a block element.
Case of inline
If you want to keep the default behavior of the image's display CSS property, you will need to wrap your image inside an other block element to which you must set text-align: center;
Case of block
If you want to consider the image as a block element of its own, then text-align property does not make a sens, and you should do this instead:
IMG.display {
display: block;
margin-left: auto;
margin-right: auto;
}
The answer to your question:
Is the property text-align: center; a good way to center an image
using CSS?
Yes and no.
- Yes, if the image is the only element inside its wrapper.
- No, in case you have other elements inside the image's wrapper because t all the children elements which are siblings of the image will inherit the
text-alignproperty: and may be you would not like this side effect.
References
- List of inline elements
- Centering things
add a comment |
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
add a comment |
One more way to scale - display it:
img {
width: 60%; /* or required size of image. */
margin-left: 20% /* or scale it to move image. */
margin-right: 20% /* doesn't matters much if using left and width */
}
add a comment |
use this to your img css
img{
margin-right: auto;
margin-left: auto;
}
add a comment |
display: block with margin: 0 didn't work for me, neither wrapping with a text-align: center element.
This is my solution:
img.center {
position: absolute;
transform: translateX(-50%);
left: 50%;
}
translateX is supported by most browsers
1
did you meanmargin: 0 auto;? The key is settingmargin-leftandmargin-righttoauto.margin: 0 auto;is just a shortcut for that.
– Web_Designer
Apr 25 '16 at 19:49
1
@Web_Designer I triedmargin: 0 auto,margin: 0, andmargin: auto, none worked. Note that in Chrome's inspector, when usingmargin: 0 auto, is strikes the property with an exclamation mark sayinginvalid property value(or whatever that means that)
– OverCoder
Apr 25 '16 at 20:03
I think you meant "position: absolute;" instead of "display: absolute;"
– WebDevDaniel
Sep 26 '16 at 15:37
Thanks @WebDevDaniel for pointing out the typo. Oh any by the way, you might want to userelativepositioning rather thanabsolute, both work pretty well.
– OverCoder
Sep 28 '16 at 12:28
Got it, thanks!
– WebDevDaniel
Sep 29 '16 at 22:19
add a comment |
I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen:
https://codepen.io/artforlife/pen/MoBzrL?editors=1100
add a comment |
Sometimes we directly add the content and images on wordpress admin inside the pages. When we insert the images in side the content and want to align that center. Code is display as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
in that case you can add css like this:
article p img{
margin:0 auto;
display:block;
text-align:center;
float:none;
}
Hope it will help in that situation.
add a comment |
<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>
i think this is the way for center image in laravel frame work.
add a comment |
The simplest solution I found was to add this to my img-element:
style="display:block; margin: auto;"
Seems I don't need to add "0" before the "auto" as suggested by others. Maybe that is the proper way, but it works well enough for my purposes without the "0" as well. At least on latest FireFox and Chrome and Edge.
only auto meanauto auto auto autoand0 automeans0 auto 0 auto... and by default auto for bottom and top margin is0so adding 0 or not is exactly the same in this case which make you answer a nth-duplicate of ones already provided
– Temani Afif
Nov 12 '18 at 21:13
Your comment explains why it works. Great. But which of the previous answers said plain 'auto' without '0' works as well? Is that fact not worth mentioning?
– Panu Logic
Nov 13 '18 at 19:52
in this case it should be a comment, because in this particular case both are the same. I don't think we should have an answer for all the equivalent values,in this case we can write :auto,auto auto,auto auto auto,auto auto auto auto,0 auto 0,0 auto,0 auto 0 auto, and so on ... you think each one deserve a different answer? I don't think so.
– Temani Afif
Nov 13 '18 at 19:55
I would not say that if two different segments of CSS produce the same end-result, those two CSS-segments "are the same". Their output is the same but their source-code is not the same. Just like in general you can write any number of different programs that produce the same output. Then it is valuable to know (and tell people who ask) which of such "equivalent" programs seems to be the simplest and shortest to write.
– Panu Logic
Nov 15 '18 at 1:22
and all this should be written within one answer. That's why this answer is more suitable as a comment than a whole new answer. We should present all the equivalent things together and not make them separate feature which may sound that they are completely different because isn't the case [my opinion btw, no need to agree with it or argue the opposite]. And I am not talking about different programs that produce the same output, it's a complete different thing as the logic may not be the same. Here we are talking about the same property and the same value (likei++are the same asi+=1)
– Temani Afif
Nov 15 '18 at 1:28
add a comment |
protected by Josh Crozier Mar 1 '14 at 20:05
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?
23 Answers
23
active
oldest
votes
23 Answers
23
active
oldest
votes
active
oldest
votes
active
oldest
votes
That will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. See the W3C spec.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
5
i tried this method and it works! but when I tried 2 images, it didn't work, it just stack on top of each other like a totem, any ideas how to align 2 images on the same line in the middle?
– PatrickGamboa
Feb 28 '13 at 4:20
2
@PatrickSerrano: See this answer: stackoverflow.com/a/11819439/244353
– Mrchief
Mar 8 '13 at 5:44
1
How is it not supported by the W3C? Can you provide with links to back that claim?
– Madara Uchiha♦
Apr 7 '15 at 14:44
4
@Mrchief Images are inline elements, not blocks.text-alignworks just as well on them.
– Madara Uchiha♦
Apr 7 '15 at 14:50
4
That explanation is kind of weird isn't it? You say 'text-align' applies to blocks and not inlines, which I believe, then you literally change it to a block element yet avoid using text-align. I mean your code works, but that paragraph needs to be completely reworded, imo.
– Octopus
Aug 18 '16 at 4:19
|
show 8 more comments
That will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. See the W3C spec.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
5
i tried this method and it works! but when I tried 2 images, it didn't work, it just stack on top of each other like a totem, any ideas how to align 2 images on the same line in the middle?
– PatrickGamboa
Feb 28 '13 at 4:20
2
@PatrickSerrano: See this answer: stackoverflow.com/a/11819439/244353
– Mrchief
Mar 8 '13 at 5:44
1
How is it not supported by the W3C? Can you provide with links to back that claim?
– Madara Uchiha♦
Apr 7 '15 at 14:44
4
@Mrchief Images are inline elements, not blocks.text-alignworks just as well on them.
– Madara Uchiha♦
Apr 7 '15 at 14:50
4
That explanation is kind of weird isn't it? You say 'text-align' applies to blocks and not inlines, which I believe, then you literally change it to a block element yet avoid using text-align. I mean your code works, but that paragraph needs to be completely reworded, imo.
– Octopus
Aug 18 '16 at 4:19
|
show 8 more comments
That will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. See the W3C spec.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
That will not work as the text-align property applies to block containers, not inline elements, and img is an inline element. See the W3C spec.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
edited Sep 25 '15 at 0:10
A Morris
184
184
answered Aug 14 '11 at 6:25
Mrchief
59.3k16109170
59.3k16109170
5
i tried this method and it works! but when I tried 2 images, it didn't work, it just stack on top of each other like a totem, any ideas how to align 2 images on the same line in the middle?
– PatrickGamboa
Feb 28 '13 at 4:20
2
@PatrickSerrano: See this answer: stackoverflow.com/a/11819439/244353
– Mrchief
Mar 8 '13 at 5:44
1
How is it not supported by the W3C? Can you provide with links to back that claim?
– Madara Uchiha♦
Apr 7 '15 at 14:44
4
@Mrchief Images are inline elements, not blocks.text-alignworks just as well on them.
– Madara Uchiha♦
Apr 7 '15 at 14:50
4
That explanation is kind of weird isn't it? You say 'text-align' applies to blocks and not inlines, which I believe, then you literally change it to a block element yet avoid using text-align. I mean your code works, but that paragraph needs to be completely reworded, imo.
– Octopus
Aug 18 '16 at 4:19
|
show 8 more comments
5
i tried this method and it works! but when I tried 2 images, it didn't work, it just stack on top of each other like a totem, any ideas how to align 2 images on the same line in the middle?
– PatrickGamboa
Feb 28 '13 at 4:20
2
@PatrickSerrano: See this answer: stackoverflow.com/a/11819439/244353
– Mrchief
Mar 8 '13 at 5:44
1
How is it not supported by the W3C? Can you provide with links to back that claim?
– Madara Uchiha♦
Apr 7 '15 at 14:44
4
@Mrchief Images are inline elements, not blocks.text-alignworks just as well on them.
– Madara Uchiha♦
Apr 7 '15 at 14:50
4
That explanation is kind of weird isn't it? You say 'text-align' applies to blocks and not inlines, which I believe, then you literally change it to a block element yet avoid using text-align. I mean your code works, but that paragraph needs to be completely reworded, imo.
– Octopus
Aug 18 '16 at 4:19
5
5
i tried this method and it works! but when I tried 2 images, it didn't work, it just stack on top of each other like a totem, any ideas how to align 2 images on the same line in the middle?
– PatrickGamboa
Feb 28 '13 at 4:20
i tried this method and it works! but when I tried 2 images, it didn't work, it just stack on top of each other like a totem, any ideas how to align 2 images on the same line in the middle?
– PatrickGamboa
Feb 28 '13 at 4:20
2
2
@PatrickSerrano: See this answer: stackoverflow.com/a/11819439/244353
– Mrchief
Mar 8 '13 at 5:44
@PatrickSerrano: See this answer: stackoverflow.com/a/11819439/244353
– Mrchief
Mar 8 '13 at 5:44
1
1
How is it not supported by the W3C? Can you provide with links to back that claim?
– Madara Uchiha♦
Apr 7 '15 at 14:44
How is it not supported by the W3C? Can you provide with links to back that claim?
– Madara Uchiha♦
Apr 7 '15 at 14:44
4
4
@Mrchief Images are inline elements, not blocks.
text-align works just as well on them.– Madara Uchiha♦
Apr 7 '15 at 14:50
@Mrchief Images are inline elements, not blocks.
text-align works just as well on them.– Madara Uchiha♦
Apr 7 '15 at 14:50
4
4
That explanation is kind of weird isn't it? You say 'text-align' applies to blocks and not inlines, which I believe, then you literally change it to a block element yet avoid using text-align. I mean your code works, but that paragraph needs to be completely reworded, imo.
– Octopus
Aug 18 '16 at 4:19
That explanation is kind of weird isn't it? You say 'text-align' applies to blocks and not inlines, which I believe, then you literally change it to a block element yet avoid using text-align. I mean your code works, but that paragraph needs to be completely reworded, imo.
– Octopus
Aug 18 '16 at 4:19
|
show 8 more comments
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
13
When does that "not work"?
– Madara Uchiha♦
Apr 7 '15 at 15:02
add a comment |
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
13
When does that "not work"?
– Madara Uchiha♦
Apr 7 '15 at 15:02
add a comment |
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
answered Aug 14 '11 at 6:24
craftsycandymonster
1,348289
1,348289
13
When does that "not work"?
– Madara Uchiha♦
Apr 7 '15 at 15:02
add a comment |
13
When does that "not work"?
– Madara Uchiha♦
Apr 7 '15 at 15:02
13
13
When does that "not work"?
– Madara Uchiha♦
Apr 7 '15 at 15:02
When does that "not work"?
– Madara Uchiha♦
Apr 7 '15 at 15:02
add a comment |
Came across this post and it worked for me:
img {
position: absolute;
top: 0; bottom:0; left: 0; right:0;
margin: auto;
}
(Vertical and horizontal alignment)
20
Only works if parent div is position:relative;
– Ulad Kasach
Apr 8 '15 at 22:50
add a comment |
Came across this post and it worked for me:
img {
position: absolute;
top: 0; bottom:0; left: 0; right:0;
margin: auto;
}
(Vertical and horizontal alignment)
20
Only works if parent div is position:relative;
– Ulad Kasach
Apr 8 '15 at 22:50
add a comment |
Came across this post and it worked for me:
img {
position: absolute;
top: 0; bottom:0; left: 0; right:0;
margin: auto;
}
(Vertical and horizontal alignment)
Came across this post and it worked for me:
img {
position: absolute;
top: 0; bottom:0; left: 0; right:0;
margin: auto;
}
(Vertical and horizontal alignment)
edited Jan 7 '14 at 9:18
Krishna Raj Salim
5,50752154
5,50752154
answered May 22 '12 at 14:28
Shai Reznik - HiRez.io
6,45312933
6,45312933
20
Only works if parent div is position:relative;
– Ulad Kasach
Apr 8 '15 at 22:50
add a comment |
20
Only works if parent div is position:relative;
– Ulad Kasach
Apr 8 '15 at 22:50
20
20
Only works if parent div is position:relative;
– Ulad Kasach
Apr 8 '15 at 22:50
Only works if parent div is position:relative;
– Ulad Kasach
Apr 8 '15 at 22:50
add a comment |
Another way of doing would be centering an enclosing paragraph:
<p style="text-align:center"><img src="..."/></p>
19
I would disagree, I think this does answer the question. The OP asked whether or not the propertytext-align: centeris a good way to center an image, and did not specify that the property had to be a part of the img tag. This answer uses the property in question in an effort to provide a solution (that does work).
– MandM
Mar 19 '13 at 22:19
2
This worked for me when display:block, etc. would not.
– matthewsheets
Aug 27 '15 at 20:35
add a comment |
Another way of doing would be centering an enclosing paragraph:
<p style="text-align:center"><img src="..."/></p>
19
I would disagree, I think this does answer the question. The OP asked whether or not the propertytext-align: centeris a good way to center an image, and did not specify that the property had to be a part of the img tag. This answer uses the property in question in an effort to provide a solution (that does work).
– MandM
Mar 19 '13 at 22:19
2
This worked for me when display:block, etc. would not.
– matthewsheets
Aug 27 '15 at 20:35
add a comment |
Another way of doing would be centering an enclosing paragraph:
<p style="text-align:center"><img src="..."/></p>
Another way of doing would be centering an enclosing paragraph:
<p style="text-align:center"><img src="..."/></p>
edited Jan 20 '13 at 12:51
regilero
24.5k44788
24.5k44788
answered Jan 20 '13 at 12:32
EssamSoft
588412
588412
19
I would disagree, I think this does answer the question. The OP asked whether or not the propertytext-align: centeris a good way to center an image, and did not specify that the property had to be a part of the img tag. This answer uses the property in question in an effort to provide a solution (that does work).
– MandM
Mar 19 '13 at 22:19
2
This worked for me when display:block, etc. would not.
– matthewsheets
Aug 27 '15 at 20:35
add a comment |
19
I would disagree, I think this does answer the question. The OP asked whether or not the propertytext-align: centeris a good way to center an image, and did not specify that the property had to be a part of the img tag. This answer uses the property in question in an effort to provide a solution (that does work).
– MandM
Mar 19 '13 at 22:19
2
This worked for me when display:block, etc. would not.
– matthewsheets
Aug 27 '15 at 20:35
19
19
I would disagree, I think this does answer the question. The OP asked whether or not the property
text-align: center is a good way to center an image, and did not specify that the property had to be a part of the img tag. This answer uses the property in question in an effort to provide a solution (that does work).– MandM
Mar 19 '13 at 22:19
I would disagree, I think this does answer the question. The OP asked whether or not the property
text-align: center is a good way to center an image, and did not specify that the property had to be a part of the img tag. This answer uses the property in question in an effort to provide a solution (that does work).– MandM
Mar 19 '13 at 22:19
2
2
This worked for me when display:block, etc. would not.
– matthewsheets
Aug 27 '15 at 20:35
This worked for me when display:block, etc. would not.
– matthewsheets
Aug 27 '15 at 20:35
add a comment |
You can do:
<center><img src="..." /></center>
9
unfortunately,<center>is not supported in html5, but damn, it works.
– Ayrat
Oct 11 '15 at 12:58
add a comment |
You can do:
<center><img src="..." /></center>
9
unfortunately,<center>is not supported in html5, but damn, it works.
– Ayrat
Oct 11 '15 at 12:58
add a comment |
You can do:
<center><img src="..." /></center>
You can do:
<center><img src="..." /></center>
edited Aug 10 '15 at 9:01
answered Aug 10 '15 at 8:42
Dimitris Maniatis
19114
19114
9
unfortunately,<center>is not supported in html5, but damn, it works.
– Ayrat
Oct 11 '15 at 12:58
add a comment |
9
unfortunately,<center>is not supported in html5, but damn, it works.
– Ayrat
Oct 11 '15 at 12:58
9
9
unfortunately,
<center> is not supported in html5, but damn, it works.– Ayrat
Oct 11 '15 at 12:58
unfortunately,
<center> is not supported in html5, but damn, it works.– Ayrat
Oct 11 '15 at 12:58
add a comment |
Actually, the only problem with your code is that the text-align attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span tag around the image and set its style to text-align: center, as so:
span.centerImage {
text-align: center;
}<span class="centerImage"><img src="http://placehold.it/60/60" /></span>The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span (or div).
2
Aspanelement isdisplay: inline;by default, so this runs into the same problem as placingtext-align: center;on theimgitself. You must set thespantodisplay: block;or replace it with adivfor this to work.
– Web_Designer
May 22 '17 at 20:05
add a comment |
Actually, the only problem with your code is that the text-align attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span tag around the image and set its style to text-align: center, as so:
span.centerImage {
text-align: center;
}<span class="centerImage"><img src="http://placehold.it/60/60" /></span>The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span (or div).
2
Aspanelement isdisplay: inline;by default, so this runs into the same problem as placingtext-align: center;on theimgitself. You must set thespantodisplay: block;or replace it with adivfor this to work.
– Web_Designer
May 22 '17 at 20:05
add a comment |
Actually, the only problem with your code is that the text-align attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span tag around the image and set its style to text-align: center, as so:
span.centerImage {
text-align: center;
}<span class="centerImage"><img src="http://placehold.it/60/60" /></span>The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span (or div).
Actually, the only problem with your code is that the text-align attribute applies to text (yes, images count as text) inside of the tag. You would want to put a span tag around the image and set its style to text-align: center, as so:
span.centerImage {
text-align: center;
}<span class="centerImage"><img src="http://placehold.it/60/60" /></span>The image will be centered. In response to your question, it is the easiest and most foolproof way to center images, as long as you remember to apply the rule to the image's containing span (or div).
span.centerImage {
text-align: center;
}<span class="centerImage"><img src="http://placehold.it/60/60" /></span>span.centerImage {
text-align: center;
}<span class="centerImage"><img src="http://placehold.it/60/60" /></span>edited May 22 '17 at 20:03
Web_Designer
34.4k74180239
34.4k74180239
answered Oct 18 '12 at 0:40
Code Monkey
5253824
5253824
2
Aspanelement isdisplay: inline;by default, so this runs into the same problem as placingtext-align: center;on theimgitself. You must set thespantodisplay: block;or replace it with adivfor this to work.
– Web_Designer
May 22 '17 at 20:05
add a comment |
2
Aspanelement isdisplay: inline;by default, so this runs into the same problem as placingtext-align: center;on theimgitself. You must set thespantodisplay: block;or replace it with adivfor this to work.
– Web_Designer
May 22 '17 at 20:05
2
2
A
span element is display: inline; by default, so this runs into the same problem as placing text-align: center; on the img itself. You must set the span to display: block; or replace it with a div for this to work.– Web_Designer
May 22 '17 at 20:05
A
span element is display: inline; by default, so this runs into the same problem as placing text-align: center; on the img itself. You must set the span to display: block; or replace it with a div for this to work.– Web_Designer
May 22 '17 at 20:05
add a comment |
Only if you need to support ancient IE browsers.
The modern approach is to do margin: 0 auto in your CSS.
Example here: http://jsfiddle.net/bKRMY/
HTML:
<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>
CSS:
p.pic {
width: 48px;
margin: 0 auto;
}
Only issue ihere is that the width of the paragraph must be the same as the width of the image. If you don't put a width on the paragraph, it will not work, because it will assume 100% and your image will be aligned left, unless of course you use text-align:center.
Try out the fiddle and experiment with it if you like.
add a comment |
Only if you need to support ancient IE browsers.
The modern approach is to do margin: 0 auto in your CSS.
Example here: http://jsfiddle.net/bKRMY/
HTML:
<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>
CSS:
p.pic {
width: 48px;
margin: 0 auto;
}
Only issue ihere is that the width of the paragraph must be the same as the width of the image. If you don't put a width on the paragraph, it will not work, because it will assume 100% and your image will be aligned left, unless of course you use text-align:center.
Try out the fiddle and experiment with it if you like.
add a comment |
Only if you need to support ancient IE browsers.
The modern approach is to do margin: 0 auto in your CSS.
Example here: http://jsfiddle.net/bKRMY/
HTML:
<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>
CSS:
p.pic {
width: 48px;
margin: 0 auto;
}
Only issue ihere is that the width of the paragraph must be the same as the width of the image. If you don't put a width on the paragraph, it will not work, because it will assume 100% and your image will be aligned left, unless of course you use text-align:center.
Try out the fiddle and experiment with it if you like.
Only if you need to support ancient IE browsers.
The modern approach is to do margin: 0 auto in your CSS.
Example here: http://jsfiddle.net/bKRMY/
HTML:
<p>Hello the following image is centered</p>
<p class="pic"><img src="https://twimg0-a.akamaihd.net/profile_images/440228301/StackoverflowLogo_reasonably_small.png"/></p>
<p>Did it work?</p>
CSS:
p.pic {
width: 48px;
margin: 0 auto;
}
Only issue ihere is that the width of the paragraph must be the same as the width of the image. If you don't put a width on the paragraph, it will not work, because it will assume 100% and your image will be aligned left, unless of course you use text-align:center.
Try out the fiddle and experiment with it if you like.
edited Aug 14 '11 at 6:42
answered Aug 14 '11 at 6:24
Ray Toal
65.4k10121179
65.4k10121179
add a comment |
add a comment |
There are three methods for centering an element that I can suggest.
using
text-alignproperty
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
using
marginproperty
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
using
positionproperty
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
The first and second methods only work if the parent is at least as wide as the image. When the image is wider than its parent, the image will not stay centered!!!
But:
Third method is a good way for that!
Here's an example:
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>
#3 is so precise that I've been able to center animginside a justifieddivin aWebViewwith CSS injection. Thank you!
– JorgeAmVF
Nov 16 '18 at 22:19
add a comment |
There are three methods for centering an element that I can suggest.
using
text-alignproperty
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
using
marginproperty
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
using
positionproperty
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
The first and second methods only work if the parent is at least as wide as the image. When the image is wider than its parent, the image will not stay centered!!!
But:
Third method is a good way for that!
Here's an example:
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>
#3 is so precise that I've been able to center animginside a justifieddivin aWebViewwith CSS injection. Thank you!
– JorgeAmVF
Nov 16 '18 at 22:19
add a comment |
There are three methods for centering an element that I can suggest.
using
text-alignproperty
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
using
marginproperty
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
using
positionproperty
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
The first and second methods only work if the parent is at least as wide as the image. When the image is wider than its parent, the image will not stay centered!!!
But:
Third method is a good way for that!
Here's an example:
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>There are three methods for centering an element that I can suggest.
using
text-alignproperty
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
using
marginproperty
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
using
positionproperty
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
The first and second methods only work if the parent is at least as wide as the image. When the image is wider than its parent, the image will not stay centered!!!
But:
Third method is a good way for that!
Here's an example:
img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div> .parent {
text-align: center;
} <div class="parent">
<img src="https://placehold.it/60/60" />
</div> .parent {
text-align: center;
} <div class="parent">
<img src="https://placehold.it/60/60" />
</div>img {
display: block;
margin: 0 auto;
}<img src="https://placehold.it/60/60" />img {
display: block;
margin: 0 auto;
}<img src="https://placehold.it/60/60" />img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="https://placehold.it/60/60" />
</div>img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="https://placehold.it/60/60" />
</div>img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>img {
display: block;
position: relative;
left: -50%;
}
.parent {
position: absolute;
left: 50%;
}<div class="parent">
<img src="http://imgsv.imaging.nikon.com/lineup/lens/zoom/normalzoom/af-s_dx_18-140mmf_35-56g_ed_vr/img/sample/img_01.jpg" />
</div>edited May 22 '17 at 20:25
Web_Designer
34.4k74180239
34.4k74180239
answered Dec 28 '15 at 15:40
Amin Fazlali
731613
731613
#3 is so precise that I've been able to center animginside a justifieddivin aWebViewwith CSS injection. Thank you!
– JorgeAmVF
Nov 16 '18 at 22:19
add a comment |
#3 is so precise that I've been able to center animginside a justifieddivin aWebViewwith CSS injection. Thank you!
– JorgeAmVF
Nov 16 '18 at 22:19
#3 is so precise that I've been able to center an
img inside a justified div in a WebView with CSS injection. Thank you!– JorgeAmVF
Nov 16 '18 at 22:19
#3 is so precise that I've been able to center an
img inside a justified div in a WebView with CSS injection. Thank you!– JorgeAmVF
Nov 16 '18 at 22:19
add a comment |
if you are using a class with image then the following will do
class{
display: block;
margin: 0 auto;
}
if it is only an image in a specific calss that you want to center align then following will do
class img {
display: block;
margin: 0 auto;
}
I'd change the bottom one to beimg.classor add animg.classversion too. Thx for this.
– Chuck Savage
Dec 15 '15 at 19:32
add a comment |
if you are using a class with image then the following will do
class{
display: block;
margin: 0 auto;
}
if it is only an image in a specific calss that you want to center align then following will do
class img {
display: block;
margin: 0 auto;
}
I'd change the bottom one to beimg.classor add animg.classversion too. Thx for this.
– Chuck Savage
Dec 15 '15 at 19:32
add a comment |
if you are using a class with image then the following will do
class{
display: block;
margin: 0 auto;
}
if it is only an image in a specific calss that you want to center align then following will do
class img {
display: block;
margin: 0 auto;
}
if you are using a class with image then the following will do
class{
display: block;
margin: 0 auto;
}
if it is only an image in a specific calss that you want to center align then following will do
class img {
display: block;
margin: 0 auto;
}
answered May 18 '13 at 6:26
Baig
2,03732759
2,03732759
I'd change the bottom one to beimg.classor add animg.classversion too. Thx for this.
– Chuck Savage
Dec 15 '15 at 19:32
add a comment |
I'd change the bottom one to beimg.classor add animg.classversion too. Thx for this.
– Chuck Savage
Dec 15 '15 at 19:32
I'd change the bottom one to be
img.class or add an img.class version too. Thx for this.– Chuck Savage
Dec 15 '15 at 19:32
I'd change the bottom one to be
img.class or add an img.class version too. Thx for this.– Chuck Savage
Dec 15 '15 at 19:32
add a comment |
img{
display: block;
margin-right: auto;
margin-left: auto;
}
dream hunter, if you are proposing an alternative way of centering an image using css, then you need to expand your question a bit. You could explain how and why this proposed alternative would be a better way to achieve the questioner's goal, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.
– Vince Bowdren
Aug 19 '16 at 15:59
add a comment |
img{
display: block;
margin-right: auto;
margin-left: auto;
}
dream hunter, if you are proposing an alternative way of centering an image using css, then you need to expand your question a bit. You could explain how and why this proposed alternative would be a better way to achieve the questioner's goal, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.
– Vince Bowdren
Aug 19 '16 at 15:59
add a comment |
img{
display: block;
margin-right: auto;
margin-left: auto;
}
img{
display: block;
margin-right: auto;
margin-left: auto;
}
edited Mar 15 '18 at 22:22
sg7
4,18712236
4,18712236
answered Aug 19 '16 at 9:36
Dream Hunter
2,02911532
2,02911532
dream hunter, if you are proposing an alternative way of centering an image using css, then you need to expand your question a bit. You could explain how and why this proposed alternative would be a better way to achieve the questioner's goal, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.
– Vince Bowdren
Aug 19 '16 at 15:59
add a comment |
dream hunter, if you are proposing an alternative way of centering an image using css, then you need to expand your question a bit. You could explain how and why this proposed alternative would be a better way to achieve the questioner's goal, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.
– Vince Bowdren
Aug 19 '16 at 15:59
dream hunter, if you are proposing an alternative way of centering an image using css, then you need to expand your question a bit. You could explain how and why this proposed alternative would be a better way to achieve the questioner's goal, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.
– Vince Bowdren
Aug 19 '16 at 15:59
dream hunter, if you are proposing an alternative way of centering an image using css, then you need to expand your question a bit. You could explain how and why this proposed alternative would be a better way to achieve the questioner's goal, perhaps including a link to the relevant documentation. That would make it more useful to them, and also more useful to other site readers who are looking for solutions to similar problems.
– Vince Bowdren
Aug 19 '16 at 15:59
add a comment |
simply change parent align :)
Try this one on parent properties text-align:center
add a comment |
simply change parent align :)
Try this one on parent properties text-align:center
add a comment |
simply change parent align :)
Try this one on parent properties text-align:center
simply change parent align :)
Try this one on parent properties text-align:center
edited Nov 9 '13 at 22:35
answered Mar 17 '13 at 21:37
Mo.
13.2k28104168
13.2k28104168
add a comment |
add a comment |
you can use text-align: center on the parent and change the img to display: inline-block ->it therefore behaves like a text-element and is will be centered if the parent has a width!
img {
display: inline-block
}
add a comment |
you can use text-align: center on the parent and change the img to display: inline-block ->it therefore behaves like a text-element and is will be centered if the parent has a width!
img {
display: inline-block
}
add a comment |
you can use text-align: center on the parent and change the img to display: inline-block ->it therefore behaves like a text-element and is will be centered if the parent has a width!
img {
display: inline-block
}
you can use text-align: center on the parent and change the img to display: inline-block ->it therefore behaves like a text-element and is will be centered if the parent has a width!
img {
display: inline-block
}
answered Apr 29 '15 at 7:25
Doml The-Bread
1,5441616
1,5441616
add a comment |
add a comment |
On the container holding image You can use css3 flex box to perfectly center the image inside, both vertically and horizontally.
Let assume You have as image holder:
then as css You have to use
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
And this will make all your content inside this div perfectly centered.
whate in the justify-content can use for set align in <p>??
– teran teshara
Sep 8 '18 at 9:54
add a comment |
On the container holding image You can use css3 flex box to perfectly center the image inside, both vertically and horizontally.
Let assume You have as image holder:
then as css You have to use
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
And this will make all your content inside this div perfectly centered.
whate in the justify-content can use for set align in <p>??
– teran teshara
Sep 8 '18 at 9:54
add a comment |
On the container holding image You can use css3 flex box to perfectly center the image inside, both vertically and horizontally.
Let assume You have as image holder:
then as css You have to use
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
And this will make all your content inside this div perfectly centered.
On the container holding image You can use css3 flex box to perfectly center the image inside, both vertically and horizontally.
Let assume You have as image holder:
then as css You have to use
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
And this will make all your content inside this div perfectly centered.
answered Dec 6 '16 at 9:42
Santoni
762
762
whate in the justify-content can use for set align in <p>??
– teran teshara
Sep 8 '18 at 9:54
add a comment |
whate in the justify-content can use for set align in <p>??
– teran teshara
Sep 8 '18 at 9:54
whate in the justify-content can use for set align in <p>??
– teran teshara
Sep 8 '18 at 9:54
whate in the justify-content can use for set align in <p>??
– teran teshara
Sep 8 '18 at 9:54
add a comment |
If you want to set the image as background, I've got solution:
.image{
background-image: url(yourimage.jpg);
background-position: center;
}
add a comment |
If you want to set the image as background, I've got solution:
.image{
background-image: url(yourimage.jpg);
background-position: center;
}
add a comment |
If you want to set the image as background, I've got solution:
.image{
background-image: url(yourimage.jpg);
background-position: center;
}
If you want to set the image as background, I've got solution:
.image{
background-image: url(yourimage.jpg);
background-position: center;
}
edited Sep 28 '16 at 12:28
jmattheis
5,60282441
5,60282441
answered Sep 5 '16 at 18:42
Wojciechu
1,508167
1,508167
add a comment |
add a comment |
To center a non background image depends on whether you want to display the image as an inline (default behavior) or a block element.
Case of inline
If you want to keep the default behavior of the image's display CSS property, you will need to wrap your image inside an other block element to which you must set text-align: center;
Case of block
If you want to consider the image as a block element of its own, then text-align property does not make a sens, and you should do this instead:
IMG.display {
display: block;
margin-left: auto;
margin-right: auto;
}
The answer to your question:
Is the property text-align: center; a good way to center an image
using CSS?
Yes and no.
- Yes, if the image is the only element inside its wrapper.
- No, in case you have other elements inside the image's wrapper because t all the children elements which are siblings of the image will inherit the
text-alignproperty: and may be you would not like this side effect.
References
- List of inline elements
- Centering things
add a comment |
To center a non background image depends on whether you want to display the image as an inline (default behavior) or a block element.
Case of inline
If you want to keep the default behavior of the image's display CSS property, you will need to wrap your image inside an other block element to which you must set text-align: center;
Case of block
If you want to consider the image as a block element of its own, then text-align property does not make a sens, and you should do this instead:
IMG.display {
display: block;
margin-left: auto;
margin-right: auto;
}
The answer to your question:
Is the property text-align: center; a good way to center an image
using CSS?
Yes and no.
- Yes, if the image is the only element inside its wrapper.
- No, in case you have other elements inside the image's wrapper because t all the children elements which are siblings of the image will inherit the
text-alignproperty: and may be you would not like this side effect.
References
- List of inline elements
- Centering things
add a comment |
To center a non background image depends on whether you want to display the image as an inline (default behavior) or a block element.
Case of inline
If you want to keep the default behavior of the image's display CSS property, you will need to wrap your image inside an other block element to which you must set text-align: center;
Case of block
If you want to consider the image as a block element of its own, then text-align property does not make a sens, and you should do this instead:
IMG.display {
display: block;
margin-left: auto;
margin-right: auto;
}
The answer to your question:
Is the property text-align: center; a good way to center an image
using CSS?
Yes and no.
- Yes, if the image is the only element inside its wrapper.
- No, in case you have other elements inside the image's wrapper because t all the children elements which are siblings of the image will inherit the
text-alignproperty: and may be you would not like this side effect.
References
- List of inline elements
- Centering things
To center a non background image depends on whether you want to display the image as an inline (default behavior) or a block element.
Case of inline
If you want to keep the default behavior of the image's display CSS property, you will need to wrap your image inside an other block element to which you must set text-align: center;
Case of block
If you want to consider the image as a block element of its own, then text-align property does not make a sens, and you should do this instead:
IMG.display {
display: block;
margin-left: auto;
margin-right: auto;
}
The answer to your question:
Is the property text-align: center; a good way to center an image
using CSS?
Yes and no.
- Yes, if the image is the only element inside its wrapper.
- No, in case you have other elements inside the image's wrapper because t all the children elements which are siblings of the image will inherit the
text-alignproperty: and may be you would not like this side effect.
References
- List of inline elements
- Centering things
edited Nov 2 '16 at 9:27
answered Nov 1 '16 at 11:31
Billal Begueradj
5,679132638
5,679132638
add a comment |
add a comment |
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
add a comment |
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
add a comment |
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
answered Mar 14 '17 at 11:15
Akintunde-Rotimi
3,67931123
3,67931123
add a comment |
add a comment |
One more way to scale - display it:
img {
width: 60%; /* or required size of image. */
margin-left: 20% /* or scale it to move image. */
margin-right: 20% /* doesn't matters much if using left and width */
}
add a comment |
One more way to scale - display it:
img {
width: 60%; /* or required size of image. */
margin-left: 20% /* or scale it to move image. */
margin-right: 20% /* doesn't matters much if using left and width */
}
add a comment |
One more way to scale - display it:
img {
width: 60%; /* or required size of image. */
margin-left: 20% /* or scale it to move image. */
margin-right: 20% /* doesn't matters much if using left and width */
}
One more way to scale - display it:
img {
width: 60%; /* or required size of image. */
margin-left: 20% /* or scale it to move image. */
margin-right: 20% /* doesn't matters much if using left and width */
}
answered Jun 26 '15 at 9:21
Kishore Banala
3631412
3631412
add a comment |
add a comment |
use this to your img css
img{
margin-right: auto;
margin-left: auto;
}
add a comment |
use this to your img css
img{
margin-right: auto;
margin-left: auto;
}
add a comment |
use this to your img css
img{
margin-right: auto;
margin-left: auto;
}
use this to your img css
img{
margin-right: auto;
margin-left: auto;
}
answered Nov 23 '15 at 17:33
treb
133212
133212
add a comment |
add a comment |
display: block with margin: 0 didn't work for me, neither wrapping with a text-align: center element.
This is my solution:
img.center {
position: absolute;
transform: translateX(-50%);
left: 50%;
}
translateX is supported by most browsers
1
did you meanmargin: 0 auto;? The key is settingmargin-leftandmargin-righttoauto.margin: 0 auto;is just a shortcut for that.
– Web_Designer
Apr 25 '16 at 19:49
1
@Web_Designer I triedmargin: 0 auto,margin: 0, andmargin: auto, none worked. Note that in Chrome's inspector, when usingmargin: 0 auto, is strikes the property with an exclamation mark sayinginvalid property value(or whatever that means that)
– OverCoder
Apr 25 '16 at 20:03
I think you meant "position: absolute;" instead of "display: absolute;"
– WebDevDaniel
Sep 26 '16 at 15:37
Thanks @WebDevDaniel for pointing out the typo. Oh any by the way, you might want to userelativepositioning rather thanabsolute, both work pretty well.
– OverCoder
Sep 28 '16 at 12:28
Got it, thanks!
– WebDevDaniel
Sep 29 '16 at 22:19
add a comment |
display: block with margin: 0 didn't work for me, neither wrapping with a text-align: center element.
This is my solution:
img.center {
position: absolute;
transform: translateX(-50%);
left: 50%;
}
translateX is supported by most browsers
1
did you meanmargin: 0 auto;? The key is settingmargin-leftandmargin-righttoauto.margin: 0 auto;is just a shortcut for that.
– Web_Designer
Apr 25 '16 at 19:49
1
@Web_Designer I triedmargin: 0 auto,margin: 0, andmargin: auto, none worked. Note that in Chrome's inspector, when usingmargin: 0 auto, is strikes the property with an exclamation mark sayinginvalid property value(or whatever that means that)
– OverCoder
Apr 25 '16 at 20:03
I think you meant "position: absolute;" instead of "display: absolute;"
– WebDevDaniel
Sep 26 '16 at 15:37
Thanks @WebDevDaniel for pointing out the typo. Oh any by the way, you might want to userelativepositioning rather thanabsolute, both work pretty well.
– OverCoder
Sep 28 '16 at 12:28
Got it, thanks!
– WebDevDaniel
Sep 29 '16 at 22:19
add a comment |
display: block with margin: 0 didn't work for me, neither wrapping with a text-align: center element.
This is my solution:
img.center {
position: absolute;
transform: translateX(-50%);
left: 50%;
}
translateX is supported by most browsers
display: block with margin: 0 didn't work for me, neither wrapping with a text-align: center element.
This is my solution:
img.center {
position: absolute;
transform: translateX(-50%);
left: 50%;
}
translateX is supported by most browsers
edited Sep 28 '16 at 12:26
answered Apr 25 '16 at 13:32
OverCoder
6551227
6551227
1
did you meanmargin: 0 auto;? The key is settingmargin-leftandmargin-righttoauto.margin: 0 auto;is just a shortcut for that.
– Web_Designer
Apr 25 '16 at 19:49
1
@Web_Designer I triedmargin: 0 auto,margin: 0, andmargin: auto, none worked. Note that in Chrome's inspector, when usingmargin: 0 auto, is strikes the property with an exclamation mark sayinginvalid property value(or whatever that means that)
– OverCoder
Apr 25 '16 at 20:03
I think you meant "position: absolute;" instead of "display: absolute;"
– WebDevDaniel
Sep 26 '16 at 15:37
Thanks @WebDevDaniel for pointing out the typo. Oh any by the way, you might want to userelativepositioning rather thanabsolute, both work pretty well.
– OverCoder
Sep 28 '16 at 12:28
Got it, thanks!
– WebDevDaniel
Sep 29 '16 at 22:19
add a comment |
1
did you meanmargin: 0 auto;? The key is settingmargin-leftandmargin-righttoauto.margin: 0 auto;is just a shortcut for that.
– Web_Designer
Apr 25 '16 at 19:49
1
@Web_Designer I triedmargin: 0 auto,margin: 0, andmargin: auto, none worked. Note that in Chrome's inspector, when usingmargin: 0 auto, is strikes the property with an exclamation mark sayinginvalid property value(or whatever that means that)
– OverCoder
Apr 25 '16 at 20:03
I think you meant "position: absolute;" instead of "display: absolute;"
– WebDevDaniel
Sep 26 '16 at 15:37
Thanks @WebDevDaniel for pointing out the typo. Oh any by the way, you might want to userelativepositioning rather thanabsolute, both work pretty well.
– OverCoder
Sep 28 '16 at 12:28
Got it, thanks!
– WebDevDaniel
Sep 29 '16 at 22:19
1
1
did you mean
margin: 0 auto;? The key is setting margin-left and margin-right to auto. margin: 0 auto; is just a shortcut for that.– Web_Designer
Apr 25 '16 at 19:49
did you mean
margin: 0 auto;? The key is setting margin-left and margin-right to auto. margin: 0 auto; is just a shortcut for that.– Web_Designer
Apr 25 '16 at 19:49
1
1
@Web_Designer I tried
margin: 0 auto, margin: 0, and margin: auto, none worked. Note that in Chrome's inspector, when using margin: 0 auto, is strikes the property with an exclamation mark saying invalid property value (or whatever that means that)– OverCoder
Apr 25 '16 at 20:03
@Web_Designer I tried
margin: 0 auto, margin: 0, and margin: auto, none worked. Note that in Chrome's inspector, when using margin: 0 auto, is strikes the property with an exclamation mark saying invalid property value (or whatever that means that)– OverCoder
Apr 25 '16 at 20:03
I think you meant "position: absolute;" instead of "display: absolute;"
– WebDevDaniel
Sep 26 '16 at 15:37
I think you meant "position: absolute;" instead of "display: absolute;"
– WebDevDaniel
Sep 26 '16 at 15:37
Thanks @WebDevDaniel for pointing out the typo. Oh any by the way, you might want to use
relative positioning rather than absolute, both work pretty well.– OverCoder
Sep 28 '16 at 12:28
Thanks @WebDevDaniel for pointing out the typo. Oh any by the way, you might want to use
relative positioning rather than absolute, both work pretty well.– OverCoder
Sep 28 '16 at 12:28
Got it, thanks!
– WebDevDaniel
Sep 29 '16 at 22:19
Got it, thanks!
– WebDevDaniel
Sep 29 '16 at 22:19
add a comment |
I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen:
https://codepen.io/artforlife/pen/MoBzrL?editors=1100
add a comment |
I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen:
https://codepen.io/artforlife/pen/MoBzrL?editors=1100
add a comment |
I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen:
https://codepen.io/artforlife/pen/MoBzrL?editors=1100
I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.
HTML:
<div class="picture-group">
<h2 class="picture-title">Picture #1</h2>
<img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
<p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
</div>
CSS:
.picture-group {
border: 1px solid black;
width: 25%;
float: left;
height: 300px;
#overflow:scroll;
padding: 5px;
text-align:center;
}
CodePen:
https://codepen.io/artforlife/pen/MoBzrL?editors=1100
answered Jul 6 '17 at 15:54
MadPhysicist
1,31731238
1,31731238
add a comment |
add a comment |
Sometimes we directly add the content and images on wordpress admin inside the pages. When we insert the images in side the content and want to align that center. Code is display as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
in that case you can add css like this:
article p img{
margin:0 auto;
display:block;
text-align:center;
float:none;
}
Hope it will help in that situation.
add a comment |
Sometimes we directly add the content and images on wordpress admin inside the pages. When we insert the images in side the content and want to align that center. Code is display as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
in that case you can add css like this:
article p img{
margin:0 auto;
display:block;
text-align:center;
float:none;
}
Hope it will help in that situation.
add a comment |
Sometimes we directly add the content and images on wordpress admin inside the pages. When we insert the images in side the content and want to align that center. Code is display as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
in that case you can add css like this:
article p img{
margin:0 auto;
display:block;
text-align:center;
float:none;
}
Hope it will help in that situation.
Sometimes we directly add the content and images on wordpress admin inside the pages. When we insert the images in side the content and want to align that center. Code is display as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
in that case you can add css like this:
article p img{
margin:0 auto;
display:block;
text-align:center;
float:none;
}
Hope it will help in that situation.
answered Apr 17 '18 at 5:19
Sangrai
5911517
5911517
add a comment |
add a comment |
<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>
i think this is the way for center image in laravel frame work.
add a comment |
<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>
i think this is the way for center image in laravel frame work.
add a comment |
<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>
i think this is the way for center image in laravel frame work.
<dev class="col-sm-8" style="text-align: center;"><img src="{{URL('image/car-trouble-with-clipping-path.jpg')}}" ></dev>
i think this is the way for center image in laravel frame work.
answered Sep 8 '18 at 9:48
teran teshara
138114
138114
add a comment |
add a comment |
The simplest solution I found was to add this to my img-element:
style="display:block; margin: auto;"
Seems I don't need to add "0" before the "auto" as suggested by others. Maybe that is the proper way, but it works well enough for my purposes without the "0" as well. At least on latest FireFox and Chrome and Edge.
only auto meanauto auto auto autoand0 automeans0 auto 0 auto... and by default auto for bottom and top margin is0so adding 0 or not is exactly the same in this case which make you answer a nth-duplicate of ones already provided
– Temani Afif
Nov 12 '18 at 21:13
Your comment explains why it works. Great. But which of the previous answers said plain 'auto' without '0' works as well? Is that fact not worth mentioning?
– Panu Logic
Nov 13 '18 at 19:52
in this case it should be a comment, because in this particular case both are the same. I don't think we should have an answer for all the equivalent values,in this case we can write :auto,auto auto,auto auto auto,auto auto auto auto,0 auto 0,0 auto,0 auto 0 auto, and so on ... you think each one deserve a different answer? I don't think so.
– Temani Afif
Nov 13 '18 at 19:55
I would not say that if two different segments of CSS produce the same end-result, those two CSS-segments "are the same". Their output is the same but their source-code is not the same. Just like in general you can write any number of different programs that produce the same output. Then it is valuable to know (and tell people who ask) which of such "equivalent" programs seems to be the simplest and shortest to write.
– Panu Logic
Nov 15 '18 at 1:22
and all this should be written within one answer. That's why this answer is more suitable as a comment than a whole new answer. We should present all the equivalent things together and not make them separate feature which may sound that they are completely different because isn't the case [my opinion btw, no need to agree with it or argue the opposite]. And I am not talking about different programs that produce the same output, it's a complete different thing as the logic may not be the same. Here we are talking about the same property and the same value (likei++are the same asi+=1)
– Temani Afif
Nov 15 '18 at 1:28
add a comment |
The simplest solution I found was to add this to my img-element:
style="display:block; margin: auto;"
Seems I don't need to add "0" before the "auto" as suggested by others. Maybe that is the proper way, but it works well enough for my purposes without the "0" as well. At least on latest FireFox and Chrome and Edge.
only auto meanauto auto auto autoand0 automeans0 auto 0 auto... and by default auto for bottom and top margin is0so adding 0 or not is exactly the same in this case which make you answer a nth-duplicate of ones already provided
– Temani Afif
Nov 12 '18 at 21:13
Your comment explains why it works. Great. But which of the previous answers said plain 'auto' without '0' works as well? Is that fact not worth mentioning?
– Panu Logic
Nov 13 '18 at 19:52
in this case it should be a comment, because in this particular case both are the same. I don't think we should have an answer for all the equivalent values,in this case we can write :auto,auto auto,auto auto auto,auto auto auto auto,0 auto 0,0 auto,0 auto 0 auto, and so on ... you think each one deserve a different answer? I don't think so.
– Temani Afif
Nov 13 '18 at 19:55
I would not say that if two different segments of CSS produce the same end-result, those two CSS-segments "are the same". Their output is the same but their source-code is not the same. Just like in general you can write any number of different programs that produce the same output. Then it is valuable to know (and tell people who ask) which of such "equivalent" programs seems to be the simplest and shortest to write.
– Panu Logic
Nov 15 '18 at 1:22
and all this should be written within one answer. That's why this answer is more suitable as a comment than a whole new answer. We should present all the equivalent things together and not make them separate feature which may sound that they are completely different because isn't the case [my opinion btw, no need to agree with it or argue the opposite]. And I am not talking about different programs that produce the same output, it's a complete different thing as the logic may not be the same. Here we are talking about the same property and the same value (likei++are the same asi+=1)
– Temani Afif
Nov 15 '18 at 1:28
add a comment |
The simplest solution I found was to add this to my img-element:
style="display:block; margin: auto;"
Seems I don't need to add "0" before the "auto" as suggested by others. Maybe that is the proper way, but it works well enough for my purposes without the "0" as well. At least on latest FireFox and Chrome and Edge.
The simplest solution I found was to add this to my img-element:
style="display:block; margin: auto;"
Seems I don't need to add "0" before the "auto" as suggested by others. Maybe that is the proper way, but it works well enough for my purposes without the "0" as well. At least on latest FireFox and Chrome and Edge.
answered Nov 12 '18 at 21:09
Panu Logic
586413
586413
only auto meanauto auto auto autoand0 automeans0 auto 0 auto... and by default auto for bottom and top margin is0so adding 0 or not is exactly the same in this case which make you answer a nth-duplicate of ones already provided
– Temani Afif
Nov 12 '18 at 21:13
Your comment explains why it works. Great. But which of the previous answers said plain 'auto' without '0' works as well? Is that fact not worth mentioning?
– Panu Logic
Nov 13 '18 at 19:52
in this case it should be a comment, because in this particular case both are the same. I don't think we should have an answer for all the equivalent values,in this case we can write :auto,auto auto,auto auto auto,auto auto auto auto,0 auto 0,0 auto,0 auto 0 auto, and so on ... you think each one deserve a different answer? I don't think so.
– Temani Afif
Nov 13 '18 at 19:55
I would not say that if two different segments of CSS produce the same end-result, those two CSS-segments "are the same". Their output is the same but their source-code is not the same. Just like in general you can write any number of different programs that produce the same output. Then it is valuable to know (and tell people who ask) which of such "equivalent" programs seems to be the simplest and shortest to write.
– Panu Logic
Nov 15 '18 at 1:22
and all this should be written within one answer. That's why this answer is more suitable as a comment than a whole new answer. We should present all the equivalent things together and not make them separate feature which may sound that they are completely different because isn't the case [my opinion btw, no need to agree with it or argue the opposite]. And I am not talking about different programs that produce the same output, it's a complete different thing as the logic may not be the same. Here we are talking about the same property and the same value (likei++are the same asi+=1)
– Temani Afif
Nov 15 '18 at 1:28
add a comment |
only auto meanauto auto auto autoand0 automeans0 auto 0 auto... and by default auto for bottom and top margin is0so adding 0 or not is exactly the same in this case which make you answer a nth-duplicate of ones already provided
– Temani Afif
Nov 12 '18 at 21:13
Your comment explains why it works. Great. But which of the previous answers said plain 'auto' without '0' works as well? Is that fact not worth mentioning?
– Panu Logic
Nov 13 '18 at 19:52
in this case it should be a comment, because in this particular case both are the same. I don't think we should have an answer for all the equivalent values,in this case we can write :auto,auto auto,auto auto auto,auto auto auto auto,0 auto 0,0 auto,0 auto 0 auto, and so on ... you think each one deserve a different answer? I don't think so.
– Temani Afif
Nov 13 '18 at 19:55
I would not say that if two different segments of CSS produce the same end-result, those two CSS-segments "are the same". Their output is the same but their source-code is not the same. Just like in general you can write any number of different programs that produce the same output. Then it is valuable to know (and tell people who ask) which of such "equivalent" programs seems to be the simplest and shortest to write.
– Panu Logic
Nov 15 '18 at 1:22
and all this should be written within one answer. That's why this answer is more suitable as a comment than a whole new answer. We should present all the equivalent things together and not make them separate feature which may sound that they are completely different because isn't the case [my opinion btw, no need to agree with it or argue the opposite]. And I am not talking about different programs that produce the same output, it's a complete different thing as the logic may not be the same. Here we are talking about the same property and the same value (likei++are the same asi+=1)
– Temani Afif
Nov 15 '18 at 1:28
only auto mean
auto auto auto auto and 0 auto means 0 auto 0 auto ... and by default auto for bottom and top margin is 0 so adding 0 or not is exactly the same in this case which make you answer a nth-duplicate of ones already provided– Temani Afif
Nov 12 '18 at 21:13
only auto mean
auto auto auto auto and 0 auto means 0 auto 0 auto ... and by default auto for bottom and top margin is 0 so adding 0 or not is exactly the same in this case which make you answer a nth-duplicate of ones already provided– Temani Afif
Nov 12 '18 at 21:13
Your comment explains why it works. Great. But which of the previous answers said plain 'auto' without '0' works as well? Is that fact not worth mentioning?
– Panu Logic
Nov 13 '18 at 19:52
Your comment explains why it works. Great. But which of the previous answers said plain 'auto' without '0' works as well? Is that fact not worth mentioning?
– Panu Logic
Nov 13 '18 at 19:52
in this case it should be a comment, because in this particular case both are the same. I don't think we should have an answer for all the equivalent values,in this case we can write :
auto, auto auto, auto auto auto, auto auto auto auto, 0 auto 0, 0 auto, 0 auto 0 auto, and so on ... you think each one deserve a different answer? I don't think so.– Temani Afif
Nov 13 '18 at 19:55
in this case it should be a comment, because in this particular case both are the same. I don't think we should have an answer for all the equivalent values,in this case we can write :
auto, auto auto, auto auto auto, auto auto auto auto, 0 auto 0, 0 auto, 0 auto 0 auto, and so on ... you think each one deserve a different answer? I don't think so.– Temani Afif
Nov 13 '18 at 19:55
I would not say that if two different segments of CSS produce the same end-result, those two CSS-segments "are the same". Their output is the same but their source-code is not the same. Just like in general you can write any number of different programs that produce the same output. Then it is valuable to know (and tell people who ask) which of such "equivalent" programs seems to be the simplest and shortest to write.
– Panu Logic
Nov 15 '18 at 1:22
I would not say that if two different segments of CSS produce the same end-result, those two CSS-segments "are the same". Their output is the same but their source-code is not the same. Just like in general you can write any number of different programs that produce the same output. Then it is valuable to know (and tell people who ask) which of such "equivalent" programs seems to be the simplest and shortest to write.
– Panu Logic
Nov 15 '18 at 1:22
and all this should be written within one answer. That's why this answer is more suitable as a comment than a whole new answer. We should present all the equivalent things together and not make them separate feature which may sound that they are completely different because isn't the case [my opinion btw, no need to agree with it or argue the opposite]. And I am not talking about different programs that produce the same output, it's a complete different thing as the logic may not be the same. Here we are talking about the same property and the same value (like
i++ are the same as i+=1)– Temani Afif
Nov 15 '18 at 1:28
and all this should be written within one answer. That's why this answer is more suitable as a comment than a whole new answer. We should present all the equivalent things together and not make them separate feature which may sound that they are completely different because isn't the case [my opinion btw, no need to agree with it or argue the opposite]. And I am not talking about different programs that produce the same output, it's a complete different thing as the logic may not be the same. Here we are talking about the same property and the same value (like
i++ are the same as i+=1)– Temani Afif
Nov 15 '18 at 1:28
add a comment |
protected by Josh Crozier Mar 1 '14 at 20:05
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?