HTML 布局的几种方式

前端开发   发布日期:2025年05月12日   浏览次数:197

1.浮动

2.定位

3.分栏布局

  column-count:auto | 整数;---控制栏数

    column-width: auto | length;---每栏的宽度

      column-gap : length ;---两栏之间的间距

      column-rule : 宽度,线型,颜色;---栏与栏的间隔线 类似border,solid | dotted | dashed 实线 | 点线 | 虚线

     column-width和column-count可以让一个元素进行多列布局 column-gap和column-rule就处在相邻两列之间

例子:

  1. <div class="con">
  2. <h1>大数据下个人隐私的保护分析与研究</h1>
  3. <div>
  4. 一堆内容
  5. </div>
  6. </div>

css

  1. .con{
  2. width: 600px;
  3. column-count: 3; 分几栏
  4. column-gap: 10px; 每栏之间的距离
  5. column-rule: 3px dotted red; 栏目之间的线
  6. }
  7. .con h1{
  8. -webkit-column-span: all; 标题是否跨栏显示
  9. }

4.弹性布局

  优点:

     1 适应性强,在做不同屏幕分辨率的界面时非常实用

     2 可以随意按照宽度、比例划分元素的宽高

     3 可以轻松改变元素的显示顺序

       4 弹性布局实现快捷,易维护

  display:box;将一个元素的子元素以弹性布局进行布局

  box-orient:horizontal  || vertical || inherit 子元素排列方式 

  box-direction:normal || reverse || inherit 子元素的排列顺序

  box-align:start || end || center 子元素的对齐方式 (规定水平框中垂直位置 或 垂直框中水平位置)

  box-pack: start || end || center 子元素的对齐方式(规定水平框中水平位置 或 垂直框中垂直位置)

  box-flex:number;子元素如何分配剩余空间

  box-ordinal-group:number;子元素显示顺序

例子:

  1. <style>
  2. body,html{
  3. width: 100%;
  4. height: 100%;
  5. display: -webkit-box;
  6. -webkit-box-orient:vertical;
  7. -webkit-box-align:center;
  8. -webkit-box-pack:center;
  9. }
  10. .con{
  11. width: 90%;
  12. height: 90%;
  13. display: -webkit-box;
  14. -webkit-box-orient:vertical;
  15. border: 1px solid red;
  16. }
  17. .con .head{
  18. height: 200px;
  19. display: -webkit-box;
  20. -webkit-box-orient:horizontal;
  21. }
  22. .con .head .logo{
  23. width: 100px;
  24. height: 200px;
  25. background: pink;
  26. }
  27. .con .head .logoCon{
  28. height: 200px;
  29. -webkit-box-flex:;
  30. background: green;
  31. }
  32. .con .content{
  33. -webkit-box-flex:;
  34. background: orange;
  35. display: -webkit-box;
  36. -webkit-box-orient:horizontal;
  37. -webkit-box-direction:reverse;
  38. }
  39. .content div{
  40. width: 200px;
  41. text-align: center;
  42. }
  43. .con .footer{
  44. height: 100px;
  45. background: blue;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div class="con">
  51. <div class="head">
  52. <div class="logo"></div>
  53. <div class="logoCon"></div>
  54. </div>
  55. <div class="content">
  56. <div class="con1">111</div>
  57. <div class="con2">222</div>
  58. <div class="con3">333</div>
  59. </div>
  60. <div class="footer">
  61. </div>
  62. </div>

5.响应式布局

一个网站能够兼容多个终端---而不是为每个终端做一个特定的版本

  @media all(用于所有的设备) || screen (用于电脑屏幕,平板电脑,智能手机等)  and|not|only(三个关键字可以选)

  1. <style media="screen">
  2. @media screen and (max-width:600px){
  3. .con{
  4. background:red;
  5. }
  6. }
  7. @media screen and (min-width:600px) and (max-width:800px){
  8. .con{
  9. background:blue;
  10. }
  11. }
  12. @media screen and (min-width:800px){
  13. .con{
  14. background:green;
  15. }
  16. }
  17. .con{
  18. width: 100%;
  19. height: 100px;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="con">
  25. </div>
  26. </body>

 

以上就是HTML 布局的几种方式的详细内容,更多关于HTML 布局的几种方式的资料请关注九品源码其它相关文章!