I have div with ajax-loader gif image
<div id="mydiv" style="height: 400px; text-align: center;"> <img src="/Content/ajax-loader.gif" class="ajax-loader"/> </div>
.ajax-loader { /*hidden from IE 5-6 */ margin-top: 0; /* to clean up, just in case IE later supports valign! */ vertical-align: middle; margin-top: expression(( 150 - this.height ) / 2); }
But could not get it displayed in the center (both vertically and horizontally). Need help with that.
You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.
To center an image, we have to set the value of margin-left and margin-right to auto and make it a block element by using the display: block; property. If the image is in the div element, then we can use the text-align: center; property for aligning the image to center in the div.
Using the <center> tagYou can center a picture by enclosing the <img> tag in the <center></center> tags. This action centers that, and only that, picture on the web page.
The following assumes that the width and height of the image is known:
#mydiv { height: 400px; position: relative; background-color: gray; /* for demonstration */ } .ajax-loader { position: absolute; left: 50%; top: 50%; margin-left: -32px; /* -1 * image width / 2 */ margin-top: -32px; /* -1 * image height / 2 */ }
<div id="mydiv"> <img src="http://dummyimage.com/64x64/000/fff.gif&text=LOADING" class="ajax-loader"> </div>
UPDATE: in modern browsers margin: auto
will produce the desired result withing knowing the width/height of the image:
#mydiv { height: 400px; position: relative; background-color: gray; /* for demonstration */ } .ajax-loader { position: absolute; left: 0; top: 0; right: 0; bottom: 0; margin: auto; /* presto! */ }
<div id="mydiv"> <img src="http://dummyimage.com/64x64/000/fff.gif&text=LOADING" class="ajax-loader"> </div>
Full screen ajax loader, with some opacity.
using
$('#mydiv').show(); $('#mydiv').hide();
to toggle it.
#mydiv { position:absolute; top:0; left:0; width:100%; height:100%; z-index:1000; background-color:grey; opacity: .8; } .ajax-loader { position: absolute; left: 50%; top: 50%; margin-left: -32px; /* -1 * image width / 2 */ margin-top: -32px; /* -1 * image height / 2 */ display: block; } <div id="mydiv"> <img src="lib/jQuery/images/ajax-loader.gif" class="ajax-loader"/> </div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With