calc() lets you do some real CSS magic

  Рет қаралды 180,030

Kevin Powell

Kevin Powell

Күн бұрын

Пікірлер: 216
@biomcanx1
@biomcanx1 5 жыл бұрын
Awesome thanks! The only thing I would add is how important the spaces around the ' + ' are in the calc. It will not perform a calculation if they are not there. Everyone else may know this already, but it bit me for a little bit trying to figure it out.
@KevinPowell
@KevinPowell 5 жыл бұрын
Good point Gene, I probably should have talked a bit more about the very basics of it right off the top before diving in deeper.
@franktielemans6624
@franktielemans6624 5 жыл бұрын
@Gene Price that's right, you can't also have a space between the calc function and first opening bracket. @@KevinPowell Did you ever experiment with calc and CSS variables? I tried to and it seems that you can define variables to a calculation but you can't calc with the variables. At least I don't know how.
@kolklik
@kolklik 5 жыл бұрын
@@franktielemans6624 Sure you can calc with variables/custom properties. jsfiddle.net/aLs8zv5h/
@ynnoz4452
@ynnoz4452 5 жыл бұрын
My favorite calc() trick is this one : html{ margin-left: calc(100vw - 100%); } It removes the "windows jumping" when a scrollbar on the right pops in or out. Perfect for variable/interactive layouts.
@kuranam
@kuranam 3 жыл бұрын
How could I use Calc, to preserve the height and width of a image. I am making an ePub, and the picture pass to another Page and it show clipped.
@pineapplerindm
@pineapplerindm 10 ай бұрын
@@kuranam 2 years late but you don't need calc - you can use `aspect-ratio` (or `object-fit: contain`, though that won't change the height/width, it'll only keep the image undistorted)
@thehigherman9918
@thehigherman9918 5 жыл бұрын
The video is about calc(), But I also learned about viewport and vh and vw. This really saved a website I had to restore.
@YungSchmutz
@YungSchmutz 5 жыл бұрын
Samesies
@badihbarakat5832
@badihbarakat5832 5 жыл бұрын
I learned a couple of new css attributes I didn't know about. I didn't even know I can hide the scroll bars without affecting the flow of the contents...
@mr.webdev3700
@mr.webdev3700 5 жыл бұрын
The calc() function is really awesome indeed! Great tutorial Kevin! 👍👍👍👍👍👍👍👍👍👍
@jenstornell
@jenstornell 5 жыл бұрын
That's an awesome trick. I tested it today and even made it fluid. It may look hackish but it works as expected now. `--wrap-padding: calc( 32px + (128 - 32) * (100vw - (var(--min-width) * 1px)) / (var(--max-width) - var(--min-width)) ); width: calc(100vw - var(--wrap-padding) - 17px); max-width: 1200px; margin: 0 auto; @media screen and (max-width: 320px) { width: calc(100vw - 2rem - 17px); } @media screen and (min-width: 1200px) { width: calc(100vw - 8rem); }`
@milleniummoses
@milleniummoses 5 жыл бұрын
Not only do I have a better understanding of how calc works, but you also answered my (unasked) question about making headline text responsive without so media queries.
@boombaby1769
@boombaby1769 5 жыл бұрын
Awesome! I've been working with CSS for quite a while now, but this feature was completely off my radar. This will be VERY useful for me, thank you!
@crooker2
@crooker2 5 жыл бұрын
That breakout div is great for creating dynamic content. I build cms sites for clients, but the content they enter is always contained in a bootstrap container. This gives me the option of allowing clients to build their own wide block WITHOUT having to break out of the container and re-instating it. Great tip! Thanks, Kev!
@MoeBass
@MoeBass 5 жыл бұрын
I was introduced to object-fit with this video, and the break out of container technique was clever!
@GnomePuntTrainerYT
@GnomePuntTrainerYT 5 жыл бұрын
The break out container technique is mindblowing to me. I won't have to wrap a div or section or something around the text that I'd like to give a background for.
@120tonns
@120tonns 5 жыл бұрын
developer.mozilla.org/en-US/docs/Web/CSS/object-fit
@violet-trash
@violet-trash 2 жыл бұрын
Object-fix is amazing, I can't believe I spent so long making divs with image backgrounds like a sucker.
@bruhcsp
@bruhcsp 5 жыл бұрын
Perfect!!! I loved what you did with the font-size!
@busyrand
@busyrand 5 жыл бұрын
Very nice tutorial. First time I've ever seen a clear practical use for the calc function.
@novachi09
@novachi09 5 жыл бұрын
This is sooo helpful, honestly. I didn't even look specifically for this but I'm glad it poped up in my recommended section
@techscoville
@techscoville 4 жыл бұрын
Really awesome, I remember trying the calc( ) function, it was tricky, but it now clear and simple
@moy2010
@moy2010 5 жыл бұрын
To prevent the horizontal scrolling issue, I do the following with css calc: calc(100% + calc(100vw - 100%))
@KevinPowell
@KevinPowell 5 жыл бұрын
Hrm, never would have thought of that, cool :)
@stepanostapuk4120
@stepanostapuk4120 5 жыл бұрын
Calc in Calc bad work for IE and Safari
@radoslawzielinski9016
@radoslawzielinski9016 4 жыл бұрын
which rule do you apply that to please?
@franktielemans6624
@franktielemans6624 4 жыл бұрын
@@radoslawzielinski9016 to the html element or body element i guess
@AdolfoBarreto75
@AdolfoBarreto75 5 жыл бұрын
Yes! I've been waiting for this one. THANK YOU! Haven't watched it yet but I know it will be good!
@jmerino06
@jmerino06 4 жыл бұрын
I have been coding websites for almost 20 years. watching your videos reminds me i have a lot to learn. thank you for the time, skills and know how you share on every video.
@PeggyOConnor1946
@PeggyOConnor1946 5 жыл бұрын
You are a talented master teacher. Kudos. This tip was useful to me immediately.
@sonydroid2253
@sonydroid2253 4 жыл бұрын
Kevin, I want to take this time and opportunity to say a genuine heartfelt thank. Sharing your passion on KZbin is helping me understand how the modern web is built. My sites are responsive with a modern stylish feel now. I even have confidence to secure paid work now. X
@JohnMcCormack
@JohnMcCormack 5 жыл бұрын
Really nicely explained, Kevin. Thank you!
@danielelgressy
@danielelgressy 4 жыл бұрын
very calm and accurate explanation :) really like your videos , short ,and professional
@Tryanide
@Tryanide 5 жыл бұрын
Calc is great in many places, but sometimes there's simpler solutions. In the case of the big-image, instead of using a "complicated" calc(-50vw + 50%) (which is, arguably, difficult to understand, especially for newcomers), simply use margin: 0 -3em instead. This will also pull the image 3em to the left and 3em to the right (the combined 6em you subtract from your container). One thing to keep in mind when using techniques like these or when working with EM units in general: local font-size changes on an element will override the EM size inherited from parents. If possible, try to make your lives easier by using REM whenever you can.
@laht93
@laht93 5 жыл бұрын
Using calc(-50vw + 50%) does have the advantage of working within any parent container, no matter the width. You could make it a class to put on things, (almost) regardless of context.
@erezspeiser4976
@erezspeiser4976 4 жыл бұрын
Awesome video. I stumbled upon it by accident and it solved a problem I had for a long time and thought it could not be solved. Thanks! Great channel!
@briankgarland
@briankgarland 3 жыл бұрын
I been looking everywhere for these very examples. Thanks!
@MisterMajister
@MisterMajister 5 жыл бұрын
THANK YOU! Just made my image gallery so much easier to handle. Now I can simply mix padding in px's to my img-width in percentage: width:calc(50% - 12px); since I have a 6px padding on each side. Looks exactly as I want it to!
@leogarza5022
@leogarza5022 5 жыл бұрын
This was very informative! Thank you for sharing!
@jovaniedelacruz
@jovaniedelacruz 5 жыл бұрын
Subscribed! That vw and break out container image is super cool
@Griesinho
@Griesinho 5 жыл бұрын
Thx so much for this tutorial... and your other ones as well 🤘
@philmyglass877
@philmyglass877 5 жыл бұрын
This is brilliant, another tool in the belt. Thanks so much!
@badihbarakat5832
@badihbarakat5832 5 жыл бұрын
Ur the best, Kevin... I've learnt a long more from this video than just the calc()... Many thanks... 😁
@jamesrosemary2932
@jamesrosemary2932 5 жыл бұрын
How could I have been living my life without this?
@drewbird87
@drewbird87 5 жыл бұрын
Thank you for giving me something new to love!
@Kozi03
@Kozi03 Жыл бұрын
Life changing for images and headings!
@brunoliveiralcantara
@brunoliveiralcantara 5 жыл бұрын
Really good function. Congratulations on your video!
@soultouchingsongs
@soultouchingsongs 5 жыл бұрын
Awesome video.. that container close and open was a bit too much.. now got a solution with this video. Thanks a million!
@mohan9285
@mohan9285 4 жыл бұрын
Awesome :) I normally use calc() for calculating the height of a container/div and give overflow-y: auto for getting scroll. It helps me in calculating dynamically according to the other content on the page. In your video, I liked the way how we can get the section out from container and styling those. pretty cool.
@sashikantanayak6354
@sashikantanayak6354 3 жыл бұрын
Very good Kevin, Thank you!
@5tevend
@5tevend 3 жыл бұрын
I work for an event app / event website company and we have a left-sidenav that displays on desktop by default with a width of 320px, that can collapse to being around 60px wide. sometimes i need to create a square container so i often find myself using calc functions, or wrapping calcs for max-widths, i had a client the other week who didn't like how when the left-sidenav was collapsed things flexed and stretched, so another useful trick there was width:calc(100vw - 320px); and then wrap that calc function in another calc such as width:calc( calc(100vw - 320px) / 2); so if i have a container with specific info/ a cta it doesn't flex at any point, i can then set it's max width as well so i have this fixed point for larger screen sizes, which if you want to save time could also be made into a css-variable for easier use so you can have --widget-width:calc(100vw - 320px); and use it in the realms of width:calc(--widget-width / 2) or 3 or 4 or etc; and a max width of lets say 1200px, although in this example this is only really used in a media query for min-width tablet and onwards i've found some use to it, especially if you need to make scalable square pieces, then you can do @media(orientation:landscape) { div { width:calc(100vh - 2em); } } and for portrait width:calc(100vw - 2em);
@RockstahRolln
@RockstahRolln 5 жыл бұрын
This is Terrific!! Thank You!
@sshuddho
@sshuddho 3 жыл бұрын
Tried to use vw as font size, but it was horrible on small screen. This "+ rem" will really help. Thanks Kevin!
@nonchalant8473
@nonchalant8473 5 жыл бұрын
Great tutorial. Thanks!
@noelabey
@noelabey 5 жыл бұрын
5:00 forward the fun starts. I subscribed and hit that bell
@fluntimes
@fluntimes 3 жыл бұрын
I can definitely see myself using that full width thing. The alternative is to make a whole bunch of divs that are full width with an inner to constrain content when necessary. Calc() rocks!
@Valky1954
@Valky1954 5 жыл бұрын
Thanks for this, learned something new :)
@Brandonstiles
@Brandonstiles 5 жыл бұрын
Great video! I learned a lot!
@OutOfNameIdeas2
@OutOfNameIdeas2 5 жыл бұрын
Shared and liked! Thank you for sharing this!!!
@mkmalikcom
@mkmalikcom 5 жыл бұрын
wow.... loved it you are great
@Zefrem23
@Zefrem23 5 жыл бұрын
The, er, hidden gem here is the 'overflow-x:hidden' to sort out the 100vw issue, which is going to fix some mobile SEO issues on a couple of client's sites for sure. Thanks for this video!
@Max_Griswald
@Max_Griswald 5 жыл бұрын
My god, this was such a lifesaver for me...
@richtraube2241
@richtraube2241 Жыл бұрын
Great info, as usual.
@nazanin_ashrafi
@nazanin_ashrafi 2 жыл бұрын
Thank you for the awesome video Kevin 🙏🏻🙏🏻 calc() is so complicated though 😵‍💫😵‍💫
@fragrantbloom
@fragrantbloom 2 жыл бұрын
Thank you Kevin!
@franktielemans6624
@franktielemans6624 5 жыл бұрын
Awesome stuff!
@dean6046
@dean6046 5 жыл бұрын
Thank you Ross! Good video
@vjvizar
@vjvizar 5 жыл бұрын
Cool tricks, thanks! :)
@holalabellebille
@holalabellebille 5 жыл бұрын
C'est la vie, c'est incroyable ! thanks from france... - Getting out of the container is great. - Object-fit (i didn't know about !) is huge. Merci chef.
@KevinPowell
@KevinPowell 5 жыл бұрын
De rien! And if it's easier for you to write comments in French, go for it. I can read it no problem, I just can't write it to save my life 😂
@stell4you
@stell4you 5 жыл бұрын
"Object-fit" is not supported by edge, so you might wait until MS switches to webkit engine.
@KevinPowell
@KevinPowell 5 жыл бұрын
It's supported by Edge for images, just not video
@lewiekab
@lewiekab 5 жыл бұрын
I always wanted to know how fittext worked. Very cool.
@marv92
@marv92 5 жыл бұрын
I came here to kill some time watching some nice tips & tricks and that outside the box thing actually solved one of my biggest pains ever. Thank you so much!
@KevinPowell
@KevinPowell 5 жыл бұрын
So glad to hear that Marvin!
@TheElkster
@TheElkster 5 жыл бұрын
really interesting!!!! will be having a play! :)
@TheBorsten
@TheBorsten 5 жыл бұрын
Great Tipps there, Thank you! Especially the font-size trick is really handy. I want to make a suggestion though: To position stuff it’s far less expensive on the rendering (performance) to use transforms rather than margin. You could litterally See the stuttering when resizing the viewport. Margin affects the layout while transforms only touch composite layers.
@johnaweiss
@johnaweiss Жыл бұрын
Yes, i noticed the stuttering too. Can you share the transform code to do the same thing?
@farhanawan9956
@farhanawan9956 5 жыл бұрын
Awesome thank you so much!
@michaeloosthuizen2383
@michaeloosthuizen2383 5 жыл бұрын
I always learn at least one useful thing when watching your videos.
@gunasekarant6498
@gunasekarant6498 2 жыл бұрын
Cool one :-) Thanks @Kevin
@czawiel
@czawiel 5 жыл бұрын
awsome mate! thanks a lot!
@aaronaaronaaron5922
@aaronaaronaaron5922 5 жыл бұрын
o_o dude, you really know about tracks. Thanks ;)
@SalamoYemen
@SalamoYemen 5 жыл бұрын
Top top lesson Thanks so much.
@alinisar87
@alinisar87 5 жыл бұрын
Hey Kevin, great video. I have a question (not related to calc though), what do you think of using % units (e.g. font-size: 140%; ) for setting fonts of different elements, after setting main font in html selector as font-size: 16px?
@klaasvaak4110
@klaasvaak4110 4 жыл бұрын
The black & white picture on 0:48 is the HQ of ABN AMRO in Amsterdam.
@yallayeho1238
@yallayeho1238 5 жыл бұрын
i used it for Containers, which are on the same Level. for example: width: calc(100% / 3) dir Three Containers
@kubakakauko
@kubakakauko 5 жыл бұрын
thanks, that was interesting
@xmelsky
@xmelsky 5 жыл бұрын
Very useful function
@billaddison82
@billaddison82 5 жыл бұрын
Object fit, unreal!
@yassmaboby296
@yassmaboby296 5 жыл бұрын
its really cool useful!
@deathtoby
@deathtoby 3 жыл бұрын
Hi Kevin, I was researching on calc() and I realized that calculation can still work with or without the word calc() in the css. And I see from the start of your video, you also omitted it from the width. So I am wondering if you can tell me what is the implications if we omitted it as I couldn't find any website that talks about that. Thanks
@markjordan7800
@markjordan7800 5 жыл бұрын
using calc with big fonts is super useful ;)
@technophile7024
@technophile7024 5 жыл бұрын
Thanks for your hard work and dedication for us! Nice tutorials! May I know which keyboard are you using?
@KevinPowell
@KevinPowell 5 жыл бұрын
It's an old Dell mechanical keyboard. I don't know the model and I'm not on my computer right now, but if I don't answer this now I probably won't see your comment again :\. I did a quick google search, found some that look similar, but I didn't see my exact model
@technophile7024
@technophile7024 5 жыл бұрын
@@KevinPowell Thanks for your swift response! Don't worry! I loved your tutorials!
@kolklik
@kolklik 5 жыл бұрын
About the issue with the vw calculations leaving your item to suck up space behind the scrollbar, it's possible to overcome this. If you dont need to worry about IE its fairly simple. The general idea is to calculate the width of the scrollbar using a bit of javascript, then using css custom properties which can be updated by javascript, and used in the margin calculation. Below is a link to a codepen that shows the technique. codepen.io/anon/pen/JVJjzX For those who doesn't want to go look, this is what the margin expression would look like. margin: 0 calc(-50vw + (var(--scrollbar-width) / 2) + 50%); In english this would translate to: "Move it half of the viewports width out. Move it in by whatever the scrollbar-width divided by 2 is. Then move it further in by half of its own width" In order to calculate the scrollbar width you generate a div, with a width that is greater than whatever the scrollbar could possibly be (say 100px) then you get that divs offsetWidth (outer width) and the clientWidth (innerWidth) By subtracting those two numbers, you have the width that the scrollbar occupies. This can then be used to update the css custom property through document.documentElement.style.setProperty. A working example can be seen in the codepen linked above. Having the width of the users scrollbar can be quite handy :-)
@VovaBlow
@VovaBlow 5 жыл бұрын
*Dear Kevin, your lessons are awesome! Hello from Ukraine!*
@crunckNATIon
@crunckNATIon 5 жыл бұрын
awesome....i had no choice but to subscribe...great content
@KevinPowell
@KevinPowell 5 жыл бұрын
Thanks for the sub, glad you liked it :)
@AlThePal78
@AlThePal78 Жыл бұрын
awesome vid :)
@lucastavares206
@lucastavares206 5 жыл бұрын
I didnt know css had function It seems that i have a lot of ground to cover, since none of the courses i've taken mentioned that
@moza260
@moza260 3 жыл бұрын
this one is awesome...!!
@Alessandro-nq3tm
@Alessandro-nq3tm 5 жыл бұрын
Amazing, best web tutorials on yt!! Please Kevin can you do a pro video about the scss mixins?
@KevinPowell
@KevinPowell 5 жыл бұрын
Got it on my list 😁
@rushikeshmore2809
@rushikeshmore2809 4 жыл бұрын
Hi Kevin, please make video on RFS how it works? And how to use it ?
@FER_R
@FER_R 5 жыл бұрын
Just came across this video and subscribed t0 your channel. Really cool content! I will have to play around with it. As a few people here said in the comments, I came for one thing and learned a bunch in the process. Do you know how supported this would be in HTML emails? Most of the CSS I write is tailored to that and I was curious if you had tried it. Thanks.
@twph.5890
@twph.5890 5 жыл бұрын
Thank you :)
@CarlStreet
@CarlStreet 5 жыл бұрын
Nice - Tks!
@StefanIvanov188
@StefanIvanov188 3 жыл бұрын
Hey Im trying to find out how Facebook new design calcs the widh of images containers on their Newsfeed. If you inspect the photo you will see parent div who haves for example if the original width of image is 500x321 then the calc is width: calc(-506.231px + 155.763vh); I can't find out where the "-506.231px" and 155.763vh; are coming from....
@rafi_45
@rafi_45 5 жыл бұрын
Thank you sir
@yenumulasrinivasareddy5825
@yenumulasrinivasareddy5825 3 жыл бұрын
Thank you kevin,I learnt a new trick today. can we use CSS variables in Calc() like below? ex: font-size: calc( var(--title-fontSize) - 8px); I tried but not working for me. thanks.
@johnaweiss
@johnaweiss Жыл бұрын
Can calc use the existing value of an element property?
@greenmansession
@greenmansession 5 жыл бұрын
Should definitely do a Sass video ;)
@tanveerkhan9430
@tanveerkhan9430 5 жыл бұрын
Good tip
@shohanrahman9392
@shohanrahman9392 3 жыл бұрын
4:50 when he uses max-width: 100vw, how come it only fills up the right side and not the entirety of the viewport width? Is it because some other property (like margin-left) overrides it?
@borcsaster
@borcsaster 5 жыл бұрын
..love it!
@moizkhalid2714
@moizkhalid2714 5 жыл бұрын
Could you tell us how to start html and css for website templates in psd.. How to aprouch
@KevinPowell
@KevinPowell 5 жыл бұрын
I have a few series where I do that! Look for my 'building a responsive page' type series.
@youssefmuhamad3213
@youssefmuhamad3213 5 жыл бұрын
This font-size with calc is perfect for responsiveness, loved it
@ahsath
@ahsath 5 жыл бұрын
Almost didn't click on this video, I'm glad i did.
@DonAndress
@DonAndress 3 жыл бұрын
Is it possible to use calc() to calculate height based on width? I mean, I'd like to set height = width or height = 40% of width. Is it even possible in pure CSS or SASS?
@lewisrobinson508
@lewisrobinson508 5 жыл бұрын
I've been playing with calc() and custom properties to generate responsive font sizes while being able to set a min/max font size. Eg. :root { // font sizes --fsMin: 16; --fsMax: 26; // viewport widths --vwMin: 420; --vwMax: 1280; } html { font-size: calc((var(--fsMin) * 1px) + (var(--fsMax) - var(--fsMin)) * (100vw - (var(--vwMin) * 1px)) / (var(--vwMax) - var(--vwMin))); @media screen and (max-width: 420px) { font-size: calc(var(--fsMin) * 1px); } @media screen and (min-width: 1280px) { font-size: calc(var(--fsMax) * 1px); } } ---- codepen: codepen.io/lewisvrobinson/pen/VWoNLP?editors=0100
@KevinPowell
@KevinPowell 5 жыл бұрын
Really cool. That initial font-size calculation broke my head a little, lol. Works super well!
@himanshuverma3764
@himanshuverma3764 3 жыл бұрын
Sir You have to make new layout on topics calc() thanks when i declare width: clac() its is necessary to declare width or not i am confuse sir
@0the0ambient0
@0the0ambient0 4 жыл бұрын
As a calc believed, thank you!
min(), max(), and clamp() are CSS magic!
18:12
Kevin Powell
Рет қаралды 293 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 16 МЛН
Iron Chin ✅ Isaih made this look too easy
00:13
Power Slap
Рет қаралды 35 МЛН
The new CSS pseudo-classes explained - :is() :where() :has()
13:50
Kevin Powell
Рет қаралды 133 М.
5 CSS mistakes that I see way too often
19:03
Kevin Powell
Рет қаралды 359 М.
100+ Docker Concepts you Need to Know
8:28
Fireship
Рет қаралды 880 М.
Nature's Incredible ROTATING MOTOR (It’s Electric!) - Smarter Every Day 300
29:37
Learn CSS BEM (and avoid these common mistakes)
15:36
Dmitry Mayorov
Рет қаралды 5 М.
Flexbox design patterns you can use in your projects
15:33
Kevin Powell
Рет қаралды 438 М.
Top 10 CSS One Liners That Will Blow Your Mind
13:34
developedbyed
Рет қаралды 930 М.
CSS Units: vh, vw, vmin, vmax #css #responsive #design
14:27
Kevin Powell
Рет қаралды 198 М.
The problems with viewport units
13:23
Kevin Powell
Рет қаралды 357 М.
Learn Flexbox CSS in 8 minutes
8:16
Slaying The Dragon
Рет қаралды 1,5 МЛН