一个定位和居中问题的探讨

本文章根据IFE(百度前端技术学院)第一阶段任务四总结整理

任务四:定位和居中问题

实现效果图

这里写图片描述

HTML代码:

    <div class="container">
        <div class="top-left-round"></div>
        <div class="bottom-right-round"></div>
    </div>

CSS代码一:

   * {
        margin: 0;
        padding: 0;
    }
    .container{
        width:400px;
        height:200px;
        background:#ccc;
        position:absolute;
        top:50%;
        left:50%;
        margin:-100px 0 0 -200px;
    }
    .top-left-round{
        width:50px;
        height:50px;
        border-bottom-right-radius:50px;
        background:#fc0;
    }
    .bottom-right-round{
       width:50px;
        height:50px;
        border-top-left-radius:50px;
        background:#fc0;
        float:right;
        position:relative;
        bottom:-50%;
        right:0;    
    }

CSS代码二:

    * {
        margin: 0;
        padding: 0;
    }
    .container {
        position: absolute;
        width: 400px;
        height: 200px;
        background-color: #ccc;
        top: 50%;
        margin-top: -100px;
        left: 50%;
        margin-left: -200px;
    }
    .left-top-round {
        width: 50px;
        height: 50px;
        border-radius: 0 0 50px 0;
        position: absolute;
        background-color: #FC0;
    }
    .right-bottom-round {
        width: 50px;
        height: 50px;
        border-radius: 50px 0 0 0;
        position: absolute;
        background-color: #FC0;
        right: 0;
        bottom: 0;
    }

CSS代码三:当在元素不定宽高的情况下,即随着浏览器窗口的变化,宽高也随之改变。

  *{margin:0; padding:0;}
    .container{
        background: #ccc; 
        position:absolute; 
        position: absolute;
        top: 25%;
        left: 25%;
        bottom:25%;
        right: 25%;
    }
    .top-left-round{
        width: 50px;
        height: 50px; 
        background: #fc0;
        border-radius: 0 0 100% 0;
        position: absolute;
        left: 0; top: 0;
    }
    .right-bottom-round{
        width: 50px; 
        height: 50px; 
        background: #fc0;
        border-radius: 100% 0 0 0; 
        position: absolute;
        bottom:0;right:0;
    }

补充实现水平居中,垂直居中定位的几种方法

  • position方法:见CSS1及CSS2;

  • Flex布局方法:

 *{
      padding:0;
      margin:0;
    }
      html,body{
      width: 100%;
      height: 100%;
    }
    .container{
       width:100%;
       height:100%;     
       display: -webkit-flex;
       display: flex;
       justify-content:center;
       align-items:center;
    }
  • Table方法:
    *{
        padding:0;
        margin:0;
     }
    html,body{
            width: 100%;
            height: 100%;
     }
    .wrap{
        width: 100%;
        height:100%;
        display: table;
    }
        .content{
            display: table-cell;
        vertical-align: middle;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值