[front end] CSS positioning

Four links and one new star 2020-11-13 03:08:30
end css positioning


Junior developer , There is something wrong with the article, please give me some advice , thank you !


1. Why use positioning ?

 Insert picture description here

  • If we need to achieve the above effect , Using standard flows and floats makes it impossible to achieve .
  • But positioning can make the box in a certain position Free to float in other boxes ( Including standard flow and float ) On top of .
  • A floating box can't hold img Of such elements .

2. Positioning of the 4 Middle class

value semantics
static static state location
relative relative location
absolute absolute location
fixed Fix location
  • stay CSS in , adopt position Attribute defines the Positioning mode , The grammar is as follows :
 Selectors  {
 position: Property value ; }

3. 4 The characteristics of each species

  • Static positioning
    (1) Static positioning It's the default way to locate elements , No location . It is equivalent to border Inside none, Don't use .

    (2) Static positioning Place according to standard flow characteristics , It has no edge offset .

    (3) We hardly use static positioning in layout

  • Relative positioning

    (1) Relative positioning Is an element relative In it The original position in the standard flow Speaking of ;

    (2) be relative to I used to move in the standard stream ;

    (3) original Continue to occupy in the area of standard flow , The back box still treats it like a standard stream .

  • Relative positioning example :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Relative positioning example </title>
<style>
div {

width: 300px;
height: 300px;
background-color: #f00;
margin: 100px;
position: relative;
top: 50px;
left: 50px;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

 Insert picture description here

  • analysis : Relative positioning is relative to the original position of the box , Because of the addition of margin( Margin ), The start of the box is no longer the top left corner of the browser , The beginning of the box changes to (100,100) , At this point, the starting position of relative positioning becomes (100,100).

  • Absolute positioning

    (1) Absolute positioning It's the element that moves the position with the parent element of the location

    (2) Complete de labeling ( Out of the standard stream ) —— Completely out of place ;

    (3) The parent element is not positioned , with browser Accurate positioning (Document file ).

  • Absolute positioning example ( When the parent box is not positioned but the child box is positioned )

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Absolute positioning example ( The parent box is not positioned )</title>
<style>
/* This example is where the parent box is not positioned : At this time, the sub box will be positioned based on the browser */
.father {

width: 300px;
height: 300px;
margin:100px
background-color: #f00;
}
.son {

position:absolute;
top: 100px;
left: 100px;
width: 100px;
height: 100px;
background-color: #ff0;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>

 Insert picture description here

  • Absolute positioning example ( When both father and son boxes are located )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Absolute positioning example ( Both father and son's boxes have positions )</title>
<style>
/* This example is when both parent and child boxes are positioned , At this time, the child box will be positioned by referring to the parent box */
.father {

width: 300px;
height: 300px;
background-color: #f00;
margin: 100px;
position: relative;
}
.son {

width: 100px;
height: 100px;
background-color: #ff0;
position:absolute;
top: 100px;
left: 100px;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>

 Insert picture description here

  • Absolute positioning example ( The parent box is not positioned , But the parent box and the child box of the parent box have positioning )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Absolute positioning ( Father's box is not located , But the parent box and the child box of the parent box have positioning </title>
<style>
.grandFather {

width: 500px;
height: 500px;
background-color: #f00;
margin: 100px;
position: relative;
}
.father {

width: 300px;
height: 300px;
background-color: #0f0;
}
.son {

width: 100px;
height: 100px;
background-color: #00f;
position: absolute;
top: 50px;
left: 50px;
}
</style>
</head>
<body>
<div class="grandFather">
<div class="father">
<div class="son"></div>
</div>
</div>
</body>
</html>

 Insert picture description here

  • analysis : Absolute positioning is based on the parent element with positioning , If the parent element is not positioned , The browser document is used as the benchmark for positioning , The parent element is not just the parent element of the previous level , It can also be ‘ Grandpa ’ Elements , If the parent element is not located , however ‘ Grandpa ’ The element also positions it according to **‘ Grandpa ’** Elements are positioned .

  • Absolute positioning attention :
    (1) The pithy formula of absolute positioning is The son is the father .

  • Why do we use relative positioning when the child box absolutely locates the parent box ?

    (1) The absolute positioning element does not occupy the position , It's out of the standard stream , The elements of relative position occupy the position , Will affect the display of later elements .

    (2) Relative positioning occupies the position , Normal display according to standard flow , The box at the back is shown as normal . If the parent box uses absolute positioning instead of relative positioning , The back box will appear below the parent box , Can't follow normal display .

    (3) Subboxes can be separated from the standard flow display by using absolute positioning , Achieve some special effects .
     Insert picture description here

  • Use the child absolute father phase to realize the small button effect suspended above the picture :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Example of the father and the son </title>
<style>
.up {

width: 1000px;
height: 90px;
position: relative;
}
.arr-l {

position: absolute;
top: 25px;
left: 0;
width: 40px;
height: 40px;
background-color: rgba(0, 0, 222, .7);
}
.arr-r {

position: absolute;
top: 25px;
right: 0;
width: 40px;
height: 40px;
background-color: rgba(0, 0, 222, .7);
}
.down {

width: 1000px;
height: 150px;
margin: 10px 0;
background-color: #000;
}
</style>
</head>
<body>
<div class="up">
<img src="../images/img.jpg" alt="">
<div class="arr-l"></div>
<div class="arr-r"></div>
</div>
<div class="down"></div>
</body>
</html>

 Insert picture description here


  • Fixed position
    (1) Fixed position yes Absolute positioning A special form of : ( The pattern of recognizing death ) If absolute positioning is a rectangle that Fixed positioning is like a square

    (2) Complete de labeling —— Completely out of place ;

    (3) Recognize only Browser's visual window —— Browser visual window + Edge offset property To set the location of the elements ;

     - It has nothing to do with the parent element ; Used alone
    - Don't scroll with the scroll bar .
    
  • Fixed positioning example ( Fixed positioning has nothing to do with the parent element )

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Fixed position ( Fixed positioning has nothing to do with the parent box )</title>
<style>
.box {

position: relative;
width: 300px;
height: 300px;
margin: 100px auto;
background-color: #ff0;
}
.box img {

position: fixed;
left: 0;
top: 0;
}
</style>
</head>
<body>
<div class="box">
<img src="../images/sun.jpg" alt=" monkey " width="150">
</div>
</body>
</html>

 Insert picture description here

  • Fixed positioning example ( Fixed location of the element value to recognize the browser's visual window )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> Fixed position ( Fixed location browser only visual window )</title>
<style>
.box {

width: 300px;
height: 300px;
background-color: #ff0;
margin: 100px auto;
}
.box img {

position: fixed;
right: 0;
bottom: 0;
}
</style>
</head>
<body>
<div class="box">
<img src="../images/sun.jpg" alt="" width="150">
</div>
</body>
</html>

 Insert picture description here

  • analysis : The fixed location element has nothing to do with the parent element , The fixed positioning element is actually a browser visible window . Where to display is mainly determined by edge offset .top bottom left right
版权声明
本文为[Four links and one new star]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple