SMF & Programmer > Programmer

จะกำหนดให้กล่องแสดงอยู่กลางหน้าจอได้อย่างไร ? [css]

(1/1)

Admin:
layout แบบหนึ่งที่นิยมใช้กัน คืือการใช้กล่องที่มีขนาดตายตัวซึ่งแสดงอยู่กลางหน้าจอในการแสดง content

เราสามารถใช้ css ทำำให้กล่องที่มีขนาดตายตัวแสดงอยู่กลางหน้าจอได้ด้วยการกำหนดค่า properties margin สำหรับด้านซ้ายและด้านขวาให้มีค่าเป็น auto ดังนี้

index.html

--- โค๊ด: ---<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<link rel="stylesheet" type="text/css" href="center.css" />
</head>
<body>
<div id="content">
<p>จะกำหนดให้กล่องแสดงอยู่กลางหน้าจอได้อย่างไร</p>
</div>
</body>
</html>

--- End code ---

center.css

--- โค๊ด: ---body {
background-color: #000000;
color: #000000;
text-align: center;
}
#content {
width 630px;
margin-left: auto;
margin-right: auto;
border: 2px solid #000000;
background-color: #00000;
color: #000000;
padding: 0 20px 0 20px;
text-align: left;
}

--- End code ---

นำร่อง

[0] ดัชนีข้อความ

Go to full version