Das Aussehen von HTML-Formularen kann mit CSS verbessert werden.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=text], select { |
| width: 80%; |
| padding: 10px 18px; |
| margin: 10px 0; |
| display: inline-block; |
| border: 1px solid black; |
| border-radius: 4px; |
| box-sizing: border-box; |
| } |
| input[type=submit] { |
| width: 80%; |
| background-color: black; |
| color: white; |
| padding: 12px 22px; |
| margin: 10px 0; |
| border: none; |
| border-radius: 5px; |
| cursor: pointer; |
| } |
| input[type=submit]:hover { |
| background-color: #404040; |
| transition: ease 0.4s; |
| } |
| div.frame { |
| border-radius: 6px; |
| background-color: grey; |
| padding: 16px; |
| margin: 2rem; |
| } |
| </style> |
| </head> |
| <body> |
| <h3>Benutze CSS um ein Formular zu stylen</h3> |
| <div class="frame"> |
| <form action="action_page.php"> |
| <label for="Vorname">Vorname</label> |
| <input type="text" id="Vorname" name="Vorname" placeholder="Dein Vorname.."> |
| <label for="Nachname">Nachname</label> |
| <input type="text" id="Nachname" name="Nachname" placeholder="Dein Nachname.."> |
| <label for="Kontinent">Aus welchem Kontinent stammen Sie?</label> |
| <select id="Kontinent" name="Kontinent"> |
| <option value="europe">Europa</option> |
| <option value="asia">Asien</option> |
| <option value="africa">Afrika</option> |
| <option value="australia">Australien</option> |
| <option value="america">Nord-und Südamerika</option> |
| </select> |
| <input type="submit" value="Submit"> |
| </form> |
| </div> |
| </body> |
| </html> |
Style das Aussehen von Input-Feldern.
Wenn Sie nur einzelne input-Felder stylen möchten, verwenden sie Attribut-Selektoren:
| input[type=text]-Felder. |
| input[type=password]-Felder. |
| input[type=number]-Felder. |
Die maximale Breite(Weite) eines input-Feldes.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input { |
| width: 100%; |
| } |
| p { |
| text-align: center; |
| } |
| </style> |
| </head> |
| <body> |
| <p>Die maximale Breite(Weite) eines input-Feldes:</p> |
| <form> |
| <label for="Vorname">Vorname</label><br> |
| <input type="text" id="Vorname" name="Vorname"> |
| </form> |
| </body> |
| </html> |
Füge einem input-Feld einen Innen- und Aussenabstand hinzu mit padding und margin.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=text] { |
| width: 100%; |
| padding: 16px 24px; |
| margin: 12px 0; |
| box-sizing: border-box; |
| } |
| </style> |
| </head> |
| <body> |
| <p>Füge einem input-Feld einen Innen- und Aussenabstand hinzu:</p> |
| <form> |
| <label for="Vorname">Vorname</label> |
| <input type="text" id="Vorname" name="Vorname"> |
| <label for="Nachname">Nachname</label> |
| <input type="text" id="Nachname" name="Nachname"> |
| </form> |
| </body> |
| </html> |
Füge einem input-Feld einen Rahmen hinzu mit border.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=text] { |
| width: 100%; |
| padding: 12px 20px; |
| margin: 8px 0; |
| box-sizing: border-box; |
| border: 2px solid dodgerblue; |
| border-radius: 4px; |
| } |
| </style> |
| </head> |
| <body> |
| <p>Füge einem input-Feld einen Rahmen hinzu:</p> |
| <form> |
| <label for="Vorname">Vorname</label> |
| <input type="text" id="Vorname" name="Vorname"> |
| <label for="Nachname">Nachname</label> |
| <input type="text" id="Nachname" name="Nachname"> |
| </form> |
| </body> |
| </html> |
Füge einem input-Feld einen Hintergrundfarbe hinzu mit background-color.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=text] { |
| width: 80%; |
| padding: 12px 20px; |
| margin: 8px 0; |
| box-sizing: border-box; |
| border: none; |
| background-color: dodgerblue; |
| color: black; |
| } |
| </style> |
| </head> |
| <body> |
| <p>Füge einem input-Feld einen Hintergrundfarbe hinzu:</p> |
| <form> |
| <label for="Vorname">Vorname</label><br> |
| <input type="text" id="Vorname" name="Vorname"><br> |
| <label for="Nachname">Nachname</label><br> |
| <input type="text" id="Nachname" name="Nachname"><br> |
| </form> |
| </body> |
| </html> |
Füge eine Hintergrundfarbe hinzu wenn das input-Feld angeklickt wird (mit focus).
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=text] { |
| width: 80%; |
| padding: 12px 20px; |
| margin: 8px 0; |
| box-sizing: border-box; |
| border: 1px solid black; |
| outline: none; |
| } |
| input[type=text]:focus { |
| background-color: grey; |
| } |
| </style> |
| </head> |
| <body> |
| <p>Füge eine Hintergrundfarbe hinzu wenn das input-Feld angeklickt wird (mit focus).</p> |
| <form> |
| <label for="Vorname">Vorname</label><br> |
| <input type="text" id="Vorname" name="Vorname"><br> |
| <label for="Nachname">Nachname</label><br> |
| <input type="text" id="Nachname" name="Nachname"><br> |
| </form> |
| </body> |
| </html> |
input-Feld mit einem Icon versehen.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=text] { |
| width: 100%; |
| box-sizing: border-box; |
| border: 2px solid black; |
| border-radius: 4px; |
| font-size: 16px; |
| background-color: white; |
| background-image: url('Bilder/searchicon.png'); |
| background-position: 10px 10px; |
| background-repeat: no-repeat; |
| padding: 12px 20px 12px 40px; |
| } |
| </style> |
| </head> |
| <body> |
| <p>input-Feld mit einem Icon:</p> |
| <form> |
| <input type="text" name="search" placeholder="Search.."> |
| </form> |
| </body> |
| </html> |
input-Feld animieren mit :focus und transition.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=text] { |
| width: 20%; |
| box-sizing: border-box; |
| border: 2px solid black; |
| border-radius: 4px; |
| font-size: 16px; |
| background-color: white; |
| background-image: url('Bilder/searchicon.png'); |
| background-position: 10px 10px; |
| background-repeat: no-repeat; |
| padding: 12px 20px 12px 40px; |
| transition: width 0.4s ease-in-out; |
| } |
| input[type=text]:focus { |
| width: 80%; |
| } |
| </style> |
| </head> |
| <body> |
| <p>input-Feld animieren mit :focus und transition:</p> |
| <form> |
| <input type="text" name="search" placeholder="Search.."> |
| </form> |
| </body> |
| </html> |
Style ein select-Menu.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| select { |
| width: 100%; |
| padding: 12px 18px; |
| border: none; |
| border-radius: 6px; |
| background-color: grey; |
| color: black; |
| } |
| p { |
| text-align: center; |
| } |
| </style> |
| <body> |
| <p>Style ein select-Menu.</p> |
| <form> |
| <select id="Kontinent" name="Kontinent"> |
| <option value="australia">Australien</option> |
| <option value="europe">Europa</option> |
| <option value="asia">Asien</option> |
| <option value="america">Nord-und Südamerika</option> |
| <option value="africa">Afrika</option> |
| </select> |
| </form> |
| </body> |
| </html> |
Style input-Buttons.
| <!DOCTYPE html> |
| <html> |
| <head> |
| <style> |
| input[type=submit], input[type=reset] { |
| background-color: black; |
| border: none; |
| color: orange; |
| padding: 14px 30px; |
| text-decoration: none; |
| margin: 6px 4px; |
| cursor: pointer; |
| } |
| p { |
| text-align: center; |
| } |
| .alignCenter { |
| display: flex; |
| justify-content: center; |
| } |
| </style> |
| </head> |
| <body> |
| <p>Style input-Buttons.</p> |
| <div class="alignCenter"> |
| <input type="reset" value="Reset"> |
| <input type="submit" value="Submit"> |
| </div> |
| </body> |
| </html> |