web开发之-前端css(5)

yumo66610个月前 (04-26)技术文章150

显示

控制一个元素的显示方式,我们可以使用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>


我们缩小一下窗口就能看到效果。

注:布局属性很重要,大家一定要去看,去写!

相关文章

videojs

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&#...

deepseek制作&quot;网页端学生签到系统&quot;

网页端学生签到系统制作经验分享 在日常教学管理中,高效准确的签到统计工作至关重要。为了提升签到管理的效率,我着手开发了一个网页端学生签到系统,它能够实现名单导入、签到状态记录与修改以及结果导出等功能,...

掌握这些CSS知识点,Coding如飞

许多入门学前端的同学,或是准备面试的同学都会去死记硬背一些前端知识点,笔者也是这么经历过来的,但却不推荐这种囫囵吞枣、不求甚解的学习方式,因为这样会走很多弯路,属于“应试”学习,我们更应该的是从基础到...

CSS基本布局16例

单行单列单行两列1:采用float浮在左上角,固定宽度。单行两列2:固定在左上角,固定宽度,采用的是绝对(absolute)定位。单行两列3:固定在左上角,不固定宽度,采用百分比(%)定义宽度来自适应...

广州蓝景分享—实用的CSS技巧,助你成为更好的前端开发者

Hello~~各位小伙伴,相信在前端开发项目中,CSS实现如修改输入占位符样式,多行文本溢出,隐藏滚动条,修改光标颜色,水平和垂直居中等等,这些都是我们非常熟悉的开发场景!前端开发者几乎每天都会和它们...

零基础教你学前端——24、表格标题和结构分组

这节课,我们学习表格的标题,行分组和列分组等有用的标签。表格标题,顾名思义,就是一个表格的大标题,默认情况下位于表格顶部水平居中位置。标签名为 caption 译为 说明文字。它是一个双标签,语法为:...