CSS深入理解學習筆記之absolute

来源:https://www.cnblogs.com/tinyTea/archive/2018/01/17/8288868.html
-Advertisement-
Play Games

1、absolute和float 擁有相同的特性表現: ①包裹性(容器應用之後,可以包裹裡面的內容); ②破壞性(內容應用之後,會破壞父容器) 2、absolute和relative absolute和relative是分離的,對立的。父容器是relative,子元素是absolute,可以限制子元 ...


1、absolute和float

  擁有相同的特性表現:

    ①包裹性(容器應用之後,可以包裹裡面的內容);

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <meta name="viewport" content="width=device-width">
 6 <title>absolute的包裹性</title>
 7 <style>
 8 .box {
 9     padding: 10px;
10     background-color: #f0f0f0;    
11 }
12 input {
13     position: absolute; top: 234px;    
14     width: 160px; height: 32px;
15     font-size: 100%;
16 }
17 </style>
18 </head>
19 
20 <body>
21 <div id="box" class="box"><img src="http://img.mukewang.com/54447b06000171a002560191.jpg" width="256" height="191"></div>
22 <input id="button" type="button" value="容器absolute化">
23 <script>
24 var eleBox = document.getElementById("box"), eleBtn = document.getElementById("button");
25 if (eleBox != null && eleBtn != null) {
26     eleBtn.onclick = function() {
27         if (this.absolute) {
28             eleBox.style.position = "";
29             this.value = "容器absolute化";
30             this.absolute = false;
31         } else {
32             eleBox.style.position = "absolute";
33             this.value = "容器去absolute";
34             this.absolute = true;
35         }
36     };
37 }
38 </script>
39 </body>
40 </html>

    ②破壞性(內容應用之後,會破壞父容器)

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <meta name="viewport" content="width=device-width">
 6 <title>absolute的破壞性</title>
 7 <style>
 8 .box {
 9     padding: 10px;
10     background-color: #f0f0f0;    
11 }
12 input {
13     position: absolute; top: 234px;    
14     width: 160px; height: 32px;
15     font-size: 100%;
16 }
17 </style>
18 </head>
19 
20 <body>
21 <div class="box"><img id="image" src="http://img.mukewang.com/54447b06000171a002560191.jpg" width="256" height="191"></div>
22 <input id="button" type="button" value="圖片absolute化">
23 <script>
24 var eleImg = document.getElementById("image"), eleBtn = document.getElementById("button");
25 if (eleImg != null && eleBtn != null) {
26     eleBtn.onclick = function() {
27         if (this.absolute) {
28             eleImg.style.position = "";
29             this.value = "圖片absolute化";
30             this.absolute = false;
31         } else {
32             eleImg.style.position = "absolute";
33             this.value = "圖片去absolute";
34             this.absolute = true;
35         }
36     };
37 }
38 </script>
39 </body>
40 </html>

2、absolute和relative

  absolute和relative是分離的,對立的。父容器是relative,子元素是absolute,可以限制子元素對父元素破壞性的影響。

  獨立的absolute可以擺脫overflow的限制,無論是滾動還是隱藏。

<div style = "overflow:scroll;">
    <a href="javascript:void(0);" title="close" style="position:absolute;"></a>
    <img src="img1.jpg" />
    <img src="img2.jpg" />
</div>

3、無依賴的absolute定位 

   無依賴是指不受父元素relative限制的absolute定位,行為表現上是不使用top/right/bottom/left任何一個屬性或使用auto作為值。

  特性:①脫離文檔流;

     ②位置跟隨;

      

     ③去浮動(效果同上圖);

     ④IE7下會inline-block化(解決方案:在元素外層套一個空div標簽)

4、absolute的實際應用

  (1)圖片圖標絕對定位覆蓋:

    先來點小技巧,在實際開發中,為了好看標簽一般都各占一行。為了消除不必要的空格,可以如下處理:

    

    效果圖:

    

    實例代碼:

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>圖標定位二三事</title>
 6 <style>
 7 body { font: 14px/1.4 "Microsoft YaHei"; background-color: #EDEFF0; }
 8 body, h3, h5 { margin: 0; }
 9 img { border: 0 none; vertical-align: bottom; }
10 .l { float: left; }.r { float: right; }
11 .constr { width: 1200px; margin-left: auto; margin-right: auto; }
12 .header { background-color: #2A2C2E; }
13 .nav { height: 60px; }
14 .nav-list { float: left; font-size: 14px; font-weight: 400; }
15 .nav-a { display: inline-block; line-height: 20px; padding: 20px 35px; color: #B5BDC0; text-decoration: none; }
16 .nav-a:hover { color: #fff; }
17 
18 .course { padding-top: 10px; }
19 .course-list { float: left; width: 280px; height: 240px; margin: 5px 10px 15px; border-radius: 0 0 1px 1px; background-color: #F7FAF9; background-color: rgba(255,255,255,1); box-shadow: 0 1px 2px #c5c5c5; text-decoration: none; }
20 .course-list-img { background-color: #6396F1; }
21 .course-list-h { line-height: 50px; font-size: 14px; font-weight: 400; color: #363d40; text-align: center; }
22 .course-list-tips { margin: 0 14px; font-size: 12px; color: #b4bbbf; overflow: hidden; }
23 
24 .icon-hot { position: absolute; width: 28px; height: 11px; margin: -6px 0 0 2px; background: url(http://img.mukewang.com/545304730001307300280011.gif); }
25 .icon-recom { position: absolute; line-height: 20px; padding: 0 5px; background-color: #f60; color: #fff; font-size: 12px; }
26 .icon-vip { position: absolute; width: 36px; height: 36px; margin-left: -36px; background: url(http://img.mukewang.com/5453048000015d8800360036.gif); text-indent: -9em; overflow: hidden; }
27 </style>
28 </head>
29 
30 <body>
31 <div class="header">
32     <div class="constr">
33         <div class="nav">
34             <h3 class="nav-list">
35                 <a href="http://www.imooc.com/course/list" class="nav-a">課程</a>
36             </h3>
37             <h3 class="nav-list">
38                 <a href="http://www.imooc.com/wenda" class="nav-a">問答</a>
39             </h3>
40             <h3 class="nav-list">
41                 <a href="http://www.imooc.com/seek/index" class="nav-a">
42                     求課<i class="icon-hot"></i>
43                 </a>
44             </h3>
45         </div>
46     </div>
47 </div>
48 
49 <div class="main">
50     <div class="constr">
51         <div class="course">
52             <a href="http://www.imooc.com/view/121" class="course-list">
53                 <div class="course-list-img">
54                     <span class="icon-recom">推薦</span>
55                     <img width="280" height="160" alt="分享:CSS深入理解之float浮動" src="http://img.mukewang.com/53d74f960001ae9d06000338-300-170.jpg"><!--
56                     --><i class="icon-vip">vip</i>
57                 </div>
58                 <h5 class="course-list-h">分享:CSS深入理解之float浮動</h5>
59                 <div class="course-list-tips">
60                     <span class="l">已完結</span>
61                     <span class="r">3514人學習</span>
62                 </div>
63             </a>
64         </div>
65     </div>
66 </div>
67 </body>
68 </html>

  (2)下拉框定位最佳實踐:

    效果圖:

    

    實例代碼:

 1 <!doctype html>
 2 <html>
 3 <head>
 4 <meta charset="utf-8">
 5 <title>下拉框定位二三事</title>
 6 <style>
 7 body { margin: 0; font: 14px/1.4 "Microsoft YaHei"; background-color: #EDEFF0; }
 8 .constr { width: 1200px; max-width: 80%; margin-left: auto; margin-right: auto; padding-bottom: 300px; overflow: hidden; }
 9 .course-sidebar { width: 262px; float: left; }
10 .course-sidebar > div { border
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 首先必須使用釘釘開發版,並確保已經通過此鏈接打開了調試功能: https://open-doc.dingtalk.com/docs/doc.htm?spm=5176.10694750.0.0.3tPHed&treeId=171&articleId=104908&docType=1 如果出現點擊Ins ...
  • 小程式的二維碼,是靜態的,在公眾號里傳播的時候,效果不太好,但是因為小程式的logo 很大,可以容納很多的內容,所以就希望中間的部分可以動態顯示。 經過我們的驗證,是可行的,來幾張效果圖 1. 二次元小程式碼,將一些搞小的gif動畫放在小程式碼里,非常適合在二次元的世界里傳播 2. 游戲小程式碼 , ...
  • 1.首先進入蘋果官網 找到support https://support.apple.com 2.找到查詢ipad型號的地方 點擊Check coverage for your product 3.輸入你的ipad序列號,序列號在ipad上 通用 >關於本機 >序列號 ...
  • 一,Jenkins http://jenkins-ci.org 二,iOS單元測試的持續集成 在Xcode進入OCUnit作為單元測試框架前,把單元測試分為兩種:Logic Test和Application Test.Logic Test負責測試邏輯部分,一般邏輯部分是沒有UI的。 Applicat ...
  • 字元串方法字元串切割slicestrObj.slice(start[,end])參數為負,將它作為length+end處理,此處length為字元串的長度。 str.slice( 2)可以取字元串後兩位substringstrObj.substring(start[,end])  ...
  • 文章系國內領先的 ITOM 管理平臺供應商 OneAPM 編譯呈現。 您是網站管理員還是網頁開發人員?想創建超快速的網站嗎? 今天我們來看看 JavaScript,這項神奇而又複雜的技術。它使網站內容更加豐富,但常常出現的運行性能問題又降低了用戶的體驗。事實已經證明,最佳的終端用戶體驗能提升網站的轉 ...
  • 我們在上一篇文章中講到了JS中變數的概念,本篇文章講一下運算符和表達式。 ...
  • 1、children與childNodes children: 獲取子元素節點,無相容問題 childnNodes: IE:獲取子元素節點 非IE(chrome,Firefox等):獲取子節點,包括元素節點和文本節點 2、firstChild與firstElementChild firstChild ...
一周排行
    -Advertisement-
    Play Games
  • .Net8.0 Blazor Hybird 桌面端 (WPF/Winform) 實測可以完整運行在 win7sp1/win10/win11. 如果用其他工具打包,還可以運行在mac/linux下, 傳送門BlazorHybrid 發佈為無依賴包方式 安裝 WebView2Runtime 1.57 M ...
  • 目錄前言PostgreSql安裝測試額外Nuget安裝Person.cs模擬運行Navicate連postgresql解決方案Garnet為什麼要選擇Garnet而不是RedisRedis不再開源Windows版的Redis是由微軟維護的Windows Redis版本老舊,後續可能不再更新Garne ...
  • C#TMS系統代碼-聯表報表學習 領導被裁了之後很快就有人上任了,幾乎是無縫銜接,很難讓我不想到這早就決定好了。我的職責沒有任何變化。感受下來這個系統封裝程度很高,我只要會調用方法就行。這個系統交付之後不會有太多問題,更多應該是做小需求,有大的開發任務應該也是第二期的事,嗯?怎麼感覺我變成運維了?而 ...
  • 我在隨筆《EAV模型(實體-屬性-值)的設計和低代碼的處理方案(1)》中介紹了一些基本的EAV模型設計知識和基於Winform場景下低代碼(或者說無代碼)的一些實現思路,在本篇隨筆中,我們來分析一下這種針對通用業務,且只需定義就能構建業務模塊存儲和界面的解決方案,其中的數據查詢處理的操作。 ...
  • 對某個遠程伺服器啟用和設置NTP服務(Windows系統) 打開註冊表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServer 將 Enabled 的值設置為 1,這將啟用NTP伺服器功 ...
  • title: Django信號與擴展:深入理解與實踐 date: 2024/5/15 22:40:52 updated: 2024/5/15 22:40:52 categories: 後端開發 tags: Django 信號 松耦合 觀察者 擴展 安全 性能 第一部分:Django信號基礎 Djan ...
  • 使用xadmin2遇到的問題&解決 環境配置: 使用的模塊版本: 關聯的包 Django 3.2.15 mysqlclient 2.2.4 xadmin 2.0.1 django-crispy-forms >= 1.6.0 django-import-export >= 0.5.1 django-r ...
  • 今天我打算整點兒不一樣的內容,通過之前學習的TransformerMap和LazyMap鏈,想搞點不一樣的,所以我關註了另外一條鏈DefaultedMap鏈,主要調用鏈為: 調用鏈詳細描述: ObjectInputStream.readObject() DefaultedMap.readObject ...
  • 後端應用級開發者該如何擁抱 AI GC?就是在這樣的一個大的浪潮下,我們的傳統的應用級開發者。我們該如何選擇職業或者是如何去快速轉型,跟上這樣的一個行業的一個浪潮? 0 AI金字塔模型 越往上它的整個難度就是職業機會也好,或者說是整個的這個運作也好,它的難度會越大,然後越往下機會就會越多,所以這是一 ...
  • @Autowired是Spring框架提供的註解,@Resource是Java EE 5規範提供的註解。 @Autowired預設按照類型自動裝配,而@Resource預設按照名稱自動裝配。 @Autowired支持@Qualifier註解來指定裝配哪一個具有相同類型的bean,而@Resourc... ...