浅谈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网页的布局问题的详细内容,更多请关注【创想鸟】其它相关文章!

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

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2899473.html

(0)
上一篇 2025年3月10日 23:46:26
下一篇 2025年3月10日 23:46:37

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • 谈谈CSS中的几种选择器

    今天本文主要和大家谈谈CSS中的几种选择器 ,需要的朋友可以参考下,希望能帮助到大家。 1、通配符选择器 通配符选择器用“*”号表示,是所有选择器中作用范围最广的,能匹配页面中所有的元素 /*设置当前页面中所有标签的颜色为红色*…

    编程技术 2025年3月10日
    200
  • 使用html和css实现康奈尔笔记

    这次给大家带来使用html和css实现康奈尔笔记,使用html和css实现康奈尔笔记的注意事项有哪些,下面就是实战案例,一起来看一下。 缘起 人家都说 康奈尔笔记 法,很好用呢,能抵抗遗忘曲线,让你的笔记事半功倍,有兴趣的同学自行百度哈。 …

    编程技术 2025年3月10日
    200
  • CSS的子代选择符

    这次给大家带来CSS的子代选择符,使用CSS的子代选择符的注意事项有哪些,下面就是实战案例,一起来看一下。 后代选择符用于选取一个标签的所有后代,包括子代和孙辈等;而子代选择符只选取指定父辈的子代标签(指定标签元素的第一代子元素)。 子代选…

    2025年3月10日 编程技术
    200
  • 纯css实现树形结构

    这次给大家带来纯css实现树形结构,纯css实现树形结构的注意事项有哪些,下面就是实战案例,一起来看一下。 本文我给大家介绍如何使用CSS和HTML就可以将一个多级无序列表的节点展现成树状结构。树状结构我们在很多项目中要应用,如公司组织架构…

    2025年3月10日
    200
  • CSS实现手风琴布局

    这次给大家带来CSS实现手风琴布局,CSS实现手风琴布局的注意事项有哪些,下面就是实战案例,一起来看一下。 昨天在做一个旅游页面的项目,前端页面实现的过程中遇到这样一个需求。需要把一组图片形成手风琴的展示效果。认真的思考一遍后,决定就用普通…

    2025年3月10日
    200
  • CSS3中的transform功能详解

    这次给大家带来CSS3中的transform功能详解,CSS3中transform功能的注意事项有哪些,下面就是实战案例,一起来看一下。 CSS3中的变形功能:在CSS3中可以利用transform功能来实现文字或图像的旋转、缩放、倾斜、移…

    2025年3月10日 编程技术
    200
  • CSS怪异盒模型和标准盒模型如何使用

    这次给大家带来CSS怪异盒模型和标准盒模型如何使用,使用CSS怪异盒模型和标准盒模型的盒模型有哪些,下面就是实战案例,一起来看一下。 在盒模型中,每个渲染在页面中的标签都是一个个盒模型。 盒子模型又分为 : W3C标准的盒子模型 和 IE标…

    2025年3月10日 编程技术
    200
  • css3的动画序列animation

    这次给大家带来css3的动画序列animation,使用css3动画序列animation的注意事项有哪些,下面就是实战案例,一起来看一下。 首先复习一下animation动画添加各种参数 (1)infinite参数,表示动画将无限循环。在…

    2025年3月10日 编程技术
    200
  • 用css隐藏input的光标

    这次给大家带来用css隐藏input的光标,用css隐藏input的光标的有哪些,下面就是实战案例,一起来看一下。 前言 最近公司的ui突然跑过来问我一个问题:“如何在不影响操作的情况下,把input的光标隐藏了?”。 我相信很多人会跟我一…

    2025年3月10日
    200
  • css实现缩略图悬停界面

    这次给大家带来css实现缩略图悬停界面,css实现缩略图悬停界面的注意事项有哪些,下面就是实战案例,一起来看一下。 前言 前端本身很杂,想必在学前端的大家都懂,前端技能的各种学习心得,各种教程,只要你有一颗学习的心都可以搞定,关键在于你有没…

    2025年3月10日
    200

发表回复

登录后才能评论