CSS布局&居中&媒体查询

作者: 好奇男孩 | 来源:发表于2018-03-21 14:05 被阅读25次

1.CSS布局

实现一个两栏布局,右侧固定宽度200px,左侧自适应宽度

浮动元素 + 普通元素margin

    <style>
  
        .layout::after{
            content: "";
            clear: both;
            display: block;
        }
        .aside{
            float: left;
            width: 200px;
            height: 400px;
            background-color: #f00;
        }
        .main{
            background-color: #00f;
            margin-left: 210px;
            height: 600px;
        }

    </style>
</head>
<body>
<div class="layout">
<div class="aside"></div>
<div class="main"></div>
</div>
</body>

实现一个三栏布局,两侧固定宽度200px,中间宽度自适应撑满

两侧两列固定宽度,中间列自适应宽度

    <style>
  
        .layout::after{
            content: "";
            clear: both;
            display: block;
        }
        .aside1{
            float: left;
            width: 200px;
            height: 400px;
            background-color: #f00;
        }
        .aside2{
            float: right;
            width: 200px;
            height: 400px;
            background-color: #00f;
        }
        .main{
            background-color: pink;
            margin-left: 210px;
            margin-right: 210px;
            height: 600px;
        }

    </style>
</head>
<body>
<div class="layout">
<div class="aside1"></div>
    <div class="aside2"></div>
<div class="main"></div>
</div>
</body>

2.居中

水平居中

  • text-align
    在父元素上设置 text-align: center 使文字/图片水平居中。
  • margin
.container {
  width: 80%;
  margin-left: auto;
  margin-right: auto;
}

块级元素固定宽度,水平居中

    <style>
        .layout{
            background-color: grey;
            height: 600px;
        }
        .content{
            width: 300px;
            height: 200px;
            margin: 0 auto;
            background-color: #f00;
        }
    </style>
</head>
<body>
<div class="layout">
    <div class="content">main</div>
</div>

垂直居中

  • pading
  • 绝对定位实现居中
  • vertical-align实现居中
  • table-cell实现居中

大段文字在容器内水平垂直居中

    <style>
        .layout{
            background-color: grey;
            text-align: center;
            padding: 40px 0;
        }

    </style>
</head>
<body>
<div class="layout">
    <h3 >这是标题</h3>
    <p >这是内容这是内容这是内容这是内容这是内容</p>
</div>
</body>

要求:图片在容器内水平垂直居中,容器宽高大于图片宽高

方法一:table-cell实现居中

    <style>
     .container
     {
         border: 1px solid red;
         width: 600px;
         height: 600px;
         text-align: center;
         display: table-cell;
         vertical-align: middle;
         /*margin: 0 auto;*/
     }
    </style>
</head>
<body>
<div class="container">
    <img src="http://ww1.sinaimg.cn/large/006JM2pKgy1fp6w57wxw6j305k05kt8l.jpg" alt=""/>
</div>

方法二:vertical-align实现居中

    <style>
     .container
     {
         border: 1px solid red;
         width: 600px;
         height: 600px;
         text-align: center;
         vertical-align: middle;
         margin: 0 auto;
     }
     .container::before{
         content:"";
         display: inline-block;
         height: 100%;
         vertical-align: middle;
     }
     .container>img{
         vertical-align: middle;
     }
    </style>

方法三:绝对定位实现居中

     .container
     {
         border: 1px solid red;
         width: 600px;
         height: 600px;
         position: relative;
         margin: 0 auto;
         
     }

     .container>img{
         position: absolute;
         left: 50%;
         top: 50%;
       /*   transform: translate(-50%,-50%); */
         margin-left: -50px;
         margin-top: -50px;
         width: 100px;
         height: 100px;
          
     }
    </style>

    <style>
     .container
     {
         border: 1px solid red;
         width: 600px;
         height: 600px;
         position: relative;
         margin: 0 auto;
         
     }

     .container>img{
         position: absolute;
         left: 50%;
         top: 50%;
       /*   transform: translate(-50%,-50%); */
         margin-left: -50px;
         margin-top: -50px;
         width: 100px;
         height: 100px;
          
     }
    </style>

固定宽高的块在浏览器窗口水平垂直居中

     .container
     {
         border: 1px solid red;
         position: fixed;
         background-color: #000;
         left: 50%;
         top: 50%;
        /*  transform: translate(-50%,-50%); */
        margin-left: -50px;
         margin-top: -50px; 
         width: 100px;
         height: 100px;
     }
    

要求: 不定宽高的块在浏览器窗口水平垂直居中

    <style>
     .container
     {
         text-align: center;
     }
     .container>p{
       position: fixed;
         top: 50%;
         left: 50%;
         transform: translate(-50%,-50%);
     }
    </style>
</head>
<body>
<div class="container">
    <p>这是内容这是内容这是内容这是内容</p>
</div>

3.@meida 媒体查询

根据媒体特征(设备类型、窗口大小、分辨率...)来执行相应的 CSS 代码

<!-- link元素中的CSS媒体查询 -->
<link rel="stylesheet" media="(max-width: 800px)" href="example.css" />

<!-- 样式表中的CSS媒体查询 -->
<style>
@media (max-width: 600px) {
  .facet_sidebar {
    display: none;
  }
}
</style>

栅格系统的实现

.col-xs-1, .col-xs-2, .col-xs-3, .col-xs-4, .col-xs-5, .col-xs-6, .col-xs-7, .col-xs-8, .col-xs-9, .col-xs-10, .col-xs-11, .col-xs-12 {
    float: left;
}
.col-xs-12 {
    width: 100%;
}
.col-xs-11 {
    width: 91.66666667%;
}
.col-xs-10 {
    width: 83.33333333%;
}
.col-xs-9 {
    width: 75%;
}
.col-xs-8 {
    width: 66.66666667%;
}
.col-xs-7 {
    width: 58.33333333%;
}
.col-xs-6 {
    width: 50%;
}
.col-xs-5 {
    width: 41.66666667%;
}
.col-xs-4 {
    width: 33.33333333%;
}
.col-xs-3 {
    width: 25%;
}
.col-xs-2 {
    width: 16.66666667%;
}
.col-xs-1 {
    width: 8.33333333%;
}





@media (min-width: 668px) {
    .col-sm-1, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-10, .col-sm-11, .col-sm-12 {
        float: left;
    }
    .col-sm-12 {
        width: 100%;
    }
    .col-sm-11 {
        width: 91.66666667%;
    }
    .col-sm-10 {
        width: 83.33333333%;
    }
    .col-sm-9 {
        width: 75%;
    }
    .col-sm-8 {
        width: 66.66666667%;
    }
    .col-sm-7 {
        width: 58.33333333%;
    }
    .col-sm-6 {
        width: 50%;
    }
    .col-sm-5 {
        width: 41.66666667%;
    }
    .col-sm-4 {
        width: 33.33333333%;
    }
    .col-sm-3 {
        width: 25%;
    }
    .col-sm-2 {
        width: 16.66666667%;
    }
    .col-sm-1 {
        width: 8.33333333%;
    }

}
@media (min-width: 992px) {
    .col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12 {
        float: left;
    }
    .col-md-12 {
        width: 100%;
    }
    .col-md-11 {
        width: 91.66666667%;
    }
    .col-md-10 {
        width: 83.33333333%;
    }
    .col-md-9 {
        width: 75%;
    }
    .col-md-8 {
        width: 66.66666667%;
    }
    .col-md-7 {
        width: 58.33333333%;
    }
    .col-md-6 {
        width: 50%;
    }
    .col-md-5 {
        width: 41.66666667%;
    }
    .col-md-4 {
        width: 33.33333333%;
    }
    .col-md-3 {
        width: 25%;
    }
    .col-md-2 {
        width: 16.66666667%;
    }
    .col-md-1 {
        width: 8.33333333%;
    }
    .col-md-x {
        width: 14.28571428%;
    }
    .word{
        font-size: 40px;
        border-bottom-width: 12px;
    }

}
@media (min-width: 1200px) {
    .col-lg-1, .col-lg-2, .col-lg-3, .col-lg-4, .col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-10, .col-lg-11, .col-lg-12 {
        float: left;
    }
    .col-lg-12 {
        width: 100%;
    }
    .col-lg-11 {
        width: 91.66666667%;
    }
    .col-lg-10 {
        width: 83.33333333%;
    }
    .col-lg-9 {
        width: 75%;
    }
    .col-lg-8 {
        width: 66.66666667%;
    }
    .col-lg-7 {
        width: 58.33333333%;
    }
    .col-lg-6 {
        width: 50%;
    }
    .col-lg-5 {
        width: 41.66666667%;
    }
    .col-lg-4 {
        width: 33.33333333%;
    }
    .col-lg-3 {
        width: 25%;
    }
    .col-lg-2 {
        width: 16.66666667%;
    }
    .col-lg-1 {
        width: 8.33333333%;
    }

}

想了解更多去学Bootstrap
自己做的一个小项目冰与火之歌网站
作者:彭荣辉
链接:https://www.jianshu.com/u/0f804364a8a8
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

相关文章

网友评论

    本文标题:CSS布局&居中&媒体查询

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