Skip to content

Instantly share code, notes, and snippets.

@Nargonath
Last active March 18, 2020 17:46
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 Nargonath/9dae039cf03c8dce1203649e7728fad9 to your computer and use it in GitHub Desktop.
Save Nargonath/9dae039cf03c8dce1203649e7728fad9 to your computer and use it in GitHub Desktop.
Used to regroup knowledge about CSS language to use as a fallback when having memory loss CSS-wise.

Calculate selectors priority/specificity

The priority of a selector can be expressed as a list of 4 digits: 0,0,0,0.

Starting from the right:

  • the first digit is the number of elements and pseudo-elements in the selector.
  • The second is the number of classes, attributes and pseudo-classes.
  • The third is the number of ID elements.
  • The last one if there is inline style property.

As an example:

#myId.myClass {} // 0,1,1,0
h1.myClass {} // 0,0,1,1

The first one is hence more specific than the second one.

Dimensioning

For the property height and width to be taken into account the element has to be of display: block|inline-block. However when the element is in absolute positioning it doesn't need to be in a specific display.

Absolute positioning

When specifying top/left/right/bottom in %, it refers to the size of the relative referral.

Transform

Translate

When using translateX/translateY with %, it refers to the dimension of the element itself. You can use absolute positioning and translate both in % to center an element in relative container.

Font-size

EM

It is another unit to specify font-size. It depends on the context in which it is used thus influenced by parents font-size.

A good practice is to set font-size: 16px on <html> tag and then font-size: 62.5% on the <body> tag so that 1em = 10px.

The calculation you have to use to get the pixels you want is: target size / context size = em value. For example, we want to set a <p> at 14px which is inside a <div> having font-size: 2em considering the value on html and body tag as expressed above. The resulting calculation would be 14px / 20px = 0.7em.

Image

Images that are part of the content should be inlined as an HTML tag. Images that are part of the layout should be defined as background images.

Cropping

Overflow method

On the parent element of the image you add overflow: hidden and set a width and height on it. That way you are sure the images are all the same size but it will crop some of the images if the dimensions do not respect the images ratio.

Height/Width method

If you put either a width or height property on the image and set the other one to auto, the image will take into account the dimension and resize as to respect its ratio. You can pair that method with the previous one by setting the height of the image to the height of the container and adding width: auto. It will force the image to respect its ratio and have the same height as the other images.

Logo and text replacement

When you want to display a logo as a background image of <a> tag the link tag should not be empty therefore you can add the text inside and then use the text-ident: -999999px property to set if off the viewport.

Sprites

When you want to display a different image when an event is triggered on an image (hover for example) sprites are a good way to do it. A sprite is an image containing multiples image and then you change the one displayed based on the background-position property. The container has to be the same size as one of the image.

Benefits of sprites are:

  • Less HTTP requests to get the assets
  • No flashing when switching between images

Another way to optimize the images could be to use the base64 encoding directly in the CSS sheet.

Margin

Collapse

Margins collapse. If you have 2 subsequent elements, the first with a bottom 40px margin and the second with a top 20px margin the total margin will be 40px.

Margins collapse does not occur when one or more elements has:

  • Padding or border
  • Relative or absolute positioning
  • A float left or right

Inline vs Block/Inline-block

Inline elements take into account only the horizontal margin whereas block and inline-block respect both horizontal and vertical margin.

Vertical alignment

Inline / Inline-block element

You can v-align elements that are inline using the property :

vertical-align: middle; 

It has to be added on all inline elements you want to align.

Scroll

If you want to add scroll behavior to an element, it has to have a fixed height either max-height or height.

Overflow

If a container has a height set and its content overflows the container siblings and upper will not take into account the height of the content but only the height of container.

Animating stuff

There are 2 ways to animate stuff in CSS:

  • Transitions
  • Animations

The former is better suited when the movement has to be triggered (hover, click, adding class ...) and the latter is more of something that runs automatically (when the page loads for example).

Transitions

Characteristics

  • They don't loop.
  • Cannot define intermediate stages during the animation. They are designed to go from A to B and the value in-between are interpolated.
  • Interoperates well with JS.

How to

Use CSS transition property to declare a transition. It can have comma separated values to declare multiple transition. If you want to transition when the property changes due to user interaction and then reverts back (hover for example) then you need to put the transition on the element without the :hover state selector otherwise it will be applied only when the element gets into the hover state.

Animations

Characteristics

  • They can loop.
  • You can define as many intermediate stages (keyframe) of your animation. Interpolation will be used for the points between the stages you defined.
  • Does not interoperate well with JS. It is really complex to alter a keyframe.

How to

Define a @keyframes section where you describe the different stages of your animation. You need to name that keyframe. Bind that keyframe to a selection using the CSS animation property. You can use comma separated values to declare multiple animation i.e:

@keyframes my-animation { ... }
@keyframes my-second-animation { ... }

div {
animation: my-animation 2s .5s infinite linear alternate,
           my-second-animation 1s 2s infinite ease-in forward;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment