Formatting Blocks

Contact Us or call 1-877-932-8228
Formatting Blocks

Formatting Blocks

Although the output of our Winnie the Pooh story from the previous lesson is readable, it isn't formatted very nicely. In this section, we'll examine how we can use Cascading Style Sheet (CSS) properties to apply formatting to block FOs.

Box Model

Before we look at the specific attributes for formatting blocks, we'll take a look at a diagram that illustrates how width, border, padding and indent attributes all fit together.

The diagram shows two block elements in a block-container, which has a width of one inch. Each block contains a single word.

  • Padding is used to create one inch of space between the content and the edge of the box.
  • A one-centimeter border is then added to the edge of each block.
  • The bottom block is indented half an inch from the left.

Now let's look at some of the attributes of block FOs.

Border

By default, block FOs have no border; however, you can easily add one with the border-style and border-width attributes. You can also change the border color with the border-color attribute. As the table below shows, these properties can be applied to portions of the border as well through the use of more specific attributes.

Border Attributes
Attribute Values
border-width
  • <length> - A non-negative length
  • medium
  • thick
  • thin
border-top-width Same as border-width.
border-bottom-width Same as border-width.
border-left-width Same as border-width.
border-right-width Same as border-width.
border-color <color>
border-top-color <color>
border-bottom-color <color>
border-left-color <color>
border-right-color <color>
border-style
  • dashed
  • dotted
  • double
  • groove
  • hidden
  • inset
  • none
  • outset
  • ridge
  • solid
border-top-style Same as border-style.
border-bottom-style Same as border-style.
border-left-style Same as border-style.
border-right-style Same as border-style.

The diagrams below show the output of border-width and border-style attributes.

Space, Indent and Padding

Space attributes are used to separate FOs from other FOs on the page.

Indent attributes are used to indent FOs from the left and right borders of their containers.

Padding attributes are used to separate the content of FOs from the borders or edges of the block.

Space Attributes
Attribute Values
space-before <length> - A non-negative length
space-after <length> - A non-negative length

The values of space attributes are not cumulative. Notice that the space between the first two blocks is the same (10px) as the space between the second block, which wants 10px of space between it and the following block, and third block, which wants 10px of space between it and the preceding block.

Indent Attributes
Attribute Values
start-indent
  • <length> - A non-negative length
  • <percentage> - A percentage of the containing block
end-indent Same as start-indent

Indents can be specified in length (e.g, 30px) or percentage of the containing block (e.g, 50%).

Padding is specified in length and is used to create a buffer between the content and the border or edge of a block.

Padding Attributes
Attribute Values
padding <length> - A non-negative length
padding-top Same as padding
padding-bottom Same as padding
padding-left Same as padding
padding-right Same as padding

Background

Background attributes are used to set the background properties of a FO.

Background Attributes
Attribute Values
background-color color
background-image path to image
background-position-vertical
  • <length> - A non-negative length
  • <percentage> - n% from the top of the image is n% from the top of the containing area
  • top
  • center
  • bottom
background-position-horizontal
  • <length> - A non-negative length
  • <percentage> - n% from the left of the image is n% from the left of the containing area
  • left
  • center
  • right
background-repeat
  • no-repeat- doesn't tile
  • repeat-x - tiles horizontally
  • repeat-y - tiles vertically
  • repeat - tiles horizontally and vertically

A Formatting Example

The following example shows how some of these attributes can be applied to our "Hello-World" example.

Code Sample:

BlocksAndInlines/Demos/HelloWorld2.fo
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

	<fo:layout-master-set>
		<fo:simple-page-master master-name="hello" page-height="11in" 
			page-width="8.5in" margin-top="1in" margin-bottom="1in" 
			margin-left="1in" margin-right="1in">
			<fo:region-body margin-top="1in" margin-bottom=".5in"/>
			<fo:region-before extent=".5in" background-color="silver"/>
			<fo:region-after extent=".5in" background-color="silver"/>
		</fo:simple-page-master>
	</fo:layout-master-set>
	
	<fo:page-sequence master-reference="hello">
		<fo:static-content flow-name="xsl-region-before">
			<fo:block font-size="24pt">Hello World!</fo:block>
		</fo:static-content>
		<fo:static-content flow-name="xsl-region-after">
			<fo:block font-size="24pt" text-align="right">
				Page <fo:page-number/> of <fo:page-number-citation ref-id="last-page"/>
			</fo:block>
		</fo:static-content>
		<fo:flow flow-name="xsl-region-body">	
			<fo:block font-size="36pt" font-weight="bold" text-align="center"
					space-before="2in" space-after="1in"
					start-indent="1.5in" end-indent="1.5in"
					border-color="red" border-style="solid" border-width="1mm"
					padding="1cm" background-color="#aaaaff">
				Hello World!
			</fo:block>
			<fo:block background-color="red">
				Goodbye!
			</fo:block>
			<fo:block id="last-page"/>
		</fo:flow>
	</fo:page-sequence>
	
</fo:root>

The example above will render as follows.

Formatting Text

Font and text formatting can be applied at the block level with the following attributes.

Text Formatting Attributes
Attribute Values
font-family
  • font name
  • cursive
  • fantasy
  • monospace
  • sans-serif
  • serif
font-size
  • length
font-style
  • normal
  • italic
font-variant
  • normal
  • small-caps
font-weight
  • normal
  • bold
  • bolder
  • lighter
color
  • color
text-decoration
  • none
  • underline
  • overline
  • line-through

The example below shows some of these attributes in effect.

Code Sample:

BlocksAndInlines/Demos/HelloWorld3.fo
<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

	<fo:layout-master-set>
		<fo:simple-page-master master-name="hello" page-height="11in" 
			page-width="8.5in" margin-top="1in" margin-bottom="1in" 
			margin-left="1in" margin-right="1in">
			<fo:region-body margin-top="1in" margin-bottom=".5in"/>
			<fo:region-before extent=".5in" background-color="silver"/>
			<fo:region-after extent=".5in" background-color="silver"/>
		</fo:simple-page-master>
	</fo:layout-master-set>
	
	<fo:page-sequence master-reference="hello">
		<fo:static-content flow-name="xsl-region-before">
			<fo:block font-size="24pt">Hello World!</fo:block>
		</fo:static-content>
		<fo:static-content flow-name="xsl-region-after">
			<fo:block font-size="24pt" text-align="right">
				Page <fo:page-number/> of <fo:page-number-citation ref-id="last-page"/>
			</fo:block>
		</fo:static-content>
		<fo:flow flow-name="xsl-region-body">
			<fo:block font-size="18pt" font-style="italic">
				Hello World!
			</fo:block>
			<fo:block font-size="18pt" font-family="monospace">
				Hello World!
			</fo:block>
			<fo:block font-size="18pt" font-weight="bold">
				Hello World!
			</fo:block>
			<fo:block font-size="18pt" font-variant="small-caps">
				Hello World!
			</fo:block>
			<fo:block font-size="18pt" text-decoration="line-through">
				Hello World!
			</fo:block>
			<fo:block id="last-page"/>
		</fo:flow>
	</fo:page-sequence>
	
</fo:root>

The example above will render as follows. Note that only the top left quarter of the page is shown.

Next