diff --git a/files/en-us/web/css/css_writing_modes/index.md b/files/en-us/web/css/css_writing_modes/index.md index 19f3dbd3db8b95e..7140b88a6c3f7f3 100644 --- a/files/en-us/web/css/css_writing_modes/index.md +++ b/files/en-us/web/css/css_writing_modes/index.md @@ -20,6 +20,11 @@ The **CSS writing modes** module defines various international writing modes, su - {{cssxref("unicode-bidi")}} - {{cssxref("writing-mode")}} +## Guides + +- [Creating vertical form controls](/en-US/docs/Web/CSS/CSS_writing_modes/Vertical_controls) + - : The article explains how to use the CSS {{cssxref("writing-mode")}} and {{cssxref("direction")}} properties to create and configure vertical form controls. + ## Specifications {{Specifications}} diff --git a/files/en-us/web/css/css_writing_modes/vertical_controls/index.md b/files/en-us/web/css/css_writing_modes/vertical_controls/index.md new file mode 100644 index 000000000000000..084661f0add4d31 --- /dev/null +++ b/files/en-us/web/css/css_writing_modes/vertical_controls/index.md @@ -0,0 +1,474 @@ +--- +title: Creating vertical form controls +slug: Web/CSS/CSS_writing_modes/Vertical_controls +page-type: guide +--- + +{{CSSRef}} + +The guide explains how to use the CSS {{cssxref("writing-mode")}} and {{cssxref("direction")}} properties to create and configure vertical form controls. This includes: + +- [``](/en-US/docs/Web/HTML/Element/input/range) sliders, {{htmlelement("progress")}} bars, and {{htmlelement("meter")}} elements. +- {{htmlelement("select")}} elements. +- {{htmlelement("button")}} elements and button input types such as [``](/en-US/docs/Web/HTML/Element/input/button), [``](/en-US/docs/Web/HTML/Element/input/reset), and [``](/en-US/docs/Web/HTML/Element/input/submit). +- {{htmlelement("textarea")}} elements and text-based input types such as [``](/en-US/docs/Web/HTML/Element/input/text), [``](/en-US/docs/Web/HTML/Element/input/datetime-local), and [``](/en-US/docs/Web/HTML/Element/input/url). + +## General technique + +In modern browsers, the {{cssxref("writing-mode")}} property can be set to a vertical value to vertically display form controls with text characters that are normally horizontal (for example in Latin languages), with text displayed at a 90-degree angle from the default. Normally vertical text characters, for example in Chinese or Japanese, are unaffected in this regard. + +This is useful when creating vertical language forms. + +Specifically: + +- `writing-mode: vertical-lr` will create vertical form controls with a left-to-right block flow direction, meaning that in controls with wrapping or multiple lines of text, subsequent lines will appear to the right of previous lines. +- `writing-mode: vertical-rl` will create vertical form controls with a right-to-left block flow direction, meaning that in controls with wrapping or multiple lines of text, subsequent lines will appear to the left of previous lines. + +You could use a [transform](/en-US/docs/Web/CSS/transform) to rotate the controls by 90 degrees, but that would place the controls in their own layer and cause unintended layout side effects such as other content being overlapped. Using `writing-mode` offers a more reliable solution. + +> **Note:** While the {{cssxref("writing-mode")}} property is well supported, creating vertically-oriented form controls with `writing-mode` only gained full browser support in 2024. + +> **Note:** The experimental `sideways-lr` and `sideways-rl` values have a similar effect as `vertical-lr` and `vertical-rl` respectively, except that normally vertical text characters (for example in Chinese or Japanese) are rotated 90 degrees to display on their sides, while horizontal text characters (for example in Latin languages) are unaffected by these values. + +In addition, the {{cssxref("direction")}} property can be used to control the direction of the content inside the controls: + +- `direction: ltr` will cause the content to start at the top and flow towards the bottom. +- `direction: rtl` will cause the content to start at the bottom and flow towards the top. + +The `direction` property can be used to set the **inline base direction** — the primary direction in which content is ordered on a line, which defines on which sides the "start" and "end" of a line are. For text-based form controls the difference is obvious — the flow of text starts at the top or bottom. In non-text-based controls such as range sliders, `direction` sets the direction in which the control is drawn. For example, including `direction: ltr` on a vertical slider sets the lowest value at the top of the slider and the highest value at the bottom of the slider. + +The sections below show how to create different types of vertical form control, along with examples of each. Consult the browser compatibility information on each of the linked reference pages to find out the exact support information for each type. + +## Range sliders, meters, and progress bars + +Let's have a look at creating vertical range sliders, meters, and progress bars. + +### Basic example + +A typical set of visual [``](/en-US/docs/Web/HTML/Element/input/range) slider, {{htmlelement("progress")}}, and {{htmlelement("meter")}} controls is created like this: + +```html +
+``` + +> **Note:** Best practice is to include a {{htmlelement("label")}} element for each form control, to associate a meaningful text description with each field for accessibility purposes (see [Meaningful text labels](/en-US/docs/Learn/Accessibility/HTML#meaningful_text_labels) for more information). We haven't done that here, as this article focuses purely on aspects of the form controls' visual rendering, but you should make sure you do so in production code. + +To display the controls vertically, we can use CSS like this: + +```css +input[type="range"], +meter, +progress { + margin-block-end: 20px; + writing-mode: vertical-lr; +} +``` + +{{cssxref("writing-mode", "writing-mode: vertical-lr")}} (and `vertical-rl`) causes the controls to be displayed vertically in modern browsers. + +The result of this looks like so: + +{{ EmbedLiveSample("Basic example", "100%", "170") }} + +### Drawing the control from bottom to top + +By default, the controls have a {{cssxref("direction")}} value of `ltr`. This causes your sliders, meters, and progress bars to be drawn from top to bottom, as seen above. + +You can change this by setting `direction: rtl` — this causes them to be drawn from bottom to top instead: + +```html hidden + +``` + +```css +input[type="range"], +meter, +progress { + margin-block-end: 20px; + writing-mode: vertical-lr; + direction: rtl; +} +``` + +The result of this looks like so: + +{{ EmbedLiveSample("Drawing the control from bottom to top", "100%", "170") }} + +### Creating vertical range sliders in older browsers + +In older browsers that do not support the creation of vertical form controls with `writing-mode` and `direction`, there are limited alternatives available. The following only work on ``, causing the text to flow from bottom to top — they have no effect on `