How to style input and submit button with CSS?

前端 未结 11 816
天涯浪人
天涯浪人 2020-12-12 13:25

I\'m learning CSS. How to style input and submit button with CSS?

I\'m trying create something like this but I have no idea how to do

11条回答
  •  天涯浪人
    2020-12-12 13:51

    For reliability I'd suggest giving class-names, or ids to the elements to style (ideally a class for the text-inputs, since there will presumably be several) and an id to the submit button (though a class would work as well):

    
        
        
        
        
        
    
    

    With the CSS:

    .textInput {
        /* styles the text input elements with this class */
    }
    
    #submitBtn {
        /* styles the submit button */
    }
    

    For more up-to-date browsers, you can select by attributes (using the same HTML):

    .input {
        /* styles all input elements */
    }
    
    .input[type="text"] {
         /* styles all inputs with type 'text' */
    }
    
    .input[type="submit"] {
         /* styles all inputs with type 'submit' */
    }
    

    You could also just use sibling combinators (since the text-inputs to style seem to always follow a label element, and the submit follows a textarea (but this is rather fragile)):

    label + input,
    label + textarea {
        /* styles input, and textarea, elements that follow a label */
    }
    
    input + input,
    textarea + input {
        /* would style the submit-button in the above HTML */
    }
    

提交回复
热议问题