Skip to content

Instantly share code, notes, and snippets.

@smuemd
Forked from cesarandreu/sensible-defaults.css
Created June 20, 2018 09:10
Show Gist options
  • Save smuemd/0a35634cd5e6563354bdca2c0989be06 to your computer and use it in GitHub Desktop.
Save smuemd/0a35634cd5e6563354bdca2c0989be06 to your computer and use it in GitHub Desktop.
Sensible css defaults taken from css-layout
div, span {
box-sizing: border-box;
position: relative;
display: flex;
flex-direction: column;
align-items: stretch;
flex-shrink: 0;
align-content: flex-start;
border: 0 solid black;
margin: 0;
padding: 0;
}
@smuemd
Copy link
Author

smuemd commented Jun 20, 2018

Taken from css-layout README:

Goals

CSSLayout is a cross-platform implementation of flexbox. The goal of CSSLayout is allow native developers to have the same expressive layout system as developers developing for the modern web are used to. CSSLayout allows developers for web, android, iOS, and windows to use the same layout primitives across platforms. This saves time, increases collaboration between platform teams, and makes it easier for developers to work on multiple platforms.

The goal of CSSLayout is not to re-implement all of css. CSSLayout only targets flexbox, and does not have any plans on implementing support for tables, floats, or any other css concepts. CSSLayout also does not plan on supporting styling properties which do not affect layout such as color or background properties.

Differences from web

CSSLayout tries to stay as close as possible to the web implementation of flexbox. There are however certain cases where CSSLayout differs from the web implementation.

Default values

CSSLayout has chosen to make changes to the default values of certain properties. These default values were chosen based on our usage of the library. When testing layout with tools such as JSFiddle you can apply the following css style to ensure the defaults match those of CSSLayout. Or fork the following JSFiddle.

div, span {
  box-sizing: border-box;
  position: relative;

  display: flex;
  flex-direction: column;
  align-items: stretch;
  flex-shrink: 0;
  align-content: flex-start;

  border: 0 solid black;
  margin: 0;
  padding: 0;
  min-width: 0;
}
  • box-sizing: border-box is the most convenient way to express the relation between width and borderWidth.
  • Everything is display: flex by default. All the behaviors of block and inline-block can be expressed in term of flex but not the opposite.
  • All the flex elements are oriented from top to bottom, left to right and do not shrink. This is how things are laid out using the default CSS settings and what you'd expect.
  • Everything is position: relative. This makes position: absolute target the direct parent and not some parent which is either relative or absolute. If you want to position an element relative to something else, you should move it in the DOM instead of relying of CSS. It also makes top, left, right, bottom do something when not specifying position: absolute.

@smuemd
Copy link
Author

smuemd commented Jun 20, 2018

via http://jsfiddle.net/vjeux/y11txxv9/

body {
  padding: 0;
  margin: 0;
  font-family: Helvetica;
  font-size: 14px;
  font-weight: 100;
}

div, span {
  box-sizing: border-box;
  position: relative;

  display: flex;
  flex-direction: column;
  align-items: stretch;
  flex-shrink: 0;
  justify-content: flex-start;

  border: 0 solid black;
  margin: 0;
  padding: 0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment