Syntax Video
7 CSS Features You Have Not Heard Of
Intro
what's up CJ here in this video I'm going to talk about seven CSS features that you might not have heard of and might not be using but you should definitely check out now this was inspired by episode 784 of syntax Scott and West were answering viewer questions specifically one viewer asked about CSS hyphens and both Scott and Wes had never heard of it I hadn't heard of it either and then there's also overflow rap which uh Scott and West mentioned that they came across recently so we're going to talk about those two I'm going to show you some examples but I also have five others that I think are pretty interesting and you should check out as as well so this first one is hyphenation so let's say you have a container and really long words if you say hyphens Auto the browser will automatically insert hyphens where it sees fit to make sure that your text fits with within the container now sometimes you might want to explicitly specify where the browser should insert those hyphens and you can do that with ersan shy semicolon so this is known as a soft hyphen and basically the browser will only show it if it actually needs to break on that specific
hyphens
area so right here you see I have hyphens manual and this tells it to use these uh shy hyphens or these soft hyphens that I have specified here and you can see that it didn't insert the one in between super Cali but it did insert the one after Cali because it saw that it needed to actually insert that breakpoint so if you ever want to explicitly specify where it should break you can use these soft hyphens now the other thing to consider is breaking based on specific language now um in this example I have laying equals in on the paragraph tag here and that tells the browser that this text is in English so whenever we set hyphens to Auto it's going to break up those words based on the English language it's not going to just insert them in random places it actually has a dictionary that tells it the the right places to insert uh hyphens to still make sense so for example there's a dash after the ex and squirrel there's a dash after the squirr and incomprehensibilities it's inserting all of those in in a logical place and that's because it has a dictionary built in for it the browser also has
dictionaries for other languages as well so if you take this exact same text but written in German we set Lang equal to De which is Deutch deuta and uh if we set hyphens Auto now the browser is going to use that German dictionary to break things in the correct place so you can see that example bishel is broken up in the same way but these other words like aorin which is squirel was broken up into three instead of just two and unin I don't know um was broken up in more ways than incomprehensibilities so uh browsers actually have language dictionaries to know when to insert those hyphens so that works just by specifying the Lang attribute on the element itself uh now this has pretty wide support it's part of Baseline 2023 which are features that are heavily supported across all major browsers 97% support so you can definitely start using it today the other feature that Scott and West recently came across was overflow WAP break word so in this example we don't want to insert hyphens
overflow-wrap
whenever we have text that's overflowing because it's a URL and inserting hyph would make it like an invalid URL so if we say overflow wrap break word now the browser will break it accordingly without inserting hyphens but still fit it within the container so this has huge Support over 97% support you can use this pretty much anywhere now this next one has existed for a very long time but I find it interesting and even I just came across it recently and completely forgot that it existed but let's say you're a gossip columnist and you want to make your letters pop with the first letter pseudo selector this allows to Target the first letter of a given element so in this case we're targeting the first letter of every paragraph we're changing the font size and making it italic uh the other thing we're doing is setting the line height because without that it uses the line height of the font size but I want it to match the line height of the rest of the paragraph and so like I said this is existed for a very long time but I think it's pretty cool and it should definitely get more usage on like blogs and stuff like that 98% support you can use this in most places uh now this next one has to do
:first-letter
image-rendering: pixelated
with rendering images so say for example you're rendering out some pixel art and you can see by default they look a little bit blurry and that's because the browser is trying to smooth out the lines when it's rendering these images but I know that this should be pixel art so if you say image rendering pixelated we're telling the browser how to render the image and now you can see they pop a little bit more it's a little more crisp because it's actually rendering out those pixelated edges now there's a few other things that you can do like crisp edges pixelated there's also like high quality so basically you're just signaling to the browser how it should render the specific image uh this has 97% support you can use it across most web browsers the next one has existed for a very long time but I still come across people that have never heard of it so let's say you have a container and we have a bunch of images inside of it and so by default all of these images they have a width 100% so they're just in a single column the column count property will set the columns on the container boom We instantly get a
column-count
masonry like layout and this has nothing to do with CSS Grid it's not flexbox initially this actually was for like text on websites right cuz you might have text that you want to split across multiple columns and so this is literally the only property we need on the container to split it into three columns or four columns or 12 columns um and it's pretty slick because it it like I said it gives you that masonry like layout where all of the rows don't have equal height it kind of just renders them out in order and then spaces them accordingly now the one thing to note about this is the order is different than like CSS Grid in that it's still rendering from top to bottom so it's not left to right so this is actually down and then back up and then back down and you notice at the bottom there's a little bit of like open space too so it's not ideal for all situations but if you want a quick masonry like layout this is literally all you need you don't need to bring any JavaScript or anything like that the other thing you can do is like column span so here I'm saying select every sixth image and it should take up every column and now we have an interesting layout where every sixth
Every column is rendered top to bottom.
image is kind of like a full row so there's some there's some cool stuff you can do with that uh and there's other properties there's like a column Gap where you can specify spey how much space in between the columns and then there's also column rules so you can specify a line in between the columns and then also column width so if you don't want to specify how many columns you can specify width and the browser will automatically figure out how many columns to insert like I said this has existed for a long time it has almost 97% support you can use this pretty much anywhere this next one is aspect ratio this is a newer one it's part of Baseline 2023 uh but this is really great for things like skeleton loader so if we take that last example maybe we're dynamically loading those images and they're going to take a little while before they actually show up I want the parent image container to take up the right amount of space and we're doing that with aspect ratio and so right now I have these hardcoded but you can imagine like you're getting this data back from an API maybe you have width and height and you can specify that aspect ratio dynamically like with JavaScript or something like that but basically for every one of these
aspect-ratio
containers I have the aspect ratio set to the correct thing based on the actual size of the image that's going to be rendered out so now the container is always taking up the correct amount of space and then finally when those images load in there's no shift in layout everything takes up the exact same amount of space that the container was taking up so this is great for like responsive websites as well because I'm not specifying a fixed width and height on that parent container I can just specify the width and then the aspect ratio takes care of calculating the height accordingly um and so this one is also part of Baseline 2023 but you can do other things like if you do aspect ratio of one that's going to make it a square 169 is like horizontal video you could do 916 which is vertical video you can specify decimals like I was doing in my example so it's pretty awesome now this has 94% support still supported across all the major browsers but a little bit less support than the others now this last one I want to show you is scroll snapping so this is really great if you're implementing like carousels or like slides on a website so you can see here that I have these div slides and they've been styled to take up the whole
scroll-snap
page and if I scroll you can see that I'm I'm coming across all of the slides but if we want presentation like functionality where if I scroll or I use the arrow keys it jumps to the next one we can use scroll snap so the first thing on the scroll container I need to set the scroll snap type in this case I'm setting it to Y mandatory because my scroll is in the in the y direction if you're implementing like a horizontal Carousel you would use x and then for the elements that I want to snap I can set scroll snap a line and for these slides I'm setting scroll snap a line to start and you can see the moment I enabled it it kind of jumped up and so now when I scroll it instantly snaps to the top of that slide just like that and another thing you typically would combine this with is scroll Behavior so if I set scroll Behavior smooth now when I scroll between the slides it's a little bit smoother it doesn't just instantly jump to the next slide this one's pretty slick definitely use it in your apps and and remove some of that JavaScript that was doing that stuff before now like I said this is newer so it only has about 94% support but still
supported across all the major web browsers now that's all the features I have for you but if you want to have your questions answered by Scott or Wes or myself definitely go over to syntax. FM click on on potluck you can ask your question and it might get answered in a future episode now I also want to mention that syntax is brought to you by centry they're an error monitoring platform if you're running code in production and you're not using centry you're Flying Blind and so centry lets you keep track of all of your errors and it's really great so if you want to try it out go to sentry.io syntax use cod tasty treat for two months free all right that's all I got if you have CSS properties that you use that you think more people should know about let us know down in the comments and then also let us know if some of the ones that I showed you was the first time seeing them and if you're going to use them in your apps all right I'll see you in the next one
Ask your questions!
Brought to you by Sentry
Thanks!
Quote it with credit and a link. Quoting Syntax