2024.4.5-CSS 布局模型(层模型)

简介: 2024.4.5-CSS 布局模型(层模型)

个人主页:学习前端的小z

个人专栏:HTML5和CSS3悦读

本专栏旨在分享记录每日学习的前端知识和学习笔记的归纳总结,欢迎大家在评论区交流讨论!

作业

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>定位</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        ul {
            list-style: none;
        }
        body {
            font: bolder 25px "Microsoft YaHei","Heiti SC",tahoma,arial,"Hiragino Sans GB","\5B8B\4F53",sans-serif;
            color: white;
        }
        .auto-center {
            width: 1000px;
            margin-left: auto;
            margin-right: auto;
        }
        .full-center {
            min-width: 1000px;
        }
        .text-center {
            text-align: center;
        }
        .clearfix::after {
            content: '';
            display: block;
            clear: both;
        }
        .top {
            background-color: #000079;
        }
        .top>.auto-center {
            line-height: 100px;
            background-color: #EA0000;
        }
        .banner {
            background-color: #003E3E;
            line-height: 300px;
        }
        .main {
            margin-top: 20px;
            background-color: #467500;
        }
        /*基于浮动实现两栏布局,左边200px,右侧自动填充剩余空间*/
        .content-one {
            position: relative;
            height: 500px;
        }
        .content-one>.left {
            width: 200px;
            background-color: #613030;
            line-height: 500px;
            /* float: left; */
            position: absolute;
        }
        .content-one>.right {
            /* 思考,right为绝对定位是否可以实现两栏布局? */
            margin-left: 200px;
            background-color: #336666;
            line-height: 300px;
        }
        /*基于浮动实现栏栅布局*/
        .content-tow {
            margin-top: 10px;
        }
        .content-tow>.list-item {
            float: left;
            width: 250px;
            height: 150px;
            box-sizing: border-box;
            background-color: #CAFFFF;
            text-align: center;
            line-height: 150px;
            color: black;
            border: 1px dashed #ccc;
        }
        /* 基于浮动实现三栏布局,左固定200px,右固定250px,中间自适应宽度(填充剩余空间) 
        注意:中间一栏必须放到最后
        */
        .content-three {
            margin-top: 20px;
            position: relative;
            height: 300px;
        }
        .content-three>.left {
            position: absolute;
            width: 200px;
            left: 0;
            top: 0;
            /* float: left; */
            line-height: 300px;
            background-color: #F9F900;
        }
        .content-three>.right {
            position: absolute;
            width: 250px;
            right: 0;
            top: 0;
            /* float: right; */
            line-height: 300px;
            background-color: #A5A552;
        }
        .content-three>.middle {
            background-color: #5CADAD;
            margin-left: 200px;
            margin-right: 250px;
        }
        /* 基于浮动实现三栏布局,左固定200px,右固定250px,中间自适应宽度(填充剩余空间) 
        使用父级外边距实现
        */
        .content-four {
            margin-top: 20px;
            padding-left: 200px;
            padding-right: 250px;
        }
        .content-four>.left {
            width: 200px;
            float: left;
            line-height: 300px;
            background-color: #F9F900;
            margin-left: -200px;
        }
        .content-four>.right {
            width: 250px;
            float: right;
            line-height: 300px;
            background-color: #A5A552;
            margin-right: -250px;
        }
        .content-four>.middle {
            background-color: #5CADAD;
        }
        
        .footer {
            margin-top: 20px;
            background-color: #642100;
            line-height: 200px;
        }
        .fixed-ad {
            position: fixed;
            width: 100px;
            height: 300px;
            right: 20px;
            top: 50%;
            margin-top: -150px;
            background-color: #4D0000;
        }
        .other {
            margin-top: 30px;
            background-color: #FF60AF;
            border: 1px dashed #ccc;
            position: relative;
        }
        .other .bottom {
            position: absolute;
            bottom: -10px;
            left: 20px;
            right: 20px;
            background-color: #B15BFF;
        }
    </style>
</head>
<body>
    <div class="top">
        <div class="auto-center text-center">top</div>
    </div>
    <div class="banner text-center full-center">
        banner
    </div>
    <div class="main auto-center">
        <div class="content-one">
            <div class="left text-center">
                left
            </div>
            <div class="right text-center">
                right
            </div>
        </div>
        <ul class="content-tow clearfix">
            <li class="list-item">list-item1</li>
            <li class="list-item">list-item2</li>
            <li class="list-item">list-item3</li>
            <li class="list-item">list-item4</li>
            <li class="list-item">list-item5</li>
            <li class="list-item">list-item6</li>
            <li class="list-item">list-item7</li>
            <li class="list-item">list-item8</li>
        </ul>
        <div class="content-three clearfix">
            <div class="left text-center">
                left
            </div>
            <div class="right text-center">
                right
            </div>
            <div class="middle text-center">
                middle
            </div>
        </div>
        <div class="content-four clearfix">
            <div class="left text-center">
                left
            </div>
            <div class="right text-center">
                right
            </div>
            <div class="middle text-center">
                middle
            </div>
        </div>
    </div>
    <div class="other auto-center">
        <div>content</div>
        <div>content</div>
        <div>content</div>
        <div>content</div>
        <div>content</div>
        <div class="bottom text-center">title</div>
    </div>
    <div class="footer text-center full-center">
        footer
    </div>
    <div class="fixed-ad text-center">ad</div>
    
    <div style="position: fixed;
    top: 200px;
    left: 50%;
    margin-left: 500px;
    width: 50px;
    height: 150px;
    background-color: #ccc;">版心固定</div>
</body>
</html>

<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="首页" content="首页">
        <meta description="首页" content="首页">
        <title>首页</title>
        <style>
            * {
                padding: 0;
                margin: 0;
            }
            .auto-center {
                width: 1000px;
                margin-left: auto;
                margin-right: auto;
            }
            .banner {
                margin-top: 30px;
                background-color: #35c3d9;
                padding: 20px 0;
            }
            .full-center {
                min-width: 1000px;
            }
            .banner .auto-center {
                position: relative;
            }
            .banner .title {
                position: absolute;
                right: 20px;
                top: 50%;
                width: 344px;
                line-height: 80px;
                margin-top: -80px;
                color: white;
                font-size: 56px;
                text-align: center;
            }
            .banner .backward, .banner .forward {
                position: absolute;
                top: 50%;
                margin-top: -30px  
            }
            .banner .backward {
                left: -80px;
            }
            .banner .forward {
                right: -80px;
            }
        </style>
    </head>
    <body>
        <div class="banner full-center">
            <div class="auto-center">
            <img src="./images/2.png">
        
        <span class="title">查看我们最新时装</span>
        <img class="backward" src="./images/1.png">
        <img class="forward" src="./images/3.png">
            </div>
        </div>
    </body>
</html>

2024.4.5-学习笔记

1 CSS定位

布局思路:盒子模型+浮动+定位+Flex布局

position不管浮动不浮动,都是static

1.1 相对定位 relative

只有当设置了定位,边偏移才有效果。偏移是基于原来的位置来移动的,显示效果甚至可以超出盒子,但原有标准流位置保留不变,相对定位并没有脱标。相当于块元素。

bottom向上走

top向下走

right向左走

1.2 绝对定位 absolute

开启了position: absolute,无论你是行内元素,行内块元素,块级元素,它都会变成行内块表现形式。

float也是。

(行内块)表现 形式:默认内容有多宽,有多高,那么该盒子就会有多宽有多高,若设置了width、height,就以width、height为准,设置margin-left,margin-right为auto是对标准流块级元素有效。

绝对定位的偏移是根据祖先元素是否定位(非static的标签元素)最近的一个或Document进行移动。

绝对定位,不仅脱标,还会遮盖标准流。

找不到定位元素,是基于文档第一屏当前窗口进行定位。一般不要写在外面,不要裸露外面。

绝对定位,浮动只是表现和行内块类似,但是它还是块级元素(display:block),因为它们会脱标没有在标准流上面,所以这种脱标的块级一般就是和行内块类似采用内容宽度,而不是横跨一行

1.3 子绝父相

子级是绝对定位,父级要用相对定位。

如果父元素不需要占有位置,则可以出现子绝父绝。

1,4 固定定位

position: fixed;

是基于浏览器可视窗口进行定位的。

脱标

1.5粘性定位

有top相当于固定定位

没有top相当于相对定位

2 定位叠放次序

z-index是针对于有定位(非static)的标签元素的堆叠顺序。

如果子级有层级堆叠,最好给它的定位父级设置一个有效的z-index的数值。就不会影响父级外的元素。

3 居中技巧:

相关文章
|
3天前
|
Web App开发 前端开发 JavaScript
技术心得记录:瀑布流的布局原理分析(纯CSS瀑布流与JS瀑布流)
技术心得记录:瀑布流的布局原理分析(纯CSS瀑布流与JS瀑布流)
|
2天前
|
前端开发
CSS中的display属性:布局控制的关键
CSS中的display属性:布局控制的关键
105 42
|
5天前
|
移动开发 前端开发 HTML5
深掘 CSS3 Flex 布局的奥秘:打造无界限的灵活页面布局
深掘 CSS3 Flex 布局的奥秘:打造无界限的灵活页面布局
|
14天前
|
前端开发 开发者 容器
CSS进阶-Grid布局高级应用
【6月更文挑战第16天】**CSS Grid布局是CSS3的强大力量,用于复杂二维布局。然而,隐式网格、未命名Grid线和缺少响应式设计是常见问题。解决方法包括显式定义网格结构、命名Grid线和结合媒体查询实现响应式。高级技巧涉及自适应列宽、复杂区域布局和元素层叠对齐。代码示例展示了响应式Grid的用法。掌握这些能提升布局效率和设计灵活性。**
|
14天前
|
前端开发 开发者 容器
CSS进阶-Flexbox高级布局技巧
【6月更文挑战第16天】Flexbox是CSS3的布局模块,简化响应式设计和复杂多列布局。文章探讨了Flex容器与项目属性的区分、垂直居中、防止元素溢出等常见问题及解决方案。此外,还分享了等宽不同高列、圣杯布局和自适应间距等高级技巧。通过示例展示了如何创建垂直居中布局,强调实践和理解核心概念是掌握Flexbox的关键。
|
11天前
|
前端开发 容器
如何用css实现两列布局?
如何用css实现两列布局?
14 1
|
16天前
|
前端开发 开发者
CSS进阶-CSS3多列布局
【6月更文挑战第14天】CSS3的多列布局简化了复杂布局,提供杂志样式排版。通过`column-*`属性如`column-count`和`column-gap`实现内容分割和列间距控制。常见问题包括内容溢出、列间距不当和兼容性问题。解决方法包括使用`word-break`和`hyphens`处理长单词,灵活设置列宽和列数,以及为旧浏览器提供回退方案。代码示例展示了一个自动平衡列高的两列布局。理解并解决这些问题将帮助开发者更好地利用CSS3多列布局。尽管有兼容性挑战,但它是现代网页设计的重要工具。
|
19天前
|
前端开发 开发者 容器
CSS基础-Flexbox布局基础
【6月更文挑战第11天】Flexbox是CSS3的全新布局模式,简化了网页动态布局问题。通过`display: flex`设置容器,调整`flex-direction`、`justify-content`、`align-items`等属性控制项目排列和对齐。适用于响应式布局、均匀分布空间和元素对齐。注意浏览器兼容性、选择合适布局模式及区分对齐与排序属性。通过实例代码学习和实践,掌握Flexbox能有效提升网页布局效率。
|
19天前
|
前端开发 开发者 容器
CSS基础-Grid布局基础
【6月更文挑战第11天】CSS Grid布局简化了网页设计,提供前所未有的灵活性。本文探讨Grid基础、常见问题及解决方案。学习重点包括理解容器和项目、正确使用网格线、避免固定单位、有效对齐元素以及选择合适布局模型。通过深入学习、实践调试和参考资源,设计师能避免陷阱,掌握这一现代布局技术。实践是关键,不断尝试将使你在Grid布局中游刃有余。
|
25天前
|
前端开发 容器
7. CSS 网格布局
7. CSS 网格布局
17 4