Skip to content

Instantly share code, notes, and snippets.

@skullface
Last active March 10, 2019 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skullface/3be7a74963c74eb5587a47d2cd73545b to your computer and use it in GitHub Desktop.
Save skullface/3be7a74963c74eb5587a47d2cd73545b to your computer and use it in GitHub Desktop.
20190308 Rustbelt Refresh: CSS layout + alignment notes

Here’s the non-Grid stuff I learned at Rustbelt Refresh 2019: “Next Steps with CSS Layout” with Rachel Andrew~! You can also check out my notes for Grid stuff.


⚡️ Cool layout declarations

display: flow-root

Clearfix begone!!! 📒 W3C Spec · CSS Tricks · MDN · Can I Use?

From the draft spec: The element generates a block container box, and lays out its contents using flow layout. It always establishes a new block formatting context for its contents.

display: contents

Removes the excessive default styling (i.e. removes the box of the element — literally “box generation”) but keeps the accessible context of the element. Great for forms, for example!

The element itself does not generate any boxes, but its children and pseudo-elements still generate boxes and text runs as normal. For the purposes of box generation and layout, the element must be treated as if it had been replaced in the element tree by its contents (including both its source-document children and its pseudo-elements, such as ::before and ::after pseudo-elements, which are generated before/after the element’s children as normal).

Note: As only the box tree is affected, any semantics based on the document tree, such as selector-matching, event handling, and property inheritance, are not affected. 📒 W3C Spec · CSS Tricks · MDN · Can I Use?

column-span: all

Makes an element actually span across columns in a multicol layout 😱 You don’t have to move it out of the element with column styling! 📒 W3C Spec · CSS Tricks · MDN · Can I Use?

break-inside: avoid

Avoid an element within a multi-column layout to keep the property from breaking apart (“wrapping” to an additional column when using card styling). 📒 W3C Spec · CSS Tricks · MDN · Can I Use?

Indicate that content should be kept together. Avoid a break before/after the principal box (block-level element).

✏️ Codepen using all of the above declarations in a multicolumn layout

💪 Alignment + Flexbox

Some flex-y properties aren’t just for Flexbox like I thought they were! Flexbox styling is great for fiddling and fine-tuning layout elements, but isn’t the best option to layout content in a grid system (guess what is… Grid!!!).

justify-content

When we’re justifying content on the inline ("main") axis, we use justify-content! (No need for the old flex- prefix in the value, e.g. justify-content: start will do!)

align-

Use this when you need to align elements on the block ("cross") axis, use align-content or align-items! Just like justify-content 👆 but on the opposite axis. Use the align-self property to override this for one child element.

flex shorthand

/* 
Longhand for below:
  flex-grow: 0;
  flex-shrink: 1;
  flex-basis: auto;
*/

flex: 1 1 0;

flex-grow: 1 means that all items can grow to fill the leftover space, much like the fr Grid unit.

If you want to have equal-space items within a flexbox container (so the flex children widths have nothing to do with the content in them), set flex-basis to 0 (a.k.a. distribute the leftover space to ALL of the space).

When you use only one #{value} for flex, it defaults to:

flex-grow: #{value};
flex-shrink: 0;
flex-basis: 0;

writing-mode: vertical-rl

Instantly change your layout to read right-left on a 90º tilt! But… if you use width/height and top/right/bottom/left then it’s pointless so you hafta use logical properties (below!)

🧠 Logical properties

inline and block

Logical replacements for width and height: inline-size and block-size. They define the horizontal or vertical size of an element's block, depending on the value of writing-mode. 📒 MDN: inline-size · MDN: inline-block

Use logical properties block-start, block-end, inline-start, inline-end instead of physical properties top, right, bottom, left to avoid mapping values directly to visual "sides" as opposed to the bounds of the content.

✏️ Codepen replacing width, border, margin, padding with logical properties

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