0

A div can be alinged center like this

<div align="center">Div to be aligned vertically</div>

Now, how can i achieve this using jquery.

In jquery we can work on css. But align="center" is not a css property.

SO how can we achieve this using jquery.

I want to mention that I wanted to know how to align="center" a div, not to just centering a div.

My question was just to apply the align="center" using jquery. The post which has been linked with this question of mine, has different approach using window.width and window.height method.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Saswat
  • 12,320
  • 16
  • 77
  • 156
  • possible duplicate of [Using jQuery to center a DIV on the screen](http://stackoverflow.com/questions/210717/using-jquery-to-center-a-div-on-the-screen) – apaul Aug 12 '14 at 04:53

5 Answers5

3

Well, 3 method for this:

Method 1 Using css Function of jquery:

$('div').css('text-align','center');

Method 2 Using attr Function of jquery: attr stands for attribute

$('div').attr('align','center');

Method 2 Using prop Function of jquery: prop stands for property

$('div').prop('align','center');
Husen
  • 955
  • 1
  • 6
  • 16
2

This is a property of div, you can use .attr or .prop,

Just doing this will work

$('div').attr('align','center');

DEMO

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
2

You can use like this:

$('div').attr('align','center');

A better apporach for this would be using prop:

$('div').prop('align','center');

See attr and prop

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
1

Simply add this:

$('div').attr('align','center');

With attr()

With Javascript:

document.getElementsByTagName("div")[0].setAttribute('align', 'center');

javascript demo

DEMO

Community
  • 1
  • 1
Manwal
  • 23,450
  • 12
  • 63
  • 93
1

I would use:

$('div').prop('align','center');

As C-link Nepal mentioned, .prop is a better supported method for getting and/or applying attributes to elements.

Example

Sheldonbrax
  • 310
  • 2
  • 7