GithubHelp home page GithubHelp logo

css-protips's Introduction

CSS Protips

A collection of tips to help take your CSS skills pro.

Ty and Al from Caddyshack

Use :not() to Apply/Unapply Borders on Navigation

/* instead of putting on the border... */
.nav-tab {
  border-right: 1px solid #424242;
}

/* ...and then taking it off... */
.nav-tab:last-child {
  border-right: 0;
}

/* ...use :not() to only apply to the elements you want */
.nav-tab:not(:last-child) {
  border-right: 1px solid #424242;
}

It's clean, readable, and easy to understand without the need for hack-y code.

Vertically Center

html, body {
  margin: 0;
  height: 100%;
}

body {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-align-items: center;  
  -ms-flex-align: center;  
  align-items: center;
}

No, it's not dark magic, you really can center elements vertically. Look how simple this is.

Select Items Using Negative nth-child

li {
  display: none;
}
/* select items 1 through 3 and show them */
li:nth-child(-n+3) {
  display: block;
}

Use negative nth-child in CSS to select items 1 through n. Well that was pretty easy.

Use SVG...for Everything!

.logo {
  background: url("logo.svg");
}

There's no reason not to use SVG for icons. SVG been supported in all browsers since IE9, so start ditching your .png, .jpg, and .gif-jif-whatev files. Icon fonts, too, but that's a different protip.

Get Rid of Margin Hacks With Flexbox

.list-of-people {
  display: flex;
  justify-content: space-between;
  .person {
    flex-basis: 23%;
  }
}

You can get rid of nth-, first-, and last-child hacks when work with column gutters by using the space-between property in flexbox. Another flexbox win!

Support

These protips work in current versions of Chrome, Firefox, Safari, and Edge, and in IE11.

Carl from Caddyshack Carl became the Cinderalla story by using CSS protips.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.