<?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: Kyle Griffin</title>
    <description>The latest articles on DEV Community by Kyle Griffin (@kylefilegriffin).</description>
    <link>https://dev.to/kylefilegriffin</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%2F149499%2F90bee454-56ba-40e1-b3ff-fbb4617d3a80.jpg</url>
      <title>DEV Community: Kyle Griffin</title>
      <link>https://dev.to/kylefilegriffin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kylefilegriffin"/>
    <language>en</language>
    <item>
      <title>Responsive CSS Layouts WITHOUT Media Queries</title>
      <dc:creator>Kyle Griffin</dc:creator>
      <pubDate>Tue, 21 Jan 2020 17:13:32 +0000</pubDate>
      <link>https://dev.to/kylefilegriffin/responsive-css-layouts-without-media-queries-5e4n</link>
      <guid>https://dev.to/kylefilegriffin/responsive-css-layouts-without-media-queries-5e4n</guid>
      <description>&lt;p&gt;The introduction of Media Queries to the CSS specification in 2012 was the beginning of an age of Responsive Web Development. With CSS getting more and more comprehensive, we could now roll out completely different looks at certain breakpoints, on certain devices and even if the user had their phone on landscape!&lt;/p&gt;

&lt;p&gt;When learning CSS, it is highly recommended to build your site and get it to a place where you are happy with its desktop layout, then to add in media queries to affect page elements as the browser's width reduces to the width of a mobile phone screen. I build to account for screens as narrow as 320 pixels; the width of the iPhone 4/iPhone SE.&lt;/p&gt;

&lt;p&gt;But what if, you could create completely responsive layouts, starting from desktop and intelligently allocating itself space on the page when it starts to get too narrow? &lt;/p&gt;

&lt;h2&gt;
  
  
  Flex - When your elements vary
&lt;/h2&gt;

&lt;p&gt;Let's take a form of inputs as an example. Some inputs are going to require a long string of text from the user, with a high amount of variance, such as an Email Address. Some are not so long, such as a name field. If elements can fit on the same line, then you would want them to, especially if the inputs are contextual to each other in the form. Then they become full width on their own line if the available screen width starts to narrow.&lt;/p&gt;

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

&lt;p&gt;Resize your screen, change your screen's orientation on mobile, or open up the HTML/CSS tab to narrow the form width. You'll see that the inputs move to their own line!&lt;/p&gt;

&lt;p&gt;The secret is the power of &lt;strong&gt;flex: auto&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;max-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nc"&gt;.fill&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nf"&gt;#fname&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;240px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nt"&gt;input&lt;/span&gt;&lt;span class="nf"&gt;#email&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;350px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;flex-wrap: wrap&lt;/code&gt; set onto the container, the child elements will attempt to drop down to a new line once it gets too narrow. If no breakpoint has been set, then it will use its content and padding to determine it's minimum width. &lt;/p&gt;

&lt;p&gt;In this example, the telephone field and the submit button are both set to &lt;code&gt;flex: auto&lt;/code&gt;, attached to the class name &lt;code&gt;fill&lt;/code&gt;. The name and email set with pixel widths via &lt;code&gt;flex-basis&lt;/code&gt;, the third value of the &lt;code&gt;flex&lt;/code&gt; shorthand. This width is the basis for which it can determine that it will grow to full width (set by the 1 in &lt;code&gt;flex: 1 0 ___px&lt;/code&gt;) if the space is available to do so.&lt;/p&gt;

&lt;p&gt;And finally, setting &lt;code&gt;max-width: 100%&lt;/code&gt; will prevent all inputs from exceeding the width of the container. Quite unsightly!&lt;/p&gt;

&lt;p&gt;So, as long as you know what elements will be last on their line, you can allow the first element on the line to determine when you want everything to drop onto its own line. Pretty neat!&lt;/p&gt;

&lt;h2&gt;
  
  
  Grid - When your elements don't vary
&lt;/h2&gt;

&lt;p&gt;Grid is the cool kid on the block. Flexbox fanatics are scared of it, but I find it sneaking it's way into many different solutions to common problems when building websites. In this case, it can also allow for a responsive layout, but with fewer lines of code than flexbox.&lt;/p&gt;

&lt;p&gt;Grid works exceptionally well when all the items you want to make responsive are of equal width and size, like a gallery of images or a grid of blog posts.&lt;/p&gt;

&lt;p&gt;You can achieve this layout with a complicated-looking &lt;code&gt;grid-template-columns&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.grid&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;grid&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="py"&gt;grid-template-columns&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auto-fill&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minmax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;250px&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="n"&gt;fr&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(The 250px can be set to allow more columns to fit onto a single line at once. You can even set it to &lt;code&gt;rem&lt;/code&gt; if you want to change the minimum size with media queries later)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;repeat&lt;/code&gt; applies this resizing logic to all grid items&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;auto-fill&lt;/code&gt; attempts to fill up the grid row. &lt;code&gt;auto-fit&lt;/code&gt; will ignore the container's full width and look at the minimum width of the item instead. Both are worth using for different situations! For a more in depth look at this, read &lt;a href="https://css-tricks.com/auto-sizing-columns-css-grid-auto-fill-vs-auto-fit/" rel="noopener noreferrer"&gt;Sara Soueidan's article on CSS Tricks&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;minmax&lt;/code&gt; allows you to decide what is the minimum and maximum width that the grid item can have. In my example, it can be as narrow as 250px and as wide as the container will allow. Since only 2 items can fit at 500px, at 450px a single item will stretch the entire width.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The main advantage over flexbox here is the inclusion of &lt;code&gt;grid-gap&lt;/code&gt; which removes a lot of stress with creating whitespace around your grid items. Bootstrap likes to have a gap of 15px around it's columns, but I feel that working in multiples of 8 is easier to scale upwards depending on your page content.&lt;/p&gt;

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

&lt;p&gt;If CSS Grid is absolutely alien to you but you're eager to dive into it, I strongly recommend bookmarking &lt;a href="https://gridbyexample.com/" rel="noopener noreferrer"&gt;Grid by example by Rachel Andrew&lt;/a&gt; and following &lt;a href="https://twitter.com/rachelandrew" rel="noopener noreferrer"&gt;Her on Twitter&lt;/a&gt;. &lt;/p&gt;

&lt;h3&gt;
  
  
  Flex is for when grid isn't suitable
&lt;/h3&gt;

&lt;p&gt;At this moment in time, the CSS grid spec struggles to "auto-fill" individual grid items. &lt;code&gt;grid-column&lt;/code&gt; allows you to span your grid item an unfixed number of columns, but it does not take into account other items on the same line. If you know where your dynamic grid item will start will, then you can do...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.grid-item&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;grid-column&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;-1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which will stretch the item to the end of the line from the 4th column. But as you can see, you would have to be aware of explicit grid item(s) previously on the same line. That's not at all responsive!&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

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