• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    html5的响应式布局的方法示例详解

    发布者: 土豆服务器 | 发布时间: 2025-6-14 15:07| 查看数: 80| 评论数: 0|帖子模式

    一 使用媒体查询响应式布局

            使用的参数@media这是常用的参数
                        width,height代表的是浏览器可视宽度,高度
                             device-width:设备屏幕的宽度
                             device-height:设备屏幕的高度
    使用的是内部样式表
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.     <meta charset="UTF-8">
    5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.     <title>媒体查询</title>
    7.     <style>
    8.         .div{
    9.             /* width:1200px; */
    10.             width:100%;
    11.             /* height:600px; */
    12.         }
    13.         .div div{
    14.             float: left;
    15.             height:100px;
    16.         }
    17.     </style>
    18.     <style media="(min-width:330px) and (max-width:430px)">
    19.             .div div{
    20.                 width:33.3%
    21.             }
    22.             .div div:nth-child(1){
    23.             background-color: aqua;
    24.             }
    25.             .div div:nth-child(2){
    26.                 background-color: yellow;
    27.             }
    28.             .div div:nth-child(3){
    29.                 background-color: green;
    30.             }
    31.     </style>
    32.     <style media="(min-width:100px) and (max-width:329px)">
    33.         .div div{
    34.                 width:50%
    35.             }
    36.             .div div:nth-child(1){
    37.             background-color: aqua;
    38.             }
    39.             .div div:nth-child(2){
    40.                 background-color: yellow;
    41.             }
    42.             .div div:nth-child(3){
    43.                 background-color: green;
    44.             }
    45.     </style>
    46.     <style media="(max-width:99px)">
    47.        .div div{
    48.                 width:100%
    49.             }
    50.            .div div:nth-child(1){
    51.             background-color: aqua;
    52.             }
    53.            .div div:nth-child(2){
    54.                 background-color: yellow;
    55.             }
    56.            .div div:nth-child(3){
    57.                 background-color: green;
    58.             }
    59.     </style>
    60. </head>
    61. <body>
    62.     <div class="div">
    63.         <div></div>
    64.         <div></div>
    65.         <div></div>
    66.     </div>
    67. </body>
    68. </html>
    复制代码
    外部样式
            进行创建三个的css的样式
                   第一个
                          css-1.css
    1. .div{
    2.     /* width:1200px; */
    3.     width:100%;
    4.     /* height:600px; */
    5. }
    6. .div div{
    7.     float: left;
    8.     height:100px;
    9. }
    10. .div div:nth-child(1){
    11.     background-color: aqua;
    12.     }
    13.     .div div:nth-child(2){
    14.         background-color: yellow;
    15.     }
    16.     .div div:nth-child(3){
    17.         background-color: green;
    18.     }
    复制代码
           第二个
                    css-2.css
    1. .div div{
    2.     width:33.3%
    3. }
    复制代码
           第三个
                    css-3.css
    1. .div div{
    2.     width:50%
    3. }
    复制代码
           将这三个分别引入到MediaQuery.html中
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4.     <meta charset="UTF-8">
    5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6.     <title>媒体查询</title>
    7.     <link rel="stylesheet" href="./css-1.css">
    8.     <link rel="stylesheet" href="./css-2.css" media="(min-width:330px) and (max-width:430px)">
    9.     <link rel="stylesheet" href="./css-3.css" media="(min-width:100px) and (max-width:329px)">
    10. </head>
    11. <body>
    12.     <div class="div">
    13.         <div></div>
    14.         <div></div>
    15.         <div></div>
    16.     </div>
    17. </body>
    18. </html>
    复制代码
           这就是我们媒体查询的响应式自适应
    二 使用flex进行响应式布局

            我们为什么使用flex
                    用来为盒状模型提供最大的灵活性。任何一个容器都可以指定为Flex布局更加符合响应        式设计的特点
            flex-direction
           作用:子元素在父元素盒子中的排列方式
            row:默认值,按从左到右的顺序显示
            row-reverse:与row相同,但是以相反的顺序
            column:灵活的项目将垂直显示,按从上到下的顺序
            column-reverse:与column相同,但是以相反的顺序
            flex-wrap
                    作用:子元素在父元素盒子中的是否换行(列)
                    nowrap:默认值。不换行或不换列。
                    wrap:换行或换列。
                    wrap-reverse:换行或换列,但以相反的顺序。
            justify-content
                    作用:用来在存在剩余空间时,设置为间距的方式
                    flex-start:默认值。从左到右,挨着行的开头。
                    flex-end:从右到左,挨着行的结尾。
                    center:居中显示。
                    space-between:平均分布在该行上,两边不留间隔空间。
                    space-around:平均分布在该行上,两边留有一半的间隔空间。
            align-items
                    作用:设置每个flex元素在交叉轴上的默认对齐方式
                    flex-start:位于容器的开头。
                    flex-end:位于容器的结尾
                    center:居中显示。
            align-content
                    作用:设置每个flex元素在交叉轴上的默认对齐方式
                    flex-start:位于容器的开头。
                    flex-end:位于容器的结尾。
                    center:位于容器的中心。
                    space-between:之间留有空白。
                    space-around:两端都留有空白。
            其他属性
                    flex-basis:设置弹性盒伸缩基准值。
                    flex-grow:设置弹性盒子的扩展比率。
                    flex-shrink:设置弹性盒子的缩小比率。
                    flex:flex-grow、flex-shrink、flex-basis的缩写
    三 CSS Grid        

    基础布局:网格容器与项目
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <style>
    5. .grid-container {
    6.   display: grid;
    7.   grid-template-columns: repeat(3, 1fr); /* 3列 */
    8.   grid-template-rows: repeat(3, 1fr);     /* 3行 */
    9.   gap: 20px;                             /* 网格间距 */
    10.   padding: 20px;
    11.   background: #eee;
    12. }
    13. .grid-item {
    14.   background: #fff;
    15.   padding: 30px;
    16.   border-radius: 8px;
    17.   text-align: center;
    18. }
    19. </style>
    20. </head>
    21. <body>
    22. <div class="grid-container">
    23.   <div class="grid-item">1</div>
    24.   <div class="grid-item">2</div>
    25.   <div class="grid-item">3</div>
    26.   <div class="grid-item">4</div>
    27.   <div class="grid-item">5</div>
    28.   <div class="grid-item">6</div>
    29.   <div class="grid-item">7</div>
    30.   <div class="grid-item">8</div>
    31.   <div class="grid-item">9</div>
    32. </div>
    33. </body>
    34. </html>
    复制代码
    响应式网格:自动换行
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <style>
    5. .container {
    6.   max-width: 1200px;
    7.   margin: 0 auto;
    8. }
    9. .items {
    10.   display: grid;
    11.   grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); /* 自动填充列 */
    12.   gap: 15px;
    13. }
    14. .item {
    15.   background: #f0f0f0;
    16.   padding: 20px;
    17.   border-radius: 6px;
    18. }
    19. </style>
    20. </head>
    21. <body>
    22. <div class="container">
    23.   <div class="items">
    24.     <div class="item">Item 1</div>
    25.     <div class="item">Item 2</div>
    26.     <div class="item">Item 3</div>
    27.     <div class="item">Item 4</div>
    28.     <div class="item">Item 5</div>
    29.     <div class="item">Item 6</div>
    30.   </div>
    31. </div>
    32. </body>
    33. </html>
    复制代码
    到此这篇关于html5的响应式布局的方法的文章就介绍到这了,更多相关html5响应式布局内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/html5/983551.html
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表