1. margin 겹친 현상
- margin 겹친 현상은 상하 margin값이 어떤 상황에서 사리지는 상황을 의미
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
h1 {
border: 1px solid tomato;
margin: 100px;
}
</style>
</head>
<body>
<h1>Hello World</h1>
<h1>Hello World</h1>
</body>
</html>
2. margin의 적용 원리
- 움직일 수 없는 요소를 밀어내어 스스로의 위치를 변경시키거나 다른 요소를 밀어내는 특성을 갖기 때문에 위치와 관련된 속성
- 바꾸어 말하면 margin 속성은 요소의 위치를 변경시키면서 주변 요소에게 영향을 주게 됨
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div#box1 {
background-color: tomato;
width: 200px;
height: 100px;
margin: -30px;
}
div#box2 {
background-color: powderblue;
width: 150px;
height: 100px;
}
</style>
</head>
<body>
<div id="box1">박스1</div>
<div id="box2">박스2</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0px;
padding: 0px;
}
#menu {
width: 100%;
height: 200px;
background: tomato;
margin-top: -180px;
}
#menu:hover {
margin-top: 0px;
background-color: orange;
transition: all 0.3s;
}
</style>
</head>
<body>
<div id="menu">메뉴 영역</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div#parent {
background-color: tomato;
width: 800px;
height: 500px;
border: 5px solid red;
margin: 0 auto;
}
div#child {
background-color: powderblue;
padding: 10px;
border: 5px solid blue;
width: 500px;
margin: auto;
}
</style>
</head>
<body>
<div id="parent">
<div id="child">박스2</div>
</div>
</body>
</html>