分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

css布局

发布时间:2023-09-06 01:50责任编辑:赖小花关键词:暂无标签

CSS布局应该说是页面开发中最基本的,常见的有两列布局、三列布局等。

如要实现一个两列布局(高度固定),左侧固定宽度(300px),右侧自适应,通常想到的方法是浮动和绝对定位,这也是我想到的第一方案,那还有没有其他方案呢?在此总结5种布局方案。

1.浮动方案

<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<title>Title</title> ???<style> ???????.layout ?div{ ???????????height: 100px; ???????} ???????/*浮动方式*/ ???????.left{ ???????????float: left; ???????????width: 300px; ???????????background-color: red; ???????} ???????.right{ ???????????margin-left: 300px; ???????????background-color: blue; ???????} ???</style></head><body><section class="layout"> ????<div class="left"></div> ????<div class="right"><h1>浮动解决方案</h1></div></section></body></html>

2.绝对定位方案

<style> ???????/*绝对定位方式*/ ???????.layout > div{ ???????????position: absolute; ???????????height:100px; ???????} ???????.left{ ???????????left: 0; ???????????width: 300px; ???????????background-color: red; ???????} ???????.right{ ???????????left: 300px; ???????????background-color: blue; ???????????right: 0; ???????}
</style><section class="layout"> ????<div class="left"></div> ????<div class="right"><h1>绝对定位解决方案</h1></div></section> ???????

3.table方式布局

<style> ???????/*table方式*/
.layout{
???display: table;
???width: 100%;
}
.layout >div{
???display: table-cell;
???height:100px;
}
.left{
???width: 300px;
???background-color: red;
}
.right{
???background-color: blue;
}
 </style> 

<section class="layout">
  <div class="left"></div>
  <div class="right">
    <h1>table解决方案</h1>
  </div>
</section>

4.flex布局

 /*flex布局*/ ???????.layout{ ???????????display: flex; ???????} ???????.layout>div{ ???????????height:100px; ???????} ???????.left{ ???????????width: 300px; ???????????background-color: red; ???????} ???????.right{ ???????????flex:1; ???????????background-color: blue; ???????} ???????<section class="layout"> ???????????<div class="left"></div> ???????????<div class="right"><h1>flex解决方案</h1></div> ???????</section>

5.grid布局

/*grid布局*/ ???????.layout{ ???????????display: grid; ???????????width:100%; ???????????grid-template-rows: 100px; ???????????grid-template-columns: 300px auto; ???????} ???????.left{ ???????????background-color: red; ???????} ???????.right{ ???????????background-color: blue; ???????}<section class="layout"> ???????<div class="left"></div> ???????<div class="right"> <h1>网格布局定位解决方案</h1></div></section>

css布局

原文地址:https://www.cnblogs.com/jingmi-coding/p/8900488.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved