css常见布局

css常见布局基础版 1 两列布局两列布局是一侧固定 另一侧自适应 一般一个浮动 另一个设置 margin 或者一个绝对定位 另一个设置 margin 代码如下 浮动方法 style gt left float left width 200px background green height 200px right

基础版

1.两列布局

  两列布局是一侧固定,另一侧自适应。一般一个浮动,另一个设置margin,或者一个绝对定位,另一个设置margin,代码如下(浮动方法)

<style> .left{ float: left; width: 200px; background: green; height: 200px; } .right{ margin-left:200px; background: red; height: 200px; } </style> <body> <div class="left">左边左边左边左边左边左边</div> <div class="right">右边右边右边右边右边右边</div> </body> 

2.三列布局

  三列布局特点是其中两列宽度固定,剩下一个自适应,方法是两个浮动自适应那个设置margin,比如左右固定,中间自适应可以左右分别设置float为left,right,然后中间那个设置margin left right分别为左右两栏宽度。代码如下

<style> .left{ float: left; width: 200px; background: green; height: 200px; } .center{ margin: 0 300px 0 200px; background: yellow; height: 200px; } .right{ float: right; width:300px; background: red; height: 200px; } </style> <body> <div class="left">左边左边左边左边左边左边</div> <div class="right">右边右边右边右边右边右边</div> <div class="center">中间中间中间中间中间中间</div> </body> 

进阶版

1.两列布局
  还是那个效果,这次改用flex布局来写,代码如下:

<style> body{ display: flex; justify-content: flex-end; } .left{ flex: 1; background: green; height: 200px; } .right{ width:300px; background: red; height: 200px; } </style> <body> <div class="left">自适应自适应自适应自适应</div> <div class="right">固定固定固定固定固定固定</div> </body> 
<style> body{ display: flex; justify-content: space-between; } .left{ width:200px; background: green; height: 200px; } .right{ width:100px; background: red; height: 200px; } .center{ flex: 1; background: yellow; height: 200px; } </style> <body> <div class="left">固定固定固定固定固定固定</div> <div class="center">自适应自适应自适应自适应</div> <div class="right">固定固定固定固定固定固定</div> </body> 

存在于面试中的版本

1.圣杯布局
  特点是有个头部底部,中间是主要内容区,主要内容区又分成左中右三块,其中中间是最主要内容区,思路是朝一个方向浮动,再用负margin把挤下去的两侧给拉上来再设置相对定位,为了避免中间里面内容被拉上来的挡住再对中间内容设置padding。代码如下:

<style> header{ background: cadetblue; } footer{ background: antiquewhite; } .center{ width: 100%; background: coral; } .main{ float: left; width: 100%; background: yellow; height: 200px; } .main p{ padding: 0 100px 0 200px; } .left{ float: left; margin-left: -100%; width:200px; position: relative; left: 0; background: green; height: 200px; } .right{ float: left; margin-left: -100px; width:100px; position: relative; right: 100px; background: red; height: 200px; } </style> <body> <header>我是头部</header> <div class="center"> <div class="main"><p>我最重要</p></div> <div class="left">左边边</div> <div class="right">右边边</div> </div> <footer>我是底部</footer> </body> 

2.双飞翼布局
  与圣杯布局类似,只是在中间内容区加了个父元素,这样可以省略对左右栏设置相对定位。代码如下:

<style> header{ background: cadetblue; } footer{ background: antiquewhite; } .center{ width: 100%; background: coral; } .main-father{ width: 100%; float: left; background: blueviolet; } .main{ padding: 0 100px 0 200px; background: yellow; height: 200px; } .left{ float: left; margin-left: -100%; width:200px; background: green; height: 200px; } .right{ float: left; margin-left: -100px; width:100px; background: red; height: 200px; } </style> <body> <header>我是头部</header> <div class="center"> <div class="main-father"> <div class="main">我最重要</div> </div> <div class="left">左边边</div> <div class="right">右边边</div> </div> <footer>我是底部</footer> </body> 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233309.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • INSERT INTO SELECT语句与SELECT INTO FROM语句区别

    INSERT INTO SELECT语句与SELECT INTO FROM语句区别1.INSERTINTOSELECT语句语句形式为:InsertintoTable2(field1,field2,…)selectvalue1,value2,…fromTable1或者:InsertintoTable2select*fromTable1注意:(1)要求目标表Table2必须存在,并且字段field,field2…也必须存在(2)注意Table2的主

    2022年7月15日
    13
  • 传感器尺寸、像素、DPI分辨率、英寸、毫米的关系

    传感器尺寸、像素、DPI分辨率、英寸、毫米的关系虽然网上有很多这种资料,但是太过于复杂,每个人的说法都不一样,看的让人云里雾里的,我总结了一下,不知道对不对!1.1英寸=25.4mm2.传感器尺寸:传感器的尺寸是指传感器的大小,一般描述大小有两种形式,以IMX386感光元件为例,其传感器尺寸1/2.9英寸,是指传感器对角线为1/2.9英寸;还可以描述成传感器尺寸4.97mm×6.2mm,是指水平(竖直)长(宽)为4.97(6.2)m…

    2022年6月13日
    54
  • SpringBoot项目:net.sf.jsqlparser.parser.ParseException: Encountered unexpected token:XXXXX

    SpringBoot项目:net.sf.jsqlparser.parser.ParseException: Encountered unexpected token:XXXXX

    2020年11月9日
    456
  • sigaction实例

    sigaction实例#include#include#include#include#include#include#include#include#include#include#include staticvoidsignal_handle(intsign){       printf(“Test:Systemsignal(%d

    2022年5月9日
    43
  • centos6 7 zabix grafana安装配置

    centos6 7 zabix grafana安装配置一 安装 zabbix0 关闭 selinuxvim etc selinux configSELINU disabled 设置后需要重启才能生效 shell gt setenforce0 临时关闭 shell gt getenforce 检测 selinux 状态 1 Zabbix 在 CentOS 基本源里不可获得或者获得到的都是老版本的 因此必须配置 Zabbix 官方 r

    2025年7月30日
    2
  • 用javascript教你打造一个免费的PC微信机器人《五》微信群发

    用javascript教你打造一个免费的PC微信机器人《五》微信群发上图先,来个效果图同一时间,向很多个群,发送你要发送的消息。现实中有很多解决方案,一个员工对应一个群,先把发送内容粘贴进文本框,读秒,同时按发送键。按键精灵,脚本?(没试过,有可能可以)还有其他方案没?一言不合,上代码先拿到你要发送的群或者好友列表(websocket,http去github去看httpclient.js):functionget_wxuser_list(){constj={id:getid(),type:USER_LIST,cont

    2022年5月4日
    71

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号