您当前的位置:首页 > 文章 > html5的响应式布局的方法示例详解

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

作者:2301_80121073 时间:2025-12-17 阅读数:25 人阅读分享到:

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

        使用的参数@media这是常用的参数
                    width,height代表的是浏览器可视宽度,高度
                         device-width:设备屏幕的宽度
                         device-height:设备屏幕的高度
 使用的是内部样式表

<!DOCTYPE html>
<htmllang="en">
<head>
    <metacharset="UTF-8">
    <metaname="viewport"content="width=device-width, initial-scale=1.0">
    <title>媒体查询</title>
    <style>
        .div{
            /* width:1200px; */
            width:100%;
            /* height:600px; */
        }
        .div div{
            float: left;
            height:100px;
        }
    </style>
    <stylemedia="(min-width:330px) and (max-width:430px)">
            .div div{
                width:33.3%
            }
            .div div:nth-child(1){
            background-color: aqua;
            }
            .div div:nth-child(2){
                background-color: yellow;
            }
            .div div:nth-child(3){
                background-color: green;
            }
    </style>
    <stylemedia="(min-width:100px) and (max-width:329px)">
        .div div{
                width:50%
            }
            .div div:nth-child(1){
            background-color: aqua;
            }
            .div div:nth-child(2){
                background-color: yellow;
            }
            .div div:nth-child(3){
                background-color: green;
            }
    </style>
    <stylemedia="(max-width:99px)">
       .div div{
                width:100%
            }
           .div div:nth-child(1){
            background-color: aqua;
            }
           .div div:nth-child(2){
                background-color: yellow;
            }
           .div div:nth-child(3){
                background-color: green;
            }
    </style>
</head>
<body>
    <divclass="div">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</html>

外部样式
        进行创建三个的css的样式
               第一个
                      css-1.css

.div{
    /* width:1200px; */
    width:100%;
    /* height:600px; */
}
.div div{
    float:left;
    height:100px;
}
.div div:nth-child(1){
    background-color:aqua;
    }
    .div div:nth-child(2){
        background-color: yellow;
    }
    .div div:nth-child(3){
        background-color:green;
    }

        第二个
css-2.css

.div div{
    width:33.3%
}
        第三个

css-3.css

.div div{
    width:50%
}

        将这三个分别引入到MediaQuery.html中

<!DOCTYPE html>
<htmllang="en">
<head>
    <metacharset="UTF-8">
    <metaname="viewport"content="width=device-width, initial-scale=1.0">
    <title>媒体查询</title>
    <linkrel="stylesheet"href="./css-1.css">
    <linkrel="stylesheet"href="./css-2.css"media="(min-width:330px) and (max-width:430px)">
    <linkrel="stylesheet"href="./css-3.css"media="(min-width:100px) and (max-width:329px)">
</head>
<body>
    <divclass="div">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>
</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        

基础布局:网格容器与项目

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr); /* 3列 */
  grid-template-rows: repeat(3, 1fr);     /* 3行 */
  gap: 20px;                             /* 网格间距 */
  padding: 20px;
  background: #eee;
}
.grid-item {
  background: #fff;
  padding: 30px;
  border-radius: 8px;
  text-align: center;
}
</style>
</head>
<body>
<divclass="grid-container">
  <divclass="grid-item">1</div>
  <divclass="grid-item">2</div>
  <divclass="grid-item">3</div>
  <divclass="grid-item">4</div>
  <divclass="grid-item">5</div>
  <divclass="grid-item">6</div>
  <divclass="grid-item">7</div>
  <divclass="grid-item">8</div>
  <divclass="grid-item">9</div>
</div>
</body>
</html>

响应式网格:自动换行

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  max-width: 1200px;
  margin: 0 auto;
}
.items {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); /* 自动填充列 */
  gap: 15px;
}
.item {
  background: #f0f0f0;
  padding: 20px;
  border-radius: 6px;
}
</style>
</head>
<body>
<divclass="container">
  <divclass="items">
    <divclass="item">Item 1</div>
    <divclass="item">Item 2</div>
    <divclass="item">Item 3</div>
    <divclass="item">Item 4</div>
    <divclass="item">Item 5</div>
    <divclass="item">Item 6</div>
  </div>
</div>
</body>
</html>
来源:https://www.jb51.net/html5/983551.html

本站大部分文章、数据、图片均来自互联网,一切版权均归源网站或源作者所有。

如果侵犯了您的权益请来信告知我们删除。邮箱:1451803763@qq.com

标签:应用HTMLCSS