imgarylai /
awesome-webservers
⚙️ Collection of one-liner static server
Loading repository data…
AllThingsSmitty / repository
⚡️ A collection of tips to help take your CSS skills pro 🦾
A collection of tips to help take your CSS skills pro.
[!TIP] For other great lists check out @sindresorhus's curated list of awesome lists.
box-sizingunset Instead of Resetting All Properties:not() to Apply/Unapply Borders on Navigationline-height to body:focus for Form Elementsaspect-ratio Instead of Height/Widthnth-childmax-height for Pure CSS Sliders:is()rem for Global Sizing; Use em for Local Sizing:root for Flexible Typefont-size on Form Elements for a Better Mobile Experiencedisplay: none on Line Breaks Used as Spacing:empty to Hide Empty HTML Elementsmargin-inline instead of marginCSS resets help enforce style consistency across different browsers with a clean slate for styling elements. There are plenty of reset patterns to find, or you can use a more simplified reset approach:
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
Now elements will be stripped of margins and padding, and box-sizing lets you manage layouts with the CSS box model.
[!TIP] If you follow the Inherit
box-sizingtip below you might opt to not include thebox-sizingproperty in your CSS reset.
box-sizingLet box-sizing be inherited from html:
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
This makes it easier to change box-sizing in plugins or other components that leverage other behavior.
unset Instead of Resetting All PropertiesWhen resetting an element's properties, it's not necessary to reset each individual property:
button {
background: none;
border: none;
color: inherit;
font: inherit;
outline: none;
padding: 0;
}
You can specify all of an element's properties using the all shorthand. Setting the value to unset changes an element's properties to their initial values:
button {
all: unset;
}
:not() to Apply/Unapply Borders on NavigationInstead of putting on the border...
/* add border */
.nav li {
border-right: 1px solid #666;
}
...and then taking it off the last element...
/* remove border */
.nav li:last-child {
border-right: none;
}
...use the :not() pseudo-class to only apply to the elements you want:
.nav li:not(:last-child) {
border-right: 1px solid #666;
}
Here, the CSS selector is read as a human would describe it.
You can check if a font is installed locally before fetching it remotely, which is a good performance tip, too.
@font-face {
font-family: "Dank Mono";
src:
/* Full name */ local("Dank Mono"), /* Postscript name */ local("Dank Mono"),
/* Otherwise, download it! */ url("//...a.server/fonts/DankMono.woff");
}
code {
font-family: "Dank Mono", system-ui-monospace;
}
H/T to Adam Argyle for sharing this protip and demo.
line-height to bodyYou don't need to add line-height to each <p>, <h*>, et al. separately. Instead, add it to body:
body {
line-height: 1.5;
}
This way textual elements can inherit from body easily.
:focus for Form ElementsSighted keyboard users rely on focus to determine where keyboard events go in the page. Make focus for form elements stand out and consistent than a browser's default implementation:
a:focus,
button:focus,
input:focus,
select:focus,
textarea:focus {
box-shadow: none;
outline: #000 dotted 2px;
outline-offset: 0.05em;
}
No, it's not black magic, you really can center elements vertically. You can do this with flexbox...
html,
body {
height: 100%;
}
body {
align-items: center;
display: flex;
justify-content: center;
}
...and also with CSS Grid:
body {
display: grid;
height: 100vh;
place-items: center;
}
[!TIP] Want to center something else? Vertically, horizontally...anything, anytime, anywhere? CSS-Tricks has a nice write-up on doing all of that.
aspect-ratio Instead of Height/WidthThe aspect-ratio property allows you to easily size elements and maintain consistent width-to-height ratio. This is incredibly useful in responsive web design to prevent layout shift. Use object-fit with it to prevent disrupting the layout if the height/width values of images changes.
img {
aspect-ratio: 16 / 9; /* width / height */
object-fit: cover;
}
Learn more about the aspect-ratio property in this web.dev post.
Make list items look like a real, comma-separated list:
ul > li:not(:last-child)::after {
content: ",";
}
Use the :not() pseudo-class and no comma will be added to the last item.
[!NOTE] This tip may not be ideal for accessibility, specifically screen readers. And copy/paste from the browser doesn't work with CSS-generated content. Proceed with caution.
nth-childUse negative nth-child in CSS to select items 1 through n.
li {
display: none;
}
/* select items 1 through 3 and display them */
li:nth-child(-n + 3) {
display: block;
}
Or, since you've already learned a little about using :not(), try:
/* select all items except the first 3 and display them */
li:not(:nth-child(-n + 3)) {
display: block;
}
There's no reason not to use SVG for icons:
.logo {
background: url("logo.svg");
}
SVG scales well for all resolution types and is supported in all browsers back to IE9. Ditch your .png, .jpg, or .gif-jif-whatev files.
[!NOTE] If you have SVG icon-only buttons for sighted users and the SVG fails to load, this will help maintain accessibility:
.no-svg .icon-only::after {
content: attr(aria-label);
}
It may have a strange name but using the universal selector (*) with the adjacent sibling selector (+) can provide a powerful CSS capability:
* + * {
margin-top: 1.5em;
}
In this example, all elements in the flow of the document that follow other elements will receive margin-top: 1.5em.
[!TIP] For more on the "lobotomized owl" selector, read Heydon Pickering's post on A List Apart.
max-height for Pure CSS SlidersImplement CSS-only sliders using max-height with overflow hidden:
.slider {
max-height: 200px;
overflow-y: hidden;
width: 300px;
}
.slider:hover {
max-height: 600px;
overflow-y: scroll;
}
The element expands to the max-height value on hover and the slider displays as a result of the overflow.
Tables can be a pain to work with. Try using table-layout: fixed to keep cells at equal width:
.calendar {
table-layout: fixed;
}
Pain-free table layouts.
When working with column gutters you can get rid of nth-, first-, and last-child hacks by using flexbox's space-between property:
.list {
display: flex;
justify-content: space-between;
}
.list .person {
flex-basis: 23%;
}
Now column gutters always appear evenly-spaced.
Display links when the <a> element has no text value but the href attribute has a link:
a[href^="http"]:empty::before {
content: attr(href);
}
That's really convenient.
[!NOTE] This tip may not be ideal for accessibility, specifically screen readers. And copy/paste from the browser doesn't work with CSS-generated content. Proceed with caution.
:is()The :is() pseudo-class is used to target multiple selectors at once, reducing redundancy and enhancing code readability. This is incredibly useful for writing large selectors in a more compact form.
:is(section, article, aside, nav) :is(h1, h2, h3, h4, h5, h6) {
color: green;
}
The above ruleset is equivalent to the following number selector rules...
section h1,
section h2,
section h3,
section h4,
section h5,
section h6,
article h1,
article h2,
article h3,
article h4,
article h5,
article h6,
aside h1,
aside h2,
aside h3,
aside h4,
Selected from shared topics, language and repository description—not editorial ratings.
imgarylai /
⚙️ Collection of one-liner static server
iamchathu /
⚡️ A collection of awesome things regarding JSON Web Tokens. Feel free to contribute!
iamchathu /
⚡️ A collection of awesome things regarding Reaction Commerce. Feel free to contribute!
Nickersoft /
A collection of privacy-first, offline alternatives to web-based software ⬇️
nekolr /
❤️ Collections
iamchathu /
⚡️A collection of awesome things regarding Vuforia Augmented Reality SDK. Feel free to contribute!