用CSS实现:左右两栏宽度固定,中间一栏的宽度随浏览器宽度的改变而改变

冷不防 2022-05-27 03:30 303阅读 0赞

方法一 ,使用calc()

  1. <div class="zuo">左边 </div>
  2. <div class="zhong">中间</div>
  3. <div class="you">右边</div>
  4. .zuo, .you{
  5. width:200px;
  6. height:100%;
  7. background-color: red;
  8. float:left;
  9. }
  10. .zhong{
  11. float:left;
  12. width:calc(100% - 400px)
  13. }

该方法使用了CSS3提供的方法calc()。 calc()可以用来动态的得到一些值 支持+-*/运算。 运算符号前后需要 “ ”

方法二,使用position:absolute

  1. <div class="zuo">左边 </div>
  2. <div class="zhong">中间</div>
  3. <div class="you">右边</div>
  4. .zuo,.you{
  5. width: 200px;
  6. height:100%;
  7. background-color: red;
  8. position:absolute;
  9. }
  10. .zuo{
  11. left:0;
  12. }
  13. .you{
  14. right: 0;
  15. }
  16. .zhong{
  17. position: absolute;
  18. left:200px;
  19. right:200px;
  20. background-color: yellow;
  21. }

方法三,使用display:flex

  1. <div class="kuang2">
  2. <div class="left2"></div>
  3. <div class="middle2"></div>
  4. <div class="right2"></div>
  5. </div>
  6. .kuang2 {
  7. display: flex;
  8. width: 100%;
  9. }
  10. .left2, .right2 {
  11. width: 200px;
  12. height: 200px;
  13. background-color: gold;
  14. }
  15. .middle2 {
  16. height: 200px;
  17. background-color: green;
  18. flex: 1
  19. }

方法四,使用display:table

  1. <div class="kuang4">
  2. <div class="left4"></div>
  3. <div class="middle4"></div>
  4. <div class="right4"></div>
  5. </div>
  6. .kuang4{
  7. display: table;
  8. width: 100%;
  9. }
  10. .kuang4 div{
  11. height: 200px;
  12. display: table-cell;
  13. }
  14. .right4 , .left4{
  15. width:200px;
  16. background-color: red;
  17. }
  18. .middle4{
  19. background-color: green;
  20. }

发表评论

表情:
评论列表 (有 0 条评论,303人围观)

还没有评论,来说两句吧...

相关阅读