眾所周知,HTML被稱為超文本標記語言,它用於在瀏覽器上顯示文本,並藉助其特殊的輔助腳本(例如JavaScript和CSS),使您的內容變得美觀。顏色編碼是美化 HTML 網頁的一部分。
HTML 中的顏色代碼充當標識符,用於在網路上標識和表示該顏色。常用的顏色編碼是十六進制,表示該顏色的“十六進位”代碼。同樣,還有其他顏色代碼,例如 RGB,“紅、綠、藍”的縮寫。另一種顏色代碼稱為 HSL,是「色調、飽和度、亮度」的縮寫。在選擇您喜歡的顏色時,HSL 是一個額外的優勢。
由於一般情況下優先使用十六進位代碼,因此我們已經盡力解釋了十六進位代碼。十六進位顏色代碼包含一個符號、一個雜湊值 (#) 和一組六位數字或數字。它們採用十六進位數字系統,因此「FF」是最高數字,代表十六進位數字系統中的「255」。
這六位數字包含三對代表 RB 顏色代碼。在這六位數字中,第一對兩位數字代表「紅色」顏色的強度。因此,第一對位置的“FF”將代表最大強度的紅色。 「00」用於最低強度,「FF」用於最高強度。為了獲得“綠色”顏色,中間的一對代表強度。
同樣,對於“藍色”,最後一對代表強度。
- 因此,諸如 #FF0000 之類的十六進位數字將導致
- 十六進位數字,例如 #00FF00 會導致
- 十六進位數字,例如 #0000FF 將導致
- 要獲得黃色(「紅色」和「綠色」的組合),會創建一個類似的十六進位數字,例如#FFFF00。
HTML 顏色選擇器
顏色選擇器建立後,讓使用者「挑選」自己選擇的顏色。最標準的顏色選擇器用於 Windows 應用程序,例如 MS Word 或 Paint 等。大家都熟悉顏色選擇器;你可以透過看下面的圖片來喚起你的記憶:
輸入類型「顏色」用於建立包含顏色的輸入欄位。但某些瀏覽器(例如 Internet Explorer 11 及更早版本)不支援此輸入類型。因此,根據瀏覽器的不同,當您使用輸入類型時會彈出一個顏色選擇器。有些瀏覽器會簡單地將此輸入欄位轉換為文字框,如下所示:
因此,當使用支援的瀏覽器時,相同的程式碼將產生以下顏色選擇器調色板。
當點選該彩色框時,會彈出一個調色盤。這裡我使用的是 Google Chrome 版本‘78.0.3904.97’,它支援輸入類型顏色屬性。
建立此類顏色選擇器的程式碼將在下一節中解釋。
建立顏色選擇器的原始碼
以下是在 HTML 中建立最簡單的顏色選擇器的說明。請參閱下面的程式碼:
代碼
上面的 HTML 程式碼包含一個使用名為「color」的輸入類型的 FORM 元素。此顏色輸入類型可建立並顯示最簡單的顏色選擇器,即 Windows 標準顏色選擇器。它允許用戶選擇自己喜歡的顏色。
作為顏色的輸入類型建立一個文字方塊或多個按鈕,其預設背景顏色為「黑色」。當我們點擊它時,它會為用戶顯示顏色選擇。
觀察下面給出的顏色選擇器的工作原理:
第 1 步: 點選預設背景色為「黑色」的按鈕。
上面的程式碼只是創建了一個如上所示的按鈕。
第 2 步: 點選並選擇您的新顏色。
第3步:我們選擇了明亮的綠色來示範。點選“確定”按鈕。
In the above screen-shots, you can easily see the selected color is shown in the last screen-shot.
The input type ‘color’ provides this simple functionality of a color picker in HTML5. After picking your color, it is your choice of what the selected color can be used for.
In the following example, I incremented the above example and modified it with some inclusions.
The following example is a combination of HTML and Javascript. This example has a FORM element that uses the input type ‘color’ tag. This FORM, when submitted, our JAVASCRIPT is triggered.
Observe the source code for the FORM element below:
Code:
We added a new line to our previous program. A submit button. This submit button is when clicked; our Java script is triggered, which is given below:
function ReturnColor(c) { //saving the selected color value by ID var c= document.getElementById("color").value; var str= new String ("You chose:"); //The color is saved as its HEX color code. document.write(str+c); }
When the ‘Submit’ button is clicked, our function in javascript is triggered. The above function, ReturnColor (), returns the HEX code, that is, Hexadecimal code for the selected color by our color picker. When the code is executed, the following is our output.
The above output is in the HEX code. The 6 numbers represent the inclusion of Red, Green and Blue colors resulting in the selected color. This HEX code can also be converted easily into RGB code.
Similarly, we can save the above code and set it as a background color or a font color for the user. To do so, we added a few more lines of code into our already existing source code.
Following is the complete code, with the HTML body remaining the same:
<script> function ReturnColor(c) { //saving the selected color value by ID var c= document.getElementById("color").value; var str= new String ("You chose:"); //The color is saved as its HEX color code document.write(str+c); document.write("<br/>"); //A HEX color code can be converted into RGB code var R=c.slice(1,3); var G=c.slice(3,5); var B=c.slice(5); //Displaying the corresponding RGB code document.write("In RGB format, RGB(" + parseInt(R,16) + "," + parseInt(G,16) + "," + parseInt(B,16) + ")"); document.write("<br/>"); //Setting our selected color as Font color var color = c; var str1 = "Your color will appear as this font color"; var str2 = str1.fontcolor(c); document.write(str2); //Setting our selected color as Background color document.write("<div style='border: solid; height: 90px; width: 90px; background-color:"+color+"'/>"); } </script>
This is our complete script. When the code is executed, and a color is selected, the following is the output that is displayed.
Conclusion
There are many ways and many combinations that can help you to create a color picker, that too smart one. For example, with the combination of HTML5 and CSS and JavaScript, you can use yet another element called ‘canvas’ that has its own libraries that helps create a lightweight, small and cross-browser color picker. But that’s for another time.
以上是HTML 顏色選擇器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章带大家了解一下HTML(超文本标记语言),介绍一下HTML的本质,HTML文档的结构、HTML文档的基本标签和图像标签、列表、表格标签、媒体元素、表单,希望对大家有所帮助!

不算。html是一种用来告知浏览器如何组织页面的标记语言,而CSS是一种用来表现HTML或XML等文件样式的样式设计语言;html和css不具备很强的逻辑性和流程控制功能,缺乏灵活性,且html和css不能按照人类的设计对一件工作进行重复的循环,直至得到让人类满意的答案。

总结了一些web前端面试(笔试)题分享给大家,本篇文章就先给大家分享HTML部分的笔试题(附答案),大家可以自己做做,看看能答对几个!

在html中,document是文档对象的意思,代表浏览器窗口的文档;document对象是window对象的子对象,所以可通过“window.document”属性对其进行访问,每个载入浏览器的HTML文档都会成为Document对象。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版
視覺化網頁開發工具