& lt輸入類型= & quot文本& quotplaceholder = & quot沒什么& quottitle = & quot谷歌& quot必需的& gt
**內部CSS文件:* *
input[type="text"]{
border: 2px solid red;
}
input::placeholder{
color:green;
}
input[title="google"]{
background-color:black;
color:white;
}
為什么字體、占位符和標題的書寫過程不同?雖然標簽內的文字看起來一樣。如何理解哪些是屬性,哪些是元素?
input::placeholder選擇& lt輸入& gt,而輸入[attribute = & quot;價值& quot]選擇一個& lt輸入& gt其屬性的值為value。這些做不同的事情。
可視化示例:
/* Selects an <input> with a 'placeholder' attribute */
input[placeholder] {
color: #2ab7ca;
}
/* Selects the placeholder itself */
input::placeholder {
color: #ff6b6b;
}
/* Ignore these */
body {
margin: 0;
padding: 2em;
}
input {
display: block;
margin: 1em 0;
height: 2em;
width: 100%;
padding: 0.5em;
}
<input
type="text"
placeholder="This placeholder is red and not editable."
>
<input
type="text" placeholder=""
value="...whereas the input content itself is blue and editable."
>
在CSS中,我們有屬性選擇器和偽元素,它們服務于不同的目的,有不同的語法。讓我們來了解它們的區別以及如何識別它們:
Attribute Selectors:
Attribute selectors allow us to target elements based on their attribute values.
To use them, we enclose the attribute and its value in square brackets, like this: [attribute="value"].
For example, in your code, input[type="text"] targets <input> elements that have their type attribute set to "text".
Similarly, input[title="google"] targets <input> elements with the title attribute set to "google".
With attribute selectors, we can apply styles to elements based on specific attribute values.
Pseudo-Elements:
Pseudo-elements, on the other hand, allow us to target specific parts or states of an element.
We indicate a pseudo-element by using double colons :: before the element name.
For instance, input::placeholder targets the placeholder text of <input> elements.
Pseudo-elements enable us to style pseudo-parts or states, such as the placeholder text, first letter, or first line of an element.
要區分屬性選擇器和偽元素:
Attribute selectors use square brackets [attribute="value"] to target elements based on their attribute values.
Pseudo-elements use double colons :: before the element name to target specific parts or states of elements.
在您的CSS代碼中,選擇器輸入[type = & quot;文本& quot],輸入[title = & quot;谷歌& quot],和input::placeholder每個目標元素的不同方面:
input[type="text"] applies styles to <input> elements that have the type attribute set to "text".
input[title="google"] targets <input> elements with the title attribute set to "google".
input::placeholder targets the placeholder text within <input> elements.
通過使用這些選擇器,我們可以根據元素的屬性或狀態將獨特的樣式應用于特定的元素或元素的一部分。