网站首页 > 技术文章 正文
显示
控制一个元素的显示方式,我们可以使用display:block;display:inline-block;display:none;其中布局相关的还有两个很重要的属性:display:flex;和display:grid;flex表示弹性盒子,grid表示网格;
布局
我们使用一个三例的布局为例来试一下:
display:flex;
<html>
<head>
<title>css</title>
</head>
<body>
<div style="height: 900px;">
<div style="width: 100%;height:100px;background:grey">我是头部</div>
<div style="display: flex;height: 100%;">
<div style="width: 100px;height: 100%;background:yellow;">左侧</div>
<div style="width:100%;background:burlywood">中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间</div>
<div style="width: 100px;height: 100%;background:yellow;">右侧</div>
</div>
</div>
</body>
</html>
<html>
<head>
<title>css</title>
</head>
<body>
<div class="container">
<div class="item">flex1</div>
<div class="item">flex2</div>
<div class="item">flex3</div>
<div class="item">flex4</div>
<div class="item">flex5</div>
<div class="item">flex6</div>
<div class="item">flex7</div>
<div class="item">flex8</div>
</div>
</body>
<style>
.container{
display: flex;
width:600px;
height: 900px;
background:grey;
flex-direction: row; /** 设置排列的方式 row 横向 row-reverse 横向反转 column 纵向 column-reverse*/
justify-content: flex-start; /** 设置每一个列的对齐方向 flex-start靠左排开,flex-end靠右排列,center居中排列,space-between左右靠边平均对齐,space-around */
flex-wrap: wrap;/***是否换行 wrap换行,nowrap不换行,wrap-reverse倒序换行*/
align-content: flex-start; /**用于设置各行之间如何对齐*/
align-items:flex-start; /**设置纵向的排列方式*/
}
.item{
width: 100px;
height: 100px;
margin: 10px;
background:yellow;
}
</style>
</html>
注:以下的属性设置大家可以去尝试一下。
flex-direction: row; /** 设置排列的方式 row 横向 row-reverse 横向反转 column 纵向 column-reverse*/
justify-content: flex-start; /** 设置每一个列的对齐方向 flex-start靠左排开,flex-end靠右排列,center居中排列,space-between左右靠边平均对齐,space-around */
flex-wrap: wrap;/***是否换行 wrap换行,nowrap不换行,wrap-reverse倒序换行*/
align-content: flex-start; /**用于设置各行之间如何对齐*/
align-items:flex-start; /**设置纵向的排列方式*/
display:grid:
<html>
<head>
<title>css</title>
</head>
<body>
<div style="height: 900px;">
<div style="width: 100%;height:100px;background:grey">我是头部</div>
<div style="display: grid;height: 100%;grid-template-columns: 100px auto 100px;">
<div style="background:yellow">左侧</div>
<div style="background:burlywood">中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间中间</div>
<div style="background:yellow">右侧</div>
</div>
</div>
</body>
</html>
以上我们只是用一个三例布局讲了一下基本的内容,这里布局还有很多属性,大家可以去试一下。一定得看,哪怕你不去手写,也要看一下那些属性的作用,因为这块太重要了,但是属性又太多了,要写起来可劲得写了,大家看不明白的,可以给我留言,我会看到给大家回复的!
浮动
浮动属性float会使元素脱离文档流,使其他内容重新排列,我们常见的文字环绕效果可以使用浮动来实现:
<html>
<head>
<title>css</title>
</head>
<body>
<div>
<div style="width: 100px;height:100px;padding:50px;border:1px solid black;float:left;left:0px;top:0px;">浮动</div>
<div>显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容
显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容
显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容
显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容显示内容内容
</div>
</div>
</body>
</html>
我们缩小一下窗口就能看到效果。
注:布局属性很重要,大家一定要去看,去写!
猜你喜欢
- 2025-06-04 不会前端也能写官网?没问题,Devbox+Cursor 带你起飞
- 2025-06-04 一战封神还是虚火?Claude 4 对决 Gemini 2.5 Pro
- 2025-06-04 2023 年的响应式设计指南(响应式实现原理)
- 2025-06-04 CSS 弹性布局总结(css3弹性布局)
- 2025-06-04 React 项目实践——创建一个聊天机器人
- 2025-06-04 【HarmonyOS Next之旅】兼容JS的类Web开发 -> stepper
- 2025-06-04 用markdown语法制作一个网址导航页面
- 2025-06-04 用markdown语法制作一个好看的网址导航页面(markdown-web-nav)
- 2025-06-04 前端Flex布局可视化布局工具介绍,vue和html5快速设计利器
- 2025-06-04 css flex布局(css flex布局 阮一峰)
- 1520℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 621℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 526℃MySQL service启动脚本浅析(r12笔记第59天)
- 492℃启用MySQL查询缓存(mysql8.0查询缓存)
- 491℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 478℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 460℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 458℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- windowsscripthost (69)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- c语言min函数头文件 (68)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)