2017-10-10 38 views
1

我有一個CSS文件,我將輸入設置爲除默認值之外的其他值。如何將屬性恢復爲默認值?

input[type=text]{ 
    text-align: center; 
    border: none; 
    background-color: #EBEBEB; 
    border-radius: 5px; 
    width: 100%; 
    padding: 12px; 
    margin-top: 25px; 
    display: inline-block; 
    box-sizing: border-box; 
    font-family: "Lucida Sans Typewriter", "Lucida Console", monaco, 
    "Bitstream Vera Sans Mono", monospace; 
} 

現在我想使用此INPUT在網頁上的其他地方使用默認值。我怎樣才能將它設置爲默認只在網頁的特定部分,而不會損壞INPUT的其餘部分?

+2

給的,而不是應用全局風格影響所有投入要素的輸入一類? – Terry

+1

給你的輸入一個ID並覆蓋你想要的任何樣式屬性。 –

回答

2

您可以使用:not和一個額外的類來解決這個問題:

input[type=text]:not(.default) { 
 
    text-align: center; 
 
    border: none; 
 
    background-color: #EBEBEB; 
 
    border-radius: 5px; 
 
    width: 100%; 
 
    padding: 12px; 
 
    margin-top: 25px; 
 
    display: inline-block; 
 
    box-sizing: border-box; 
 
    font-family: "Lucida Sans Typewriter", "Lucida Console", monaco, 
 
    "Bitstream Vera Sans Mono", monospace; 
 
}
<input type="text" value="Hello StackOverflow"/> 
 
<input class="default" type="text" value="Hello StackOverflow"/>

更好的解決方案,爲你的問題的意見已經提到的,是使用一類使用範圍。所以請僅使用一個類格式化特定的<input type="text"/>元素!請看下面的例子:

input[type=text].style1 { 
 
    text-align: center; 
 
    border: none; 
 
    background-color: #EBEBEB; 
 
    border-radius: 5px; 
 
    width: 100%; 
 
    padding: 12px; 
 
    margin-top: 25px; 
 
    display: inline-block; 
 
    box-sizing: border-box; 
 
    font-family: "Lucida Sans Typewriter", "Lucida Console", monaco, 
 
    "Bitstream Vera Sans Mono", monospace; 
 
}
<input class="style1" type="text" value="Hello StackOverflow"/> 
 
<input type="text" value="Hello StackOverflow"/>