CSS: CENTERING THINGS

Centering lines of text

P { text-align: center }
H2 { text-align: center }

Centering a block of text or an image

P.blocktext {
    margin-left: auto;
    margin-right: auto;
    width: 6em
}
...
<P class="blocktext">This rather...
IMG.displayed {
    display: block;
    margin-left: auto;
    margin-right: auto }
...
<IMG class="displayed" src="..." alt="...">

Centering a block or an image vertically

DIV.container {
    min-height: 10em;
    display: table-cell;
    vertical-align: middle }
...
<DIV class="container">
  <P>This small paragraph...
</DIV>

Ref: http://www.w3.org/Style/Examples/007/center

CSS and Preformatted

ผมลองค้นหา CSS ที่ใช้ร่วมกับ <pre> tag เพื่อนำมาใช้แสดงโค้ดของโปรแกรม ก็บังเอิญได้พบ CSS ที่ใช้แสดงผลดังนี้

.phpcode, pre {
  overflow: auto;
  padding-left: 15px;
  padding-right: 15px;
  font-size: 11px;
  line-height: 15px;
  margin-top: 10px;
  width: 93%;
  display: block;
  background-color: #eeeeee;
  color: #000000;
  max-height: 300px;
}

เราสามารถกำหนด white-space: pre; ให้กับแต่ละ element ได้เช่นกัน ดังตัวอย่างต่อไปนี้

.preElement{
  white-space: pre;
  font-weight: bold;
  color: navy;
  font-family: arial;
}

และสามารถกำหนดฟอนต์ให้กับ <pre> แท็กดังนี้

pre{
  font-weight: bold;
  color: navy;
  font-family: arial;
}