父子div元素水平垂直居中的七种方法

效果样式图如下:

在这里插入图片描述

方法一:利用定位和transform的方法来进行居中

说明:首先利用定位中的子元素绝对定位和父元素相对定位的方法来,top:50% 和left:50%会使子元素在父元素中向下移动250px,向右移动250px,子元素因自身有高度和宽度,这会导致子元素不能完全居中的情况,transform中的translate属性可以使子元素以自身为中心向上移动和向左移动分别自身的高度,以达到垂直水平居中的效果

<style>
    .box1{
        width: 500px;
        height: 500px;
        background-color: antiquewhite;
        position: relative;
    }
    .box2{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
        position: absolute;
        top: 50%;  
        left: 50%;
        transform: translate(-50%,-50%);
    }
</style>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

方法二:计算水平垂直居中的距离来定位元素

说明:方法二与方法一不同之处在于需计算子元素和父元素的距离,通过高度和宽度可知子元素需要向下和向右移动150px,利用30%(150px/500px)来进行定位,当然可以利用top和left:150px也可以水平垂直居中

<style>
    .box1{
        width: 500px;
        height: 500px;
        background-color: antiquewhite;
        position: relative;
}
    .box2{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
        position: absolute;
        top: 30%;  
        left: 30%;
        
    }
</style>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>

方法三:利用外边距margin设置数值和top方法

说明: 与方法一不同之处在于利用外边距来移动子元素的位置,margin-top和margin-left的值分别为子元素自身高度和宽度的一半的负值

<style>
    .box1{
        width: 500px;
        height: 500px;
        background-color: antiquewhite;
        position: relative;
}
    .box2{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -100px;
        margin-left: -100px;
        
    }
</style>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

方法四:利用top和margin为auto的方法

说明:当分别将top,bottom,left,right设置为0时,子元素因自身的宽度和高度不足,会在父元素上的初始位置显示,但设置margin为auto,会分别将子元素在父元素中的边距设置为水平垂直居中的距离

<style>
    .box1{
        width: 500px;
        height: 500px;
        background-color: antiquewhite;
        position: relative;
}
    .box2{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        
        
    }
</style>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

方法五:利用flex布局的方法(建议使用)

说明:当将box1设置为弹性盒子时,box2就会脱离文档流.利用justify-content和align-items设置为center就可以水平垂直居中.flex布局无需计算居中时的距离

<style>
    .box1{
        width: 500px;
        height: 500px;
        background-color: antiquewhite;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .box2{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
    }
</style>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

方法六:隔离父子元素来设置边距margin

说明:在单独给box2设置边距的情况下,会出现边距重叠的问题.可以利用box1前面添加一个空table来分离box1和box2,这样就可以单独给子元素box2设置外边距来达到垂直水平居中的效果.

<style>
    .box1{
        width: 500px;
        height: 500px;
        background-color: antiquewhite;
    }
    .box2{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
        margin-top: 150px;
        margin-left: 150px;
    }
    .box1::before{
        content: "";
        display: table;
    }
</style>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>

方法七:将父元素转化为table-cell类型

说明:将父元素box1转化为table-cell类型,并设置vertical-align为居中状态可以达到水平居中的效果.如果子元素为div等块级元素需转化为inline-block类型,设置text-align为居中状态可以达到垂直居中的效果

<style>
    .box1{
        width: 500px;
        height: 500px;
        background-color: antiquewhite;
        display: table-cell;
        vertical-align: middle;
        text-align: center;
    }
    .box2{
        width: 200px;
        height: 200px;
        background-color: aquamarine;
        display: inline-block;
    }
 
</style>
<body>
    <div class="box1">
        <div class="box2"></div>
    </div>
</body>
</html>
  • 13
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
1. 组件向子组件传值,使用 props 属性 组件: ``` <template> <div> <child-component :message="message"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { message: 'Hello, World!' } } } </script> ``` 子组件: ``` <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { props: ['message'] } </script> ``` 2. 子组件向组件传值,使用 $emit 方法 组件: ``` <template> <div> <child-component @send-message="handleMessage"></child-component> <p>{{ receivedMessage }}</p> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { receivedMessage: '' } }, methods: { handleMessage(message) { this.receivedMessage = message; } } } </script> ``` 子组件: ``` <template> <div> <button @click="sendMessage">Send Message</button> </div> </template> <script> export default { methods: { sendMessage() { this.$emit('send-message', 'Hello, World!'); } } } </script> ``` 3. 使用 provide/inject 传递数据 组件: ``` <template> <div> <child-component></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, provide() { return { message: 'Hello, World!' } } } </script> ``` 子组件: ``` <template> <div> <p>{{ message }}</p> </div> </template> <script> export default { inject: ['message'] } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Double-C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值