前端面试常见问题:css3弹性布局

2018-12-22  本文已影响0人  清汤饺子

兼容性:兼容现代浏览器,ie11;

弹性布局实战

http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

基本网格布局

image.png
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<style type="text/css">
.Grid{
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
}
.Grid-cell{
    display: flex;
    justify-content: space-between;
}
.Grid-cell-wrap{
    background: #ccc;
    display: block;
    width: 100%;
    margin: 2px;
    text-align: center;
}
</style>
<body>
<div class="Grid">
  <div class="Grid-cell">
    <div class="Grid-cell-wrap">1/2</div>
    <div class="Grid-cell-wrap">1/2</div>
  </div>
  <div class="Grid-cell">
    <div class="Grid-cell-wrap">1/3</div>
    <div class="Grid-cell-wrap">1/3</div>
    <div class="Grid-cell-wrap">1/3</div>
  </div>
  <div class="Grid-cell">
    <div class="Grid-cell-wrap">1/4</div>
    <div class="Grid-cell-wrap">1/4</div>
    <div class="Grid-cell-wrap">1/4</div>
    <div class="Grid-cell-wrap">1/4</div>
  </div>
  <div class="Grid-cell">
    <div class="Grid-cell-wrap">反反复复付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付</div>
    <div class="Grid-cell-wrap">反反复复付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付付</div>
  </div>
</div>
</body>
<script type="text/javascript">
        
</script>
</body>
</html>

百分比布局

某个网格的宽度为固定的百分比,其余网格平均分配剩余的空间。


image.png
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<style type="text/css">
.Grid{
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
}
.Grid-cell{
    display: flex;
    justify-content: space-between;
}
.Grid-cell-wrap{
    background: #ccc;
    display: block;
    width: 100%;
    margin: 2px;
    text-align: center;
    padding: 10px;
}
.flex-lof2{
    flex: 0 0 50%;
}
.flex-lof3{
    flex: 0 0 33.3%;
}
.flex-lof4{
    flex: 0 0 25%;
}
</style>
<body>
<div class="Grid">
  <div class="Grid-cell">
    <div class="Grid-cell-wrap flex-lof2">1/2</div>
    <div class="Grid-cell-wrap">auto</div>
    <div class="Grid-cell-wrap">auto</div>
  </div>
  <div class="Grid-cell">
    <div class="Grid-cell-wrap">auto</div>
    <div class="Grid-cell-wrap flex-lof3">1/3</div>
  </div>
  <div class="Grid-cell">
    <div class="Grid-cell-wrap flex-lof4">1/4</div>
    <div class="Grid-cell-wrap">auto</div>
    <div class="Grid-cell-wrap flex-lof3">1/3</div>
  </div>
 
</div>
</body>
<script type="text/javascript">
        
</script>
</body>
</html>

圣杯布局

圣杯布局(Holy Grail Layout)指的是一种最常见的网站布局。页面从上到下,分成三个部分:头部(header),躯干(body),尾部(footer)。其中躯干又水平分成三栏,从左到右为:导航、主栏、副栏。

image.png
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<style type="text/css">
.HolyGrail{
    min-height: 1000px;
}
header,footer{
    background: #ccc;
}
.HolyGrail-nav{
    background: blue;
}
.HolyGrail-ads{
    background: red;
}
.HolyGrail-content{
    background: #eee;
}
.HolyGrail{
    display: flex;
    flex-direction:column;
}
header,footer{
    flex-basis:100px;
}
.HolyGrail-body{
    flex-grow:1;
    display: flex;
}
.HolyGrail-nav,.HolyGrail-ads{
    flex-basis:150px;
}
.HolyGrail-content{
    flex-grow:1;
}
</style>
<body>
<body class="HolyGrail">
  <header>header</header>
  <div class="HolyGrail-body">
    <nav class="HolyGrail-nav">left</nav>
    <main class="HolyGrail-content">conter</main>
    <aside class="HolyGrail-ads">right</aside>
  </div>
  <footer>footer</footer>
</body>
</body>
<script type="text/javascript">
        
</script>
</body>
</html>
上一篇下一篇

猜你喜欢

热点阅读