整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          編寫你的第一行 HTML 代碼,來幫助蝙蝠俠寫一封情書

          譯自: https://medium.freecodecamp.org/for-your-first-html-code-lets-help-batman-write-a-love-letter-64c203b9360b

          作者: Kunal Sarkar

          譯者: MjSeven

          在一個美好的夜晚,你的肚子拒絕消化你在晚餐吃的大塊披薩,所以你不得不在睡夢中沖進洗手間。

          在浴室里,當你在思考為什么會發生這種情況時,你聽到一個來自通風口的低沉聲音:“嘿,我是蝙蝠俠。”

          這時,你會怎么做呢?

          在你恐慌并處于關鍵時刻之前,蝙蝠俠說:“我需要你的幫助。我是一個超級極客,但我不懂 HTML。我需要用 HTML 寫一封情書,你愿意幫助我嗎?”

          誰會拒絕蝙蝠俠的請求呢,對吧?所以讓我們用 HTML 來寫一封蝙蝠俠的情書。

          你的第一個 HTML 文件

          HTML 網頁與你電腦上的其它文件一樣。就同一個 .doc 文件以 MS Word 打開,.jpg 文件在圖像查看器中打開一樣,一個 .html 文件在瀏覽器中打開。

          那么,讓我們來創建一個 .html 文件。你可以在 Notepad 或其它任何編輯器中完成此任務,但我建議使用 VS Code。 在這里下載并安裝 VS Code 。它是免費的,也是我唯一喜歡的微軟產品。

          在系統中創建一個目錄,將其命名為 “HTML Practice”(不帶引號)。在這個目錄中,再創建一個名為 “Batman’s Love Letter”(不帶引號)的目錄,這將是我們的項目根目錄。這意味著我們所有與這個項目相關的文件都會在這里。

          打開 VS Code,按下 ctrl+n 創建一個新文件,按下 ctrl+s 保存文件。切換到 “Batman’s Love Letter” 文件夾并將其命名為 “loveletter.html”,然后單擊保存。

          現在,如果你在文件資源管理器中雙擊它,它將在你的默認瀏覽器中打開。我建議使用 Firefox 來進行 web 開發,但 Chrome 也可以。

          讓我們將這個過程與我們已經熟悉的東西聯系起來。還記得你第一次拿到電腦嗎?我做的第一件事是打開 MS Paint 并繪制一些東西。你在 Paint 中繪制一些東西并將其另存為圖像,然后你可以在圖像查看器中查看該圖像。之后,如果要再次編輯該圖像,你在 Paint 中重新打開它,編輯并保存它。

          我們目前的流程非常相似。正如我們使用 Paint 創建和編輯圖像一樣,我們使用 VS Code 來創建和編輯 HTML 文件。就像我們使用圖像查看器查看圖像一樣,我們使用瀏覽器來查看我們的 HTML 頁面。

          HTML 中的段落

          我們有一個空的 HTML 文件,以下是蝙蝠俠想在他的情書中寫的第一段。

          “After all the battles we fought together, after all the difficult times we saw together, and after all the good and bad moments we’ve been through, I think it’s time I let you know how I feel about you.”

          復制這些到 VS Code 中的 loveletter.html。單擊 “View -> Toggle Word Wrap (alt+z)” 自動換行。

          保存并在瀏覽器中打開它。如果它已經打開,單擊瀏覽器中的刷新按鈕。

          瞧!那是你的第一個網頁!

          我們的第一段已準備就緒,但這不是在 HTML 中編寫段落的推薦方法。我們有一種特定的方法讓瀏覽器知道一個文本是一個段落。

          如果你用 <p> 和 </p> 來包裹文本,那么瀏覽器將識別 <p> 和 </p> 中的文本是一個段落。我們這樣做:

          <p>After all the battles we fought together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.</p>

          通過在 <p> 和 </p>中編寫段落,你創建了一個 HTML 元素。一個網頁就是 HTML 元素的集合。

          讓我們首先來認識一些術語:<p> 是開始標簽,</p> 是結束標簽,“p” 是標簽名稱。元素開始和結束標簽之間的文本是元素的內容。

          “style” 屬性

          在上面,你將看到文本覆蓋屏幕的整個寬度。

          我們不希望這樣。沒有人想要閱讀這么長的行。讓我們設定段落寬度為 550px。

          我們可以通過使用元素的 style 屬性來實現。你可以在其 style 屬性中定義元素的樣式(例如,在我們的示例中為寬度)。以下行將在 p 元素上創建一個空樣式屬性:

          <p style="">...</p>

          你看到那個空的 "" 了嗎?這就是我們定義元素外觀的地方。現在我們要將寬度設置為 550px。我們這樣做:

          <p style="width:550px;">

          After all the battles we fought together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          我們將 width 屬性設置為 550px,用冒號 : 分隔,以分號 ; 結束。

          另外,注意我們如何將 <p> 和 </p> 放在單獨的行中,文本內容用一個制表符縮進。像這樣設置代碼使其更具可讀性。

          HTML 中的列表

          接下來,蝙蝠俠希望列出他所欽佩的人的一些優點,例如:

          You complete my darkness with your light. I love:

          - the way you see good in the worst things

          - the way you handle emotionally difficult situations

          - the way you look at Justice

          I have learned a lot from you. You have occupied a special place in my heart over time.

          這看起來很簡單。

          讓我們繼續,在 </p> 下面復制所需的文本:

          <p style="width:550px;">

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <p style="width:550px;">

          You complete my darkness with your light. I love:

          - the way you see good in the worse

          - the way you handle emotionally difficult situations

          - the way you look at Justice

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          保存并刷新瀏覽器。



          哇!這里發生了什么,我們的列表在哪里?

          如果你仔細觀察,你會發現沒有顯示換行符。在代碼中我們在新的一行中編寫列表項,但這些項在瀏覽器中顯示在一行中。

          如果你想在 HTML(新行)中插入換行符,你必須使用 <br>。讓我們來使用 <br>,看看它長什么樣:

          <p style="width:550px;">

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <p style="width:550px;">

          You complete my darkness with your light. I love: <br>

          - the way you see good in the worse <br>

          - the way you handle emotionally difficult situations <br>

          - the way you look at Justice <br>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          保存并刷新:



          好的,現在它看起來就像我們想要的那樣!

          另外,注意我們沒有寫一個 </br>。有些標簽不需要結束標簽(它們被稱為自閉合標簽)。

          還有一件事:我們沒有在兩個段落之間使用 <br>,但第二個段落仍然是從一個新行開始,這是因為 <p> 元素會自動插入換行符。

          我們使用純文本編寫列表,但是有兩個標簽可以供我們使用來達到相同的目的:<ul> and <li>。

          讓我們解釋一下名字的意思:ul 代表 無序列表(Unordered List),li 代表 列表項目(List Item)。讓我們使用它們來展示我們的列表:

          <p style="width:550px;">

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <p style="width:550px;">

          You complete my darkness with your light. I love:

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          在復制代碼之前,注意差異部分:

          • 我們刪除了所有的 <br>,因為每個 <li> 會自動顯示在新行中
          • 我們將每個列表項包含在 <li> 和 </li> 之間
          • 我們將所有列表項的集合包裹在 <ul> 和 </ul> 之間
          • 我們沒有像 <p> 元素那樣定義 <ul> 元素的寬度。這是因為 <ul> 是 <p> 的子節點,<p> 已經被約束到 550px,所以 <ul> 不會超出這個范圍。

          讓我們保存文件并刷新瀏覽器以查看結果:



          你會立即注意到在每個列表項之前顯示了重點標志。我們現在不需要在每個列表項之前寫 “-”。

          經過仔細檢查,你會注意到最后一行超出 550px 寬度。這是為什么?因為 HTML 不允許 <ul> 元素出現在 <p> 元素中。讓我們將第一行和最后一行放在單獨的 <p> 元素中:

          <p style="width:550px;">

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <p style="width:550px;">

          You complete my darkness with your light. I love:

          </p>

          <ul style="width:550px;">

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p style="width:550px;">

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          保存并刷新。

          注意,這次我們還定義了 <ul> 元素的寬度。那是因為我們現在已經將 <ul> 元素放在了 <p> 元素之外。

          定義情書中所有元素的寬度會變得很麻煩。我們有一個特定的元素用于此目的:<div> 元素。一個 <div> 元素就是一個通用容器,用于對內容進行分組,以便輕松設置樣式。

          讓我們用 <div> 元素包裝整個情書,并為其賦予寬度:550px 。

          <div style="width:550px;">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          </div>

          棒極了,我們的代碼現在看起來簡潔多了。

          HTML 中的標題

          到目前為止,蝙蝠俠對結果很高興,他希望在情書上標題。他想寫一個標題: “Bat Letter”。當然,你已經看到這個名字了,不是嗎?:D

          你可以使用 <h1>、<h2>、<h3>、<h4>、<h5> 和 <h6> 標簽來添加標題,<h1> 是最大的標題和最主要的標題,<h6> 是最小的標題。



          讓我們在第二段之前使用 <h1> 做主標題和一個副標題:

          <div style="width:550px;">

          <h1>Bat Letter</h1>

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          </div>

          保存,刷新。



          HTML 中的圖像

          我們的情書尚未完成,但在繼續之前,缺少一件大事:蝙蝠俠標志。你見過是蝙蝠俠的東西但沒有蝙蝠俠的標志嗎?

          并沒有。

          所以,讓我們在情書中添加一個蝙蝠俠標志。

          在 HTML 中包含圖像就像在一個 Word 文件中包含圖像一樣。在 MS Word 中,你到 “菜單 -> 插入 -> 圖像 -> 然后導航到圖像位置為止 -> 選擇圖像 -> 單擊插入”。

          在 HTML 中,我們使用 <img> 標簽讓瀏覽器知道我們需要加載的圖像,而不是單擊菜單。我們在 src 屬性中寫入文件的位置和名稱。如果圖像在項目根目錄中,我們可以簡單地在 src 屬性中寫入圖像文件的名稱。

          在我們深入編碼之前,從 這里 下載蝙蝠俠標志。你可能希望裁剪圖像中的額外空白區域。復制項目根目錄中的圖像并將其重命名為 “bat-logo.jpeg”。

          <div style="width:550px;">

          <h1>Bat Letter</h1>

          <img src="bat-logo.jpeg">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          </div>

          我們在第 3 行包含了 <img> 標簽。這個標簽也是一個自閉合的標簽,所以我們不需要寫 </img>。在 src 屬性中,我們給出了圖像文件的名稱。這個名稱應與圖像名稱完全相同,包括擴展名(.jpeg)及其大小寫。

          保存并刷新,查看結果。



          該死的!剛剛發生了什么?

          當使用 <img> 標簽包含圖像時,默認情況下,圖像將以其原始分辨率顯示。在我們的例子中,圖像比 550px 寬得多。讓我們使用 style 屬性定義它的寬度:

          <div style="width:550px;">

          <h1>Bat Letter</h1>

          <img src="bat-logo.jpeg" style="width:100%">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          </div>

          你會注意到,這次我們定義寬度使用了 “%” 而不是 “px”。當我們在 “%” 中定義寬度時,它將占據父元素寬度的百分比。因此,100% 的 550px 將為我們提供 550px。

          保存并刷新,查看結果。



          太棒了!這讓蝙蝠俠的臉露出了羞澀的微笑 :)。

          HTML 中的粗體和斜體

          現在蝙蝠俠想在最后幾段中承認他的愛。他有以下文本供你用 HTML 編寫:

          “I have a confession to make

          It feels like my chest does have a heart. You make my heart beat. Your smile brings a smile to my face, your pain brings pain to my heart.

          I don’t show my emotions, but I think this man behind the mask is falling for you.”

          當閱讀到這里時,你會問蝙蝠俠:“等等,這是給誰的?”蝙蝠俠說:

          “這是給超人的。”



          你說:哦!我還以為是給神奇女俠的呢。

          蝙蝠俠說:不,這是給超人的,請在最后寫上 “I love you Superman.”。

          好的,我們來寫:

          <div style="width:550px;">

          <h1>Bat Letter</h1>

          <img src="bat-logo.jpeg" style="width:100%">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          <h2>I have a confession to make</h2>

          <p>

          It feels like my chest does have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.

          </p>

          <p>

          I don't show my emotions, but I think this man behind the mask is falling for you.

          </p>

          <p>I love you Superman.</p>

          <p>

          Your not-so-secret-lover, <br>

          Batman

          </p>

          </div>

          這封信差不多完成了,蝙蝠俠另外想再做兩次改變。蝙蝠俠希望在最后段落的第一句中的 “does” 一詞是斜體,而 “I love you Superman” 這句話是粗體的。

          我們使用 <em> 和 <strong> 以斜體和粗體顯示文本。讓我們來更新這些更改:

          <div style="width:550px;">

          <h1>Bat Letter</h1>

          <img src="bat-logo.jpeg" style="width:100%">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          <h2>I have a confession to make</h2>

          <p>

          It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.

          </p>

          <p>

          I don't show my emotions, but I think this man behind the mask is falling for you.

          </p>

          <p><strong>I love you Superman.</strong></p>

          <p>

          Your not-so-secret-lover, <br>

          Batman

          </p>

          </div>



          HTML 中的樣式

          你可以通過三種方式設置樣式或定義 HTML 元素的外觀:

          • 內聯樣式:我們使用元素的 style 屬性來編寫樣式。這是我們迄今為止使用的,但這不是一個好的實踐。
          • 嵌入式樣式:我們在由 <style> 和 </style> 包裹的 “style” 元素中編寫所有樣式。
          • 鏈接樣式表:我們在具有 .css 擴展名的單獨文件中編寫所有元素的樣式。此文件稱為樣式表。

          讓我們來看看如何定義 <div> 的內聯樣式:

          <div style="width:550px;">

          我們可以在 <style> 和 </style> 里面寫同樣的樣式:

          div{

          width:550px;

          }

          在嵌入式樣式中,我們編寫的樣式是與元素分開的。所以我們需要一種方法來關聯元素及其樣式。第一個單詞 “div” 就做了這樣的活。它讓瀏覽器知道花括號 {...} 里面的所有樣式都屬于 “div” 元素。由于這種語法確定要應用樣式的元素,因此它稱為一個選擇器。

          我們編寫樣式的方式保持不變:屬性(width)和值(550px)用冒號(:)分隔,以分號(;)結束。

          讓我們從 <div> 和 <img> 元素中刪除內聯樣式,將其寫入 <style> 元素:

          <style>

          div{

          width:550px;

          }

          img{

          width:100%;

          }

          </style>

          <div>

          <h1>Bat Letter</h1>

          <img src="bat-logo.jpeg">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          <h2>I have a confession to make</h2>

          <p>

          It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.

          </p>

          <p>

          I don't show my emotions, but I think this man behind the mask is falling for you.

          </p>

          <p><strong>I love you Superman.</strong></p>

          <p>

          Your not-so-secret-lover, <br>

          Batman

          </p>

          </div>

          保存并刷新,結果應保持不變。

          但是有一個大問題,如果我們的 HTML 文件中有多個 <div> 和 <img> 元素該怎么辦?這樣我們在 <style> 元素中為 div 和 img 定義的樣式就會應用于頁面上的每個 div 和 img。

          如果你在以后的代碼中添加另一個 div,那么該 div 也將變為 550px 寬。我們并不希望這樣。

          我們想要將我們的樣式應用于現在正在使用的特定 div 和 img。為此,我們需要為 div 和 img 元素提供唯一的 id。以下是使用 id 屬性為元素賦予 id 的方法:

          <div id="letter-container">

          以下是如何在嵌入式樣式中將此 id 用作選擇器:

          #letter-container{

          ...

          }

          注意 # 符號。它表示它是一個 id,{...} 中的樣式應該只應用于具有該特定 id 的元素。

          讓我們來應用它:

          <style>

          #letter-container{

          width:550px;

          }

          #header-bat-logo{

          width:100%;

          }

          </style>

          <div id="letter-container">

          <h1>Bat Letter</h1>

          <img id="header-bat-logo" src="bat-logo.jpeg">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          <h2>I have a confession to make</h2>

          <p>

          It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.

          </p>

          <p>

          I don't show my emotions, but I think this man behind the mask is falling for you.

          </p>

          <p><strong>I love you Superman.</strong></p>

          <p>

          Your not-so-secret-lover, <br>

          Batman

          </p>

          </div>

          HTML 已經準備好了嵌入式樣式。

          但是,你可以看到,隨著我們包含越來越多的樣式,<style></style> 將變得很大。這可能很快會混亂我們的主 HTML 文件。

          因此,讓我們更進一步,通過將 <style> 標簽內的內容復制到一個新文件來使用鏈接樣式。

          在項目根目錄中創建一個新文件,將其另存為 “style.css”:

          #letter-container{

          width:550px;

          }

          #header-bat-logo{

          width:100%;

          }

          我們不需要在 CSS 文件中寫 <style> 和 </style>。

          我們需要使用 HTML 文件中的 <link> 標簽來將新創建的 CSS 文件鏈接到 HTML 文件。以下是我們如何做到這一點:

          <link rel="stylesheet" type="text/css" href="style.css">

          我們使用 <link> 元素在 HTML 文檔中包含外部資源,它主要用于鏈接樣式表。我們使用的三個屬性是:

          • rel:關系。鏈接文件與文檔的關系。具有 .css 擴展名的文件稱為樣式表,因此我們保留 rel=“stylesheet”。
          • type:鏈接文件的類型;對于一個 CSS 文件來說它是 “text/css”。
          • href:超文本參考。鏈接文件的位置。

          link 元素的結尾沒有 </link>。因此,<link> 也是一個自閉合的標簽。

          <link rel="gf" type="cute" href="girl.next.door">

          如果只是得到一個女朋友,那么很容易:D

          可惜沒有那么簡單,讓我們繼續前進。

          這是我們 “loveletter.html” 的內容:

          <link rel="stylesheet" type="text/css" href="style.css">

          <div id="letter-container">

          <h1>Bat Letter</h1>

          <img id="header-bat-logo" src="bat-logo.jpeg">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          <h2>I have a confession to make</h2>

          <p>

          It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.

          </p>

          <p>

          I don't show my emotions, but I think this man behind the mask is falling for you.

          </p>

          <p><strong>I love you Superman.</strong></p>

          <p>

          Your not-so-secret-lover, <br>

          Batman

          </p>

          </div>

          “style.css” 內容:

          #letter-container{

          width:550px;

          }

          #header-bat-logo{

          width:100%;

          }

          保存文件并刷新,瀏覽器中的輸出應保持不變。

          一些手續

          我們的情書已經準備好給蝙蝠俠,但還有一些正式的片段。

          與其他任何編程語言一樣,HTML 自出生以來(1990 年)經歷過許多版本,當前版本是 HTML5。

          那么,瀏覽器如何知道你使用哪個版本的 HTML 來編寫頁面呢?要告訴瀏覽器你正在使用 HTML5,你需要在頁面頂部包含 <!DOCTYPE html>。對于舊版本的 HTML,這行不同,但你不需要了解它們,因為我們不再使用它們了。

          此外,在之前的 HTML 版本中,我們曾經將整個文檔封裝在 <html></html> 標簽內。整個文件分為兩個主要部分:頭部在 <head></head> 里面,主體在 <body></body> 里面。這在 HTML5 中不是必須的,但由于兼容性原因,我們仍然這樣做。讓我們用 <Doctype>, <html>、 <head> 和 <body> 更新我們的代碼:

          <!DOCTYPE html>

          <html>

          <head>

          <link rel="stylesheet" type="text/css" href="style.css">

          </head>

          <body>

          <div id="letter-container">

          <h1>Bat Letter</h1>

          <img id="header-bat-logo" src="bat-logo.jpeg">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          <h2>I have a confession to make</h2>

          <p>

          It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.

          </p>

          <p>

          I don't show my emotions, but I think this man behind the mask is falling for you.

          </p>

          <p><strong>I love you Superman.</strong></p>

          <p>

          Your not-so-secret-lover, <br>

          Batman

          </p>

          </div>

          </body>

          </html>

          主要內容在 <body> 里面,元信息在 <head> 里面。所以我們把 <div> 保存在 <body> 里面并加載 <head> 里面的樣式表。

          保存并刷新,你的 HTML 頁面應顯示與之前相同的內容。

          HTML 的標題

          我發誓,這是最后一次改變。

          你可能已經注意到選項卡的標題正在顯示 HTML 文件的路徑:



          我們可以使用 <title> 標簽來定義 HTML 文件的標題。標題標簽也像鏈接標簽一樣在 <head> 內部。讓我們我們在標題中加上 “Bat Letter”:

          <!DOCTYPE html>

          <html>

          <head>

          <title>Bat Letter</title>

          <link rel="stylesheet" type="text/css" href="style.css">

          </head>

          <body>

          <div id="letter-container">

          <h1>Bat Letter</h1>

          <img id="header-bat-logo" src="bat-logo.jpeg">

          <p>

          After all the battles we faught together, after all the difficult times we saw together, after all the good and bad moments we've been through, I think it's time I let you know how I feel about you.

          </p>

          <h2>You are the light of my life</h2>

          <p>

          You complete my darkness with your light. I love:

          </p>

          <ul>

          <li>the way you see good in the worse</li>

          <li>the way you handle emotionally difficult situations</li>

          <li>the way you look at Justice</li>

          </ul>

          <p>

          I have learned a lot from you. You have occupied a special place in my heart over the time.

          </p>

          <h2>I have a confession to make</h2>

          <p>

          It feels like my chest <em>does</em> have a heart. You make my heart beat. Your smile brings smile on my face, your pain brings pain to my heart.

          </p>

          <p>

          I don't show my emotions, but I think this man behind the mask is falling for you.

          </p>

          <p><strong>I love you Superman.</strong></p>

          <p>

          Your not-so-secret-lover, <br>

          Batman

          </p>

          </div>

          </body>

          </html>

          保存并刷新,你將看到在選項卡上顯示的是 “Bat Letter” 而不是文件路徑。

          蝙蝠俠的情書現在已經完成。

          恭喜!你用 HTML 制作了蝙蝠俠的情書。



          我們學到了什么

          我們學習了以下新概念:

          • 一個 HTML 文檔的結構
          • 在 HTML 中如何寫元素(<p></p>)
          • 如何使用 style 屬性在元素內編寫樣式(這稱為內聯樣式,盡可能避免這種情況)
          • 如何在 <style>...</style> 中編寫元素的樣式(這稱為嵌入式樣式)
          • 在 HTML 中如何使用 <link> 在單獨的文件中編寫樣式并鏈接它(這稱為鏈接樣式表)
          • 什么是標簽名稱,屬性,開始標簽和結束標簽
          • 如何使用 id 屬性為一個元素賦予 id
          • CSS 中的標簽選擇器和 id 選擇器

          我們學習了以下 HTML 標簽:

          • <p>:用于段落
          • <br>:用于換行
          • <ul>、<li>:顯示列表
          • <div>:用于分組我們信件的元素
          • <h1>、<h2>:用于標題和子標題
          • <img>:用于插入圖像
          • <strong>、<em>:用于粗體和斜體文字樣式
          • <style>:用于嵌入式樣式
          • <link>:用于包含外部樣式表
          • <html>:用于包裹整個 HTML 文檔
          • <!DOCTYPE html>:讓瀏覽器知道我們正在使用 HTML5
          • <head>:包裹元信息,如 <link> 和 <title>
          • <body>:用于實際顯示的 HTML 頁面的主體
          • <title>:用于 HTML 頁面的標題

          我們學習了以下 CSS 屬性:

          • width:用于定義元素的寬度
          • CSS 單位:“px” 和 “%”

          朋友們,這就是今天的全部了,下一個教程中見。


          作者簡介:開發者 + 作者 | supersarkar.com | twitter.com/supersarkar


          via: https://medium.freecodecamp.org/for-your-first-html-code-lets-help-batman-write-a-love-letter-64c203b9360b

          作者: Kunal Sarkar 譯者: MjSeven 校對: wxy

          本文由 LCTT 原創編譯, Linux中國 榮譽推出

          點擊“了解更多”可訪問文內鏈接

          作者 | Kevin Vogel 譯者 | 彎月
          出品 | CSDN(ID:CSDNnews)

          最近,微軟宣布了一項舉世矚目的提案,表示他們將支持 JavaScript 和 TypeScript 的進一步開發,該提案幾乎在一夜之間動搖了編程語言的基礎。

          截止到目前為止,該提案還只是一個處于 Stage 0 階段的建議,但微軟宣布他們會按時將該提案提交給 TC39 委員會。如果這個提案被采納并付諸實施,那么必將在 JavaScript 和 TypeScript 界引發前所未有的動蕩。


          JavaScript 的歷史回顧

          回溯 20 年前,與現在的 Web 開發做比較,你就會發現,雖然 JavaScript 作為一種編程語言已經有了很大的發展,但圍繞 JavaScript 的生態系統則取得了更大的進步。

          語言本身與生態系統的發展是相輔相成的,一方面在過去的二十年里 JavaScript 社區變得越來越專業,而另一方面互聯網本身在現實生活中的重要性也變得越來越突出。作為開發人員,我們無法控制用戶使用哪些瀏覽器。

          這意味著用戶只有定期更新瀏覽器才能使用 JavaScript 的現代功能。雖然對于個人用戶來說,定期更新并不是難事,因為如今許多瀏覽器會自動更新而無需用戶的指示,但對于企業卻并非如此。

          各個公司對于軟件和軟件更新有著嚴格的規定。許多公司都會使用過時的軟件或瀏覽器上網。這是一個基本問題,甚至會影響到 HTML 和 CSS,此外,由于編程語言必須由各個瀏覽器解釋,因此也會對瀏覽器產生嚴重的依賴。


          轉譯與捆綁


          作為一名 Web 開發人員,你必須在兩種思路之間做出選擇:第一,依賴現代 JavaScript、CSS 或 HTML,這種方式不僅可以簡化編程,而且也可以提高易用性;第二,不使用這些現代功能,因為并非所有人都使用最新的瀏覽器,因此采用一些現代功能可能會導致一定數量的用戶無法正常使用。

          除此之外,幾十年來我們還沒有一個像樣的 JavaScript 模塊系統。Node.js 從 CommonJS 借鑒了模塊系統,但僅限于服務器。

          在最近的很長一段時間里,瀏覽器都沒有出現太大的提升,于是捆綁器與轉譯器應運而生。盡管我們使用的是 JIT(即時)編譯的編程語言,但必須經過一個復雜的構建過程,將源代碼轉換為實際代碼,然后才能在瀏覽器中執行和解釋。

          這是大約十年前的情況。


          TypeScript 的崛起


          微軟推出 TypeScript 也正好是在十年前。微軟認為,如果在部署代碼之前,還需要一個轉譯器來轉換 JavaScript 代碼,那么在這個構建過程中再添加一個步驟也沒什么大不了。

          作為回報,你可以獲得一個良好的轉譯器,將現代 JavaScript 轉換成原始的 JavaScript。此外,TypeScript 是一個靜態類型系統,它不僅提高了 JavaScript 的可擴展性,而且還提出了一些新概念,并為提高 JavaScript 的開發效率做出了重大貢獻。

          因此,TypeScript 迅速崛起,并成為當今企業 JavaScript 開發的標準。

          在過去的十年里,整個世界也發生了變化。雖然仍有一部分瀏覽器不會自動更新,而用戶使用的瀏覽器也不是最新版本,但這類用戶的比例已經大幅下降了。

          這意味著,你只需關注“常青瀏覽器”(指自動升級到最新版本的瀏覽器),那么非常有希望擺脫轉譯器。而且現在我們還可以使用 ESM(ECMAScript 2015 模塊),這是一個基于 JavaScript 的原生模塊系統,既可用于服務器端,還可用于客戶端。

          也就是說,我們不再需要捆綁器,至少從技術角度來看是這樣。捆綁器只是構建過程中的另一個步驟,為的是優化 HTTP 請求,這樣就只需從服務器加載幾個大文件,而小文件的數量就大幅縮減了。


          TypeScript 將成為一個障礙?


          在這種情況下,構建過程將變得越來越簡化,甚至根本不需要。微軟希望將來只有一個必要的工具會保留下來,那就是 TypeScript 編譯器,因為 Web 瀏覽器和其他 JavaScript 運行時環境無法直接運行 TypeScript。

          這也就是說,屆時微軟的 TypeScript 會從一個非常實用的工具突然變成一個很討人厭的東西。然而微軟表示,他們不想成為障礙,相反,他們希望給予開發人員更多啟發。

          微軟擔心的結果是,JavaScript 可能會像 20 年前一樣,朝著高速、直接和高效的方向發展,因為如果人們不再使用 TypeScript,那么也就不再需要轉譯器了。


          將 TypeScript 集成到瀏覽器


          最簡單的解決方法就是將 TypeScript 集成到 Web 瀏覽器和其他運行時環境中,成為代替 JavaScript 的編程語言。從理論上講,這也并非不可能。Deno(https://deno.land/)已經在做這方面的嘗試了。

          Node.js 中有一個 npm 包,名叫 ts-node,它采用了與 Deno 類似的解決方案。它會在構建/加載應用程序時將其編譯到內存中,就好像可以直接運行 TypeScript,但實際上并非如此。

          此外,TypeScript 語言本身如今也變得越來越復雜,微軟并不希望將 TypeScript 編譯器的所有功能直接集成到常見的 Web 瀏覽器中,因為這將是一項非常復雜的任務,需要蘋果、Google、Mozilla 等公司通力合作,整合一個新的大標準。

          無論這個發展方向正確與否,微軟都希望回避,目前這個問題的走向尚不明朗,但這項聲明表明這不是他們想要的結果。


          中間方式:JSDoc


          根據微軟最近發表的建議可以看出,他們想到了另一種不同的方式。除了(a)編寫原始的 JavaScript ;(b) 完全切換到 TypeScript,其實還存在第三種介于二者之間的一種中間方式。

          TypeScript 允許你在 JavaScript 上分析代碼,甚至可以在其中存儲類型,即編寫適當的 JSDoc 注釋即可。對于某些公司來說,這種方式不僅可以通過 TypeScript 編譯器獲得類型支持,而且無需將項目完全遷移到 TypeScript。

          這種方式最大的優勢在于,代碼仍然是純粹的 JavaScript,不僅不需要編譯,而且只需刪除所有 JSDoc 注釋,就可以褪去 TypeScript 的外殼。我曾使用過 JSDoc,不得不說感覺更像是 TypeScript 的廉價替代品。

          需要編寫的代碼更多,而且非常繁瑣,幾乎無法實現更復雜的類型,但總比沒有類型好。


          建議:將 TypeScript 作為注釋


          所以微軟的建議是,TypeScript 沿用 JSDoc 的套路,這樣就不必編譯 TypeScript 了。

          類型注釋和關鍵字(例如“public”或“private”)將成為注釋,在執行 JavaScript 代碼時會被忽略。這意味著,你可以編寫 TypeScript,而無需將 TypeScript 編譯成 JavaScript 代碼。

          但是,你仍然可以使用 TypeScript 編譯器來觸發類型檢查,只不過不必編譯代碼來運行。而“d.ts”文件將會被淘汰,因為 JavaScript 代碼中也包含這些類型。

          這樣,TypeScript 編譯器就會變成一個可選的插件,就像 ESLint 之類的 linter,所有類型注釋都不會包含在執行的代碼中。

          因此,該提案被稱為“將 TypeScript 作為注釋”。


          將 TypeScript 作為注釋的缺點


          這個思路聽起來不錯,但我看到了一些缺點。

          我們不要忘記,TypeScript 不僅包含函數參數的原始類型,而且還有接口、聯合類型、類型關鍵字、非常復雜的類型和嵌套類型、“as”關鍵字、公共/私有/受保護關鍵字、泛型類型等等。

          代碼本身就包含一些注釋,而且還包含兩種類型的注釋(單行注釋和塊注釋),如今再加上 TypeScript,那么我們就必須不使用多種類型的注釋來表達關鍵字。

          所以,我不得不思考這是不是一個好方法,因為我很喜歡當前的解決方案,即使它并不完美。畢竟,人們選擇 TypeScript 而不是使用 Flow 或 JSDoc 等替代方案是有原因的。TypeScript 比這些替代方案更受歡迎也是有其背后的原因的。

          此外,我們編寫代碼時無法使用枚舉,因為它們是值和類型的混合體。還有命名空間、TypeScript 的 JSX 支持、參數屬性等,這些都無法使用。所以如果你想使用枚舉,就必須編譯代碼;如果不想編譯,就不能使用枚舉。

          這個建議并沒有真正分割 JavaScript 與 TypeScript。我非常懷疑這是否應該成為 TypeScript 未來的發展方向。

          在我看來,這種方式會導致 TypeScript 開發人員陷入混亂,并形成兩個陣營。一些人堅持使用當前版本的 TypeScript,而另一些人將轉戰“將 TypeScript 作為注釋”。


          最后的想法


          在我看來,TypeScript 的發展有兩個方向:要么保持完整性,盡管所有代碼都需要編譯;要么給 JavaScript 創建一個可選的靜態類型系統。

          這意味著,TypeScript 會成為新一代的 JavaScript,但微軟已經否決了這個思路。因此,我的結論是,TypeScript 不應該通過這種方式從根本上發生改變,尤其是不應該以微軟目前提議或支持的方式。

          未來幾個月或幾年內 TypeScript 究竟會如何發展,就讓我們拭目以待吧。

          原文鏈接:https://betterprogramming.pub/how-microsoft-wants-to-destroy-typescript-1f1a53b18de6


          END


          新程序員001-004》全面上市,對話世界級大師,報道中國IT行業創新創造


          成就一億技術人

          片來源:攝圖網

          一、在線學習平臺Udemy遞交IPO申請,2020年營收4.30億美元

          10月5日,在線學習平臺Udemy向納斯達克提交招股書,股票代碼擬為“UDMY”,主承銷商為摩根士丹利和摩根大通。

          Udemy成立于2009年,總部位于美國舊金山,是一家為學生、企業和政府提供在線學習平臺。據介紹,Udemy為超過4400萬用戶提供了超過18萬種課程,涵蓋75種語言和180多個國家及地區。

          原文鏈接:https://www.jiemodui.com/N/128704.html


          二、弘成立業更新招股書,2021年上半年營收2.32億元

          近日,北京弘成立業科技股份有限公司(以下簡稱“弘成立業”)向深交所創業板更新招股書,擬發行7080萬股,占發行后總股本比例不低于10%。9月16日,因發行人及保薦人更新財務資料,弘成立業主動申請中止發行上市審核程序,并于9月30日完成財務資料更新,恢復發行上市審核。

          原文鏈接:https://www.jiemodui.com/N/128700.html


          三、蘋果推出兒童編程指南,擴大編程學習覆蓋年齡段

          蘋果官網10月5日消息,蘋果為小學生與教育工作者推出新的“人人能編程:早期學習者”活動指南,實現編程課程資源范圍從幼兒園到大學的覆蓋。

          編程學習是蘋果切入教育的途徑之一。蘋果曾于2017年面向世界各地高校和學生發起“人人能編程”計劃,該計劃依托其推出的開源編程語言Swift開發App。2019年,蘋果推出經重新設計的“人人能編程”課程,讓更多小學生和中學生進入編程世界。在全球逾5000所學校中,已有上百萬名學生參加“人人能編程”課程。

          原文鏈接:https://www.jiemodui.com/N/128689.html


          四、天津濱海新區明確“營轉非”認定標準

          據“濱海發布”微信公眾號消息,近日,天津濱海新區召開學科類培訓機構“營轉非”專題培訓會,明確學科類培訓機構進行“營轉非”資格認定的標準,要求機構在消課過程中,有家長要求退費要無條件退費,不能產生退費糾紛。

          原文鏈接:https://www.jiemodui.com/N/128697.html


          本文圖文均來源于芥末堆,如有侵權,請與我們聯系。


          主站蜘蛛池模板: 狠狠爱无码一区二区三区| 中文字幕精品一区二区日本| 国产成人无码一区二区三区 | 无码av人妻一区二区三区四区| 冲田杏梨AV一区二区三区| 久久无码精品一区二区三区| 亚洲熟女乱色一区二区三区| 久久青草精品一区二区三区| 中文国产成人精品久久一区| 国产一区韩国女主播| 亚洲AV无码一区二区三区网址 | 国产一区二区三区小说| 人妻体内射精一区二区三区| 国产婷婷色一区二区三区| 国产一区三区三区| 亚洲国产精品一区二区九九| 国产一区二区视频免费| 波多野结衣在线观看一区二区三区| 日本无码一区二区三区白峰美| 亚洲va乱码一区二区三区| 97av麻豆蜜桃一区二区| 99久久精品午夜一区二区| 国产精品va无码一区二区| 国产成人无码精品一区二区三区| 久久精品一区二区国产| 无码精品人妻一区二区三区人妻斩 | 国产一区二区电影| 色妞AV永久一区二区国产AV| 久久精品国产一区二区| 日本大香伊一区二区三区| 国产一区二区中文字幕| 91精品一区二区三区在线观看| 88国产精品视频一区二区三区| 日韩一区二区在线观看视频| 美女毛片一区二区三区四区| 亚洲av日韩综合一区二区三区 | 麻豆一区二区在我观看| 一区二区和激情视频| 日韩精品一区二区三区不卡 | 在线精品一区二区三区| 国产小仙女视频一区二区三区|