Nasi wolontariusze nie przetłumaczyli jeszcze tego artykułu na język Polski. Dołącz do nas i pomóż go przetłumaczyć!
Można także przeczytać artykuł w języku: English (US).

The grid-column CSS property is a shorthand property for grid-column-start and grid-column-end specifying a grid item's size and location within the grid column by contributing a line, a span, or nothing (automatic) to its grid placement, thereby specifying the inline-start and inline-end edge of its grid area.

Syntax

This property is specified as one or two <grid-line> values.

If two <grid-line> values are given they are separated by "/". The grid-column-start longhand is set to the value before the slash, and the grid-column-end longhand is set to the value after the slash.

Each <grid-line> value can be specified as:

  • either the auto keyword
  • or a <custom-ident> value
  • or an <integer> value
  • or both <custom-ident> and <integer>, separated by a space
  • or the keyword span together with either a <custom-ident> or an <integer> or both.

Values

auto
Is a keyword indicating that the property contributes nothing to the grid item’s placement, indicating auto-placement, an automatic span, or a default span of 1.
<custom-ident>
If there is a named line with the name '<custom-ident>-start'/'<custom-ident>-end', it contributes the first such line to the grid item’s placement.

Note: Named grid areas automatically generate implicit named lines of this form, so specifying grid-column: foo; will choose the start/end edge of that named grid area (unless another line named foo-start/foo-end was explicitly specified before it).

Otherwise, this is treated as if the integer 1 had been specified along with the <custom-ident>.

<integer> && <custom-ident>?
Contributes the nth grid line to the grid item’s placement. If a negative integer is given, it instead counts in reverse, starting from the end edge of the explicit grid.

If a name is given as a <custom-ident>, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines are assumed to have that name for the purpose of finding this position.

An <integer> value of 0 is invalid.

span && [ <integer> || <custom-ident> ]
Contributes a grid span to the grid item’s placement such that the corresponding edge of the grid item’s grid area is n lines from the opposite edge.

If a name is given as a <custom-ident>, only lines with that name are counted. If not enough lines with that name exist, all implicit grid lines on the side of the explicit grid corresponding to the search direction are assumed to have that name for the purpose of counting this span.

If the <integer> is omitted, it defaults to 1. Negative integers or 0 are invalid.

Formal syntax

<grid-line> [ / <grid-line> ]?

where
<grid-line> = auto | <custom-ident> | [ <integer> && <custom-ident>? ] | [ span && [ <integer> || <custom-ident> ] ]

Example

HTML Content

<div id="grid">
  <div id="item1"></div>
  <div id="item2"></div>
  <div id="item3"></div>
</div>

CSS Content

#grid {
  display: grid;
  height: 100px;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 100px;
}

#item1 {
  background-color: lime;
}

#item2 {
  background-color: yellow;
  grid-column: 2 / 4;
}

#item3 {
  background-color: blue;
  grid-column: span 2 / 7;
}

Polyfill

IE11 does not support automatic placement of grid items. All items end up in the first row/col of the grid unless explicitly annotated with -ms-grid-column and -ms-grid-row. One can use this JavaScript to enable automatic annotation (please see the source repository for updates):

// CSS Grid Annotator for IE 11
// Version: 0.2 by Tom Rothe
// URL: https://github.com/motine/css_grid_annotator
function cssGridAnnotate() {
  // check if we have IE11
  var agent = navigator.userAgent;
  var isIE11 = (agent.indexOf("Trident") >= 0) && (agent.indexOf("rv:11") >= 0);
  if (!isIE11) {
    return;
  }

  CSS_DISPLAY_GRID = "-ms-grid";
  CSS_TEMPLATE_COLS = "-ms-grid-columns";
  CSS_ROW = "-ms-grid-row";
  CSS_COL = "-ms-grid-column";

  function annotateAll(parentElement) {
    // we have to go through every single element to check the computed style
    // this is very performance heavy
    var elements = parentElement.querySelectorAll("*");
    for (var i = 0; i < elements.length; i++) {
      var elm = elements[i];
      if (isGridContainer(elm) && !containsAnnotations(elm)) { // we only check grid container, but we ignore the ones with pre-defined annotations
        annotateContainer(elm);
      }
    }
  }

  // it annotates the children with grid-column and grid-row, based on the grid-template-columns style attribute.
  function annotateContainer(container) {
    // determine columns
    var colCount = getTemplateColCount(container);
    if (!colCount) { return; }
    // annotate children
    var children = container.children;
    for (var i = 0, visibleIndex = 0; i < children.length; i++) { // i: which child do currently address?, visibleIndex: how many children were visible up until now? these two only differ if there are hidden elements
      var child = children[i];
      if (isHiddenElemeent(child)) { continue; }
      child.style[CSS_COL] = (visibleIndex % colCount) + 1;
      child.style[CSS_ROW] = Math.floor(visibleIndex / colCount) + 1;
      visibleIndex++;
    }
  }

  function handleInsert(ev) {
    if ((ev.target) && (ev.target.parentElement)) {
      annotateAll(ev.target.parentElement);
    }
  }

  function isGridContainer(elm) {
    var styles = window.getComputedStyle(elm);
    return styles.display === CSS_DISPLAY_GRID;
  }

  function isHiddenElemeent(elm) {
    return (elm.type === "hidden") || (window.getComputedStyle(elm).getPropertyValue("display") === "none");
  }

  // returns true if any of the direct children has CSS_COL or CSS_ROW in their computed style.
  function containsAnnotations(elm) {
    var children = elm.children;
    for (var i = 0; i < children.length; i++) {
      var child = children[i];
      var styles = window.getComputedStyle(child);
      if (styles.getPropertyValue(CSS_COL) != "1" || styles.getPropertyValue(CSS_ROW) != "1") { // IE will automatically determine that all elements are at (1, 1)
        return true;
      }
    }
    return false;
  }

  // returns the number of elements in a computed grid-template-columns attribute.
  // We do not need to consider functions such as repeat or minmax, because they are not supported by IE11 anyway (so either the autoprefixer resolves them or the style definition is broken for IE11 anyway).
  function getTemplateColCount(elm) {
    var styles = window.getComputedStyle(elm);
    var templateColumns = styles.getPropertyValue(CSS_TEMPLATE_COLS);
    if (!templateColumns) { return; }
    return templateColumns.split(" ").length;
  }

  annotateAll(document.body);
  window.addEventListener("DOMNodeInserted", handleInsert, false);
}

window.addEventListener("load", cssGridAnnotate);

 

Specifications

Specification Status Comment
CSS Grid Layout
The definition of 'grid-column' in that specification.
Candidate Recommendation Initial definition

Initial valueas each of the properties of the shorthand:
Applies togrid items and absolutely-positioned boxes whose containing block is a grid container
Inheritedno
Mediavisual
Computed valueas each of the properties of the shorthand:
Animation typediscrete
Canonical orderthe unique non-ambiguous order defined by the formal grammar

Browser compatibility

 

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
grid-columnChrome Full support 57
Full support 57
Full support 29
Disabled
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Edge Full support 16Firefox Full support 52
Full support 52
No support 40 — 59
Disabled
Disabled From version 40 until version 59 (exclusive): this feature is behind the layout.css.grid.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE No support NoOpera Full support 44
Full support 44
Full support 28
Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari Full support 10.1WebView Android Full support 57
Full support 57
Full support 4.4
Disabled
Disabled From version 4.4: this feature is behind the Enable experimental Web Platform features preference.
Chrome Android Full support 57
Full support 57
Full support 29
Disabled
Disabled From version 29: this feature is behind the Enable experimental Web Platform features preference. To change preferences in Chrome, visit chrome://flags.
Edge Mobile No support NoFirefox Android Full support 52
Full support 52
No support 40 — 59
Disabled
Disabled From version 40 until version 59 (exclusive): this feature is behind the layout.css.grid.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 43
Full support 43
Full support 28
Disabled
Disabled From version 28: this feature is behind the Enable experimental Web Platform features preference.
Safari iOS Full support 10.3Samsung Internet Android Full support 6.0

Legend

Full support  
Full support
No support  
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

 

See also

Autorzy i etykiety dokumentu

Ostatnia aktualizacja: motine,