LoginSignup
10
10

More than 5 years have passed since last update.

画像やブロックの高さを考慮しながら親要素の中央に配置させるjQuery拡張

Posted at

ちょうど真ん中の位置になるようにmargin-topを設定してるだけ

vertical-alignを使えない時とかのために。
微妙に毎度毎度使うので備忘。

(function($){
    $.fn.verticalMiddle = function() {
        this.each(function(){
            var $this = $(this);
            var _height = $this.height();
            var _parent_height = $this.parent().height();
            $this.css('marginTop',(_parent_height/2) - (_height/2));
        });
        return this;
    };
})(jQuery);

使い方

$(function(){
    $('.top-logo').verticalMiddle();
});
10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10