How to center table in xsl-fo?

Koray Tugay picture Koray Tugay · Aug 28, 2013 · Viewed 11.3k times · Source

I am trying to center a table in a block element in a xsl-fo namespace.

Here is what I am trying:

 <fo:block margin-right="auto" margin-left="auto" background-color="#eaeaea">
     <fo:table margin-top="1cm" margin-left="auto" margin-right="auto" margin-bottom="1cm" width="auto">

And this is the output:

enter image description here

How can I center this table in this block?

Thank you.

Answer

G. Ken Holman picture G. Ken Holman · Aug 28, 2013

Per the specification, a <table> is centred by using text-align="center" on a parent <table-and-caption> element. The <table-caption> sibling is optional and can be omitted such that the table is the only child.

Note this will not work by putting text-align on a parent <block> ... a child <table> is still a block-level construct and is not affected. It has to be on the parent <table-and-caption>.

I remind my XSL-FO students that they likely will want a text-align="start" on their <table> unless they also want the contents of the table to be centred due to the inheritance of the property on descendant constructs.

I should note a postscript based on my commercial work that not all XSL-FO processors support the specification in this regard.