Css布局系列——圣杯布局

圣杯布局是一种三列布局,两边定宽,中间自适应布局:

STEP1:Create dom struction

<div id="header"></div>
  <
div id="container">     <div id="center" class="column"></div>     <div id="left" class="column"></div>     <div id="right" class="column"></div>
  </
div>
<
div id="footer"></div>

STEP 2: Create the  frame and pad the container with the width we want our left and right columns to occupy

#container {
  padding-left: 200px;   /* LC width */
  padding-right: 150px;  /* RC width */
}

Figure 1: the outline of the header, footer, and container

STEP 3: Give the colums to add apropritate width and float them to get them inline 

#container .column {
  float: left;
}
#center {
  width: 100%;
}
#left {
  width: 200px;  /* LC width */
}
#right {
  width: 150px;  /* RC width */
}
#footer {
  clear: both;
}

Figure 2: the three columns lined up in source order

STEP 4:Get the left colum to line up with the pading-left on the container

Stetp 4.1:we`ll put it all  the way across the center colums with a 100% negative margin  
#left {
  width: 200px;        /* LC width */
  margin-left: -100%;  
}

Figure 3: the left column pulled 100% to the left

Stetp 4.2:we`ll use relative postioning with an right offset the width of the  left colum
#container .columns {
  float: left;
  position: relative;
}
#left {
  width: 200px;        /* LC width */
  margin-left: -100%;  
  right: 200px;        /* LC width */
}

Figure 4: the left column offset 200px to the left

STEP 5:Get the right colum to line up with the pading-right on the container

#right {
  width: 150px;          /* RC width */
  margin-right: -150px;  /* RC width */
}

Figure 5: the right column pulled 100% to the right

STEP 6:Design defensively , Seting the min-widh on the body keeps our colums in place when the browser window is resized

body {
  min-width: 550px;  /* 2x LC width + RC width */
}

here , the simply he holy grail mode is finished ,but the layout offends even my aesthetic sensibilites , so we need whitespace , and Pading will be added to the  colums

STEP 1:Pading is added the left column ,Keep the full width at 200px

#left {
  width: 180px;        /* LC fullwidth - padding */
  padding: 10 10px;
  right: 200px;        /* LC fullwidth */
  margin-left: -100%;
}

 

STEP 2:Pading is added to the center padding

Add padding at conter column 
#center {
  padding: 10px 20px;    /* CC padding */
  width: 100%;
}

drawback1:padding plus a width of 100% cause the center column to expand beyond the non-padding width of the container ,
solution:we need increase the padding-right of the container ,This ensures that the center column is only as large as we expect it to be
#container {
  padding-left: 200px;   /* LC fullwidth */
  padding-right: 190px;  /* RC fullwidth + CC padding */
}
drawback2:the left column do`t pull into the place
solution:we will pull the left column into place , then ,we will use relative position with  an right offset the width of the  left colum plusing double padding (40px)
#left {
  width: 180px;          /* LC width */
  padding: 0 10px;       /* LC padding */
  right: 240px;          /* LC fullwidth + CC padding */
  margin-left: -100%;
}

note:why is the double padding , as following ;

 

drawback3: the right column do`t pull into the place

solution:as following

#right {
  width: 130px;          /* RC width */
  padding: 0 10px;       /* RC padding */
  margin-right: -190px;  /* RC fullwidth + CC padding */
}

here,we finnished improving the aesthetic feeling ,the whole code

body {
  min-width: 630px;      /* 2x (LC fullwidth +
                            CC padding) + RC fullwidth */
}
#container {
  padding-left: 200px;   /* LC fullwidth */
  padding-right: 190px;  /* RC fullwidth + CC padding */
}
#container .column {
  position: relative;
  float: left;
}
#center {
  padding: 10px 20px;    /* CC padding */
  width: 100%;
}
#left {
  width: 180px;          /* LC width */
  padding: 0 10px;       /* LC padding */
  right: 240px;          /* LC fullwidth + CC padding */
  margin-left: -100%;
}
#right {
  width: 130px;          /* RC width */
  padding: 0 10px;       /* RC padding */
  margin-right: -190px;  /* RC fullwidth + CC padding */
}

参考资料:http://alistapart.com/article/holygrail

转载于:https://www.cnblogs.com/gongOnTheWay/p/4823317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值