I work with HTML & CSS daily, creating and amending websites. CSS gets a lot of hate from developers who don’t understand it, although imo it’s a very decent solution. However there are some things that catch me out & I’ve written down some solution here for future reference. I’ll probably add to this list over time. Continue reading CSS tricks that I keep having to remind myself about
Tag: CSS
Added interactivity to my SVG animation
I’ve been doing some more simple experiments with SVG animation using GSAP, and amended my chicken-in-a-fokker scene on Codepen that I wrote about in the last article. Continue reading Added interactivity to my SVG animation
Creating animation with SVGs, CSS animations, spritesheets & GSAP
I’ve created an animation using a few different methods that runs in a browser and is responsive (works on both mobiles, tablets and desktop). Continue reading Creating animation with SVGs, CSS animations, spritesheets & GSAP
Forcing a div to retain aspect ratio, but also expand if there’s more content
I’ve just been stuck on a problem for ages, and that’s to keep at a specific aspect ratio (on mobile, tablet and desktop), but also to allow it to expand if there’s more content inside it. Continue reading Forcing a div to retain aspect ratio, but also expand if there’s more content
Combining media queries in Foundation
Edit 2017: This article was originally written for Foundation 5, but I’ve updated it for the new way Foundation 6 uses CSS @includes for media queries.
I really like the way Foundation 6 makes media queries easy by putting them into variables, so that I can easily put breakpoint into a CSS class. Like this:
.my-class { font-size: 1rem; @include breakpoint(medium up) { font-size: 2rem; } }
I’ve written before about the way I write media queries (see here), and I think this is the best way to avoid hunting through documents to find a selector that might be in a separate breakpoint somewhere.
However I sometimes want to combine multiple breakpoints to save duplication. For a long time I thought this wasn’t possible, but you actually can by writing it like this:
@media screen and #{breakpoint(small only)} and #{breakpoint(large up)} { }
I find this useful with height breakpoints
Sometimes I need to use a media query that detects the height, to make sure we’re not using elements that are too large.
I’ve made my own height media queries as variables:
$short-only: "(max-height:600px)" !default; $tall-up: "(min-height:601px)" !default;
I use “short” and “tall” for height like you’d use “medium” & “large” for width (maybe Foundation should change their variable names to “narrow” and “wide” instead?).
So I can do this sort of thing to change heading menu font size if the screen is really wide, and definitely tall too.
@media screen and #{breakpoint(large)} and #{$tall-up} { font-size: rem-calc(36px); }
I admire this guy for not using a preprocessor, but I just couldn’t
I read this article today about someone who doesn’t like using preprocessors (like LESS or SASS) to compile CSS. It’s worth a read, here it is:
http://www.456bereastreet.com/archive/201603/why_i_dont_use_css_preprocessors/
I put off learning SASS for a long time, because I find it fiddly. You have to install something, and often have to run commands in a command prompt (something I hate), running gulp or grunt or whatever. But I was converted when I realised PHPStorm did it for you – which is AWESOME.
But now that I do, I feel like I can write styles so much clearer and more effectively, without getting lost in a massive page of WET CSS code. I use the word WET as the opposite of DRY (don’t repeat yourself), as without compilation your CSS can end up with lots of repetition. Especially if you need to target things inside a specific class, you might end up with this:
.main-content p, .main-content ul {} .main-content h1 {} .main-content h2 {}
Look how many times you have to type .main-content! Whereas in SASS you could write:
.main-content { p,ul {} h1 {} h2 {} }
Isn’t that cleaner?
There’s also loads of advantages – being able to split code into useful components you can paste between projects, rather than one big mass of CSS. Also, keeping colours as variables is amazing. I’ve no idea how I managed before that!
Here’s more articles about the way I like to write styles:
- How I write CSS
- Keeping CSS classes together by usage
- Don’t scrimp on classes if it means making convoluted CSS
Here’s an article to read about preprocessors: Why your reasons for no-longer using a CSS pre-processor are wrong, and you’re wrong, and you should feel bad.
4 examples of CSS being weird
I know many people find CSS quite difficult, and often I see this gif shared as an example of how people work with CSS:
But if you’re doing CSS like this, YOU’RE DOING IT WRONG! CSS isn’t that difficult, but sometimes the chosen syntax does make me WTF. Here’s some examples of that:
Comma inconsistencies
Sometimes you need a comma to differentiate multiple values, like:
transition: color 200ms, background 200s;
But sometimes not:
transform: translateX(-50%) translateY(-50%);
Why isn’t this consistent?!
Slashes in shorthand
Ever seen this?
font: 12px/18px "Lucida Grande", sans-serif;
The first 2 values are shorthand for font-size and line-height. Why they’re separated with a slash though seems unusual as I’ve not seen that used anywhere else.
Apart from you can apparently use it in border-radius (here’s an article explaining that) to separate horizontal and vertical axis, but again – that’s inconsistent to how we do it elsewhere!
Camel Case or hyphen?
CSS has always been hyphenated, for example background-color, text-transform etc, so why did we start adding camelcase like translateY, rotateX? It’s inconsistent!
Edit – Also, overflow-y and translateY – Y so different?
Vertical Percentages are Relative to Container Width, Not Height
I didn’t actually realise this for a long time. It can be really useful once you know what it’s doing. Here’s an article about it. But why can’t I also set a height based on a percentage of the container’s height?
How I write CSS (March 2016)
The way I write CSS changes, so I thought I’d record how I’m doing it right now so I can refer to it later, or possibly look back and laugh. Continue reading How I write CSS (March 2016)
Overriding a pesky parent selector with ninja specificity
I try and keep my CSS/SCSS in a fairly flat structure, and recently on a project I’ve been bugged by a parent selector which has been adding quite specific styles that have been getting in the way of my own.
The class is called .copy and has colour styles for <p>, <h2> and for <a>. So if I try to do something specific like give a result title a different colour, it gets overridden by the .copy h2 style.
I could of course make all my styles more specific, but this means adding in another class wrap all my SASS in body or an arbitary class further up the structure, but that’d annoy me because it’d be inserted into the css loads of times unnecessarily.
So I’ve taken to adding in extra selectors to provide more specificity, like this:
.result { &__title a, .copy & &__title a { }
Using the ampersand at the end of the line which in SASS will insert the parent (my new favourite thing) means that I’m covered for if the .copy class overrides my style, but without putting this style somewhere weird that I can’t find later.
It means I’ve added a bit of extra specificity which leaps in like a ninja when I need it, and I don’t have to do it to the entire section.
I’ve discovered the ampersand selector in SASS & now I’m using it everywhere!
Once I discovered you could use an ampersand selector in SASS to reference the parent, I haven’t gone back. I’m using it everywhere because it’s so useful. Here’s an example copied from an article – the article explains it better than I ever could.
h3
font-size: 20px
margin-bottom: 10px
.some-parent-selector &
font-size: 24px
margin-bottom: 20px
And here’s how the output CSS looks.
h3 {
font-size: 20px;
margin-bottom: 10px;
}
.some-parent-selector h3 {
font-size: 24px;
margin-bottom: 20px;
}
This is very useful! Especially when you want to keep css together by usage (I wrote about that here) so that you aren’t looking in multiple SASS files for information about every modification of a style.
Here’s how I used it today – I’ve got a class called “.copy” which is used for the content section of a page. There’s useful things in there like how the links and list items should look, because they’re a bit different to in the chrome (header & footer). But I wanted to create an aside section with different coloured text. Problem was, .aside p is the same specificity as .copy p, and I don’t want to start adding !important which could cause problems later on.
So I wanted to increase the specificity, but I didn’t want to add these styles for the aside inside the copy.scss – because that’s detached from the rest of the styles (which are in aside.scss). So I did this in my aside.scss:
.aside { .copy & p { color: #484848; } }
Notice it’s between the .copy and the p. It outputs like this:
.copy .vacancy-aside p { color: #484848; }