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
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...