СÑили HTML ÑоÑм
Ð ÑÑой ÑÑаÑÑе ÐÑ ÑзнаеÑ, как иÑполÑзоваÑÑ CSS Ñ HTML-ÑоÑмами, ÑÑÐ¾Ð±Ñ ÑделаÑÑ Ð¸Ñ (надеÑÑÑ) более кÑаÑивÑми. УдивиÑелÑно, но ÑÑо Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð½ÐµÐ¼Ð½Ð¾Ð³Ð¾ Ñложнее. Ðо иÑÑоÑиÑеÑким и ÑÐµÑ Ð½Ð¸ÑеÑким пÑиÑинам виджеÑÑ ÑоÑм Ð¿Ð»Ð¾Ñ Ð¾ ÑоÑеÑаÑÑÑÑ Ñ CSS. Ðз-за ÑÑÐ¸Ñ ÑÑÑдноÑÑей многие ÑазÑабоÑÑики пÑедпоÑиÑаÑÑ ÑоздаваÑÑ Ñвои ÑобÑÑвеннÑе HTML-виджеÑÑ, ÑÑÐ¾Ð±Ñ Ð¿Ð¾Ð»ÑÑиÑÑ ÐºÐ¾Ð½ÑÑÐ¾Ð»Ñ Ð½Ð°Ð´ Ñвоим внеÑним видом. Ðднако в ÑовÑеменнÑÑ Ð±ÑаÑзеÑÐ°Ñ Ð²ÐµÐ±-дизайнеÑÑ Ð²Ñе болÑÑе конÑÑолиÑÑÑÑ Ð´Ð¸Ð·Ð°Ð¹Ð½ ÑлеменÑов ÑоÑмÑ. ÐавайÑе пÑиÑÑÑпим!
ÐоÑÐµÐ¼Ñ Ñак Ñложно ÑÑилизоваÑÑ Ð²Ð¸Ð´Ð¶ÐµÑÑ ÑоÑм Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ CSS?
Ðа заÑе ÐнÑеÑнеÑа, пÑимеÑно в 1995 годÑ, в HTML 2 бÑли Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ñ ÑлеменÑÑ ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ ÑоÑмой. Ðз-за ÑложноÑÑи виджеÑов ÑоÑм ÑазÑабоÑÑики ÑеÑили полагаÑÑÑÑ Ð½Ð° базовÑÑ Ð¾Ð¿ÐµÑаÑионнÑÑ ÑиÑÑÐµÐ¼Ñ Ð´Ð»Ñ ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ Ð¸Ð¼Ð¸ и Ð¸Ñ ÑендеÑинга.
ÐеÑколÑко Ð»ÐµÑ ÑпÑÑÑÑ Ð±Ñл Ñоздан CSS, и Ñо, ÑÑо бÑло ÑÐµÑ Ð½Ð¸ÑеÑкой Ð½ÐµÐ¾Ð±Ñ Ð¾Ð´Ð¸Ð¼Ð¾ÑÑÑÑ, Ñо еÑÑÑ Ð¸ÑполÑзование ÑобÑÑвеннÑÑ Ð²Ð¸Ð´Ð¶ÐµÑов Ð´Ð»Ñ ÑеализаÑии ÑлеменÑов ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ ÑоÑмой, ÑÑало ÑÑебованием к ÑÑилÑ. РпеÑвÑе дни CSS, ÑÑилизаÑÐ¸Ñ ÑлеменÑов ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ ÑоÑÐ¼Ñ Ð½Ðµ бÑла пÑиоÑиÑеÑом.
ÐоÑколÑÐºÑ Ð¿Ð¾Ð»ÑзоваÑели пÑивÑкли к внеÑÐ½ÐµÐ¼Ñ Ð²Ð¸Ð´Ñ ÑÐ²Ð¾Ð¸Ñ ÑооÑвеÑÑÑвÑÑÑÐ¸Ñ Ð¿Ð»Ð°ÑÑоÑм, поÑÑавÑики бÑаÑзеÑов Ð½ÐµÐ¾Ñ Ð¾Ñно делаÑÑ ÑлеменÑÑ ÑпÑÐ°Ð²Ð»ÐµÐ½Ð¸Ñ ÑоÑмами ÑÑилевÑми; и по Ñей Ð´ÐµÐ½Ñ Ð²Ñе еÑÑ ÑÑезвÑÑайно ÑÑÑдно пеÑеÑÑÑоиÑÑ Ð²Ñе ÑлеменÑÑ ÑпÑавлениÑ, ÑÑÐ¾Ð±Ñ ÑделаÑÑ Ð¸Ñ ÑÑилизованнÑми.
Ðаже ÑÐµÐ³Ð¾Ð´Ð½Ñ Ð½Ð¸ один бÑаÑÐ·ÐµÑ Ð¿Ð¾Ð»Ð½Ð¾ÑÑÑÑ Ð½Ðµ ÑеализÑÐµÑ CSS 2.1. Ðднако Ñо вÑеменем поÑÑавÑики бÑаÑзеÑов ÑлÑÑÑили ÑÐ²Ð¾Ñ Ð¿Ð¾Ð´Ð´ÐµÑÐ¶ÐºÑ CSS Ð´Ð»Ñ ÑлеменÑов ÑоÑмÑ, и, неÑмоÑÑÑ Ð½Ð° Ð¿Ð»Ð¾Ñ ÑÑ ÑепÑÑаÑÐ¸Ñ Ð² оÑноÑении ÑдобÑÑва иÑполÑзованиÑ, ÑепеÑÑ Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе иÑполÑзоваÑÑ CSS Ð´Ð»Ñ ÑÑилизаÑии HTML ÑоÑм.
Ðе вÑе виджеÑÑ ÑÐ¾Ð·Ð´Ð°Ð½Ñ ÑавнÑми, когда задейÑÑвован CSS
РнаÑÑоÑÑее вÑÐµÐ¼Ñ Ð½ÐµÐºÐ¾ÑоÑÑе ÑÑÑдноÑÑи оÑÑаÑÑÑÑ Ð¿Ñи иÑполÑзовании CSS Ñ ÑоÑмами. ÐÑи пÑÐ¾Ð±Ð»ÐµÐ¼Ñ Ð¼Ð¾Ð¶Ð½Ð¾ ÑазделиÑÑ Ð½Ð° ÑÑи каÑегоÑии:
ХоÑоÑаÑ
ÐекоÑоÑÑе ÑлеменÑÑ Ð¼Ð¾Ð³ÑÑ Ð±ÑÑÑ ÑÑÐ¸Ð»Ð¸Ð·Ð¾Ð²Ð°Ð½Ñ Ñ Ð½ÐµÐ±Ð¾Ð»ÑÑим колиÑеÑÑвом пÑоблем на ÑазнÑÑ Ð¿Ð»Ð°ÑÑоÑÐ¼Ð°Ñ . Рним оÑноÑÑÑÑÑ ÑледÑÑÑие ÑÑÑÑкÑÑÑнÑе ÑлеменÑÑ:
СÑда Ñакже Ð²Ñ Ð¾Ð´ÑÑ Ð²Ñе виджеÑÑ ÑекÑÑового Ð¿Ð¾Ð»Ñ (как одноÑÑÑоÑнÑе, Ñак и многоÑÑÑоÑнÑе) и кнопки.
ÐÐ»Ð¾Ñ Ð°Ñ
ÐекоÑоÑÑе ÑлеменÑÑ Ñедко могÑÑ Ð±ÑÑÑ ÑÑилизованÑ, и могÑÑ Ð¿Ð¾ÑÑебоваÑÑ Ð½ÐµÐºÐ¾ÑоÑÑÑ ÑложнÑÑ Ñловок, иногда ÑÑебÑÑÑÐ¸Ñ ÑглÑблÑннÑÑ Ð·Ð½Ð°Ð½Ð¸Ð¹ CSS3.
Ðни вклÑÑаÑÑ Ð² ÑÐµÐ±Ñ ÑÐ»ÐµÐ¼ÐµÐ½Ñ <legend>, но его нелÑÐ·Ñ Ð¿ÑавилÑно ÑаÑположиÑÑ Ð½Ð° вÑеÑ
плаÑÑоÑмаÑ
. Флажки и пеÑеклÑÑаÑели Ñакже не могÑÑ Ð±ÑÑÑ ÑÑÐ¸Ð»Ð¸Ð·Ð¾Ð²Ð°Ð½Ñ Ð½Ð°Ð¿ÑÑмÑÑ, однако, благодаÑÑ CSS3 Ð²Ñ Ð¼Ð¾Ð¶ÐµÑе обойÑи ÑÑо. ÐонÑÐµÐ½Ñ placeholder не Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ ÑÑилизован каким-либо ÑÑандаÑÑнÑм ÑпоÑобом, однако вÑе бÑаÑзеÑÑ, коÑоÑÑе его ÑеализÑÑÑ, Ñакже ÑеализÑÑÑ ÑобÑÑвеннÑе пÑевдо-ÑлеменÑÑ CSS или пÑевдоклаÑÑÑ, коÑоÑÑе позволÑÑÑ ÐµÐ³Ð¾ ÑÑилизоваÑÑ.
ÐÑ Ð¾Ð¿Ð¸Ñем, как обÑабаÑÑваÑÑ ÑÑи более конкÑеÑнÑе ÑлÑÑаи, в ÑÑаÑÑе «РаÑÑиÑеннÑе ÑÑили Ð´Ð»Ñ HTML-ÑоÑм».
The ugly
Some elements simply can't be styled using CSS. These include: all advanced user interface widgets, such as range, color, or date controls; and all the dropdown widgets, including <select>, <option>, <optgroup> and <datalist> elements. The file picker widget is also known not to be stylable at all. The new <progress> and <meter> elements also fall in this category.
The main issue with all these widgets, comes from the fact that they have a very complex structure, and CSS is not currently expressive enough to style all the subtle parts of those widgets. If you want to customize those widgets, you have to rely on JavaScript to build a DOM tree you'll be able to style. We explore how to do this in the article How to build custom form widgets.
Basic styling
To style elements that are easy to style with CSS, you shouldn't face any difficulties, since they mostly behave like any other HTML element. However, the user-agent style sheet of every browser can be a little inconsistent, so there are a few tricks that can help you style them in an easier way.
Search fields
Search boxes are the only kind of text fields that can be a little tricky to style. On WebKit based browsers (Chrome, Safari, etc.), you'll have to tweak it with the -webkit-appearance proprietary property. We discuss this property further in the article: Advanced styling for HTML forms.
Example
<form>
<input type="search" />
</form>
input[type="search"] {
border: 1px dotted #999;
border-radius: 0;
-webkit-appearance: none;
}

As you can see on this screenshot of the search field on Chrome, the two fields have a border set as in our example. The first field is rendered without using the -webkit-appearance property, whereas the second is rendered using -webkit-appearance:none. This difference is noticeable.
Fonts and text
CSS font and text features can be used easily with any widget (and yes, you can use @font-face with form widgets). However, browsers' behaviors are often inconsistent. By default, some widgets do not inherit font-family and font-size from their parents. Many browsers use the system default appearance instead. To make your forms' appearance consistent with the rest of your content, you can add the following rules to your stylesheet:
button,
input,
select,
textarea {
font-family: inherit;
font-size: 100%;
}
The screenshot below shows the difference; on the left is the default rendering of the element in Firefox on Mac OS X, with the platform's default font style in use. On the right are the same elements, with our font harmonization style rules applied.

There's a lot of debate as to whether forms look better using the system default styles, or customized styles designed to match your content. This decision is yours to make, as the designer of your site, or Web application.
Box model
All text fields have complete support for every property related to the CSS box model (width, height, padding, margin, and border). As before, however, browsers rely on the system default styles when displaying these widgets. It's up to you to define how you wish to blend them into your content. If you want to keep the native look and feel of the widgets, you'll face a little difficulty if you want to give them a consistent size.
This is because each widget has their own rules for border, padding and margin. So if you want to give the same size to several different widgets, you have to use the box-sizing property:
input,
textarea,
select,
button {
width: 150px;
margin: 0;
-webkit-box-sizing: border-box; /* For legacy WebKit based browsers */
-moz-box-sizing: border-box; /* For legacy (Firefox <29) Gecko based browsers */
box-sizing: border-box;
}

In the screenshot above, the left column is built without box-sizing, while the right column uses this property with the value border-box. Notice how this lets us ensure that all of the elements occupy the same amount of space, despite the platform's default rules for each kind of widget.
Positioning
Positioning of HTML form widgets is generally not a problem; however, there are two elements you should take special note of:
legend
The <legend> element is okay to style, except for positioning. In every browser, the <legend> element is positioned on top of the top border of its <fieldset> parent. There is absolutely no way to change it to be positioned within the HTML flow, away from the top border. You can, however, position it absolutely or relatively, using the position property. But otherwise it is part of the fieldset border.
Because the <legend> element is very important for accessibility reasons, it will be spoken by assistive technologies as part of the label of each form element inside the fieldset, it's quite often paired with a title, and then hidden in an accessible way. For example:
HTML
<fieldset>
<legend>Hi!</legend>
<h1>Hello</h1>
</fieldset>
CSS
legend {
width: 1px;
height: 1px;
overflow: hidden;
}
textarea
By default, all browsers consider the <textarea> element to be an inline block, aligned to the text bottom line. This is rarely what we actually want to see. To change from inline-block to block, it's pretty easy to use the display property. But if you want to use it inline, it's common to change the vertical alignment:
textarea {
vertical-align: top;
}
Example
Let's look at a concrete example of how to style an HTML form. This will help make a lot of these ideas clearer. We will build the following "postcard" contact form:

If you want to follow along with this example, make a local copy of our postcard-start.html file, and follow the below instructions.
The HTML
The HTML is only slightly more involved than the example we used in the first article of this guide; it just has a few extra IDs and a title.
<form>
<h1>to: Mozilla</h1>
<div id="from">
<label for="name">from:</label>
<input type="text" id="name" name="user_name" />
</div>
<div id="reply">
<label for="mail">reply:</label>
<input type="email" id="mail" name="user_email" />
</div>
<div id="message">
<label for="msg">Your message:</label>
<textarea id="msg" name="user_message"></textarea>
</div>
<div class="button">
<button type="submit">Send your message</button>
</div>
</form>
Add the above code into the body of your HTML.
Organizing your assets
This is where the fun begins! Before we start coding, we need three additional assets:
- The postcard background â download this image and save it in the same directory as your working HTML file.
- A typewriter font: The "Secret Typewriter" font from fontsquirrel.com â download the TTF file into the same directory as above.
- A handdrawn font: The "Journal" font from fontsquirrel.com â download the TTF file into the same directory as above.
Your fonts need some more processing before you start:
- Go to the fontsquirrel Webfont Generator.
- Using the form, upload both your font files and generate a webfont kit. Download the kit to your computer.
- Unzip the provided zip file.
- Inside the unzipped contents you will find two
.wofffiles and two.woff2files. Copy these four files into a directory called fonts, in the same directory as before. We are using two different files for each font to maximise browser compatibility; see our Web fonts article for a lot more information.
The CSS
Now we can dig into the CSS for the example. Add all the code blocks shown below inside the <style> element, one after another.
First, we prepare the ground by defining our @font-face rules, all the basics on the <body> element, and the <form> element:
@font-face {
font-family: "handwriting";
src:
url("fonts/journal-webfont.woff2") format("woff2"),
url("fonts/journal-webfont.woff") format("woff");
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: "typewriter";
src:
url("fonts/veteran_typewriter-webfont.woff2") format("woff2"),
url("fonts/veteran_typewriter-webfont.woff") format("woff");
font-weight: normal;
font-style: normal;
}
body {
font: 21px sans-serif;
padding: 2em;
margin: 0;
background: #222;
}
form {
position: relative;
width: 740px;
height: 498px;
margin: 0 auto;
background: #fff url(background.jpg);
}
Now we can position our elements, including the title and all the form elements:
h1 {
position: absolute;
left: 415px;
top: 185px;
font:
1em "typewriter",
sans-serif;
}
#from {
position: absolute;
left: 398px;
top: 235px;
}
#reply {
position: absolute;
left: 390px;
top: 285px;
}
#message {
position: absolute;
left: 20px;
top: 70px;
}
That's where we start working on the form elements themselves. First, let's ensure that the <label>s are given the right font:
label {
font:
0.8em "typewriter",
sans-serif;
}
The text fields require some common rules. Simply put, we remove their borders and backgrounds, and redefine their padding and margin:
input,
textarea {
font:
0.9em/1.5em "handwriting",
sans-serif;
border: none;
padding: 0 10px;
margin: 0;
width: 240px;
background: none;
}
When one of these fields gains focus, we highlight them with a light grey, transparent, background. Note that it's important to add the outline property, in order to remove the default focus highlight added by some browsers:
input:focus,
textarea:focus {
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
outline: none;
}
Now that our text fields are complete, we need to adjust the display of the single and multiple line text fields to match, since they won't typically look the same using the defaults.
The single-line text field needs some tweaks to render nicely in Internet Explorer. Internet Explorer does not define the height of the fields based on the natural height of the font (which is the behavior of all other browsers). To fix this, we need to add an explicit height to the field, as follows:
input {
height: 2.5em; /* for IE */
vertical-align: middle; /* This is optional but it makes legacy IEs look better */
}
<textarea> elements default to being rendered as a block element. The two important things here are the resize and overflow properties. Because our design is a fixed-size design, we will use the resize property to prevent users from resizing our multi-line text field. The overflow property is used to make the field render more consistently across browsers. Some browsers default to the value auto, while some default to the value scroll. In our case, it's better to be sure every one will use auto:
textarea {
display: block;
padding: 10px;
margin: 10px 0 0 -10px;
width: 340px;
height: 360px;
resize: none;
overflow: auto;
}
The <button> element is really convenient with CSS; you can do whatever you want, even using pseudo-elements:
button {
position: absolute;
left: 440px;
top: 360px;
padding: 5px;
font: bold 0.6em sans-serif;
border: 2px solid #333;
border-radius: 5px;
background: none;
cursor: pointer;
-webkit-transform: rotate(-1.5deg);
-moz-transform: rotate(-1.5deg);
-ms-transform: rotate(-1.5deg);
-o-transform: rotate(-1.5deg);
transform: rotate(-1.5deg);
}
button:after {
content: " >>>";
}
button:hover,
button:focus {
outline: none;
background: #000;
color: #fff;
}
And voila!
ÐÑимеÑание: If your example does not work quite like you expected and you want to check it against our version, you can find it on GitHub â see it running live (also see the source code).
Conclusion
As you can see, as long as we want to build forms with just text fields and buttons, it's easy to style them using CSS. If you want to know more of the little CSS tricks that can make your life easier when working with form widgets, take a look at the form part of the normalize.css project.
In the next article, we will see how to handle form widgets which fall in the "bad" and "ugly" categories.
page(Doc) not found /ru/docs/Learn/HTML/Forms/HTML_forms_in_legacy_browsersIn this module
- Your first HTML form
- How to structure an HTML form
- The native form widgets
- Sending form data
- Form data validation
- How to build custom form widgets
- Sending forms through JavaScript
- HTML forms in legacy browsers
- Styling HTML forms
- Advanced styling for HTML forms
- Property compatibility table for form widgets