css實現一個寫信的格式

來源:互聯網
上載者:User

標籤:

一、目標

目標實現如下效果:

二、完成1、分析

這個效果看起來很簡單,實際上可能並不那麼容易實現。

首先是全部東西都置中顯示,除了“親愛的starof”這個稱呼的地方。這也是痛點,也是本文要重點說的地方。

開始我嘗試將“ 親愛的starof:” 和下面那段文字分別獨立成兩個段落,一個居左,一個居左。結果當然是不理想的,因為“親愛的starof”部分其實並不是真正意義上的居左,而是以下面這段文字作為參考的一個居左。現在說說我的實現方法。首先全部文字都用<p>包裹,inline-block顯示,然後絕對位置。具體過程如下,各位如有不同見解或實現方式歡迎指點討論。

2、實現第一步,代碼基礎架構如下

全部文字都放在一個<p>標籤內。

<!DOCTYPE html><html><head><meta charset="utf-8"/><title>demo of starof</title><style>.top{margin:0 5%;text-align:center;}.top span{color:red;}</style></head><body>  <div class="top">    <p class="first"> <span> 親愛的starof:</span><br/>      恭喜您獲得快速升級年費資格,您僅需開通<span>4個月</span>會員,即可自動升級為尊貴的年費會員,差額部分享受<span>8折優惠</span>哦!    </p>  </div></body></html>

此時效果:

為了方便後面對比。

第二步,設在<p>的display為inline-block,實現置中。

因為p本身是個區塊層級元素,我們下一步要以它為參照實現定位。所以需要設在display屬性讓它大小根據內容而定,同時實現置中。

增加下面css樣式。

.top .first{display:inline-block;position:relative;}

 效果如下

 看起來和上面很像,實際上已經發生了本質變化。

第三步,通過絕對位置實現目標效果。

增加下面css樣式。<p>相對定位作為參照,第一個<span>絕對位置。

.top .first{...position:relative;}.first span:first-child{position:absolute;}

此時效果如下:

 如果覺得效果不理想,可通過left,top稍微調整一下。

.first span:first-child{position:absolute;left:0;top:-5px;}

這就是我要的效果

第四步,完成其他部分

剩下的就都很簡單了,完整代碼如下:

<!DOCTYPE html><html><head><meta charset="utf-8"/><title>demo of starof</title><style>.top{margin:0 5%;text-align:center;}.top span{color:red;}.top .first{display:inline-block;position:relative;}.first span:first-child{position:absolute;left:0;top:-5px;}.top input{width:20%;padding:8px 20px;margin:5px;background-color:#e9322a;color:white;font-size:18px;border:1px solid #666;border-radius:5px;}</style></head><body>  <div class="top">    <p class="first"> <span> 親愛的starof:</span><br/>      恭喜您獲得快速升級年費資格,您僅需開通<span>4個月</span>會員,即可自動升級為尊貴的年費會員,差額部分享受<span>8折優惠</span>哦! </p>    <div>      <input type="button" value="立即升級"/>      <p>已有<span>23919</span>人享此優惠</p>    </div>  </div></body></html>
View Code

 

本文作者starof,因知識本身在變化,作者也在不斷學習成長,文章內容也不定時更新,為避免誤導讀者,方便追根溯源,請諸位轉載註明出處:http://www.cnblogs.com/starof/p/4832947.html有問題歡迎與我討論,共同進步。

css實現一個寫信的格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.