最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to center images while keeping them responsive? - Stack Overflow

programmeradmin1浏览0评论

Hi please take a look at my site, below is the code snippet in question i have to center my images since ive never had any luck with the css-html methods. The problem is because its set to wait for document.ready() sometimes it will place all my images to the right. Ive tried window.load() but the images center offscreen at smaller window sizes. It was also suggested i try

<div style="
  background: url('Assets/image.png') center center no-repeat;
  width: 100%;
  height: 500px;
 ">
 </div>

but this causes it to lose responsiveness. Ive searched around and i cant find a solution, i just need my images (and the one form) to stay centered and for the images to scale down with the window size.

site:

<script type="text/javascript"> //Centering Script


$(document).ready(function () {
    updateContainer();
    $(window).resize(function() {
        updateContainer();
    });
});


function updateContainer() {


(function ($) {
$.fn.vAlign = function() {
    return this.each(function(i){
    var h = $(this).height();
    var oh = $(this).outerHeight();
    var mt = (h + (oh - h)) / 2;    
    $(this).css("margin-top", "-" + mt + "px"); 
    $(this).css("top", "50%");
    $(this).css("position", "absolute");    
    }); 
};
})(jQuery);

(function ($) {
$.fn.hAlign = function() {
    return this.each(function(i){
    var w = $(this).width();
    var ow = $(this).outerWidth();  
    var ml = (w + (ow - w)) / 2;    
    $(this).css("margin-left", "-" + ml + "px");
    $(this).css("left", "50%");
    $(this).css("position", "absolute");
    });
};
})(jQuery);

Hi please take a look at my site, below is the code snippet in question i have to center my images since ive never had any luck with the css-html methods. The problem is because its set to wait for document.ready() sometimes it will place all my images to the right. Ive tried window.load() but the images center offscreen at smaller window sizes. It was also suggested i try

<div style="
  background: url('Assets/image.png') center center no-repeat;
  width: 100%;
  height: 500px;
 ">
 </div>

but this causes it to lose responsiveness. Ive searched around and i cant find a solution, i just need my images (and the one form) to stay centered and for the images to scale down with the window size.

site: http://bit.ly/11nAQJK

<script type="text/javascript"> //Centering Script


$(document).ready(function () {
    updateContainer();
    $(window).resize(function() {
        updateContainer();
    });
});


function updateContainer() {


(function ($) {
$.fn.vAlign = function() {
    return this.each(function(i){
    var h = $(this).height();
    var oh = $(this).outerHeight();
    var mt = (h + (oh - h)) / 2;    
    $(this).css("margin-top", "-" + mt + "px"); 
    $(this).css("top", "50%");
    $(this).css("position", "absolute");    
    }); 
};
})(jQuery);

(function ($) {
$.fn.hAlign = function() {
    return this.each(function(i){
    var w = $(this).width();
    var ow = $(this).outerWidth();  
    var ml = (w + (ow - w)) / 2;    
    $(this).css("margin-left", "-" + ml + "px");
    $(this).css("left", "50%");
    $(this).css("position", "absolute");
    });
};
})(jQuery);
Share Improve this question edited Jun 9, 2013 at 0:51 Robert Harvey 181k48 gold badges348 silver badges513 bronze badges asked Jun 8, 2013 at 21:41 user2340403user2340403 191 silver badge4 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 3

Remove that whole script. Place this in your CSS.

img{
    display: block;
    margin: 0 auto;
}

just do

<style>
a{
display:block;
text-align:center;
}

</style>



<a href="Assets/OrderSheet.xls">
    <img src="Assets/OrderSheet.png" class="image">
</a>

no need for repositioning

fiddle

No need for js, CSS alone is fine. Set your image to display block, set a width and Max width plus margin auto.

img {
display: block;
width: 300px;
max-width: 100%;
margin: 0 auto;
}

If you won't accept the method suggested by others, I would suggest using em's. Best to use them everywhere, but you could just apply them to your images.

Then use media queries to scale up/down all elements with values specified in em's, by changing the base font-size for different screen sizes.

center a responsive sized element

/* element/id/class */ 
/* margin is 100 - width / 2 */

img {
width:34%;    
margin: 0 33%;
}
发布评论

评论列表(0)

  1. 暂无评论