<?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: Alexander Antoniades</title>
    <description>The latest articles on DEV Community by Alexander Antoniades (@alexantoniades).</description>
    <link>https://dev.to/alexantoniades</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%2F46447%2Ffd6fc1f2-4a77-4364-99a9-0767a7a6ae5a.jpg</url>
      <title>DEV Community: Alexander Antoniades</title>
      <link>https://dev.to/alexantoniades</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alexantoniades"/>
    <language>en</language>
    <item>
      <title>Call, Apply, Bind - The Basic Usages</title>
      <dc:creator>Alexander Antoniades</dc:creator>
      <pubDate>Tue, 23 Apr 2019 15:16:17 +0000</pubDate>
      <link>https://dev.to/alexantoniades/call-apply-bind-the-basic-usages-5gpl</link>
      <guid>https://dev.to/alexantoniades/call-apply-bind-the-basic-usages-5gpl</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0klgocDO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/aekd2c0xiy9hm2oxufk8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0klgocDO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://thepracticaldev.s3.amazonaws.com/i/aekd2c0xiy9hm2oxufk8.png" width="800" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this article I'm going to show you some of the basic usages of call/apply &amp;amp; bind and  the problems they solve. I will also show you some practical examples in order to start using them in your projects.&lt;/p&gt;

&lt;p&gt;Call/apply &amp;amp; bind are all methods within the function prototype. Both methods are doing the same thing but slightly differently. What they actually do is allows us to call a function with a given &lt;em&gt;this&lt;/em&gt; context and arguments. They let us call a function and have access to the properties of another function or object. We can borrow methods of one object’s prototype and use it to another, for example we could apply Array’s &lt;em&gt;slice&lt;/em&gt; method to a string or use Math’s &lt;em&gt;max&lt;/em&gt; to find the maximum number of a bunch of numbers in an Array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;WHAT IS "THIS"?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'm not gonna dive into much details here but understanding the &lt;em&gt;this&lt;/em&gt; keyword is crucial to grasp the concept of call/apply &amp;amp; bind. &lt;/p&gt;

&lt;p&gt;Everything in javascript is an object, functions are objects too (first-class objects). Every object(or function) has a &lt;em&gt;this&lt;/em&gt; object assigned to it. &lt;em&gt;this&lt;/em&gt; object acts as a reference to the object's variables and methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = {
  name: "Alex",
  getName: function() {
    return `My Name is: ${this.name}`;
  }
}

obj.getName(); // "My Name is: Alex"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;this.name&lt;/em&gt; is a reference to the object's(obj) &lt;em&gt;name&lt;/em&gt; property value. In other words &lt;em&gt;this&lt;/em&gt; refers to the properties of this particular object.&lt;/p&gt;

&lt;p&gt;But what if we try to access the &lt;em&gt;name&lt;/em&gt; variable outside of the object?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let name = "Alex";
let obj = {
  getName: function() {
    return `My Name is: ${this.name}`;
  }
}

obj.getName(); // My Name is: Undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we get &lt;em&gt;undefined&lt;/em&gt; because &lt;em&gt;name&lt;/em&gt; is no longer within our local variable environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CALL METHOD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Call accepts a &lt;em&gt;this&lt;/em&gt; value and a list of arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.call(this, arg1,arg2,...)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's say we have an object with a &lt;em&gt;food&lt;/em&gt; property key and a &lt;em&gt;favFood&lt;/em&gt; function that accepts a string as an argument. In order for &lt;em&gt;favFood&lt;/em&gt; to have access to the object's &lt;em&gt;food key we need to call favFood using the call method and give it the _this&lt;/em&gt; context of the &lt;em&gt;obj&lt;/em&gt;. In simple words we need to attach the &lt;em&gt;obj&lt;/em&gt; to &lt;em&gt;favFood:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = { food: "Pizza" }

function favFood(text) {
  return `${this.food} ${text}`;
}

let text = "is awesome!";
favFood.call(obj, text); // "Pizza is awesome!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we've just passed the object (&lt;em&gt;obj&lt;/em&gt;) as a first parameter to the call method -therefore it's context (&lt;em&gt;this&lt;/em&gt;)- and a single argument as a second parameter to our &lt;em&gt;favFood&lt;/em&gt; function. This way we have access to any method and property of this particular object (&lt;em&gt;obj&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;We could also pass multiple arguments seperated by a comma.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;APPLY METHOD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Apply is the same as call but instead, apply accepts a single array of arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Function.apply(this, array)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = { 
  add: function(a,b,c) {
    return a + b + c;
  }
}

function sumItUp(a,b,c) {
  return this.add(a,b,c);
}

let numbers = [1,2,3];
sumItUp.apply(obj, numbers); // 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;BIND METHOD&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Τhe tricky part about bind is that it has the same functionality as apply but instead of calling the function immediately, it returns a bound function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let obj = { 
  add: function(a,b,c) {
    return a + b + c;
  }
}

function sumItUp(numbers) {
  return this.add(...numbers);
}

let numbers = [1,2,3];
let bound = sumItUp.bind(obj); // Returns a bound function
bound(numbers) // 6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case we pass the &lt;em&gt;obj&lt;/em&gt; to the sumItUp function -in order to have access to the &lt;em&gt;obj&lt;/em&gt; context - then we call the bound function and pass an array of numbers as an argument. Nice thing about bind is that you can call the returned bound function whenever you need.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;BORROWING PROTOTYPE METHODS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The cool thing about these methods (call, apply, bind) is that we can borrow methods and functionality from other object's prototype.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Borrowing Max from the Math Object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's say we have an array of numbers and we need to find the maximum number within the array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numArray = [1,3,4,5,6];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we know that the &lt;em&gt;Math&lt;/em&gt; object has a method for finding the minumum and maximum values of a list of numbers, but arrays are not supported because they're not considered as numbers therefore are not a valid parameter. If we try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numArray = [1,3,4,5,6];
Math.max(numArray); // NaN
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll get NaN (Not a Number) and that's totally normal beause an array is not a number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array === Number // False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But here's where the cool part comes, by using apply we can pass our array as an argument to the &lt;em&gt;Math&lt;/em&gt; object like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numArray = [1,2,3,4,5];
Math.max.apply(null, numArray); // 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we pass &lt;em&gt;null&lt;/em&gt; as the first argument because we don't need to pass any context to the &lt;em&gt;max&lt;/em&gt; method, instead we only use the second argument to pass our array which is going to be converted into arguments and finally be accepted by max as a valid parameter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Borrowing Filter from the Array Object&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now let's say we have some letters and we'd like to filter out some of them and store them in an array using the Array's filter method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let letters = 'abcdef';
let arrayFilteredLetters = letters.filter(letter =&amp;gt; letter);
console.log(arrayFilteredLettes); // Filter is not a function.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get &lt;em&gt;filter is not a function&lt;/em&gt; because &lt;em&gt;letters&lt;/em&gt; are a String object thus it doesn't have access to Array's &lt;em&gt;filter&lt;/em&gt; method. What we could do is use &lt;em&gt;call&lt;/em&gt; again to invoke &lt;em&gt;filter&lt;/em&gt; and pass &lt;em&gt;letters&lt;/em&gt; as an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let letters = 'abcdef';
let arrayFilteredLetters = Array.prototype.filter.call(letters, letter =&amp;gt; letter !== 'c' &amp;amp;&amp;amp; letter !== 'd');
console.log(arrayFilteredLetters); // [ 'a', 'b', 'e', 'f' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you see, we can use call/apply &amp;amp; bind to borrow methods from one object’s prototype and use it to another. This is one of the coolest applications of call/apply &amp;amp; bind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important notes about Arrow Functions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In case of arrow functions our methods: Call/Apply &amp;amp; Bind doesn’t work as expected.&lt;/p&gt;

&lt;p&gt;As the documentation of &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions" rel="noopener noreferrer"&gt;MDN&lt;/a&gt; states:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"Since arrow functions do not have their own this, the methods call() or apply() can only pass in parameters. thisArg is ignored."&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Arrow functions doesn't have their own &lt;em&gt;this&lt;/em&gt;. &lt;em&gt;This&lt;/em&gt; is lexically bound and it uses the &lt;em&gt;this&lt;/em&gt; of the context in which the arrow function was called. Call/Apply &amp;amp; Bind can be used only to pass parameters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CONCLUSION&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By now you should be able to understand the basic usage and applications of call/apply &amp;amp; bind and be able to attach different contexts to functions and objects. You’ll also be able to borrow methods from other prototypes and use it to pass unrelated values -like looping over a string using array’s filter-&lt;/p&gt;

&lt;p&gt;In javascript there are countless ways to do something. All the examples above has many alternative syntaxes and methods to have the same results. In this article I decided to use simple examples in order to make sure you get the basic knowledge in the most simplest way.&lt;/p&gt;

&lt;p&gt;Have Fun!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>call</category>
      <category>apply</category>
      <category>bind</category>
    </item>
    <item>
      <title>CSS Layout Centering Techniques</title>
      <dc:creator>Alexander Antoniades</dc:creator>
      <pubDate>Tue, 06 Feb 2018 20:33:21 +0000</pubDate>
      <link>https://dev.to/alexantoniades/css-layout-centering-techniques--608</link>
      <guid>https://dev.to/alexantoniades/css-layout-centering-techniques--608</guid>
      <description>&lt;p&gt;Centering elements with CSS sometimes can be tricky. There are plenty of techniques that you could use but what technique you should use depends on the element and the content. Some questions that you might ask yourself when trying to center an element could be:&lt;/p&gt;

&lt;ul&gt;
    &lt;li&gt;Is it an inline or a block element?&lt;/li&gt;
    &lt;li&gt;Is it just one line of text or multiple lines?&lt;/li&gt;
    &lt;li&gt;Does it have a fixed width and height or its size is unknown?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are likely the most common questions to ask yourself when you want to center an element.&lt;/p&gt;

&lt;p&gt;I'm gonna divide the techniques by horizontal, vertical and both.&lt;br&gt;
Let's get to the code.&lt;/p&gt;

&lt;h2&gt;&lt;b&gt;Horizontal Centering&lt;/b&gt;&lt;/h2&gt;

&lt;p&gt;&lt;b&gt;Text-align&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;&lt;span&gt;That's the simplest horizontal centering technique that you could use for text and inline-* elements.&lt;/span&gt;&lt;/p&gt;

&lt;pre&gt;p { text-align: center; }&lt;/pre&gt;

&lt;p&gt;In case you want to center an inline element you should apply it on its parent and not directly on that element:&lt;/p&gt;

&lt;pre&gt;.parent { text-align: center; }&lt;/pre&gt;

&lt;pre&gt;&amp;lt;div class="parent"&amp;gt;
 &amp;lt;strong&amp;gt;I'm Centered&amp;lt;/strong&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;
&lt;span&gt;In case we have an inline-block element we could make it full width and apply &lt;code&gt;text-align: center;&lt;/code&gt; on it directly instead of it's parent.&lt;/span&gt;
&lt;pre&gt;strong {
 display: inline-block; 
 width: 100%;
 text-align: center;
}&lt;/pre&gt;
&lt;a href="https://codepen.io/AlanFall/pen/aqOGXw/"&gt;Pen&lt;/a&gt;
&lt;br&gt;

&lt;strong&gt;Margin Auto&lt;/strong&gt;

This is the most common technique for centering horizontally block elements, it's widely used by many frameworks like &lt;a href="https://getbootstrap.com/"&gt;Bootstrap&lt;/a&gt;. You just set the left and right&lt;code&gt;margin&lt;/code&gt; to auto, make sure you have also set a width:
&lt;pre&gt;.block-box { 
 width: 600px;
 margin: auto;
}
&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/zRGaoP/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Absolute Position&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A common technique for both horizontal and vertical centering is using an absolute positioned element as child of a relative parent.&lt;/p&gt;

&lt;p&gt;What we do is basically position our child element left by 50% and we shift it back by half of its size using a negative 50% &lt;code&gt;translateX&lt;/code&gt; in order to get it centered.&lt;/p&gt;

&lt;pre&gt;.parent { position: relative; }
.centered-element {
 position: absolute;
 left: 50%;
 transform: translateX(-50%);
}&lt;/pre&gt;
&lt;pre&gt;&amp;lt;div class="parent"&amp;gt;
 &amp;lt;div class="centered-element"&amp;gt;I'm Centered!&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;

&lt;p&gt;That's also a nice way to center elements of unknown width (and height).&lt;/p&gt;

&lt;p&gt;An alternative to that, -if you have elements of known width- would be to use a negative &lt;code&gt;margin-left&lt;/code&gt; instead of &lt;code&gt;translateX&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;.centered-element { 
 position: absolute;
 width: 600px;
 left: 50%;
 margin-left: -300px; // Shift it back by half of it's size.
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/VQLdOo/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don't care about support of older browsers, a nice way to horizontally(and vertically) center an element or a bunch of elements is by using flex.&lt;/p&gt;

&lt;pre&gt;.parent { 
 display: flex; 
 justify-content: center;
 height: 100vh;
}&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Make sure you haven't set the &lt;code&gt;flex-direction&lt;/code&gt; property to column otherwise you'll have the opposite effect (vertical center).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/GQJBpZ/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Vertical Centering&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Absolute Position&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Again, we could use the same absolute positioning technique we used previously but instead of setting the left position to 50% we set the top position to 50% and we shift it up using a negative 50% &lt;code&gt;translateY&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;.parent { position: relative; }
.centered-element {
 position: absolute;
 top: 50%;
 transform: translateY(-50%);
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/VQLBjY/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table Cell&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A nice way for vertical centering would be to use vertical-align: middle. Of course this is tricky because you have to apply it on elements of a parent that is set to&lt;code&gt; display: table&lt;/code&gt; and children to &lt;code&gt;display: table-cell&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;.parent { 
 display: table;
 height: 100vh; 
}

.centered-element {
 display: table-cell;
 vertical-align: middle;
}&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; Make sure that the parent element has height otherwise this won't work.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/PQqBbW/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Line-height&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This technique is okay if you want to center single words and not whole paragraphs or lines of text. If you have a single word inside an element you could vertically center it by setting a fixed height on that element and a&lt;code&gt; line-height&lt;/code&gt; equal the height of the element:&lt;/p&gt;

&lt;pre&gt;.box { 
 width: 600px;
 height: 600px;
 line-height: 600px;
}&lt;/pre&gt;

&lt;pre&gt;&amp;lt;div class="box"&amp;gt;
 &amp;lt;strong&amp;gt;I'm Centered&amp;lt;/strong&amp;gt;
&amp;lt;/div&amp;gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/zRGLPQ/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Ghost Element&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this technique, we have two inline-block elements: a container with a "ghost" pseudo element set to full height and a child that is set to&lt;code&gt; vertical-align: middle;&lt;/code&gt; This way the child element aligns with the ghost element -which has also &lt;code&gt;vertical-align: middle&lt;/code&gt;&lt;/p&gt;

&lt;pre&gt;.container { font-size: 0; }
.container::before {
  content: '';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.container strong {
 display: inline-block;
 vertical-align: middle;
 font-size: 1rem;
}&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; The ghost element creates an empty space usually on the left side so make sure to add a negative margin (&lt;code&gt;margin-right: -0.25em&lt;/code&gt;) or a &lt;code&gt;font-size: 0&lt;/code&gt; on the parent container and a &lt;code&gt;font-size: 1rem&lt;/code&gt; on the child.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/xYGJWK/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We've seen how to align horizontally with flex. We can do the same for vertical centering by using the &lt;code&gt;align-items&lt;/code&gt; property.&lt;/p&gt;

&lt;pre&gt;.parent { 
 display: flex; 
 align-items: center;
 height: 100vh;
}&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;/strong&gt; again, make sure you haven't set the &lt;code&gt;flex-direction&lt;/code&gt; to &lt;code&gt;row&lt;/code&gt;, otherwise you'll have the opposite effect. (horizontal centering).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/YeXjeW/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;Horizontal &amp;amp; Vertical Centering&lt;/h2&gt;

&lt;p&gt;We can combine all the techniques we used previously to center in both axis.&lt;/p&gt;

&lt;p&gt;From all the techniques we used, the absolute positioning and the flex technique will give us both Horizontal and Vertical centering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Absolute Position&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We position the element to 50% in both top and left and we shift back in both positions by using a negative translate of 50%:&lt;/p&gt;

&lt;pre&gt;.centered-element {
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/qxdyLb/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flex&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Again, using the same flex technique we use both&lt;code&gt; align-items&lt;/code&gt; and &lt;code&gt;justify-content&lt;/code&gt; set to center.&lt;/p&gt;

&lt;pre&gt;.parent { 
 display: flex; 
 align-items: center;
 justify-content: center;
 height: 100vh;
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="https://codepen.io/AlanFall/pen/MQKxam/"&gt;Pen&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have something to add? Feel free to throw it on the comments section.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Thanks to &lt;a href="http://www.pvgr.eu/" rel="noopener"&gt;PVGR&lt;/a&gt; for the useful info and corrections.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
