一 使用媒体查询响应式布局
使用的参数@media这是常用的参数
width,height代表的是浏览器可视宽度,高度
device-width:设备屏幕的宽度
device-height:设备屏幕的高度
使用的是内部样式表
媒体查询 .div{ /* width:1200px; */ width:100%; /* height:600px; */ } .div div{ float: left; height:100px; } .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; } .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; } .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; }
外部样式
进行创建三个的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中
媒体查询
这就是我们媒体查询的响应式自适应
二 使用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
基础布局:网格容器与项目
.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; }123456789
响应式网格:自动换行
.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; }Item 1Item 2Item 3Item 4Item 5Item 6
到此这篇关于html5的响应式布局的方法的文章就介绍到这了,更多相关html5响应式布局内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章,希望大家以后多多支持IT俱乐部!