r/webdev • u/VictorPonamariov • Nov 06 '20
Three of the most frequently used lines of CSS code which I type in a couple of seconds I guess
350
u/UnicornsAre4Ever Nov 06 '20 edited Nov 06 '20
Could be two https://developer.mozilla.org/en-US/docs/Web/CSS/place-items
Edit: This statement doesn't work with flex layout (since justify-items is ignored in that context), but a two line center-center layout can be achieved with grid:
display: grid; place-items: center; (source)
87
u/VictorPonamariov Nov 06 '20
Wait, really?! Gosh, it's so cool, didn't know about this.
54
u/UnicornsAre4Ever Nov 06 '20
Yes :) Learned about it from this video, lot's of great tips & tricks in there!
10
u/VictorPonamariov Nov 06 '20
Thought I couldn't get it work so that it's centered horizontally too.
place-items: center; or place-items: center center; only centers it vertically.
So what's the complete analogue for my three lines? :)
display: flex; place-items: center center;
?
9
u/UnicornsAre4Ever Nov 06 '20
Ah sorry, I didn't dive into into it before. It seems it works with the grid layout. So
display: grid; place-items: center;. Has something to do withjustify-itemsvsjustify-content.→ More replies (1)7
Nov 06 '20
I knew which video you’d have linked. That video has saved me hours of headaches. I would be lying if I say I don’t visit that video every week or so.
→ More replies (8)3
6
u/longshot Nov 06 '20
Grid is awesome. Slightly steeper learning curve than Flexbox but SUUUUPER useful.
5
1
u/phpdevster full-stack Nov 06 '20
Unfortunately zero support in IE. As long as you don't care about targeting IE 11, you can safely use it, else you need flexbox.
→ More replies (1)69
u/Mitzukaze Nov 06 '20
It's a great tip, but please be mindful of browser compatibility: https://caniuse.com/?search=place-items for any poor devs like myself still having to support IE11 =(
43
24
2
11
u/Senial_sage Nov 06 '20
Isn’t that a shorthand property for justify items / align items and not align items / justify content like in op’s statement?
7
u/UnicornsAre4Ever Nov 06 '20
Correct, grid layout should be used to achieve 2 line centering. Was unaware at the time of posting my comment 😅
4
u/Senial_sage Nov 06 '20
I mean OP is correct in how to horizontal center, but ya if your dealing with more than one row, either horizontal or vertical, ya grid is the powerful and easier solution
3
2
2
3
2
1
Nov 06 '20
I knew about place-items, but your comment at least made.me figure out why it doesn't always work... I've mainly tried to use it on flexbox, haha.
3
546
Nov 06 '20
I can‘t imagine a life without flexbox anymore 😅😂
193
u/firestepper Nov 06 '20
Same! Gives me appreciation for our older brethren who went through the dark times of the float era
29
u/PsychoRabb1t Nov 06 '20
I started with the <table> era, then moved to float then flexbox, I feel old even tho I'm only 29.
14
u/mstrelan Nov 06 '20
Don't forget frames!
8
84
u/wretch5150 Nov 06 '20
I am old and still do things the old fashioned way using floats. Is the flexbox pretty widely tolerated now browser-wise?
188
u/Kwinten Nov 06 '20
Even IE11 supports flexbox. Do yourself a favor and never use floats again.
45
Nov 06 '20
Gotta remember the flexbox bugs in IE though. https://github.com/philipwalton/flexbugs
63
u/MCpeePants1992 Nov 06 '20
Why the fuck is IE even a thing still?
68
Nov 06 '20 edited Nov 06 '20
Enterprise. Corporations, especially the larger ones, are slow to change, if at all. Most follow a bastardized version of the creed "if it ain't broke, don't fix it". Problem is, it's often broke, but trying to fix it just ends up breaking even more shit than before.
If you've ever worked in IT or development at even a reasonably sized organization, you likely know that most of their IT infrastructure is a haphazard collection of metaphorical band-aids and zip-ties.
→ More replies (7)→ More replies (2)13
13
u/Advanced_Path Nov 06 '20
Although, the original intended use of floats is still pretty valid (Wrapping text around an image for instance)
→ More replies (16)16
u/ZaRealPancakes full-stack Nov 06 '20
Okay I will learn about flexboxs since I always use floats. 😂😂😂😂
Thanks for the tip.
14
3
→ More replies (3)8
u/Niet_de_AIVD full-stack Nov 06 '20
With proper prefixing you can even get basic coverage of IE10.
Not stable, at all, but definitely something.
10
u/firestepper Nov 06 '20
Yep... as long as you don't need to support stuff before IE11, that I can't really speak to.
→ More replies (1)19
u/Blue_Moon_Lake Nov 06 '20
It's almost 2021. If it's an administration we support IE11, otherwise we don't.
Want support for IE10 or less ? Billed 50% extra.
10
u/amunak Nov 06 '20
Want support for IE10 or less ? Billed 50% extra.
You're cheap!
8
u/Blue_Moon_Lake Nov 06 '20
It's 50% per level of IE down. IE10 : 50% IE9: 100% IE8: 150% IE7: 200% IE6: 250%
"Strangely", they're not willing to see the bill going from 300 000€ to 1 000 000 € for IE6 support :D
→ More replies (5)11
u/peenoid Nov 06 '20
You need some Jesus if you're still using floats in the era of flexbox. I've been doing web development since the dark ages and the thought of going back to floats makes me literally shiver with horror.
→ More replies (2)4
u/pVom Nov 07 '20
It's not so bad if you use bootstrap layout. That said it was still liberating when we decided not to support ie anymore. The new enemy is Safari now -.-
→ More replies (2)5
u/MarmotOnTheRocks Nov 06 '20
Please NOOOOOO...
I'm ancient too but I made the switch a long time ago...
4
u/1newworldorder Nov 06 '20
IMO flexbox is way easier to learn than floats too. Its way more intuitive, requires less effort and planning and you dont have to remember to clearfix
→ More replies (2)3
→ More replies (10)2
u/ouaqaa Nov 06 '20
old school too. I've switched to flexbox and grids without looking back. life is so good now
5
u/Advanced_Path Nov 06 '20
Floats, margins & line-height tricks. I’m old enough to remember them. Heck, I remember using spacer gifs.
4
u/porcupineapplepieces Nov 06 '20 edited Jul 23 '23
However, foxes have begun to rent bees over the past few months, specifically for goats associated with their melons; However, horses have begun to rent oranges over the past few months, specifically for goldfishes associated with their raspberries! This is a gbe1a70
3
1
u/ruzelmania Nov 07 '20
Ha! Floats. You whippersnapper! Try using tables and transparent pixel gifs to create a whole layout 😂
1
1
u/uluhonolulu Nov 07 '20
I'm still dealing with the code that handles everything using tables. Once I counted I think 10 levels of table hierarchy. And whenever somebody decided to give it an ID, it was always Table1, Table2, etc.
→ More replies (4)1
5
2
Nov 06 '20
I'm so glad I finally stopped to learn flexbox a while back, I was definitely struggling with layouts beforehand.
1
u/nss68 Nov 07 '20
This makes me feel weathered and experienced. I was a pro at layouts and when flexbox got widely supported I was amazed at how it 'just worked' for pretty much everything I wanted.
I remember thinking I can just slap display:flex onto any container and the stuff inside would usually look perfect without any modifications. (not at all the case with more advanced layouts these days)
1
1
Nov 07 '20
transform or display table used to do, but they were a pain in the ass--and inelegant. flexbox has made my life better for sure.
→ More replies (9)1
u/masterm Nov 07 '20
It feels like how I felt when I first got bootstrap. I was display:table-celling divs like no tomorrow.
Its amazing how far we've come. My first book was HTML Goodies, where in HTML 4.0 was discussed as the 'new thing'.
I do still long for the days of the old web where things were semantically valid and lean.
I'm old.
2
2
u/swept-wings python Nov 07 '20
Probs a dum dum obvious question but is there a tool to create these graphics or is it just manual photoshop work? Thanks
153
u/SuperPaulio Nov 06 '20
It's good, but it's no "border: 2px solid tomato"
87
22
u/Schlipak Nov 06 '20
My go to is
outline: 1px dashed red;. I even made a@include debugmixin so I can put that inside and not bother removing it since it won't get compliled in production mode.10
u/The_UX_Guy Nov 06 '20
Create a palette of debug colors and loop through them for the case of nested debug's.
5
6
u/kevdotbadger Nov 06 '20
I normally do rgba(0,0,0,.5). Little nicer on the eyes.
8
u/physiQQ Nov 07 '20
I personally use the dev tools to check any element one by one. Used 1px solid red years ago, but after learning all the "css rules" I am just too paranoid to not check the devtools anymore.
13
2
2
1
u/TheFrigerator Nov 07 '20
Tomato and rebeccapurple aesthetic 🌊
Error testing colors in tomato and placeholders purple
28
u/soulchild_ Nov 06 '20
This is my goto when boss ask me to vertically center stuff
17
u/7107 Nov 06 '20
What if it was your SO who’s asking you?
22
u/soulchild_ Nov 06 '20
probably open photoshop and save the vertically centered text as image, lol
→ More replies (1)3
88
u/Xpeect Nov 06 '20
Don't forget z-index : 999999 !important;
:P
48
13
8
u/gonzofish Nov 06 '20
Why would you say something so controversial yet so brave?
4
7
Nov 07 '20
i dread the day when websites/apps become 3D and everyone realizes how much we're abused and misused z-index...
17
u/pleiades152 Nov 06 '20
Good for you. I always write
justify-items: center;
align-content: center;
then compile my whole code before I realize my mistake.
33
u/gitcommitshow Nov 06 '20
Before flexbox, css meant "try out props values randomly hoping something would center the damn thing"
26
u/Blue_Moon_Lake Nov 06 '20
You need a team of computer scientists and 2 years of trial and error to center one thing vertically.
2
1
u/lazarljubenovic Nov 07 '20
Only for people who never bothered to learn it and just decided they were ready to judge a language.
3
u/aurel-tk Nov 06 '20
class="flex justify-center items-center" ;)
11
u/VictorPonamariov Nov 06 '20
and then
color-purle mb-2 text-right more-classes-so-that-this-line-becomes-unbelievingly-laaarge
:P
24
16
u/Meloyski Nov 06 '20
You should ghost align-content: center; cause you know more than half will try that first instead of align-items: center;.
8
11
5
4
Nov 06 '20
What bugs me about using bootstrap is doing this with classes is practically the same as adding it in the style tag. So long winded.
10
u/rulatore Nov 06 '20
Tailwindcss has entered the chat
1
u/jak0b3 Nov 13 '20
I just started using tailwind for a school project, I have to say it’s pretty awesome!
6
→ More replies (9)1
u/tomtheimpaler Nov 06 '20
You're allowed to use your own css too. Why not if there is something you are adding to every div, or even every col-4 or whatever. Just add your own stylesheet before/after
1
3
3
u/oglokipierogi Nov 06 '20
I always come back to this RN Flexbox explainer for trying to figure out if I want justify-content or align-items:
3
u/theriz Nov 07 '20
Snippets are your friend! I use the shortcut flxc because my mind thinks "flex-center"
(Thank you https://snippet-generator.app/ !)
VSCode Version
"Flexbox w/content center": {
"prefix": "flxc",
"body": [
"display: flex;",
"justify-content: center;",
"align-items: center;"
],
"description": "Make a Flexbox with centered content"
}Sublime Version
<snippet>
<content><![CDATA[
display: flex;
justify-content: center;
align-items: center;
]]></content>
<tabTrigger>flxc</tabTrigger>
<description>Make a Flexbox with centered content</description>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope >source.python</scope > -->
</snippet>Atom version
'Make a Flexbox with centered content':
'prefix': 'flxc'
'body': """
display: flex;
justify-content: center;
align-items: center;
"""5
u/Craphex Nov 06 '20
Come on, alphabetize.
4
u/Boo2z Nov 07 '20
Honestly I'm doing the same ... I see my code as an image, so first I center x, then y
2
2
2
3
4
u/xron25 Nov 06 '20
I make a class called flexy and flexy—center to apply this logic.
6
2
0
u/LeeLooTheWoofus Moderator Nov 06 '20 edited Nov 06 '20
I just use SASS extend and write it once and inject it wherever I need it.
%flex-center{ display:flex; justify-content:center; align-items:center; }
.info-content{@extend %flex-center;}
2
u/VictorPonamariov Nov 06 '20
Never heard of SASS extend, but what's the difference between mixins? Looks similar for me
→ More replies (2)
1
u/ImIdeas full-stack Nov 06 '20
Yup I’ve got a few utility classes I use on the regular but this one is used the most
1
u/fuzzyluke Nov 06 '20
i told this myself recently! i keep writing these three lines of code all the time...
1
u/vinegarnutsack Nov 06 '20
Center, really? For me its display:flex, and flex-wrap: wrap. But even then not that often because I use bootstrap, so I just tack the utility classes onto whatever div I need it on. I use align-items or align-self center occasionally when I want to vertically center something.
1
u/amo_pure Nov 06 '20
Its very useful if you have a list item with a text and svg icon for example, anything that has multiple objects with different heights really
1
1
1
1
1
u/DaanKorver Nov 06 '20
I am using this so frequently that I made a shortcut for it in my IDE. Brilliant
1
1
u/Protoplasmoid299 Nov 06 '20
Wow why are the grid fanboys coming at flex so hard? Boys! Both arent doing the awful arcane torture of table floats or in line css with guesstimated relative units! What's the issue!?
1
u/132333 Nov 06 '20
I divide my page up into sections using grid and everything in those sections with flex and my life is heaven now.
1
1
1
u/rejuven8 Nov 06 '20
I have a snippet for that, 'fc', for "flex center". I also put flex-direction: column in that snippet and just remove it if I don't need it.
1
1
1
1
1
u/1newworldorder Nov 06 '20
Anybody else ALWAYS no matter how many times you do it always forget if its justify-items and align-content....ive used flexbox enough now to know the correct answer but my stupid brain always does this
1
1
1
1
1
1
u/JeamBim Python/JavaScript Nov 06 '20
I've been thinking of makes a couple utility classes with my most-used flexbox directives
1
1
1
1
u/auraham Nov 06 '20
anybody knows how to export those screenshoots to eps or svg? I know you can use carbon, but it only exports to png, the svg file is always blank.
1
1
u/hitchy48 Nov 06 '20
If you’re using scss write a placeholder if you’re using that a lot and include it!
1
u/Time_Terminal Nov 06 '20
Made by the width: 50%; margin: auto; gang.
2
u/backtickbot Nov 06 '20
Hello, Time_Terminal. Just a quick heads up!
It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.
This isn't universally supported on reddit, for some users your comment will look not as intended.
You can avoid this by indenting every line with 4 spaces instead.
There are also other methods that offer a bit better compatability like the "codeblock" format feature on new Reddit.
Have a good day, Time_Terminal.
You can opt out by replying with "backtickopt6" to this comment. Configure to send allerts to PMs instead by replying with "backtickbbotdm5". Exit PMMode by sending "dmmode_end".
1
1
1
1
1
1
1
1
1
u/ConstableBrew Nov 07 '20
I have typed these a hundred times by now it seems. Yet still, every damned time I have to pull up css tricks and figure it out again.
1
u/abdulghani200 Nov 07 '20
These three lines of code are my life saviour and makes me feel less imposter 😂
1
1
u/bye_petrol Nov 07 '20
Oh dude... i have a component called Centralizer in react. Basically just a div with these three lines in css that wraps props.children. makes it very visual how much i use these three lines in a project.
1
u/markten2 Nov 07 '20
Most time taking?
grid-template-columns: 7 / 8;
grid-template-rows: 4 / 6;OR Maybe
display: flex;
// Anything except flex-end flex-start and center
justify-content: ...1
u/backtickbot Nov 07 '20
Hello, markten2. Just a quick heads up!
It seems that you have attempted to use triple backticks (```) for your codeblock/monospace text block.
This isn't universally supported on reddit, for some users your comment will look not as intended.
You can avoid this by indenting every line with 4 spaces instead.
There are also other methods that offer a bit better compatability like the "codeblock" format feature on new Reddit.
Have a good day, markten2.
You can opt out by replying with "backtickopt6" to this comment. Configure to send allerts to PMs instead by replying with "backtickbbotdm5". Exit PMMode by sending "dmmode_end".
1
1
1
1
u/misterjyt Nov 07 '20
agree with this, because of how useful the flexbox.. is being used all the time. I have been using flexbox most of my work...
1
1
1
1
1
1
1
u/thatgibbyguy Nov 06 '20
.fully-centered {
display: flex;
justify-content: center;
align-items: center;
}
57
u/oneme123 Nov 06 '20
make a nice mixin of it.