浅谈css网页的布局问题

这次给大家带来浅谈css网页的布局问题,css网页的布局问题的注意事项有哪些,下面就是实战案例,一起来看一下。

1、左边固定,右边自适应布局的两种实现方式

效果图如下:

大屏展示:

浅谈css网页的布局问题

小屏展示:

浅谈css网页的布局问题 

第一种实现方式通过负边距与浮动 实现左边固定,右边自适应的布局。 主要代码如下:

.left{float: left;width: 100%;height: 200px;background-color: red;}.left-content{margin-left: 30%;}.right{float: left;width: 30%;margin-left: -100%;height: 200px;background-color: green;}.layout0{clear: both;width: 100px;height: 100px;background-color: yellow;}

设置子元素的margin,然后父元素必须浮动。用父元素包裹,主要是因为right会覆盖left,从而导致left内容不可以看到,如果直接在left上设置margin或者padding会导致布局变化,因此只能再用一个p包裹内容,并且去除right覆盖的宽度。

-margin必须大于或等于自身的宽度才会上移

实现过程中需要注意的是:

1.自适应的容器需要容器包裹住,否则容器内的内容会被覆盖。

2.right容器的负边距必须大于或等于自身的宽度才会上移。

3.如果right容器负边距等于自身的宽度它会靠右对齐,如果负边距等于-100%,则会靠左对齐。

第二种 通过浮动布局来实现左边固定,右边自适应的布局

主要的代码如下:

.left{float: left;width: 200px;height: 200px;background-color: yellow;}.right{padding-left: 200px;height: 200px;background-color: red;}@media (min-width: 650px) and (max-width: 1000px){.left{width: 150px;}.right{margin-left: 150px;}}@media (max-width: 640px){.left{width: 100px;}.right{margin-left: 100px;}}

左边固定宽度,右边自适应

实现过程中需要注意的是: 1. left需要脱离文档流,而right只需要正常显示就可以。

2.left只是覆盖在right上边,因此想要让right内容完整显示需要给right padding-left或者margin-left。

大屏展示:

浅谈css网页的布局问题

小屏展示:

浅谈css网页的布局问题 

主要代码如下:

#head{height: 200px;background-color: yellow;}#body{width: 100%;float: left;}.main{background-color: green;min-height: 200px;margin: 0 210px;}.left{float: left;background-color: red;width: 200px;height: 200px;margin-left: -100%;}.right{float: right;background-color: blue;width: 200px;height: 200px;margin-left: -200px;}#footer{clear: both;height: 200px;background-color: orange;}

当多个元素同时从标准流中脱离开来时,如果前一个元素的宽度为100%宽度,后面的元素通过负边距可以实现上移。当负的边距超过自身的宽度将上移,只要没有超过自身宽度就不会上移

实现过程中需要注意:

1.中间自适应的p需要放在left和right容器前面并且内容p需要用父容器包裹

2.left和right容器向同一个方向浮动。

主要代码如下:

#head{height: 200px;background-color: yellow;}#body{overflow: hidden;}.left{float: left;background-color: red;width: 200px;height: 200px;}.right{float: right;background-color: blue;width: 200px;height: 200px;}.main{background-color: green;height: 200px;margin: 0 210px;}#footer{clear: both;height: 200px;background-color: orange;}

该方案有一个缺陷,在小屏幕情况下回导致right被挤下去,main没有了

实现过程中需要注意:

1.该方式只需要注意中间自适应的p需要放在left和right容器的后面。

2.left和right容器向两边浮动。

主要代码如下:

使用flex 实现“双飞翼布局”#main{display: flex;display: -webkit-flex;//谷歌浏览器加前缀flex-flow: row nowrap;justify-content: flex-start;align-items: center;}.left{flex: 0 0 auto;width:100px;height: 200px;background-color: red;word-wrap: break-word; overflow: hidden;}.main{flex: 1 1 auto;height: 200px;background-color: green;}.right{flex: 0 0 auto;width: 100px;height: 200px;background-color: yellow;}

flex 语法我参照了阮一峰关于flex语法介绍 http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

如果未了解过flex布局请移至文末点击链接查看 阮一峰大神写的关于flex语法

3、定位布局

这边就不絮絮叨叨的讲一些基础的css定位知识了(ps:不会的请自行到w3c官网查阅),我主要来讲解一下工作中遇到的坑。以免其他人和我一样掉入坑中。

第一:使用多个fixed时,注意自己需要基于什么定位,因为如果父级有用transform属性时,可能会导致子元素的fixed基于父元素容器定位,而不是基于body定位。效果如下:

浅谈css网页的布局问题

在上图中我可以发现中间黑色的小框是基于父级来定位,并且宽度也基于父容器的50%。详细的请看下面代码:

    关于position的定位的坑    body{        margin: 0;        padding: 0;    }    i{        font-style: normal;        cursor: pointer;    }    #delete-button{        position: absolute;        left: 45%;        top: 45%;        text-align: center;        vertical-align: middle;        height: 50px;        margin: auto;        cursor: pointer;    }    #delete-button > i{        display: inline-block;        width: 32px;        height: 32px;        border-radius: 16px;        background-color: orange;        color: red;        font-size: 32px;        vertical-align: middle;        line-height: 28px;    }    /*第一个模态框的样式*/    #layout{        display: none;        width: 100%;        height: 100%;    }    /*使用flex布局水平竖直居中*/    /*#layout-box{        position: fixed;        width: 100%;        height: 100%;        left: 0;        top: 0;        display: flex;        display: -webkit-flex;        flex-flow: column nowrap;        justify-content: center;        align-items: center;        background-color: rgba(0,0,0,0.3);    }*/    /*使用postion 和 transform 水平垂直居中*/    #layout-box{        position: fixed;        width: 100%;        height: 100%;        background-color: rgba(0,0,0,0.3);    }    .modal-dialog{        position: absolute;        left: 50%;        top: 50%;        width: 500px;        height: 200px;        border-radius: 10px;        transform: translate(-50%, -50%);        -webkit-transform: translate(-50%, -50%);        -moz-transform: translate(-50%, -50%);        -o-transform: translate(-50%, -50%);        background-color: #fff;    }    .dialog-title{        text-align: center;        color: #333;        font-size: 28px;        margin-bottom: 10px;    }    .dialog-content{        text-align: center;        color: #666;        font-size: 18px;    }    .dialog-button{        margin-top: 20px;        width: 100%;        color: #333;    }    .dialog-button >.button-box{        display: inline-block;        width: 48%;        text-align: center;    }    .button-box span{        display: inline-block;        padding: 10px;        color: #fff;        border-radius: 6px;        cursor: pointer;    }    #confirm{        background-color: #27ad9a;    }    #cancel{        background-color: red;    }    /*添加按钮的样式*/    #add-button > i{        display: inline-block;        width: 32px;        height: 32px;        border-radius: 16px;        background-color: #27ad9a;        color: #fff;        font-size: 32px;        vertical-align: middle;        line-height: 28px;        text-align: center;    }    #add-button{        display: inline-block;        cursor: pointer;    }    /*第二个模态框的样式*/    .layout2{        display: none;        position: fixed;        width: 100%;        height: 100%;        left: 0;        top: 0;        background-color: rgba(0,0,0,0.2);    }    .modal-dialog2{        position: fixed;        left: 50%;        top: 50%;        width: 50%;        height: 50%;        border-radius: 10px;        transform: translate(-50%, -50%);        -webkit-transform: translate(-50%, -50%);        -moz-transform: translate(-50%, -50%);        -o-transform: translate(-50%, -50%);        background-color: rgba(0,0,0,0.2);    }    .modal-dialog2 > span{        display: block;    }    .modal-text{        float: left;    }    #close{        color: red;        font-size: 24px;        float: right;        cursor: pointer;    }    

-删除

提示

是否删除该项,点击确定

确定

取消

+添加

document.getElementById("delete-button").onclick= function(){ var layout = document.getElementById("layout") layout.style.display = "block" } document.getElementById("confirm").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("cancel").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("add-button").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "block" } document.getElementById("close").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "none" }

如果我们尝试把父容器上的transform属性去除,我们可以看到 子容器没有基于父容器定位,而是基于body定位的,宽度也是基于body给的50%宽度。效果图如下:

浅谈css网页的布局问题

详细请看代码:

    关于position的定位的坑    body{        margin: 0;        padding: 0;    }    i{        font-style: normal;        cursor: pointer;    }    #delete-button{        position: absolute;        left: 45%;        top: 45%;        text-align: center;        vertical-align: middle;        height: 50px;        margin: auto;        cursor: pointer;    }    #delete-button > i{        display: inline-block;        width: 32px;        height: 32px;        border-radius: 16px;        background-color: orange;        color: red;        font-size: 32px;        vertical-align: middle;        line-height: 28px;    }    /*第一个模态框的样式*/    #layout{        display: none;        width: 100%;        height: 100%;    }    /*使用flex布局水平竖直居中*/    #layout-box{        position: fixed;        width: 100%;        height: 100%;        left: 0;        top: 0;        display: flex;        display: -webkit-flex;        flex-flow: column nowrap;        justify-content: center;        align-items: center;        background-color: rgba(0,0,0,0.3);    }    /*使用postion 和 transform 水平垂直居中*/    .modal-dialog{        width: 500px;        height: 200px;        border-radius: 10px;        background-color: #fff;    }    .dialog-title{        text-align: center;        color: #333;        font-size: 28px;        margin-bottom: 10px;    }    .dialog-content{        text-align: center;        color: #666;        font-size: 18px;    }    .dialog-button{        margin-top: 20px;        width: 100%;        color: #333;    }    .dialog-button >.button-box{        display: inline-block;        width: 48%;        text-align: center;    }    .button-box span{        display: inline-block;        padding: 10px;        color: #fff;        border-radius: 6px;        cursor: pointer;    }    #confirm{        background-color: #27ad9a;    }    #cancel{        background-color: red;    }    /*添加按钮的样式*/    #add-button > i{        display: inline-block;        width: 32px;        height: 32px;        border-radius: 16px;        background-color: #27ad9a;        color: #fff;        font-size: 32px;        vertical-align: middle;        line-height: 28px;        text-align: center;    }    #add-button{        display: inline-block;        cursor: pointer;    }    /*第二个模态框的样式*/    .layout2{        display: none;        position: fixed;        width: 100%;        height: 100%;        left: 0;        top: 0;        background-color: rgba(0,0,0,0.2);    }    .modal-dialog2{        position: fixed;        left: 50%;        top: 50%;        width: 50%;        height: 50%;        border-radius: 10px;        transform: translate(-50%, -50%);        -webkit-transform: translate(-50%, -50%);        -moz-transform: translate(-50%, -50%);        -o-transform: translate(-50%, -50%);        background-color: rgba(0,0,0,0.2);    }    .modal-dialog2 > span{        display: block;    }    .modal-text{        float: left;    }    #close{        color: red;        font-size: 24px;        float: right;        cursor: pointer;    }    

-删除

提示

是否删除该项,点击确定

确定

取消

+添加

document.getElementById("delete-button").onclick= function(){ var layout = document.getElementById("layout") layout.style.display = "block" } document.getElementById("confirm").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("cancel").onclick=function(){ var layout = document.getElementById("layout") layout.style.display = "none" } document.getElementById("add-button").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "block" } document.getElementById("close").onclick=function(){ var layout = document.getElementsByClassName("layout2") layout[0].style.display = "none" }

第二:解决在手机上的抖动问题(ps:这个问题我参照了网上大神写的一篇博客请移至文末查看)

**一、**在webkit内核浏览器中 给fixed加上防抖样式 – webkit – transform: translateZ(0);

**二、**设置html 和body 的css {height:100%;overflow:auto;margin:0;} 这个影响全局样式不建议使用。

三、在fiexd内设置position:absolute,如下:

浅谈css网页的布局问题 

4、百分比布局 主要通过设置元素的宽度为百分比或者高度为百分比。比如:width:50%; height:50%; 这样的写法。

5、响应式布局(主要使用媒体查询来实现响应式设计) 主要使用CSS3 @media 来做不同终端的响应式设计

主要在css文件中写入

@media screen and (max-width:600px){    写入当屏幕小于或等于600px时的样式}@media screen and (min-width:900px){    写入当屏幕大于或等于900px时的样式}@media screen and (min-width:600px) and (max-width:900px){    写入当屏幕在600px-900px之间的样式}@media screen and (max-device-width: 480px){    写入最大设备宽度为480px,比如说iPhone上的显示,这里的max-device-width所指的是设备的实际分辨率,也就是指可视面积分辨率}@media only screen and (-webkit-min-device-pixel-ratio: 2){    写入专门针对iPhone4的移动设备样式}@media all and (orientation:portrait){    写入设备在纵向时的样式}@media all and (orientation:landscape){    写入设备在横向时的样式}@media not print and (max-width: 1200px){    not是用来排除某种制定的媒体类型    写入在除打印设备和设备宽度小于1200px下的所有设备的样式}@media only screen and (max-device-width:240px){    only用来定某种特定的媒体类型,可以用来排除不支持媒体查询的浏览器。    写入只能在最大设备宽度为240px的屏幕下使用的样式}

相信看了本文案例你已经掌握了方法,更多精彩请关注创想鸟其它相关文章!

推荐阅读:

CSS Gird的布局详解

CSS实现波浪移动

以上就是浅谈css网页的布局问题的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1609222.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月24日 00:39:08
css3绘制圆形loading转圈动画
下一篇 2025年12月24日 00:39:27

相关推荐

  • 百度浏览器兼容模式切换 解决网页显示问题的实用指南

    兼容模式可解决百度浏览器访问老旧网站时的显示问题,通过切换至IE内核模拟渲染。2. 网页异常多因历史遗留技术与现代标准不兼容,兼容模式用Trident内核适配旧站,极速模式用Blink内核支持现代网页。3. 布局错乱、功能失效、资源无法加载等现象提示需切换兼容模式,点击地址栏右侧IE图标选择即可。 …

    2026年8月29日
    000
  • laravel8 的优化点

    Laravel 8 针对性能优化提供了以下选项:缓存配置:使用 Redis 缓存驱动、缓存门面、缓存视图和页面片段。数据库优化:建立索引、使用查询范围、使用 Eloquent 关系。JavaScript 和 CSS 优化:使用版本控制、合并和缩小资产、使用 CDN。代码优化:使用 Composer …

    2026年8月29日
    100
  • iaravel 如何学习

    学习 Laravel 的步骤有:具备 PHP 基础知识。安装 Laravel 框架。了解 Laravel 架构。学习基本语法:路由、控制器、视图、模型。构建小型应用练习框架使用。参阅官方文档获取详细指引。加入社区交流心得、寻求帮助。保持更新,关注新特性。 如何学习 Laravel Laravel 是…

    2026年8月29日
    000
  • VSCode盒子背景怎么居中_VSCode界面元素居中显示教程

    答案:通过Zen模式结合手动调整窗口大小,可实现VSCode代码区域的视觉居中。进入Zen模式(Ctrl+K Z)隐藏非编辑元素,再将窗口拖窄并置于屏幕中央,使代码居中显示,提升专注度;也可使用“Centered Editor”类插件强制居中,或利用系统窗口管理功能优化布局。配合主题、字体、面板位置…

    2026年8月29日
    100
  • 如何解决Laravel项目中生成PDF文档的问题?spatie/laravel-pdf助你轻松实现!

    可以通过一下地址学习composer:学习地址 在 laravel 项目中,生成 pdf 文档是一个常见的需求,尤其是在需要生成报表、发票或其他文档时。然而,传统的生成 pdf 的方法往往复杂且难以实现现代化布局。最近,我在项目中遇到了这样的问题:需要生成一个包含复杂布局的发票 pdf,传统方法难以…

    用户投稿 2026年8月29日
    500
  • VSCode怎么编写地图定位_VSCode集成地图API开发位置服务应用教程

    答案是集成地图API实现定位需选择服务商、引入SDK、初始化地图并调用定位功能。具体为:在VSCode中创建Web项目,引入百度等地图API的SDK,通过HTML页面加载地图容器,使用JavaScript初始化地图实例,并结合浏览器Geolocation API或地图SDK自带控件获取位置,最后添加…

    2026年8月29日
    100
  • ThinkPHP 6 环境配置(Nginx/Apache + PHP 8)

    配置 thinkphp 6 环境需要在 nginx 或 apache 上结合 php 8 进行设置。1) nginx 配置:编辑 nginx.conf 文件,设置 server 块以正确处理 php 文件。2) apache 配置:在 httpd.conf 文件中添加 virtualhost 配置,…

    2026年8月29日
    200
  • Mac+IDEA+百度富文本UEditor

    Mac+IDEA+百度富文本UEditorMac+IDEA+百度富文本UEditorMac+IDEA+百度富文本UEditorMac+IDEA+百度富文本UEditor

    最近,我在项目中需要嵌入一个富文本编辑器,考虑到百度ueditor的强大功能,决定使用它。然而,从%ignore_a_1%+eclipse环境转到mac+idea后,遇到了几个问题。经过一番努力,终于解决了这些问题,现分享我的经验。 环境:Mac + IDEA + UEditor 基于 Maven+…

    2026年8月29日 用户投稿
    200
  • 如何解决HTML解析问题?使用paquettg/php-html-parser可以!

    可以通过一下地址学习composer:学习地址 在开发一个需要从网页中提取特定数据的项目时,我遇到了一个棘手的问题:如何高效地解析和操纵html内容。尝试了几种方法后,我发现这些方法要么过于复杂,要么不够灵活,无法满足我的需求。最终,我找到了paquettg/php-html-parser这个库,它…

    用户投稿 2026年8月29日
    400
  • VSCode怎么更改鼠标颜色_VSCode自定义鼠标指针颜色与样式教程

    答案:可通过修改VSCode设置自定义光标颜色、样式、粗细及闪烁方式以提升编码体验。具体包括在settings.json中配置editor.cursorStyle、editor.cursorWidth、editor.cursorBlinking,以及通过workbench.colorCustomiz…

    2026年8月28日
    100
  • 推荐几款好用的文本编辑器

    推荐几款好用的文本编辑器推荐几款好用的文本编辑器推荐几款好用的文本编辑器推荐几款好用的文本编辑器

    作为程序员,编写和查看代码是日常工作的重要部分。今天,我将为大家介绍几款实用的文本编辑器。 Sublime Text 是一款轻量、简洁、高效且跨平台的编辑器。 Sublime Text的特色功能包括: 出色的扩展功能,官方称为安装包(Package)。它没有传统的右侧滚动条,而是用代码缩略图代替,这…

    2026年8月28日 用户投稿
    200
  • 如何解决PHP中生成随机内容的问题?使用pragmarx/random库可以!

    可以通过一下地址学习composer:学习地址 在开发一个php项目时,我需要生成各种随机内容,包括字符串、数字和符合特定模式的文本。这个需求看似简单,但实际上却充满了挑战。最初,我尝试使用php的内置函数如rand()和mt_rand(),但这些函数生成的随机数不够安全,无法满足我的需求。此外,我…

    用户投稿 2026年8月28日
    100
  • 如何清理浏览器缓存_各浏览器缓存清除指南

    清理浏览器缓存是指删除浏览器为加速网页加载而存储的临时文件,如图片、css、javascript等;2. 主要目的是解决网页显示错乱、加载旧内容等问题,并释放硬盘空间;3. 操作核心是进入浏览器设置或使用快捷键,选择清除数据类型时务必勾选“缓存的图片和文件”;4. 不同浏览器操作路径略有差异:chr…

    2026年8月28日
    200
  • VSCode怎么建立外部样式_VSCode链接外部CSS文件方法教程

    首先创建style.css文件并编写样式,然后在HTML的head中通过link标签引入,最后用Live Server插件实现实时预览;若样式未生效,需检查路径、语法、优先级、缓存及HTML结构,并利用开发者工具调试;大型项目应按模块或页面拆分CSS,结合预处理器、BEM规范和CSS Reset提升…

    2026年8月28日
    200
  • 悟空浏览器如何设置打印网页时不带页眉页脚_悟空浏览器打印网页去掉页眉页脚方法

    在悟空浏览器中打印网页时,可通过打印预览设置取消页眉页脚:打开打印界面后展开更多设置并取消勾选“页眉和页脚”;2. 使用开发者工具添加CSS样式@media print { @page { margin: 0; } }可彻底去除页眉页脚及边距;3. 或将网页导出为PDF:选择虚拟PDF打印机并关闭页…

    2026年8月28日
    100
  • HTTP1.1与HTTP2的区别_HTTP1.1与HTTP2有哪些区别

    http/2通过多路复用在单个tcp连接上并行传输多个独立的数据流,每个流的数据被分割为带流id的二进制帧并交错发送,接收方根据流id重新组装,从而避免了http/1.1中因单个请求阻塞导致后续请求等待的队头阻塞问题;2. hpack头部压缩通过静态字典、动态字典和哈夫曼编码减少重复头部信息的传输量…

    2026年8月28日
    400
  • 如何进入调试模式_怎样启用开发者调试模式

    启用开发者调试模式的具体步骤因设备或软件环境而异,最常见的场景是网页浏览器和安卓设备;在chrome浏览器中可通过f12快捷键或右键“检查”开启开发者工具,其中包含元素、控制台、网络等调试功能;安卓设备需在“关于手机”中连续点击“版本号”七次以激活“开发者选项”,随后可启用usb调试等功能。2. 调…

    2026年8月27日
    200
  • SSL证书是什么_SSL证书申请方式有哪些

    SSL证书是什么_SSL证书申请方式有哪些SSL证书是什么_SSL证书申请方式有哪些SSL证书是什么_SSL证书申请方式有哪些SSL证书是什么_SSL证书申请方式有哪些

    SSL证书是网站安全的保障,通过加密技术保护数据传输,提升用户信任和搜索引擎排名。申请方式主要有从CA机构购买或通过云服务商获取免费/付费证书。根据网站类型选择合适的证书:DV适合个人网站,OV适合企业,EV适合高安全需求的金融类网站。手动配置较复杂,但云平台提供的一键部署功能可简化流程。免费证书虽…

    2026年8月27日 用户投稿
    300
  • 如何解决前端项目中的图标管理问题?使用Composer可以轻松集成BootstrapIcons

    可以通过以下地址学习Composer:学习地址 在前端开发中,图标的选择和管理一直是一个让人头疼的问题。最近在开发一个新项目时,我遇到了一个棘手的情况:项目中需要使用大量图标,但这些图标来自不同的来源,导致样式不统一,管理起来也非常复杂。这不仅影响了项目的美观度,还严重拖慢了开发进度。 为了解决这个…

    用户投稿 2026年8月27日
    100
  • 如何优雅地在LaravelBlade视图中使用图标?mallardduck/blade-boxicons助你轻松搞定!

    可以通过一下地址学习composer:学习地址 在现代 web 应用中,图标扮演着不可或缺的角色。它们不仅能美化界面,还能直观地传达信息,提升用户交互体验。然而,对于 laravel 开发者来说,将图标整合到 blade 视图中,却常常伴随着一些“甜蜜的烦恼”。 还记得那些日子吗?为了在项目中引入一…

    用户投稿 2026年8月27日
    100

发表回复

登录后才能评论
关注微信