<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Joshua Swiss</title>
    <description>The latest articles on DEV Community by Joshua Swiss (@jswiss).</description>
    <link>https://dev.to/jswiss</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F240255%2F55d52177-e31c-46aa-a659-3e5d7ed8fbf4.jpeg</url>
      <title>DEV Community: Joshua Swiss</title>
      <link>https://dev.to/jswiss</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jswiss"/>
    <language>en</language>
    <item>
      <title>Re-learning CSS Part 2: Simplify Cards with Flexbox</title>
      <dc:creator>Joshua Swiss</dc:creator>
      <pubDate>Thu, 18 Jun 2020 11:00:34 +0000</pubDate>
      <link>https://dev.to/jswiss/re-learning-css-part-2-simplify-cards-with-flexbox-nf6</link>
      <guid>https://dev.to/jswiss/re-learning-css-part-2-simplify-cards-with-flexbox-nf6</guid>
      <description>&lt;p&gt;&lt;em&gt;This is an ongoing series where I am starting over learning modern CSS. You can find other posts in this series below:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/jswiss/re-learning-css-part-1-grid-or-flexbox-4e8h"&gt;Part 1: CSS Grid or Flexbox for Layouts?&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;TL;DR - Styling cards used to be a pain. With Flexbox it's pretty easy!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;CSS is tough. When I started professionally developing websites about five years ago, there was zero incentive to develop CSS knowledge; even basic layouts seemed hacky, with prolific use of &lt;code&gt;float: right;&lt;/code&gt; this and &lt;code&gt;margin: 0 auto;&lt;/code&gt; that. It didn't intrinsically make sense and only added to the mystique and inapproachability that was CSS at the time.&lt;/p&gt;

&lt;p&gt;I often dreaded styling anything beyond minor tweaks. If I had to create a card, for example, I'd reach for &lt;a href="https://getbootstrap.com/"&gt;Bootstrap&lt;/a&gt; or &lt;a href="https://bulma.io/"&gt;Bulma&lt;/a&gt; or &lt;a href="https://getmdl.io/"&gt;Material Design&lt;/a&gt;. But with Flexbox there's no need! Let's start with a super simple example.&lt;/p&gt;

&lt;h2&gt;
  
  
  Card 1: Simple and centred
&lt;/h2&gt;

&lt;p&gt;Let's say you want a very basic card that has one piece of child content in it, and that content is centred vertically and horizontally. For the sake of this example, let's say it's holiday destinations. Here's some simple code to get started:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/jswiss/embed/LYGRpRo?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Each card is an &lt;code&gt;article&lt;/code&gt; element. The cards are inside a &lt;code&gt;section&lt;/code&gt; element, where I applied some basic Grid styles. The important part for this example is inside the &lt;code&gt;.card&lt;/code&gt; class:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ot3Yn6uG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zqjybhvxliw4zwk91iin.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ot3Yn6uG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zqjybhvxliw4zwk91iin.png" alt="Basic Card Flex Styles to Center Vertically and Horizontally" title="Basic Card Flex Styles to Center Vertically and Horizontally"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Flex, like grid, are known as 'display-inside' keywords that can be assigned to the &lt;code&gt;display&lt;/code&gt; property. 'Display-inside' keywords change how the element's inner content is displayed. This is in contrast to 'display-outside' keywords that change the element's outer display type, or the role the element has in the layout flow. Some 'display-outside' keywords are &lt;code&gt;block&lt;/code&gt; or &lt;code&gt;inline&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Within an element where the &lt;code&gt;display: flex;&lt;/code&gt; property/keyword is assigned, child elements are centred vertically with &lt;code&gt;align-items: center;&lt;/code&gt; and horizontally with &lt;code&gt;justify-content: center;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;While this card layout is pretty simple, it's not all that useful. Let's move on to something we could more realistically use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Card 2: Centred with sections
&lt;/h2&gt;

&lt;p&gt;Building on the last example; we need a little more for your holiday cards. Just saying the destination name doesn't really do much. Let's add a photo, and the date visited:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/jswiss/embed/ZEQpbNw?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Now each card's content is horizontally centre-aligned, and it has three sections: one for the header, one for the main content (a photo for this example), and one for the footer.&lt;/p&gt;

&lt;p&gt;I made some changes to the card's max and min width and height, and added some basic styles to each section and the img elements, but those changes aren't that interesting, and you can check them out in the Pen above. The most interesting line of code for this example, at least that relates to Flexbox, is: &lt;code&gt;flex-direction: column;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This one line transposes the contents of our three new &lt;code&gt;section&lt;/code&gt; elements to display vertically instead of horizontally, similar to the &lt;code&gt;old-school display: block;&lt;/code&gt; versus display: &lt;code&gt;inline-block;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Ok! Our cards are starting to look pretty decent, but I think they could be better. Let's stick with the three sections we have for each card, but add some extra styling for functionality we might add later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Card 3: Handling complex sections
&lt;/h2&gt;

&lt;p&gt;The final example has the card's title and date in the 'header' section aligned to the left, and changes the footer to a 'card-action-area' where there are 'open' and 'edit' buttons.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/jswiss/embed/ZEQpWWp?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;In the new &lt;code&gt;header&lt;/code&gt; element we have a &lt;code&gt;card-title&lt;/code&gt; and &lt;code&gt;card-date,&lt;/code&gt; each with styles specific to them. The header element has the style &lt;code&gt;align-self: flex-start;&lt;/code&gt; applied, which pull the child sections to the beginning (the flex start) of the element.&lt;/p&gt;

&lt;p&gt;The new &lt;code&gt;footer&lt;/code&gt; element also has two children, &lt;code&gt;open&lt;/code&gt; and &lt;code&gt;edit&lt;/code&gt; buttons, but we want these to be displayed horizontally rather than vertically. With flex that's easy! Simply set the &lt;code&gt;card-action-area&lt;/code&gt; class to &lt;code&gt;display: flex;&lt;/code&gt;, which will apply to all it's child elements, and reset the &lt;code&gt;flex-direction&lt;/code&gt; to its default &lt;code&gt;row&lt;/code&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping up
&lt;/h2&gt;

&lt;p&gt;Flexbox makes styling components like cards a breeze. It's powerful and applicable for simple or more complex components. I wish I had it when I started web development!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://joshuaswiss.dev"&gt;For more, check out my personal site!&lt;/a&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>flexbox</category>
      <category>card</category>
      <category>design</category>
    </item>
    <item>
      <title>Re-learning CSS Part 1: Grid or Flexbox</title>
      <dc:creator>Joshua Swiss</dc:creator>
      <pubDate>Sun, 10 May 2020 14:17:50 +0000</pubDate>
      <link>https://dev.to/jswiss/re-learning-css-part-1-grid-or-flexbox-4e8h</link>
      <guid>https://dev.to/jswiss/re-learning-css-part-1-grid-or-flexbox-4e8h</guid>
      <description>&lt;p&gt;&lt;em&gt;TL;DR - CSS Grid and Flexbox are great. I use Grid for layouts and two-dimensional renderings, and Flexbox for one-dimensional renderings and components.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;CSS is tough. When I started professionally developing websites about five years ago, there was zero incentive to develop CSS knowledge; even basic layouts seemed hacky, with prolific use of &lt;code&gt;float: right;&lt;/code&gt; this and &lt;code&gt;margin: 0 auto;&lt;/code&gt; that. It didn't intrinsically make sense and only added to the mystique and inapproachability that was CSS at the time.&lt;/p&gt;

&lt;p&gt;Like many other software engineers at the time, I reached for CSS frameworks to solve the problem. I &lt;a href="https://getbootstrap.com/"&gt;Bootstrapped&lt;/a&gt;, &lt;a href="https://bulma.io/"&gt;Bulma'ed&lt;/a&gt;, and &lt;a href="https://material.io/"&gt;Material Designed&lt;/a&gt; my way through a lot of them. Frameworks like these enable teams to develop products quickly and with a degree of uniformity. Professionally, I exclusively used frameworks for a long time, with the occasional tweak here and there in regular CSS. But frameworks are not without downsides: they can add a lot of weight to your package bundle, add complexity to your markup, and are yet another thing to learn.&lt;/p&gt;

&lt;p&gt;I had all but resigned myself to working with frameworks forever, until I came across this tweet, in a thread discussing how a lot of folk (like me!) think CSS is a pain:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--fvHMAo2r--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/953713101571960832/2svb3VEV_normal.jpg" alt="Shaw profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Shaw
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="comment-mentioned-user" href="https://dev.to/shshaw"&gt;@shshaw&lt;/a&gt;

      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--52oNvK_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-ff4bdab814039c4cb172a35ea369e0ea9c6a4b59b631a293896ae195fa26a99d.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      &lt;a href="https://twitter.com/WebDojo"&gt;@WebDojo&lt;/a&gt; &lt;a href="https://twitter.com/chriscoyier"&gt;@chriscoyier&lt;/a&gt; Modern CSS has solved so much of this. Come back to it with a fresh mind removing any remembrance of floats and Internet Explorer.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      03:49 AM - 09 Apr 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1248095631077507073" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1248095631077507073" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      1
      &lt;a href="https://twitter.com/intent/like?tweet_id=1248095631077507073" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      108
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;This inspired me. Have I ignored major improvements in CSS? Is it easier now? Does it make more sense? What if &lt;em&gt;I&lt;/em&gt; come back to it with a 'fresh mind'?&lt;/p&gt;

&lt;p&gt;And thus, this blog series was born. I'm going to relearn CSS and write about it along the way. I'll be looking at layouts, components, 'pure-CSS' images, and animations.&lt;/p&gt;

&lt;p&gt;First up, layouts. Specifically, should I build my layout with CSS Grid or Flexbox?&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid or Flexbox?
&lt;/h2&gt;

&lt;p&gt;According to our good friends at CanIUse (as of 8 May, 2020) both Flexbox and Grid work with the browsers a significant majority of people use.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yG3Hmt0u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yb7czgfmdw46jjlft8p0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yG3Hmt0u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yb7czgfmdw46jjlft8p0.png" alt="Can I Use Grid?"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;a href="https://caniuse.com/#feat=css-grid"&gt;Can I Use Grid?&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gIViq-CP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fakth3z8rq0uup0ybwn7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gIViq-CP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fakth3z8rq0uup0ybwn7.png" alt="Can I Use Flexbox?"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;a href="https://caniuse.com/#feat=flexbox"&gt;Can I Use Flexbox?&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While Grid has a bit more red in its image, it still works with the browsers 95% of the population use. For my audience, that works fine. If you need to accommodate for that remaining ~5%, I'd suggest adding appropriate polyfills.&lt;/p&gt;
&lt;h2&gt;
  
  
  Building Layouts
&lt;/h2&gt;

&lt;p&gt;Most layouts look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--khg9ukjB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wku1hczk9cs7r0iyrdl5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--khg9ukjB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wku1hczk9cs7r0iyrdl5.png" alt="The 'Holy Grail' Layout"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;&lt;a href="https://alistapart.com/article/holygrail/"&gt;The 'Holy Grail' Layout. What was once a pain, is now pretty simple&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I'd hazard a guess that a ridiculous proportion of websites have some form of this basic layout in place, with a header for title/nav/avatar, a footer for contact, external navigation, etc. two sidebars for whatever, and a main section in the middle. This layout is so popular, its been dubbed the &lt;a href="https://css-tricks.com/snippets/css/css-grid-starter-layouts/"&gt;'Holy Grail' Layout&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;So, let's build this layout using both CSS Grid and Flexbox!&lt;/p&gt;

&lt;p&gt;Here's the layout using Grid:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/jswiss/embed/BaorJOo?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Overall, pretty simple! However, there are a few things worth noting, that may not be otherwise obvious:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The element with &lt;code&gt;display: grid;&lt;/code&gt; (here a &lt;code&gt;div&lt;/code&gt; with the id of grid) sets top-level child elements of that div to exist in a grid display;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;grid-template-rows&lt;/code&gt; and &lt;code&gt;grid-template-columns&lt;/code&gt; set up the grid. Here we have a 3x3 grid, with the header and footer set to 50px height, and the left and right sidebars set to 100px width;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;grid-gap&lt;/code&gt; is shorthand for &lt;code&gt;grid-column-gap&lt;/code&gt; &lt;em&gt;and&lt;/em&gt; &lt;code&gt;grid-row-gap&lt;/code&gt;. Here I set both to 1em;&lt;/li&gt;
&lt;li&gt;Similarly, &lt;code&gt;grid-column&lt;/code&gt; is shorthand for &lt;code&gt;grid-column-start&lt;/code&gt; and &lt;code&gt;grid-column-end&lt;/code&gt;, where the grid item is not inclusive of the end column. Where I have &lt;code&gt;grid-column: 1 / 4;&lt;/code&gt;, it means &lt;code&gt;grid-column-start: 1;, grid-column-end: 4;&lt;/code&gt;, which means this grid item spans columns 1, 2, and 3.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let's check out the same layout, this time using Flexbox:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/jswiss/embed/dyYmdWy?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;A little more verbose! I wrapped the second row in a &lt;code&gt;section&lt;/code&gt; element so I could have rows inside columns (notice how Flexbox's rows/columns are the inverse of Grid's?). Let's break it down a little:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For the topmost &lt;code&gt;div&lt;/code&gt;, I set the display to flex, set a minimum height and width, and set the &lt;code&gt;flex-direction&lt;/code&gt; to column. Column here means one column, with multiple rows;&lt;/li&gt;
&lt;li&gt;In the &lt;code&gt;#main&lt;/code&gt; section, I again set the display to flex, and set &lt;code&gt;flex-direction&lt;/code&gt; to row. Row here means one row, with multiple columns. I also gave the section a margin top and bottom of &lt;code&gt;1em&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;I gave the header and footer and the asides default height and width; and&lt;/li&gt;
&lt;li&gt;I gave the main article a margin left and right of &lt;code&gt;1em&lt;/code&gt;, and set it to &lt;code&gt;flex-grow: 1&lt;/code&gt;. &lt;code&gt;flex-grow&lt;/code&gt; allows the &lt;code&gt;article&lt;/code&gt; element to expand to take up the extra space in the &lt;code&gt;flex-row&lt;/code&gt; it is in.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  BONUS!
&lt;/h3&gt;

&lt;p&gt;Here's the same layout using Bootstrap!&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/jswiss/embed/xxwWWqO?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Funnily enough, even though I imported Bootstrap here, I'm &lt;em&gt;still&lt;/em&gt; using Flexbox! This is because Bootstrap uses the 12-column grid convention, and I &lt;em&gt;hate&lt;/em&gt; it. The 12-column grid layout is a web design convention that, at best, requires you to do some calculations, and at worst, requires you to overuse &lt;a href="https://css-tricks.com/magic-numbers-in-css/"&gt;magic numbers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I cannot tell you how long I've spent fiddling with different configurations of the 12-column layout to get it just right; tweaking &lt;code&gt;col-10\&lt;/code&gt; to &lt;code&gt;col-8\&lt;/code&gt;, adjusting margins as I go. It's a waste of time, and you don't learn anything in the process.&lt;/p&gt;

&lt;p&gt;So, if you still need Flexbox to use Bootstrap, why do you need to use Bootstrap. It's a very good question...&lt;/p&gt;

&lt;h2&gt;
  
  
  Which to use?
&lt;/h2&gt;

&lt;p&gt;In the end, I prefer Grid for layouts or anything two-dimensional, and Flexbox for one-dimensional renderings (like the icons in the bottom of my site!) or for components. Of course, that's personal preference; yours may differ, and mine may change over time!&lt;/p&gt;

&lt;p&gt;For me, it is definitely time to rid myself of unnecessary frameworks, wipe the slate clean, and embrace CSS!&lt;/p&gt;

</description>
      <category>css</category>
      <category>grid</category>
      <category>flexbox</category>
      <category>fundamentals</category>
    </item>
  </channel>
</rss>
