Horizontal image align CSS - css

I'm having some trouble aligning a main image. It should be center-aligned horizontally, yet keeps going all over the place. The page can be found here http://0034.eu/propmanager/
<img src="images/background-space.png" class="displayed" border="0" />
IMG.displayed{
display: inline-block;
margin-left: auto;
margin-right: auto;
}
That's basically the CSS I have applied to the image, all the source code is on main index.html (no separate style sheet).

Add this to your CSS style.
img.displayed {
display: table-caption;
margin: 0 auto;
}
EDIT
From the inputs of IlyaStreltsyn, I agree with the point of clearing the right with display:block for the image to be centered.
For Instance,
img.displayed {
display: block;
margin: 0 auto;
clear: right;
}

Add display: block;
img.displayed{
display: block;
margin:0 auto;
}
DEMO

Inline blocks (like just inlines, which are the images by default) participate in the inline formatting context, not the block formatting context. That's why they don't obey margin:auto (which effectively means margin: 0 for them), but do obey the text-align of their ancestor block element.

check the Fiddle here with css and code
#header {
text-align:center;
}
img.displayed{
display: block;
margin:0 auto;
}
<div id="header">
<img src="http://www.0034.eu/propmanager/images/background-space.png" class="displayed" border="0" width="100" height="100"/>
</div>

Related

Mobile Responsive Logo [duplicate]

Is the property text-align: center; a good way to center an image using CSS?
img {
text-align: center;
}
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 specification.
Use this instead:
img.center {
display: block;
margin: 0 auto;
}
<div style="border: 1px solid black;">
<img class="center" src ="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
That doesn't always work... if it doesn't, try:
img {
display: block;
margin: 0 auto;
}
I came across this post, and it worked for me:
img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
<div style="border: 1px solid black; position:relative; min-height: 200px">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a">
</div>
(Vertical and horizontal alignment)
Not recommendad:
Another way of doing it would be centering an enclosing paragraph:
<p style="text-align:center"><img src="https://via.placeholder.com/300"></p>
Update:
My answer above is correct if you want to start learning HTML/CSS, but it doesn't follow best practices
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).
You can do:
<center><img src="..." /></center>
There are three methods for centering an element that I can suggest:
Using the text-align property
.parent {
text-align: center;
}
<div class="parent">
<img src="https://placehold.it/60/60" />
</div>
Using the margin property
img {
display: block;
margin: 0 auto;
}
<img src="https://placehold.it/60/60" />
Using the position property
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:
The 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>
On the container holding image you can use a CSS 3 Flexbox to perfectly center the image inside, both vertically and horizontally.
Let's assume you have <div class="container"> as the 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.
Only if you need to support ancient versions of Internet Explorer.
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;
}
The only issue here 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.
img{
display: block;
margin-right: auto;
margin-left: auto;
}
If you are using a class with an image then the following will do
class {
display: block;
margin: 0 auto;
}
If it is only an image in a specific class that you want to center align then following will do:
class img {
display: block;
margin: 0 auto;
}
The simplest solution I found was to add this to my img-element:
style="display:block;margin:auto;"
It 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, Chrome, and Edge.
Simply change parent align :)
Try this one on parent properties:
text-align:center
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
}
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 another 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 all the children elements which are siblings of the image will inherit the text-align property: and may be you would not like this side effect.
References
List of inline elements
Centering things
.img-container {
display: flex;
}
img {
margin: auto;
}
this will make the image center in both vertically and horizontally
I would use a div to center align an image. As in:
<div align="center"><img src="your_image_source"/></div>
If you want to set the image as the background, I've got a solution:
.image {
background-image: url(yourimage.jpg);
background-position: center;
}
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% /* It doesn't matters much if using left and width */
}
Use this to your img CSS:
img {
margin-right: auto;
margin-left: auto;
}
Use Grids To Stack images. It is very easy here is the code
.grid {
display:grid;
}
.grid img {
display:block;
margin:0 auto;
}
If your img element is inside a div, which is itself inside another div whose display has been set as flexbox, as in my case here:
(HTML)
<nav class="header">
<div class="image">
<img
src=troll
alt="trollface"
></img>
</div>
<div class="title">
Meme Generator
</div>
<div class="subtitle">
React Course - Project 3
</div>
</nav>
(CSS)
.header{
display: flex;
}
.image{
width: 5%;
height: 100%;
}
.image > img{
width: 100%;
}
You could set your .image div to align itself vertically by doing this:
.image{
width: 5%;
height: 100%;
align-self: center;
}
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
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
Sometimes we directly add the content and images on the WordPress administrator inside the pages. When we insert the images inside the content and want to align that center. Code is displayed as:
**<p><img src="https://abcxyz.com/demo/wp-content/uploads/2018/04/1.jpg" alt=""></p>**
In that case you can add CSS content like this:
article p img{
margin: 0 auto;
display: block;
text-align: center;
float: none;
}
Use:
<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 to center an image in the Laravel framework.
To center an image with CSS.
img{
display: block;
margin-left: auto;
margin-right: auto;
}
You can learn more here
If you want to center image to the center both vertically and horizontaly, regardless of screen size, you can try out this code
img{
display: flex;
justify-content:center;
align-items: center;
height: 100vh;
}

How to align js clock to center of page?

How do I align a javascript countdown box to the center of the page? The url is where the js is located is http://www.freelanceseohelp.com/offer/mobile/index.html
I tried aligning the following but I'm having problems:
.flip-clock-wrapper {
I tried adding the above to
<div class="clock"></div>
Any help fixing this will be appreciated. Thanks.
I'm pretty late to the party here but the following works for me at the time of writing (Version: 0.7.4 Beta). The idea is to wrap it in a container and override some of it's CSS.
HTML
<div class="container">
<div class="flip-clock"></div>
<!-- Flip clock is attached the this element, not .container -->
</div>
CSS
.container {
text-align: center;
}
.flip-clock {
display: inline-block;
width: auto;
}
By default .flip-clock has width: 100% and display: block. By setting width: auto and display: inline-block the item no longer spans the width of it's parent and is able to be centered by adding text-align: center to it's container.
.flip-clock-wrapper {
/* text-align: center; */
/* position: relative; */
/* width: 100%; */
margin: 0 auto;
max-width: 460px;
margin-top: 2em;
}
Remove the Commented CSS in the above CSS Class and add the rest 3 lines in your CSS class to get your Page like Below
through testing, i determined the width of the widget is 460px when considering margin. here is the css to make it happen.
.flip-clock-wrapper{
max-width: 460px;
margin: 3em auto 2em;
}
I also added top margin of 3em and bottom margin of 2em to align it correctly. that page has a ton of problems with the CSS.
Add new div element with specify display: inline-block; to archive this.
<div class="clock flip-clock-wrapper">
<div class="middle"> // Add div element (opening)
....
....
....
</div> // Add div element (closing)
</div>
CSS
.middle{
display:inline-block;
*display:inline;/* IE*/
*zoom:1;/* IE*/
}
Screenshot
Another way:
.flip-clock-wrapper {
display: table; //added
}
.flip-clock-divider {
float: left; // remove
}
.flip-clock-wrapper ul {
float: left; // remove
display: inline-block; // added
}
Add this is your css-
.flip-clock-wrapper{
width:460px;
margin:1em auto;
}
I hope I'll helps you.
Just decrease the width of your clock wrapper & set margin value.
.flip-clock-wrapper{
width: 480px;
margin: 30px auto;
}
Try like this
.flip-clock-wrapper{
max-width:480px;
margin:3em auto;
}
Using the .js file from a CDN I did not wanted to edit it.
I applied those lines to my website main CSS:
.flip-clock-wrapper {
width: inherit !important;
}
.flip-clock {
display: inline-block;
width: auto;
}
<table align="center">
<tr>
<td></td>
<td>
<div class="clock" style="margin:Inherited;"></div>
<div class="message"></div>
<script type="text/javascript">
code!!!
</script>
</td>
<td></td>
</tr>
</table>
Make the .flip-clock-wrapper contain width:auto and display:inline-block. That is:
.flip-clock-wrapper {
text-align: center !important;
position: relative;
width: auto;
margin: 1em;
display: inline-block;
}

Why is there gap above images?

I want to achieve this:
I have achieved this:
I don't understand why is there gap above the button images? The page is live at http://shrineweb.in/other-files/clients/omypet/tellerest/index.html
Markup:
<section id="banner">
<nav> <img src="images/tellerest-homepage-design_09.png" alt=""><img src="images/tellerest-homepage-design_10.png" alt=""><img src="images/tellerest-homepage-design_11.png" alt=""><img src="images/tellerest-homepage-design_12.png" alt="">
</nav>
</section>
CSS:
#banner { width: 950px; margin: 0 auto; text-align: center;}
#banner nav { margin: 0; padding: 0;}
#banner nav img { margin: 0; padding: 0;}
The issue is created by the logo image. Images are inline elements so they have a white-space after them. To remove the white-space, you can display the image as a block element by adding this CSS :
header > img{
display: block;
}
Change your header css and add same height as your image like this
header { width: 950px; height: 56px; margin: 0 auto; text-align: left;}
There is a float issue please add this code
header img {
display: block ;
}
You can use either of these to prevent this:
header img {display: block;}
header img {vertical-align:top;}
The gap is caused by the image's default vertical-align: baseline, which aligns it with the baseline of any text it might sit beside.

Difficulty Centering Pictures

I am having problems centering two pictures on a page:
<div name="pics"style="text-align:center;padding:70px">
<a name="picofday"><img class="picofday" src="castle.jpg" alt=""/></a>
<a name="frame"><img class="frame" src="frame.png" width="50%" height="50%" alt=""/>
</a>
</div>
I have tried adding a text-align attribute and centering the pictures in CSS:
frame{display: block;
margin-left: auto;
margin-right: auto;
}
.picofday{
display: block;
margin-left:auto;
margin-right:auto;
}
Try
a {
display: block;
}
a img {
dsiplay: block;
margin: 0 auto;
width: auto;
}
There could be a few reasons why your elements didn't center. First try adding margin: 0px auto to your element.
For margin: 0 auto; to work, an element needs to have a set width like width: 100px;.
If the element has a set width and still does't center, try display: block;.
Those two things will always work.
How to center elements without margin: 0 auto;:
If you're just trying to center all elements inside the body tag, just use text-align: center; on the body tag to accomplish that.
That said, I don't think you were trying to do that though.
make your A tags display to block and set margin to 0px auto;
it will justify the elements

Text-Align:center centers images too

Whenever I center text in my css, it also centers the images too. Why is that? The text is inside a div ("left") and the images are inside the second div ("right"). Here is the HTML -
<div class="left">
<p>
BANKS</div>
<div class="right">
<p>
<img src="http://www.dogtraining-hampshire.co.uk/dog_training_classes_puppy_1.jpg" border="0">
</div>
And the CSS -
#wrapper {
width: 100%;
overflow: auto;
text-align: center;
}
.left, .right {
width:48%;
margin-bottom: 5px;
}
.left {
clear:both;
float:left;
margin-right:1%;
}
.right {
float:right;
margin-left:1%;}
}
I've tried adding "#wrapper img" to the CSS and setting the images to float left but this hasn't worked either.
Here is a jsfiddle of the full css and html - http://jsfiddle.net/rHEJA/
Downvoters
Any reason for the downvote? I am happy to change my answer!
Images are inline elements, so they abide by the rules given in text-align. If you don't want them to follow it, you can very well give another rule like:
#wrapper img {text-align: left;}
Or, in a better way, just to center the div, you can also do something like:
.centerDiv {width: 50%; margin: auto;}
This makes the <div> to be centred.
This is just what text-align: center; does as a CSS property. Images will also align to the right if you set it to text-align: right;.
You can change your CSS to include the following:
#wrapper img { text-align: left; }

Resources