HTML/CSS/Box Model/padding
Содержание
- 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 Designing Instant Gratification
- 7 padding sets the size of the padding surrounding the inner box.
- 8 Box Model
- 9 A heading
how whitespace can make a page much easier to read
<source lang="html4strict">
<?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>
This example shows how whitespace can make a page much easier to read.
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 | 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 |
</body> </html>
</source>
margin collapse and nested elements with default padding
<source lang="html4strict">
<!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>
No margins, ey?
</body> </html> </source>
margin collapse with nested elements padding
<source lang="html4strict">
<!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>
this is a test.
</body> </html> </source>
No padding
<source lang="html4strict">
<!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>
This is a test. This is a test. This is a test. This is a test.
</body> </html> </source>
padding:0.25em; assigns one-quarter of the font size to padding (i.e., font-sizemultiplied by 0.25).
<source lang="html4strict">
<!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>
Designing Instant Gratification
This is a test.
</body> </html>
</source>
padding sets the size of the padding surrounding the inner box.
<source lang="html4strict">
<!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>
Box Model
</body> </html>
</source>
Padding shortcut "10px
<source lang="html4strict">
<!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>
This is the title
This is a text.
</body> </html>
</source>
Set margin and padding to 0
<source lang="html4strict">
<!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>
header 2
header 3
This is a text.
</body>
</html>
</source>
padding:75em 0
<source lang="html4strict">
<!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>
- Database
- Privacy
- Best
- Automation?
- Smart Choice
</body> </html>
</source>
Use padding shortcut
<source lang="html4strict">
<!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>
This is the title
This is a text.
</body> </html>
</source>
Using Container Attributes
<source lang="html4strict">
<!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>
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. this is a test. this is a test. this is a test.
</body>
</html>
</source>
Using padding on a parent element to avoid problems with margin widths
<source lang="html4strict">
<?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>
A heading
Some body text
</body> </html>
</source>
with padding
<source lang="html4strict">
<!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>
This is a test. This is a test. This is a test.
</body> </html></source>