docs.rodeo

MDN Web Docs mirror

field-sizing

{{CSSRef}} {{seecompattable}} 

The field-sizing CSS property enables you to control the sizing behavior of elements that are given a default preferred size, such as form control elements. This property enables you to override the default sizing behavior, allowing form controls to adjust in size to fit their contents.

This property is typically used to style text {{htmlelement("input")}}  and {{htmlelement("textarea")}}  elements to allow them to shrinkwrap their content as well as grow when more text is entered into the form control.

Syntax

/* Keyword values */
field-sizing: content;
field-sizing: fixed;

/* Global values */
field-sizing: inherit;
field-sizing: initial;
field-sizing: revert;
field-sizing: revert-layer;
field-sizing: unset;

Values

Description

field-sizing: content overrides the default preferred sizing of form elements. This setting provides an easy way to configure text inputs to shrinkwrap their content and grow as more text is entered. They stop expanding when they reach maximum size limits (defined by the size of their containing element or set via CSS), at which point scrolling is required to view all the content.

Elements affected by field-sizing: content

Specifically, field-sizing to content affects the following elements:

field-sizing interaction with other size settings

The sizing flexibility provided to form controls by field-sizing: content can be overridden if you use other CSS sizing properties. Avoid setting a fixed {{cssxref("width")}}  and {{cssxref("height")}}  when using field-sizing: content because they will reimpose a fixed size on the control. However, using properties like {{cssxref("min-width")}}  and {{cssxref("max-width")}}  alongside field-sizing: content is quite effective because they allow the control to grow and shrink with the entered text and also prevent the control from becoming too large or too small.

The maxlength attribute causes the control to stop growing in size when the maximum character limit is reached.

Formal definition

{{cssinfo}} 

Formal syntax

{{csssyntax}} 

Examples

Growing and shrinking text fields

This example illustrates the effect of field-sizing: content on single- and multi-line text fields. The fields adjust their size as text is added or removed, effectively shrinkwrapping the contents, until a lower or upper size limit is reached.

HTML

The HTML in this example contains three form fields, each with an associated {{htmlelement("label")}} : two <input> elements of types text and email and a {{htmlelement("textarea")}}  element.

<div>
  <label for="name">Enter name:</label>
  <input type="text" id="name" maxlength="50" />
</div>
<div>
  <label for="email">Enter email:</label>
  <input type="email" id="email" maxlength="50" placeholder="e.g. a@b.com" />
</div>
<div>
  <label for="comment">Enter comment:</label>
  <textarea id="comment">This is a comment.</textarea>
</div>

Note the following points about the HTML:

CSS

In the CSS, we set field-sizing: content on the three form fields, along with a {{cssxref("min-width")}}  and {{cssxref("max-width")}}  to constrain the input size. It is worth reiterating that, if no minimum width was set on the fields, they would be rendered only as wide as the text cursor.

We also give the <label>s some rudimentary styling so that they sit neatly next to the fields.

body {
  box-sizing: border-box;
  padding: 20px;
}

div {
  margin-bottom: 20px;
  display: flex;
}
input,
textarea {
  field-sizing: content;
  min-width: 50px;
  max-width: 350px;
}

label {
  width: 150px;
  margin-right: 20px;
  text-align: right;
}

Result

Try entering and removing text in the fields below to explore the effects of field-sizing: content alongside other sizing properties.

{{ EmbedLiveSample('Growing/shrinking text fields', '100%', '200') }} 

Controlling <select> element display

This example illustrates the effect of field-sizing: content on {{htmlelement("select")}}  elements, both drop-down menu types and multiline listbox types.

HTML

The HTML contains two sets of <select> elements: one with field-sizing: content applied, and one without, allowing you to see the difference (though the effect may be less obvious than on text fields). Each set includes one drop-down menu type and one multiline listbox type (with the multiple attribute set).

<div class="field-sizing">
  <h2>With <code>field-sizing: content</code></h2>
  <select>
    <option>Bananas</option>
    <option>Strawberries</option>
    <option selected>Apples</option>
    <option>Raspberries</option>
    <option>Pomegranate</option>
  </select>
  <select multiple>
    <option>Bananas</option>
    <option>Strawberries</option>
    <option>Apples</option>
    <option>Raspberries</option>
    <option>Pomegranate</option>
  </select>
</div>
<div>
  <h2>Without <code>field-sizing: content</code></h2>
  <select>
    <option>Bananas</option>
    <option>Strawberries</option>
    <option selected>Apples</option>
    <option>Raspberries</option>
    <option>Pomegranate</option>
  </select>
  <select multiple>
    <option>Bananas</option>
    <option>Strawberries</option>
    <option>Apples</option>
    <option>Raspberries</option>
    <option>Pomegranate</option>
  </select>
</div>

[!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 Use meaningful text labels for more information). We haven’t done so in this example, as it focuses purely on aspects of the form controls’ visual rendering, but you should make sure you include form labels in production code.

CSS

In the CSS, field-sizing: content is set only on the first set of <select> elements.

body {
  box-sizing: border-box;
  display: flex;
  gap: 20px;
  font-family: sans-serif;
}

h2 {
  margin-top: 0;
  font-size: 1rem;
  text-align: center;
  flex: 1 0 100%;
}

div {
  margin-bottom: 20px;
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: space-around;
  flex-flow: row wrap;
}

select {
  valign: top;
}
.field-sizing select {
  field-sizing: content;
}

Result

{{ EmbedLiveSample('Controlling select element display', '100%', '170') }} 

Note the following effects of field-sizing: content:

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN