HTML/CSS/Box Model/padding
Версия от 09:21, 26 мая 2010; (обсуждение)
Содержание
- 1 how whitespace can make a page much easier to read
- 2 margin collapse and nested elements with default padding
- 3 margin collapse with nested elements padding
- 4 No padding
- 5 padding:0.25em; assigns one-quarter of the font size to padding (i.e., font-sizemultiplied by 0.25).
- 6 padding sets the size of the padding surrounding the inner box.
- 7 Padding shortcut "10px
- 8 Set margin and padding to 0
- 9 padding:75em 0
- 10 Use padding shortcut
- 11 Using Container Attributes
- 12 Using padding on a parent element to avoid problems with margin widths
- 13 with padding
how whitespace can make a page much easier to read
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A CSS Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style rel="stylesheet" type="text/css">
table,th,td {
border: solid 2px #000000;
}
table {
margin: 15px;
}
th,td {
padding: 20px;
}
</style>
</head>
<body>
<p>This example shows how whitespace can make a page much easier to read. </p>
<table>
<tr>
<td>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit</td>
<td>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit</td>
</tr>
</table>
<p></p>
</body>
</html>
margin collapse and nested elements with default padding
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
div {
margin: 20px;
padding: 20px;
background: #ddd;
}
p {
padding: 20px;
background: #ccc;
}
</style>
</head>
<body>
<div>
<p> No margins, ey?</p>
</div>
</body>
</html>
margin collapse with nested elements padding
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
div {
margin: 20px;
padding: 20px;
background: #ddd;
}
p {
margin: 0;
padding: 20px;
background: #ccc;
}
</style>
</head>
<body>
<div>
<p> this is a test. </p>
</div>
</body>
</html>
No padding
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
p {
border: thin solid black;
text-align: justify;
width: 150px;
}
</style>
</head>
<body>
<p>This is a test.
This is a test.
This is a test.
This is a test.
</p>
</body>
</html>
padding:0.25em; assigns one-quarter of the font size to padding (i.e., font-sizemultiplied by 0.25).
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
h2 {
font: bold italic 2em Georgia, Times, "Times New Roman", serif;
margin: 0;
padding:0.25em;
}
p {
margin: 0;
padding: 3em;
}
</style>
</head>
<body>
<h2>Designing Instant Gratification</h2>
<p>This is a test.</p>
</body>
</html>
padding sets the size of the padding surrounding the inner box.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
* .box {
display: static;
overflow: visible;
visibility: visible;
width: 160px;
height: 150px;
padding: 30px;
margin-left: 230px;
margin-top: 80px;
background-color: #ccc;
}
</style>
</head>
<body>
<h1>Box Model</h1>
<div class="box"></div>
</body>
</html>
Padding shortcut "10px
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
p {
margin: 0;
padding: 10px 0 0 0;
}
</style>
</head>
<body>
<h2>This is the title</h2>
<p>This is a text.</p>
</body>
</html>
Set margin and padding to 0
<!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" lang="en">
<head>
<title></title>
<style type="text/css">
h3 {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<h2>header 2</h2>
<h3>header 3</h3>
<p>This is a text.</p>
</body>
</html>
padding:75em 0
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
ul {
width: 30%;
padding: 0 0 0.75em 0;
margin: 0;
list-style: none;
}
li {
text-indent: -0.75em;
margin: 0.33em 0.5em 0.5em 1.5em;
}
</style>
</head>
<body>
<ul>
<li>Database</li>
<li>Privacy</li>
<li>Best</li>
<li>Automation?</li>
<li class="last">Smart Choice</li>
</ul>
</body>
</html>
Use padding shortcut
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
h2 {
padding: 0.5em 0 0.5em 0;
}
</style>
</head>
<body>
<h2>This is the title</h2>
<p>This is a text.</p>
</body>
</html>
Using Container Attributes
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Using Container Attributes</title>
<style type="text/css" rel="stylesheet">
p {
border: thin outset gray;
margin: 10px;
padding: 1em;
}
</style>
</head>
<body>
<p>
this is a test. this is a test. this is a test.
</p>
<p>
this is a test. this is a test. this is a test. this is a test.
this is a test. this is a test. this is a test. this is a test.
this is a test. this is a test. this is a test. this is a test.
this is a test. this is a test. this is a test. this is a test.
</p>
</body>
</html>
Using padding on a parent element to avoid problems with margin widths
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Using padding on a parent element to avoid problems with margin widths</title>
<style type="text/css">
div {
padding: 0 0 0 2em;
}
p {
font-size: 1em;
margin: 0;
}
h1 {
font-size: 1.5em;
margin: 0;
}
</style>
</head>
<body>
<div>
<h1>A heading</h1>
<p>Some body text</p>
</div>
</body>
</html>
with padding
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
div {
padding: 10px;
margin: 10px;
border: thin solid black;
}
</style>
</head>
<body>
<div style="width: 100px; height: 100px;">
This is a test.
This is a test.
This is a test.
</div>
</body>
</html>