目录
方法二:给子元素2设置display: inline-block
编辑 方法四:给其中一个子元素添加一个父元素,将子元素的margin值修改为父元素的pading值
margin塌陷和margin重叠都是垂直方向上的,水平方向没有这些问题。
- <template>
- <div>
- <div class="title-wrap">不设置margin-top值</div>
- <div class="parent-wrap">
- <div class="child-wrap"></div>
- </div>
- <div class="title-wrap">设置margin-top值</div>
- <div class="parent-wrap">
- <div class="child2-wrap" ></div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .title-wrap {
- width: 300px;
- border: 1px solid gray;
- padding: 5px 0;
- }
- .parent-wrap {
- background-color: aqua;
- width: 100px;
- height: 100px;
- }
- .child-wrap {
- width: 50px;
- height: 50px;
- background-color: blueviolet;
- }
- .child2-wrap {
- width: 50px;
- height: 50px;
- background-color: blueviolet;
- margin-top: 20px;
- }
- </style>
不对子元素设置margin-top时,父子元素都和title元素的底部边框线对齐,但是当我对子元素设置margin-top时(本意只想影响子元素),父子元素一起向下移动,都有了一定的间距。这种现象就是margin塌陷。
注意以下解决方法可能会引起副作用,所以在使用时应该根据实际情况进行选择。
- .child2-wrap {
- width: 50px;
- height: 50px;
- background-color: blueviolet;
- margin-top: 20px;
- float: left;
- }
- <template>
- <div class="container-wrap">
- <div class="parent-wrap">
- <div class="child2-wrap"></div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .container-wrap {
- margin-left: 10px;
- }
- .parent-wrap {
- background-color: aqua;
- width: 100px;
- height: 100px;
- float: left;
- }
-
- .child2-wrap {
- width: 50px;
- height: 50px;
- background-color: blueviolet;
- margin-top: 20px;
- }
- </style>
- .parent-wrap {
- background-color: aqua;
- width: 100px;
- height: 100px;
- display:inline-block;
- }
- <template>
- <div class="container-wrap">
- <div class="parent-wrap">
- <div class="child2-wrap"></div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .container-wrap {
- margin-left: 10px;
- }
- .parent-wrap {
- background-color: aqua;
- width: 100px;
- height: 100px;
- position: absolute;
- }
-
- .child2-wrap {
- width: 50px;
- height: 50px;
- background-color: blueviolet;
- margin-top: 20px;
- }
- </style>
- <template>
- <div class="container-wrap">
- <div class="parent-wrap">
- <div class="child2-wrap"></div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .container-wrap {
- margin-left: 10px;
- }
- .parent-wrap {
- background-color: aqua;
- width: 100px;
- height: 100px;
- overflow: hidden;
- }
-
- .child2-wrap {
- width: 50px;
- height: 50px;
- background-color: blueviolet;
- margin-top: 20px;
- }
- </style>
- .parent-wrap::before{
- display: table;
- content: '';
- }
- <template>
- <div class="container-wrap">
- <div class="parent-wrap">
- <!-- 空白元素 -->
- <div class="space-wrap"></div>
- <div class="child2-wrap"></div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .container-wrap {
- margin-left: 10px;
- }
- .parent-wrap {
- background-color: aqua;
- width: 100px;
- height: 100px;
- }
-
- .child2-wrap {
- width: 50px;
- height: 50px;
- background-color: blueviolet;
- margin-top: 19px;
- }
- /* 本意设置margin-top是20px,现在通过margin-top的19px和空白元素的height的1px实现20px的空白*/
- .space-wrap {
- width: 100%;
- height: 1px;
- }
- </style>
- <template>
- <div class="container-wrap">
- <div class="child1-wrap"></div>
- <div class="child2-wrap"></div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .container-wrap {
- margin-left: 10px;
- width: 110px;
- height: 500px;
- border: 1px solid gray;
- }
- .child1-wrap {
- width: 100px;
- height: 100px;
- background-color: aqua;
- margin-bottom: 200px;
- }
- .child2-wrap {
- width: 100px;
- height: 100px;
- background-color: blue;
- margin-top: 100px;
- }
- </style>
给子元素1设置了margin-bottom: 200px,给子元素2设置了margin-top: 100px,按理来说应该两个子元素上下间隔应该是300px,但是实际两个子元素的上下间隔只有200px。
这种兄弟元素垂直外边距重合,选取兄弟元素中设置的margin的最大值的现象就是margin重合。
- .child2-wrap {
- width: 100px;
- height: 100px;
- background-color: blue;
- margin-top: 100px;
- float: left;
- }
- .child2-wrap {
- width: 100px;
- height: 100px;
- background-color: blue;
- margin-top: 100px;
- display: inline-block;
- }
- <template>
- <div class="container-wrap">
- <div class="child1-wrap"></div>
- <div class="space-wrap"></div>
- <div class="child2-wrap"></div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .container-wrap {
- margin-left: 10px;
- width: 110px;
- height: 500px;
- border: 1px solid gray;
- }
- .child1-wrap {
- width: 100px;
- height: 100px;
- background-color: aqua;
- margin-bottom: 200px;
- }
- .child2-wrap {
- width: 100px;
- height: 100px;
- background-color: blue;
- margin-top: 99px;
- }
-
- .space-wrap {
- width: 100%;
- height: 1px;
- }
- </style>
- <template>
- <div class="container-wrap">
- <div class="child1-wrap"></div>
- <div class="parent2-wrap">
- <div class="child2-wrap"></div>
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "MarginDemo",
- };
- </script>
- <style scoped>
- .container-wrap {
- margin-left: 10px;
- width: 110px;
- height: 500px;
- border: 1px solid gray;
- }
- .child1-wrap {
- width: 100px;
- height: 100px;
- background-color: aqua;
- margin-bottom: 200px;
- }
- .parent2-wrap {
- padding-top: 100px;
- }
- .child2-wrap {
- width: 100px;
- height: 100px;
- background-color: blue;
- }
- </style>
给第二个子元素的父元素添加以下属性都可以解决问题:
1、 float: left;
2、 display: inline-block;
3、 overflow: hidden;
4、position: absolute;
- .container-wrap {
- margin-left: 10px;
- width: 110px;
- height: 500px;
- border: 1px solid gray;
- display: flex;
- flex-direction: column;
- }