How to Center Elements using CSS Flexbox Properties

How to Center a Div with CSS

There are a few common coding problems you might encounter when you start practicing what you've learned by building projects.

One common problem you'll face as a web developer is how to place an element at the center of a page or within an element acting as its container. It's the ubiquitous "How do I center a div?" problem.

In this article, we'll see how we can center elements using various CSS properties. We'll see code examples in each section and a visual representation of the elements in all the examples.

How to Center a Div Using the CSS Flexbox Property

In this section, we'll see how we can use the CSS Flexbox property to center an element horizontally, vertically, and at the center of a page/container.

You can use an image if you prefer that, but we'll just use a simple circle drawn with CSS. Here's the code:

<div class="container">

      <div class="circle">

      </div>

</div>
.container {
  width: 500px;
  height: 250px;
  margin: 50px;
  outline: solid 1px black;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: black;
}

Screenshot--276-

Positioning with Flexbox requires that we write the code in the parent or container element's class.

How to Center a Div Horizontally Using Flexbox

Now we'll write the code to center the div element horizontally. We're still making use of the circle we created above.  

.container {
  width: 500px;
  height: 250px;
  margin: 50px;
  outline: solid 1px black;
  display: flex;
  justify-content: center;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: black;
}

We've added two lines of code to center the circle horizontally. These are the lines we added:

display: flex;
justify-content: center;

display: flex; allows us to use Flexbox and its properties, while justify-content: center; aligns the circle to the center horizontally.

Here is the position of our circle:

Screenshot--278-

How to Center a Div Vertically Using Flexbox

What we'll be doing in this section is similar to the last one, except for one line of code.

.container {
  width: 500px;
  height: 250px;
  margin: 50px;
  outline: solid 1px black;
  display: flex;
  align-items: center;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: black;
}

In this example, we used align-items: center; to center the circle vertically. Recall that we are required to write display: flex; first before specifying the direction.

Here's the position of our circle:

Screenshot--280-

How to Position a Div at the Center Using Flexbox

In this section, we'll position the circle at the center of the page using both the horizontal and vertical alignment properties of CSS Flexbox. Here's how:

.container {
  width: 500px;
  height: 250px;
  margin: 50px;
  outline: solid 1px black;
  display: flex;
  justify-content: center;
  align-items: center;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: black;
}

Here are the three lines of code we added to the container class above:

display: flex;
justify-content: center;
align-items: center;

As expected, we begin with display: flex; which allows us to use Flexbox in CSS. We then used both the justify-content (horizontal alignment) and align-items (vertical alignment) properties to position the circle at the center of the page.

Here is the position of our circle:

Screenshot--282-

How to Center a Div Horizontally Using the CSS margin Property

In this section, we'll be using the margin property to center our circle horizontally.

Let's create our circle again.

<div class="container">

      <div class="circle">

      </div>

</div>
.container {
  width: 500px;
  height: 250px;
  margin: 50px;
  outline: solid 1px black;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: black;
}

Screenshot--276-

This time we'll write the code in the circle class. Here's how:

.container {
  width: 500px;
  height: 250px;
  margin: 50px;
  outline: solid 1px black;
}

.circle {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: black;
  margin: 0 auto;
}

All we've added is the margin: 0 auto; line of code to the circle class.

Let's have a look at the position of the circle:

Screenshot--278--1

How to Center Text Horizontally Using the CSS text-align Property

In this section, we'll see how to center text horizontally.

This method only works when we are working with text written within an element.

Here is an example:

<div class="container">

    <h1>Hello World!</h1>
      
</div>

In the example above, we have created a div with a class of container and a h1 element with some text. This is what it looks like at the moment:

Screenshot--272-

Let's write the CSS code.

.container {
  width: 500px;
  height: 250px;
  margin: 50px;
  outline: solid 1px black;
}

h1 {
  text-align: center;
}

In other to align the text in the h1 element at the center of the page, we had to use the text-align property, giving it a value of center. Here's what it looks like now in the browser:

Screenshot--274-

Conclusion

In this article, we saw how we can center elements horizontally, vertically, and at the center of the page using Flexbox and the margin and text-align properties in CSS.

In each section, we saw both a code example and a visual representation of what the code does.

Happy coding!

Original article source at https://www.freecodecamp.org

#css #programming #developer 

What is GEEK

Buddha Community

How to Center Elements using CSS Flexbox Properties
Aisu  Joesph

Aisu Joesph

1618024175

CSS Alignment Made Simple

CSS is seen as an impediment in web development for many of us. Most of the time it looks like even when you follow the rules and everything seems clear, it still doesn’t work the way you want it to.

Therefore, the purpose of this article is to make some features of CSS much easier to understand.

The thing I want to address now is the alignment of the elements.

Without further ado, here are some of the most common scenarios one might encounter when it comes to this topic and how they can be approached.

#css-center #css-position #css-flexbox #css-center-image-in-a-div #css

CSS Flexbox: What I learned from Flexbox Zombies

I started playing an educational game called Flexbox Zombies, which has been teaching me the fundamentals of flexbox in a fun way. In the game, you fight zombies by using features of flexbox to aim your crossbow at the zombies.

MDN docs explain flexbox as:

a one-dimensional layout method for laying out items in rows or columns. Items flex to fill additional space and shrink to fit into smaller spaces.

The Flexbox Zombies game teaches flexbox through a story, with each lesson building on the previous, thus reinforcing the fundamentals of flexbox in a fun and effective way.

#flexbox #css #css flexbox #flexbox zombies

anita maity

anita maity

1618667723

Sidebar Menu Using Only HTML and CSS | Side Navigation Bar

how to create a Sidebar Menu using HTML and CSS only. Previously I have shared a Responsive Navigation Menu Bar using HTML & CSS only, now it’s time to create a Side Navigation Menu Bar that slides from the left or right side.

Demo

#sidebar menu using html css #side navigation menu html css #css side navigation menu bar #,pure css sidebar menu #side menu bar html css #side menu bar using html css

CSS Flex Box: A Flexible Way To Layout

Every element of HTML is a rectangular box. Every Box has a defined height and width. This way you can increase or decrease its size. CSS is used to style HTML elements so that they look nice and decorated. CSS treats every element in the view of its box model. So every element has padding, margin, and border too.

You can learn more about CSS BOX Model here.

Box layout means to position a box on the page. So you may like to center an element horizontally or vertically or you may want to move the element to any other position on the page. Laying out your page is the most important task which determines the overall look of the page.

CSS has got many ways to align a box. You could choose floats, position property or you could try aligning it using margin and padding. But it’s not always so easy to align an element as you wish to. Developers have always been having difficulties to center an element horizontally or vertically. If you try using floats, you will see that it requires more work and gives you extra lines of code to position the element. So what’s the way out?

Here comes the modern CSS Flex Box technique. After using Flex Box for the first time you will forget the difficulties you have been facing with your layout. You will make your layout with fewer lines of code and very quickly.

Now after having Flex Box in your hand you don’t need to worry about every single element in your container. What you need is just add one or two lines of code and there you go.

What Are Some Of The Most Popular Uses Of Flex Box?

You can use Flex Box Almost anywhere on your website to align your content, but I found it more useful to apply it on certain parts of my page than others.

1: Navigation bar

The Navigation menu is mostly a horizontal or vertical bar on top or side of the page with links to other parts of the page. You can create a container for it and apply Flex Box to it so that you can move it’s items wherever it’s suitable for your page layout.

2: Footer

Footer of a website mostly includes contact details, logo, and some links to other parts of the site. You can align your footer content with the help of Flex Box too.

3: Horizontal Alignment

You can align your container’s elements on the horizontal line wherever you like and can add space in them.

4: Vertical Alignment

It often requires to position elements vertically, so there is a very easy way to achieve it with Flex Box. You just need to add one line and it’s already done.

4: Re-Ordering Elements

Flex Box has a function that allows you to rearrange the order of your elements in a container. You can change the order of any element you like.

#web-development #technology #css3 #flexbox #css #html-css #learning-css #learn-flexbox-css

Easter  Deckow

Easter Deckow

1625669220

How to Center Text (or any element) using CSS Flexbox

Learn how to center text, or any other element inside its parent element using CSS Flexbox properties.

#css flexbox #css