首页 > 其他分享 >浮动和定位

浮动和定位

时间:2024-07-08 16:22:09浏览次数:6  
标签:浮动 定位 100px height 1px border display block

4.1标准文档流

快级元素:独占一行

h1~h6 p div 列表

行内元素

span a img strong

行内元素可以被包含在快级元素中,反之则不行

4.2 display

   <style>
       /*
      block 块级元素
      inline 行内元素
      inline-block 是块元素 但是可以内联,在一行
      none
      */
     div{
       width: 100px;
       height: 100px;
       border: 1px solid red;
       display: inline-block;
    }
     span{
       width: 100px;
       height: 100px;
       border: 1px solid yellow;
       display: block;
    }
   </style>

 

4.3 float

 

div{
  margin: 10px;
  padding: 5px;
}

.father{
  border: 1px solid red;
}

.lay01{
  border:1px dashed yellow;
  display: inline-block;
  float: left;
}

.lay02{
  border:1px dashed #00f;
  display: inline-block;
  float: left;
}

.lay03{
  border:1px #060 dashed;
  display: inline-block;
  float: left;
}

.lay04{
  border:1px #666 dashed;
  font-size: 12px;
  line-height: 23px;
  display: inline-block;
}


<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <link rel="stylesheet" href="css/css.css">
</head>
<body>
<div class="father">
   <div class="lay01"><img src="images/7b5a2e7106b446a128867def60f2396.png" alt=""></div>
   <div class="lay02"><img src="images/1578d3d603227fc0a8032ad56d3ca46.png" alt=""></div>
   <div class="lay03"><img src="images/f00b6f9124eacef11086bbfbaa7df94.png" alt=""></div>
   <div class="lay04">浮动的盒子可以向左浮动,也可以向右浮动</div>
</div>
</body>

 

4.4 父级文档流塌陷的问题

clear
/*
clear:right 右侧不允许浮动
clear:left 左侧不允许浮动
clear:both 两侧不允许浮动
clear:none
*/

解决方案

1.增加父级元素的高度

.father{
   border: 1px solid red;
   height: 800px;
}

2.增加一个空的div标签,清除浮动

<div class="clear"></div>
.clear{
   margin: 0;
   padding: 0;
   clear: both;
}

3.overflow

在父级元素中增加一个overflow:hidden

4.在父类中添加一个伪类

.father:after{
   content:'';
   display: block;
   clear: both;
}

小结:

1.浮动元素中增加一个空div

简单,代码中尽量避免空div

2.设置父元素的高度

简单,元素假设有了固定的高度,就会被限制

3.overflow

简单,下拉的一些场景避免使用

4.父类添加一个伪类:after(推荐)

没有副作用,推荐使用

4.5 display和float

display

方向不可以控制

float

浮动起来的会脱离标准文档流,所以要解决父级边框塌陷的问题

5定位

5.1相对定位

position: relative;

方块练习

    <style>
       #box{
           padding: 20px;
           border: 1px solid red;
           width: 300px;
           height: 300px;
      }
       a{
           width: 100px;
           height: 100px;
           text-decoration: none;
           background-color: rgba(183, 0, 255, 0.99);
           line-height: 100px;
           text-align: center;
           color: white;
           display: block;
      }
       a:hover{
           background: #47a4ff;
      }
       #a2{
           position: relative;
           left: 200px;
           top:-100px

      }
       #a5{
           position: relative;
           left:200px;
           top:-200px;
      }
       #a4{
           position: relative;
           left:100px;
           top:-200px;
      }
   </style>
</head>
<body>
<div id="box">
   <a href="#" id="a1">链接1</a>
   <a href="#" id="a2">链接2</a>
   <a href="#" id="a3">链接3</a>
   <a href="#" id="a4">链接4</a>
   <a href="#" id="a5">链接5</a>
</div>
</body>

5.2 绝对定位

定位:基于xxx定位,上下左右

1.没有父级元素定位的前提下,相对于浏览器定位

2.假设父级元素存在定位,我们通常会相对于父级元素进行偏移

3.在父级元素范围内移动

相对于父级或浏览器的位置,进行定位的偏移,绝对定位的话,它不在标准文档流中,原来的位置不会被保留

    <style>
       div{
           margin: 10px;
           padding: 5px;
           font-size: 12px;
           line-height: 25px;
      }
       #father{
           border: 1px solid red;
           padding: 0;
      }
       #first{
           background-color: #a13d;
           border: 1px dashed #b27;
      }
       #second{
           background-color: #255099;
           border: 1px dashed #255066;
           position: absolute;
           right:30px;
      }
       #third{
           background-color: #1c6699;
           border:1px dashed #1c6615;
      }
   </style>
</head>
<body>
<div id="father">
   <div id="first">第一个盒子</div>
   <div id="second">第一个盒子</div>
   <div id="third">第一个盒子</div>
</div>
</body>

5.3 固定定位

    <style>
       body{
           height: 900px;
      }
       div:nth-of-type(1){/*绝对定位:相对于浏览器*/
           width: 100px;
           height: 100px;
           background: red;
           position: absolute;
           right:0;
           bottom: 0;
      }
       div:nth-of-type(2){/*fixed,固定定位*/
           width: 50px;
           height: 50px;
           background: yellow;
           position: fixed;
           right:0;
           bottom: 0;
      }
   </style>
</head>
<body>
<div>div1</div>
<div>div2</div>
</body>

5.4 z-index

z-index:默认是0,最高无限制

<head>
   <meta charset="UTF-8">
   <title>Title</title>
 <link rel="stylesheet" href="css/style1.css">
</head>
<body>
<div id="content">
   <ul>
       <li><img src="images/01.png" alt=""></li>
       <li class="tipText">学习微服务</li>
       <li class="tipBg"></li>
       <li>时间</li>
   </ul>
</div>
</body>
#content{
   margin: 0;
   padding: 0;
   overflow: hidden;
   font-size: 12px;
   line-height: 25px;
   border: 1px solid #000000;
   width: 300px;
}

ul,li{
   margin: 0px;
   padding: 0px;
   list-style: none;
}
/*父级元素*/
#content ul{
   position: relative;
}

.tipText,.tipBg{
   position: absolute;
   width: 300px;
   height: 25px;
   top: 124px;
   color: white;
}
.tipBg{
   background: black;
   opacity: 0.5;/*背景透明度*/
}
.tipText{
   /*z-index: 990;*/
}
 

标签:浮动,定位,100px,height,1px,border,display,block
From: https://www.cnblogs.com/huangjiangfei/p/18290129

相关文章

  • CSS【详解】定位 position (静态定位 static -- 文档流排布 、相对定位 relative、绝对
    静态定位position:static【默认】此时,元素按文档流的方式排布:以左上角为起点内联元素从左到右依次排布,当一行排不下时,自动换到下一行继续从左到右排布块级元素独占一行此时,top、left、right、bottom、z-index等样式无效。相对定位position:relative相对......
  • MinGW GCC Boost Serialization 无法定位程序输入点 _ZSt19uncaught_exceptionsv 于动
     在Windows下使用MinGWGCC编译Boost和Demo程序,运行时报错:GCC: gccversion8.1.0(i686-posix-dwarf-rev0,BuiltbyMinGW-W64project)boost:boost1.85.0排查原因是GCC和Boost不匹配,适当降低boost版本后正常。GCC8.1是2018年,Boost1.85.0是2024年,时间差距比较大。......
  • 谷歌推广:精准定位潜在客户群体,提高广告转化率
    借助谷歌推广,你的品牌将如火箭升空般迅速崛起。作为全球最大的搜索引擎,其影响力犹如站在巨人之肩,触及的受众规模高达数十亿人。更为重要的是,我们可以准确锁定那些最有可能对贵公司的产品或服务产生浓厚兴趣的群体。这并非普通的广告宣传,而是助推您的品牌声名远扬的强大引擎。......
  • wsl安装Linux系统到指定位置
    默认情况下,wsl安装的系统,会安装到系统C盘,长期下去,很容易把C盘的空间消耗完,从而影响系统的正常运行,所以我建议是将wsl所有的系统都安装到其它磁盘中,便于维护。1、导出镜像通过wsl-l-v查看当前已安装的系统版本。导出到当前目录位置,也可以指定目录位置。wsl--......
  • 如何在JSP文件中接入高德地图实现地图展示和定位功能
    代码改进建议确保页面结构正确:检查页面的HTML结构,确保地图的容器<divid="container">正确放置。修改按钮功能:修正获取定位按钮的功能,确保它不会导致页面提交或其他问题。调整样式:调整地图容器的高度,确保地图有足够的空间显示。<!doctypehtml><%@pagecontentType=......
  • 智能扫地机器人编译过程中的定位导航技术
    智能扫地机器人的定位导航技术主要包括以下几种,下面将分点表示并归纳这些技术及其特点:1.随机碰撞导航:这是最早期的导航方式,扫地机器人通过随机移动来覆盖地面。特点:效率较低,容易重复清洁或遗漏区域。2.红外线导航:利用红外线传感器检测并避开障碍物。特点:相对简单,但清扫......
  • 第四章 对象的实例化内存布局与访问定位
    对象的实例化内存布局与访问定位对象的实例化 对象创建的方式(1)new:最常见的方式、单例类中调用getInstance的静态类方法,XXXFactory的静态方法(2)Class的newInstance方法:在JDK9里面被标记为过时的方法,因为只能调用空参构造器,并且权限必须为public(3)Cons......
  • 武汉凯迪正大分享电缆断点检测:定位电缆断点的技术与方法
    由于环境因素、施工质量等多种原因,电缆在使用过程中难免会出现断点故障,给生产和通信带来严重影响。因此如何快速准确地定位到电缆断点,成为了电力和通信领域亟待解决的问题。本文将从电缆断点检测的原理、方法以及技术实践等方面进行阐述,希望能给大家提供有益的参考也欢迎大家留......
  • vue elementUI el-tree 下拉树功能(包括搜索/默认高亮/展开下拉框默认定位于选中项的位
    <template><div><el-form:model="formData"ref="refFormData"label-width="180px"><el-form-itemlabel="景点"prop="location_id"><el-selectv-model="formData.location_name&qu......
  • 麻烦问一下xpath标签定位的这个索引是做什么用的?
    大家好,我是Python进阶者。一、前言前几天在Python最强王者交流群【杨又串......