css布局

作者: 心安是归处_91b1 | 来源:发表于2019-04-18 03:38 被阅读0次

1.左右布局

左侧 div 设置 float 属性为 left,右侧 div 设置 margin-left 属性等于或大于左侧 div 宽度

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>左右布局</title>
  <style>
    html, body {
      margin: 0;
      padding: 0;
    }
 
    .left {
      float: left;
      width: 300px;
      height: 300px;
      background: #bfbfbf;
    }
 
    .right {
      margin-left: 310px;
      height: 300px;
      background: #efefef;
    }
  </style>
</head>
<body>
<p>1 左侧 DIV 设置 float 属性为 left,右侧 DIV 设置 margin-left 属性等于或大于左侧 DIV 宽度</p>
<div class="left">left</div>
<div class="right">right</div>
 
</body>
</html>

2.左中右布局

左右两侧分别使用float:left;float:right,中间位置用margin

            width: 200px;
            height:100%;
            float: left;
            background-color: #00a0dc;
        }
        .middle{
            height:100%;
            margin-left:200px;
            margin-right: 300px;
            background-color: red;
        }
        .right{
            height:100%;
            width: 300px;
            float: right;
            background-color: #00a0dc;
        }

3.水平居中

1、text-align

text-align:center; 行内元素(图片或文字)居中

在父元素上设置text-align: center 使文字/图片在整个页面上水平居中

 .container{
    display:inline-block;
    text-align:center;
}

2、margin

margin: 0 auto; 用于块级元素的居中

 <style>
     .wrap{
       max-width:600px;
       background: #ccc;
       margin: 0 auto;
     }
     
  </style>
</head>

  <div class="wrap">
    <h1>hello</h1>
    <p>world</p>
  </div>

4.垂直居中

绝对定位实现居中

#box {
    width: 300px;
    height: 300px;
    background: #ddd;
    position: relative;
}
#child {
    width: 150px;
    height: 100px;
    background: orange;
    position: absolute;
    top: 50%;
    margin: -50px 0 0 0; 
}

相关文章

网友评论

      本文标题:css布局

      本文链接:https://www.haomeiwen.com/subject/qiuawqtx.html