<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alessandro Henrique on Medium]]></title>
        <description><![CDATA[Stories by Alessandro Henrique on Medium]]></description>
        <link>https://medium.com/@alessandro.henrique?source=rss-63f98f7cdf57------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*GJFjGbB58-w6hA9g1zuW6g.jpeg</url>
            <title>Stories by Alessandro Henrique on Medium</title>
            <link>https://medium.com/@alessandro.henrique?source=rss-63f98f7cdf57------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 12:56:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alessandro.henrique/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to Django Channels]]></title>
            <link>https://medium.com/labcodes/introduction-to-django-channels-d1047e56f218?source=rss-63f98f7cdf57------2</link>
            <guid isPermaLink="false">https://medium.com/p/d1047e56f218</guid>
            <category><![CDATA[django-channels]]></category>
            <category><![CDATA[d3js]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Alessandro Henrique]]></dc:creator>
            <pubDate>Tue, 28 Nov 2017 01:49:48 GMT</pubDate>
            <atom:updated>2017-11-28T01:58:55.188Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*W6_wPkhuJGnsf59nGUBHTw.png" /></figure><p>In this article, we’re going to talk about Django channels and how it can be a game changer for the Django framework.</p><p>Django is an already established framework in the market, but it is synchronous and nowadays with the high demand for real time applications, It could not be left behind. Django channels comes into play to solve this issue.</p><p>Django channels’ goal is to extend the Django framework, adding to it a new layer to handle the use of WebSockets and background tasks.</p><p>To use as an example, we’re going to create a simple webapp called <a href="http://coinpricemonitor.labcodes.com.br/"><em>coinpricemonitor</em></a>, to watch the bitcoin and litecoin prices in real time. If you want to take a look at the full code, it’s <a href="https://github.com/alessandroHenrique/bitcoinmonitor">here</a>. To create this example I used Django 1.11 and Channels 1.1.6.</p><h4>What do I have to change to use Django channels?</h4><p>All that comes with Channels is entirely optional and, as said before, It just aims to extend Django, giving it the ability to create real time applications. It would not be good if a new release of the framework would break everyone’s request system.</p><p>If Django Channels was in the core of Django already, as it intends to be in the future, you wouldn’t need to install anything. You would only have to update your project to Django’s newest release.</p><h4>How to install</h4><p>While Channels is not in the core of Django yet, we need to install it. In order to do it, just run the command: pip install channels.</p><p>After that, add channels to your INSTALLED_APPS tuple.</p><h4>Django channels request cycle</h4><p>Using Channels, now you will have two types of requests to handle, the http requests and the websocket requests.</p><p>When the request arrives from the client, it will pass through an Interface server, a server that understands the ASGI (Asynchronous Server Gateway Interface) protocol. It will redirect the request to an ASGI router, the channel layer that will check if it’s a <strong>http request</strong>, and send it to its respective view, or a <strong>websocket request</strong>, and send it to its consumer, which is very similar to a view.</p><p>In the following sections, we will see these components that integrate the new request cycle with Channels.</p><h4>ASGI server</h4><p>Nowadays the Django framework uses a WSGI (Web Server Gateway Interface), which is an interface for python applications to handle requests. But to work with asynchronous applications, we need to use another interface, which is ASGI (Asynchronous Server Gateway Interface), that can handle websocket requests as well.</p><p>You will have to use a server that uses this approach. Good for us that Andrew Goodwin, the creator of Channels, made as well a server called Daphne. To handle this protocol as well.</p><p>Daphne already comes with Channels, so you won’t need to install it.</p><h4>ASGI router</h4><p>Now, you are handling two types of requests: the http requests that are going to be routed to views as normal, and websocket requests that are going to be routed to consumers, which are similar to your already known views. To handle both requests we need a router. It is a part of the channel layer, which is the backend of Channels.</p><p>Some brokers that can be used as routers are:</p><ul><li>asgi_r<a href="https://redis.io/">edis</a> — You will need to install R<a href="https://redis.io/download">edis</a> to use this broker.</li><li>asgi_r<a href="https://www.rabbitmq.com/">abbitmq</a> — You will need to install Rabbitmq to use this broker.</li><li>a<a href="https://github.com/django/asgiref/">sgiref</a></li></ul><p>You can find all these brokers in <a href="https://pypi.python.org/pypi">PyPI</a>, meaning that you can use easy_install or pip to install them.</p><p>Here’s an example how to setup the channel layer with the asgi_redis broker:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*H97xcwFBYtVqG2RitjldJw.png" /><figcaption>settings.py</figcaption></figure><p>It’s quite simple to setup the channels layer, here’s the meaning of each key:</p><ul><li>default — Default settings for the channel layer.</li><li>BACKEND — Broker you are going to use. In our case, asgi_redis.</li><li>CONFIG — Configurations of the channels, like hosts and capacity of each channel. You can check out all options <a href="https://github.com/django/asgi_redis">here</a>.</li><li>ROUTING — To where the incoming messages are going to be routed to.</li></ul><h4>Web workers</h4><p>They are the ones that are going to process the consumers.</p><h3>Basic concepts</h3><h4>Channel</h4><p>The main data structure in Channels is a <em>Channel</em>, and its concept is described as:</p><blockquote>Channel is a data structure that is a first-in first-out queue It is an <em>ordered</em>, <em>first-in first-out queue</em> with <em>message expiry</em> and <em>at-most-once delivery</em> to <em>only one listener at a time</em>. — <a href="https://channels.readthedocs.io/en/stable/">Channels documentation</a></blockquote><h4>Types of Channels</h4><p>One type of channel is the routing one, that depending from where the message came, it will route to the proper consumer. There are three routing channels:</p><ul><li>websocket.connect</li><li>wesocket.receive</li><li>websocket.disconnect</li></ul><p>Do you remember in the configuration of the channel layer that we set up routing? So, when a message arrives, it will be routed by one of the routing channels inside the routing.py file. We have three routing channels, one when the user is trying to start a websocket connection, another one when the user sends a message and a third for when a client wants to close the connection.</p><p>When a message arrives in the websocket.connect channel, the message will be sent to the ws_connect consumer, which will process the message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*DE3pqZqOnZd1Yp6yX50S1Q.png" /><figcaption>routing.py</figcaption></figure><p>Another type of Channel is the reply_channel , which comes with the message object. It is an identifier to knowledge from where that message came and to know where the consumer should send back its response.</p><h4>Consumers and Groups</h4><p>A consumer is a function that will receive a message (websocket request), process it and may return or not a response.</p><p>Let’s start taking a look step by step how our <em>coinpricemonitor</em> was built. First with its ws_connect consumer:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*DTZLD000P1kjzy8JVSPwOQ.png" /><figcaption>Generic consumer</figcaption></figure><p>This consumer receives a message and replies with a flag accept which will tell that the browser should accept the connection. We can return just a few flags besides accept, let’s take a look at them:</p><p><strong>accept</strong> — To accept a new websocket connection.</p><p><strong>text</strong> — To send a string, but you can import json and use json.dumps() to send a dictionary.</p><p><strong>bytes</strong> — Send encrypted data to the frontend, in order to not let others see what you are sending.</p><p>In our case, We are using the accept key, so when the message is sent, it arrives in the onOpen socket method in the frontend and will print into the console the message “Connected to websocket!”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*-qQrYLoctUO3DakJVCx6_Q.png" /></figure><p>Notice that we used the method send to send a response to the client, but this method is not from the message object, it’s from the message’s attribute reply_channel. This is another type of channel that represents the user’s channel, to inform which channel send back the response.</p><p>As every client has its reply_channel, when we have to send the new price to each user, we would need to keep track of the users connected in a list, and send them the new price. But if we wanted to separate users in groups, like in rooms in a chat in real time, we’d have to keep track of different lists with channels for each room. That’d be hard to do.</p><p>Fortunately, Channels has a way to solve that problem with a data structure called Group. We create a group and add in it all channels, so when a update is required, we just need to send the message to the group.</p><p>Let’s create a new group called “btc-price” and add the user’s channel to it when he connects to the server:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*t2_N0Y2-mNNMZV4VTmlVAA.png" /><figcaption>ws_connect using Group</figcaption></figure><p>Now that we have a way to group channels, we actually are going to need one more group, because besides showing the bitcoin price, we can change to see the litecoin (which is another cryptocurrency) price. A group of channels is going to receive the bitcoin price, and the other one is going to receive the litecoin price.</p><p>It is necessary to keep track in which group the user is. To accomplish that, we’re going to store in the user’s session the name of the group.</p><p>Here the channel_session_user is imported, It is a channel decorator that gets a Django session for us and enables it in the message object with the attribute channel_session. So a new key called coin-group is added with the value btc-price to keep track in which group the user’s channel is in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*aM6VuP6NFAfygnEwEo07Gg.png" /><figcaption>Final ws_connect consumer</figcaption></figure><p>Then the new bitcoin price is sent to the frontend. I did it through a periodic task on celery, to make a simple get request to the <a href="https://bittrex.com/">bittrex</a> API to get the last bitcoin price and broadcast that price to the btc-price group.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*eMI_IOZmvICw-qLu7SbSjw.png" /><figcaption>tasks.py</figcaption></figure><p>Here’s the get_coin_price helper function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/769/1*b-QxB_J8j4ewI089Etsk5A.png" /><figcaption>helpers.py</figcaption></figure><p>Now that you know how channels handles a connection, what about the frontend? How to get to the ws_connect consumer? When the page loads, It’s required to create a websocket connection to the backend, to start receiving the bitcoin current price in real time. After the load event is triggered, the setupWebsocket function will be called, which will create a WebSocket object with a url, if the project is running locally, you can use ws://localhost:8000/ws/. We setup socket methods for when the connection is established (onopen) and when a message comes from the backend (onmessage).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*yHF5Ter_DhVy6oY70DcblA.png" /><figcaption>main.js</figcaption></figure><p>After the creation of a WebSocket object, it will try to connect to its Url. This message will arrive in the channel layer, which will be routed to the websocket.connect channel, leading to the ws_connect consumer.</p><p>Another flow from the frontend to the backend is when you click in the button to see the litecoin price. In resume, the bitcoin price and icon are being hidden and switched for the litecoin ones. The main part is in the end:websocket.send(). This is a websocket method that enables data to be sent through it. A flag is being sent to let the backend knows that the litecoin price is wanted, not bitcoin anymore.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*XnnjhsTbvNi9s6yeMvidZQ.png" /><figcaption>main.js</figcaption></figure><p>Since the data is being sent, the message will be routed to the websocket.receive channel, leading to the ws_receive consumer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*DE3pqZqOnZd1Yp6yX50S1Q.png" /><figcaption>routing.py</figcaption></figure><p>Here we get the data that was sent from the frontend and make an if statement in order to check either the user wants the litecoin price or the bitcoin price. If the user wants the litecoin price, we will remove the user from the group of channels that receives the bitcoin price and put the user’s channel in the group that receives the litecoin price and change the value of the key coin-group ltc-price to keep track of the user’s channel. In the other hand, if the client wants the bitcoin price, we put the client’s channel in the btc-price group and discard the channel from the ltc-price group and change the value of the key coin-group to btc-price.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*ZvEVuIVo2tGjMjdoP7hnjg.png" /><figcaption>consumers.py</figcaption></figure><p>After your group is changed, a new value is received in the onMessage socket method in the frontend.</p><p>Depending of which type of coin value was received, it will need to be changed in the proper element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*36KolmJ5wX96JdChzRyLXQ.png" /><figcaption>main.js</figcaption></figure><p>The last flow is the disconnection flow. It’s triggered when the browser tab is closed. Doing so, the Websocket object in the frontend will trigger a disconnect event to the backend to finish the websocket connection.</p><p>The message will be routed to the websocket.disconnect channel, which will lead to the ws_disconnect consumer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*XLcvB9zQH5mhMH1ZxdZl8A.png" /><figcaption>routing.py</figcaption></figure><p>The ws_disconnect just removes the user’s channel from the last group that it was in. In order to know which group it was, we check the value in the coin-group stored key is checked.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/517/1*9bimglrwA-BgjXRamZYb1Q.png" /><figcaption>consumers.py</figcaption></figure><h4>Conclusion</h4><p>Channels is a really powerful library for real time applications, as shown, and this was just a small taste of what it is capable of doing. If you already know Django and have the need to make these types of applications, give channels a chance and see how it works for you.</p><p>That’s basically all you need to know to start with Django channels. If you got interested, here are some links that might be helpful:</p><ul><li><a href="https://blog.heroku.com/in_deep_with_django_channels_the_future_of_real_time_apps_in_django">Another amazing tutorial that builds a chat in real time</a></li><li><a href="http://channels.readthedocs.io/en/stable/">Channels documentation</a></li><li><a href="https://github.com/django/channels">Django channels github</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d1047e56f218" width="1" height="1" alt=""><hr><p><a href="https://medium.com/labcodes/introduction-to-django-channels-d1047e56f218">Introduction to Django Channels</a> was originally published in <a href="https://medium.com/labcodes">Labcodes Software Studio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing Pythonic code Tip #2: Lambda functions]]></title>
            <link>https://medium.com/@alessandro.henrique/writing-pythonic-code-tip-2-lambda-functions-c74ac8869d6d?source=rss-63f98f7cdf57------2</link>
            <guid isPermaLink="false">https://medium.com/p/c74ac8869d6d</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-programming]]></category>
            <dc:creator><![CDATA[Alessandro Henrique]]></dc:creator>
            <pubDate>Tue, 27 Jun 2017 17:14:08 GMT</pubDate>
            <atom:updated>2017-06-27T17:14:08.397Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qTG6ADwI-Toq6lXf3DWsUA.png" /></figure><p>Lambda functions are anonymous functions that are created at runtime, using the keyword “lambda”. Let’s take a look at their syntax:</p><blockquote>lambda arguments: expression</blockquote><p>Differently from normal functions, lambda’s arguments don’t need to be inside of parenthesis, if you do that, you’d be passing a tuple. First let’s make a simple function that adds two numbers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ff692a38f62b89264794e6340de7a86/href">https://medium.com/media/9ff692a38f62b89264794e6340de7a86/href</a></iframe><p>That same example using a lambda function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83740fa03757755f17b95d0e4ccc6ccb/href">https://medium.com/media/83740fa03757755f17b95d0e4ccc6ccb/href</a></iframe><p>After assigning a lambda function to a variable, you can use it as a normal function, the call above would print “<em>3</em>” in the console.</p><p>You can use them to evaluate a conditional expression, referred in other languages as ternary operator:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bddfed1d24c562f5a0f38a58b78dfd81/href">https://medium.com/media/bddfed1d24c562f5a0f38a58b78dfd81/href</a></iframe><p>The “<em>can_drink_beer</em>” function above would print “<em>False</em>”.</p><p>You can use them as callback functions like when you want to use the built in map function, which returns a list of the results of applying the function to the items of the argument sequence(s), like in the next example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/977cea90e54df3c5d14ec1814cb38b40/href">https://medium.com/media/977cea90e54df3c5d14ec1814cb38b40/href</a></iframe><p>The code above will print “<em>[1, 4, 9, 16, 25]</em>”.</p><p>There’s just a big drawback to lambda functions that you probably already noticed, that these functions only support a single expression in their body. Because of that, that you should use them when you want to create simple functions, using them for destructuring of objects, simple converters, callback functions.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c74ac8869d6d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing Pythonic code Tip #1: If statements with chain comparisons]]></title>
            <link>https://medium.com/@alessandro.henrique/writing-pythonic-code-tip-1-if-statements-with-chain-comparisons-36e1aa870e00?source=rss-63f98f7cdf57------2</link>
            <guid isPermaLink="false">https://medium.com/p/36e1aa870e00</guid>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Alessandro Henrique]]></dc:creator>
            <pubDate>Sun, 25 Jun 2017 21:18:35 GMT</pubDate>
            <atom:updated>2017-06-26T14:15:04.805Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RD41a80VdU3C-HfFcjAleA.png" /></figure><p>Sometimes you want to make a simple comparison like the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c297d459e61d4bb3aa08cb7cca37aa76/href">https://medium.com/media/c297d459e61d4bb3aa08cb7cca37aa76/href</a></iframe><p>This logic works not just for Python, but for other programming languages like Java, C# etc.</p><p>Using Python, there’s a more Pythonic way to do it, which is using chained comparisons, look at the same example using it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fe1a8efeed3ecfab03646d68a17bcf4/href">https://medium.com/media/1fe1a8efeed3ecfab03646d68a17bcf4/href</a></iframe><p>It simply means that the expressions are concatenated removing the “<em>and</em>” so you don’t need to write “<em>age</em>” twice, increasing the readability.</p><p>References:</p><p>Tip based from the book <a href="https://www.amazon.com/Writing-Idiomatic-Python-Jeff-Knupp-ebook/dp/B00B5VXMRG/ref=sr_1_fkmr0_2?ie=UTF8&amp;qid=1498425151&amp;sr=8-2-fkmr0&amp;keywords=idiomatic+python">Writing idiomatic Python</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36e1aa870e00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Basics of CSS grid layout]]></title>
            <link>https://medium.com/labcodes/basics-of-css-grid-layout-3a47aea089dc?source=rss-63f98f7cdf57------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a47aea089dc</guid>
            <category><![CDATA[css-grid]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Alessandro Henrique]]></dc:creator>
            <pubDate>Tue, 23 May 2017 17:42:50 GMT</pubDate>
            <atom:updated>2017-05-23T17:46:06.398Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article, we are going to take a look at some concepts and show you the power of this new way to layout complex web pages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eHJmi0Cw-UKclhAfocv_GA.png" /></figure><h3>What is the new CSS grid layout?</h3><p>It’s a <a href="https://www.w3.org/TR/css3-grid-layout/">new CSS specification</a> from W3C that finally adds a powerful way to create layouts controlling two dimensions. It includes several new CSS properties to let web developers layout the elements in the way they want, specifying where the elements should be, how much space they should occupy and their alignment to optimize the space inside the container.</p><h3>Basic concepts</h3><h4>Grid container</h4><p>The container is the wrapper of all content. Its direct children are called grid items. To create a grid container, you just need to add the property <strong>display: grid; </strong>to your container. Another option is to add the property <strong>display: inline-grid; </strong>and your container will behave like an inline element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*2w9g3uBhYeGtbsiOi2bOyA.png" /></figure><h4>Grid items</h4><p>They are the direct children of the Grid container, consisting of the space within four lines, just like a table cell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/305/1*u0y7MWA3RE04SeyxJZRQcQ.png" /></figure><h4>Grid lines</h4><p>They are the vertical and horizontal lines that compose the grid. The following grid has 4 column lines and 3 row lines.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*ivr94P7bAPsHIUxN1zwKmA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*0DHoaEJS_aNww9_zfdFvng.png" /></figure><h4>Grid tracks</h4><p>It’s the space between two grid lines (vertically or horizontally). The following grid has 3 column tracks and 2 row tracks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*UPHpBKVlH3jcBkb4X2EUYg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/304/1*SQRXrHLCYOsI-KmePswPcQ.png" /></figure><h4>Grid area</h4><p>It’s an area that takes more than one Grid item.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/307/1*qFqw6ENJ4G-bQtLIQBGHfA.png" /></figure><h4>Let’s get our hands dirty!</h4><p>Now that we already know the basic concepts, let’s write some code to see them in action.</p><p>The class <strong>container </strong>has the property <strong>display: grid; </strong>to turn it into a Grid container. Since we didn’t specify any columns or rows tracks, we got only 1 column with each Grid item in it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/215e0fc10605c2e9359d6e787babfbda/href">https://medium.com/media/215e0fc10605c2e9359d6e787babfbda/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*6exbgAQNJhfDr6bjvdHIgw.png" /></figure><p>If you want to see the example working, check it here: <a href="http://codepen.io/darknessHO/pen/JWLXeO?editors=1100">http://codepen.io/darknessHO/pen/JWLXeO?editors=1100</a></p><p>To create columns and rows, we will need to use the properties <strong>grid-template-columns</strong> and <strong>grid-template-rows. </strong>Use the properties <strong>grid-column-gap</strong> and <strong>grid-row-gap </strong>to set the gap between columns and rows respectively or you can use the shorthand <strong>grid-gap</strong> to specify both sizes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef68dcc33deacec571ca008215028371/href">https://medium.com/media/ef68dcc33deacec571ca008215028371/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*j8E5A2zi-VYatWdyf2Ukdw.png" /></figure><p>If you want to see the example working, check it here: <a href="http://codepen.io/darknessHO/pen/bqMeKV">http://codepen.io/darknessHO/pen/bqMeKV</a></p><p>We can position the Grid item based on the Grid line using the properties <strong>grid-column-start, grid-column-end </strong>for columns and <strong>grid-row-start</strong>, <strong>grid-row-end</strong> for rows. There are also the shorthands for them, <strong>grid-column</strong> and <strong>grid-row. </strong>Take a look at the following example where we get four Grid items from column 1 to 3 and from row 1 to 3, creating a Grid Area.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6752d8afe7c50fdcaa2e037be4927849/href">https://medium.com/media/6752d8afe7c50fdcaa2e037be4927849/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*H9EqQVHOzbgoAMe0sqcyMw.png" /></figure><p>If you want to see the example working, check it here:</p><p><a href="http://codepen.io/darknessHO/pen/GWdqPK?editors=1100">http://codepen.io/darknessHO/pen/GWdqPK?editors=1100</a></p><p>If you want to create a row with flexible items taking the same space or more space in relation to the others, you can use the fraction unit <strong>fr</strong>, which is a fraction of the available space. In the next example, we can see that the first column has 200px and it’s not flexible, while the middle column is 2fr, which means that the middle column will take the double of the available space in relation to the right column, that has 1fr.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/295743ed2f78f832cd908534110f30da/href">https://medium.com/media/295743ed2f78f832cd908534110f30da/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*x1Hl6s_3me7xKvjFq-VZWA.png" /></figure><p>If you want to see the example working, check it here:</p><p><a href="http://codepen.io/darknessHO/pen/KWRJwx?editors=1100">http://codepen.io/darknessHO/pen/KWRJwx?editors=1100</a></p><p>With the property <strong>grid-template-areas </strong>you can create a simple layout by just writing the names of the grid areas where you want to place them. In the following example we defined 5 grid areas, with the property <strong>grid-area </strong>(header, left-sidebar, article, right-sidebar and footer). Now take a look at the <strong>grid-template-areas</strong> property and how it is used. You decide which grid area goes in which column and it’s done, it’s almost like a visual editor in your code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/98c4d8e0ee5928afc70d560040f7f6ab/href">https://medium.com/media/98c4d8e0ee5928afc70d560040f7f6ab/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*uBZovdx05cFZnJd2DlTQeQ.png" /></figure><p>If you want to see the example working, check it here:</p><p><a href="http://codepen.io/darknessHO/pen/zZbxGe?editors=1100">http://codepen.io/darknessHO/pen/zZbxGe?editors=1100</a></p><h4>Support</h4><p>The grid layout is already fully supported by Google Chrome, Firefox, Safari and Opera, and partially supported by Edge 15 and IE 11.</p><p>See for yourself: <a href="http://caniuse.com/#search=grid">http://caniuse.com/#search=grid</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a47aea089dc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/labcodes/basics-of-css-grid-layout-3a47aea089dc">Basics of CSS grid layout</a> was originally published in <a href="https://medium.com/labcodes">Labcodes Software Studio</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>