How to center table in xsl-fo?
Asked Answered
T

4

7

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.

Tantalate answered 28/8, 2013 at 11:59 Comment(4)
I am not familiar with xsl-fo but would align="center" inside the tags not work?Pinna
I tried: <fo:table margin-top="1cm" margin-left="auto" margin-right="auto" margin-bottom="1cm" width="auto" display-align="center"> but did not work. @SimonStatonTantalate
Which XSL-FO processor are you using?Gwinn
Do you use FOP? If so, this might help: xmlgraphics.apache.org/fop/fo.html#fo-center-table-horizon.Minuscule
O
9

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.

Outguard answered 28/8, 2013 at 13:23 Comment(4)
Thanks for your answer. When I add table-caption like: <fo:table-and-caption text-align="center"><fo:table margin-top="1cm" text-align="start"> my table gets totally lost.. Why might this be?Tantalate
This seems to be related to this question which is still unanswered.Gwinn
My guess is that your processor is non-conformant. I use a conformant processor to solve my cusotmers' problems. Good luck ... my guess is that you may be out of options.Outguard
FOP does not support <fo:table-and-caption>. See xmlgraphics.apache.org/fop/…Tannen
C
2

If using Apache FOP, they explain a table centering technique here: https://xmlgraphics.apache.org/fop/fo.html#fo-center-table-horizon

Chapiter answered 5/3, 2015 at 17:26 Comment(3)
What is the difference between xsl-fo and Apache Fop?Tantalate
@KorayTugay as I just found out, Apache FOP does not support 100% of xsl-fo properties: xmlgraphics.apache.org/fop/compliance.htmlObliterate
This technique does work, but it's an unpleasant hack to nest the table you want inside another one that controls the horizontal layout.Bluma
O
1

If we have ten columns then we can center table as follows

<fo:table width="100%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
  <fo:table-column column-width="10%">
 </fo:table>
Outfitter answered 20/3, 2018 at 9:23 Comment(1)
That doesn't center the table, it just makes it as wide as the enclosing block (usually the text column).Arran
T
-1

you can simply add styles right to the xsl page!

Townie answered 29/9, 2021 at 19:13 Comment(2)
learn.microsoft.com/en-us/previous-versions/windows/desktop/…Townie
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Electro

© 2022 - 2024 — McMap. All rights reserved.