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
  • 37,547
  • 20
  • 91
  • 118
Saswat
  • 10,704
  • 16
  • 59
  • 126
  • 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
  • 929
  • 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
  • 22,738
  • 6
  • 47
  • 66
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
  • 73,156
  • 29
  • 131
  • 187
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
  • 22,117
  • 10
  • 57
  • 89
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
  • 300
  • 2
  • 7