2017-10-10 37 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"/>