HTML/CSS/Box Model/Box Layout
Содержание
- 1 Horizontally Stretched Absolute
- 2 Horizontally Stretched Static
- 3 Indent the left and right sides of a static element
- 4 Indent up and down with margin
- 5 Move right and left with margin
- 6 Offset Absolute and Offset Fixed
- 7 Parent position: absolute, child position fixed or absolute
- 8 Parent position: absolute, child position: relative or static
- 9 Parent position: relative, child position fixed or absolute
- 10 Positioned parent and non-position child
- 11 Relative positioned container with absolute positioned child to the bottom and left
- 12 Relative positioned container with absolute positioned child to the bottom and right
- 13 Relative positioned container with absolute positioned child to the top and left
- 14 Relative positioned container with absolute positioned child to the top and right
- 15 Shrinkwrapped Absolute
- 16 Shrinkwrapped Relative
- 17 Sized Absolute
- 18 Sized Block Outside to the Left of its parent
- 19 Sized Block Outside to the Right of its parent
- 20 Sized Float Outside Left to its parent
- 21 Sized Float Outside to the Right of its parent
- 22 Sized relative
- 23 Static container and absolute child to top and left
- 24 Static positioned container with child to the bottom and left
- 25 Static positioned container with child to the bottom and right
- 26 Static postioned container and absolute child to the top and right
- 27 Vertically Stretched Absolute
Horizontally Stretched Absolute
<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"> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen">
- .grandContainer {
position: relative; z-index: 10; background: red;
}
- hsa {
position: absolute; width: auto; left: 0; margin-left: -30px; right: 0; margin-right: -30px; height: auto; top: auto; margin-top: 30px; bottom: auto; margin-bottom: auto; background: gold;
} </style> </head> <body>
Horizontally Stretched Absolute
</body> </html>
</source>
Horizontally Stretched Static
<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"> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen">
- .grandContainer {
position: relative; z-index: 10; background: red;
}
- hss {
position: static; width: auto; margin-left: 30px; margin-right: 30px; height: auto; margin-top: auto; margin-bottom: 20px; background: pink;
} </style> </head> <body>
</body> </html>
</source>
Indent the left and right sides of a static element
<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"> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen">
- .grandContainer {
position: relative; z-index: 10; background: red;
}
- hsa {
position: static; width: auto; left: 0; margin-left: -30px; right: 0; margin-right: -30px; height: auto; top: auto; margin-top: 30px; bottom: auto; margin-bottom: auto; background: gold;
} </style> </head> <body>
Horizontally Stretched Absolute
</body> </html>
</source>
Indent up and down with margin
<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"> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen">
- .moved-left {
margin-left: -26px; background: red;
}
- .push-right {
margin-right: 50px; background: gold;
}
- .moved-right {
margin-left: 50px; background: yellow;
}
- .pull-left {
margin-right: -20px; background: blue; color: white;
}
- .moved-down {
margin-top: 20px; background: red;
}
- .push-down {
margin-bottom: 20px; background: gary;
}
- .moved-up {
margin-top: -13px; background: yellow;
}
- .pull-up {
margin-bottom: -16px; background: pink;
} </style> </head> <body>
</body> </html>
</source>
Move right and left with margin
<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"> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen">
- .moved-left {
margin-left: -26px; background: red;
}
- .push-right {
margin-right: 50px; background: gold;
}
- .moved-right {
margin-left: 50px; background: yellow;
}
- .pull-left {
margin-right: -20px; background: blue; color: white;
} </style> </head> <body>
Moved-left Moved-right Push-right Pull-left None
</body> </html>
</source>
Offset Absolute and Offset Fixed
<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"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen"> .grandContainer {
width: 800px; height: 800px; background: red;
}
- absolute {
position: absolute; width: 140px; height: auto; background: gold;
}
- fixed {
position: fixed; height: 50px; margin-top: 10px; width: auto; margin-left: 10px; background: yellow;
} </style> </head> <body>
Absolute
outside spanFixed
</div> </body> </html>
</source>
Parent position: absolute, child position fixed or absolute
<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"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen"> .grandContainer {
width: 800px; height: 800px; background: red; position: absolute;
}
- absolute {
position: absolute; width: 140px; height: auto; background: gold;
}
- fixed {
position: fixed; height: 50px; margin-top: 10px; width: auto; margin-left: 10px; background: yellow;
} </style> </head> <body>
Absolute
outside spanFixed
</div> </body> </html>
</source>
Parent position: absolute, child position: relative or static
<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"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen"> .grandContainer {
width: 800px; height: 800px; background: red; position: absolute;
}
- absolute {
position: relative; width: 140px; height: auto; background: gold;
}
- fixed {
position: static; height: 50px; margin-top: 10px; width: auto; margin-left: 10px; background: yellow;
} </style> </head> <body>
Absolute
outside spanFixed
</div> </body> </html>
</source>
Parent position: relative, child position fixed or absolute
<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"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen"> .grandContainer {
width: 800px; height: 800px; background: red; position: relative;
}
- absolute {
position: absolute; width: 140px; height: auto; background: gold;
}
- fixed {
position: fixed; height: 50px; margin-top: 10px; width: auto; margin-left: 10px; background: yellow;
} </style> </head> <body>
Absolute
outside spanFixed
</div> </body> </html>
</source>
Positioned parent and non-position child
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .parent {
position: relative; height: 295px; width: 600px; border: 2px solid black;
}
- .child{
margin: 10px; padding: 10px; padding-top: 0; border: 1px solid black;
} </style> </head> <body>
</body> </html>
</source>
Relative positioned container with absolute positioned child to the bottom and left
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: relative; width: 300px; height: 300px; margin: auto; background: red;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: yellowgreen; bottom: 0; left: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Relative positioned container with absolute positioned child to the bottom and right
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: relative; width: 300px; height: 300px; margin: auto; background: red;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: orange; bottom: 0; right: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Relative positioned container with absolute positioned child to the top and left
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: relative; width: 300px; height: 300px; margin: auto; background: red;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: pink; top: 0; left: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Relative positioned container with absolute positioned child to the top and right
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: relative; width: 300px; height: 300px; margin: auto; background: red;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: lightblue; top: 0; right: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Shrinkwrapped Absolute
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .grandContainer {
position: relative; height: 295px; width: 600px; border: 2px solid black;
}
- .parent {
margin: 10px; padding: 10px; padding-top: 0; border: 1px solid black;
}
- .rumon {
padding: 5px; border: 5px solid black; background-color: gold;
}
- myStatic {
position: static; text-align: left; margin-top: 5px; width: auto; margin-left: 0; margin-right: 0; background: yellow;
}
- wa {
position: absolute; text-align: left; top: 0; margin-top: 200px; width: auto; left: 0; margin-left: 0; right: auto; margin-right: auto; background: red;
} </style> </head> <body>
Shrinkwrapped Absolute
</body> </html>
</source>
Shrinkwrapped Relative
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .grandContainer {
position: relative; height: 295px; width: 600px; border: 2px solid black;
}
- .parent {
margin: 10px; padding: 10px; padding-top: 0; border: 1px solid black;
}
- .rumon {
padding: 5px; border: 5px solid black; background-color: gold;
}
- myStatic {
position: static; text-align: left; margin-top: 5px; width: auto; margin-left: 0; margin-right: 0; background: yellow;
}
- wa {
position: relative; text-align: left; top: 0; margin-top: 200px; width: auto; left: 0; margin-left: 0; right: auto; margin-right: auto; background: red;
} </style> </head> <body>
Shrinkwrapped Relative
</body> </html>
</source>
Sized Absolute
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .grandContainer {
position: relative; height: 295px; width: 600px; border: 2px solid black;
}
- .parent {
margin: 10px; padding: 10px; padding-top: 0; border: 1px solid black;
}
- .rumon {
padding: 5px; border: 5px solid black; background-color: gold;
}
- myStatic {
position: static; text-align: left; margin-top: 5px; width: auto; margin-left: 0; margin-right: 0; background: yellow;
}
- myAbsolute {
position: absolute; text-align: left; top: 0; margin-top: 155px; width: 400px; left: 0; margin-left: 0; right: auto; margin-right: auto; background: red;
} </style> </head> <body>
</body> </html>
</source>
Sized Block Outside to the Left of its parent
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .parent {
left: 300px; top: 300px; position: relative; height: 140px; width: 200px; background: black;
}
- .negativeMargin {
width: 220px; margin-left: -234px; background: red;
} </style> </head> <body>
Sized Block Outside Left
</body> </html>
</source>
Sized Block Outside to the Right of its parent
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .parent {
left: 300px; top: 300px; position: relative; height: 140px; width: 200px; background: black;
}
- .fullMargin {
width: 220px; margin-left: 100%; background: red;
} </style> </head> <body>
Sized Block Outside Right
</body> </html>
</source>
Sized Float Outside Left to its parent
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .parent {
left: 300px; top: 300px; position: relative; height: 140px; width: 200px; background: black;
}
- .negativeMargin {
width: 220px; margin-left: -234px; float: left; background: red;
}
</style> </head> <body>
Sized Float Outside Left
</body> </html>
</source>
Sized Float Outside to the Right of its parent
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .parent {
left: 300px; top: 300px; position: relative; height: 140px; width: 200px; background: black;
}
- .fullMargin {
width: 220px; margin-left: 100%; float: left; background: red;
}
</style> </head> <body>
Sized Float Outside Right
</body> </html>
</source>
Sized relative
<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>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title>
<style type="text/css" title="text/css">
- .grandContainer {
position: relative; height: 295px; width: 600px; border: 2px solid black;
}
- .parent {
margin: 10px; padding: 10px; padding-top: 0; border: 1px solid black;
}
- .rumon {
padding: 5px; border: 5px solid black; background-color: gold;
}
- myStatic {
position: static; text-align: left; margin-top: 5px; width: auto; margin-left: 0; margin-right: 0; background: yellow;
}
- myAbsolute {
position: relative; text-align: left; top: 0; margin-top: 155px; width: 400px; left: 0; margin-left: 0; right: auto; margin-right: auto; background: red;
} </style> </head> <body>
</body> </html>
</source>
Static container and absolute child to top and left
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: static; width: 300px; height: 300px; margin: auto;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: pink; top: 0; left: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Static positioned container with child to the bottom and left
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: static; width: 300px; height: 300px; margin: auto;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: yellowgreen; bottom: 0; left: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Static positioned container with child to the bottom and right
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: static; width: 300px; height: 300px; margin: auto;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: orange; bottom: 0; right: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Static postioned container and absolute child to the top and right
<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>positioning</title> <style rel="stylesheet" type="text/css">
body {
background: lightyellow;
} div#container {
position: static; width: 300px; height: 300px; margin: auto;
} div#one {
width: 100px; height: 100px; border: 1px solid rgb(200, 200, 200); position: absolute; background: lightblue; top: 0; right: 0;
}
</style> </head> <body>
</body>
</html>
</source>
Vertically Stretched Absolute
<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"> <head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css" media="Screen">
- .grandContainer {
position: relative; z-index: 10; background: red;
}
- vsa {
position: absolute; width: 120px; left: auto; margin-left: auto; right: 0; margin-right: 70px; height: auto; top: 0; margin-top: -30px; bottom: 0; margin-bottom: -30px; background: yellow;
} </style> </head> <body>
</body> </html>
</source>