Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center an HTML element using jQuery and CSS? [closed]

Tags:

jquery

css

I would like to center some html elements in my html page, for example a label. I want to accomplish this by using jQuery and CSS. Unfortunately I'm not familiar with jQuery.

I just need a little help from you guys.

Thank you.

like image 278
Chris N.P. Avatar asked Dec 07 '25 22:12

Chris N.P.


2 Answers

Presentation is NOT a task for jQuery.

CSS:

label {display:block;text-align:center}
like image 169
Niet the Dark Absol Avatar answered Dec 10 '25 13:12

Niet the Dark Absol


Try this simple function:

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

//Use the above function as:
$(element).center();
like image 38
Angela Taylor Avatar answered Dec 10 '25 13:12

Angela Taylor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!