Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic: How to center a div?

THE SITUATION:

I am making an app using Ionic framework. In one page i need to display an image. This image has to be horizontally centered.

ATTEMPT 1:

Following the documentation i have divided one row into three equal columns and put the image in the second one.

 <div class="row">     <div class="col col-33"></div>     <div class="col col-33">         <img src="{{ data.logo }}" alt="">     </div>     <div class="col col-33"></div>  </div> 

But unfortunately the image is far to be centered. Tend to stay in the left half of the screen.

ATTEMPT 2:

Trying with some old css tricks.

<div style="margin: 0 auto;">     <img src=" {{ data.logo }} " alt="" > </div> 

But again i am not getting the desired result.

THE QUESTION:

How can i center a div in Ionic framework?

like image 763
FrancescoMussi Avatar asked Jun 25 '15 12:06

FrancescoMussi


People also ask

How do I center a specific div?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I keep my div elements centered?

Use absolute positioning, and a transform to centre the div . The dimensions will then not matter, until the window size is smaller than the div . Show activity on this post. You are already aligning the <div> horizontally with margin: 0 auto .

How do I center an ionic label?

The new recommended way is to apply this using class="ion-text-center" .

How do I center a div image?

Step 1: Wrap the image in a div element. Step 2: Set the display property to "flex," which tells the browser that the div is the parent container and the image is a flex item. Step 3: Set the justify-content property to "center." Step 4: Set the width of the image to a fixed length value.


1 Answers

You already found your answer but I would do something like that instead:

In your css:

.center {     margin-left: auto;     margin-right: auto;     display: block; } 

And then just add this class to your image:

 <img src="{{ data.logo }}" class="center" alt=""> 

This way you don't need to adjust each image on its own and I find this very descriptive when you look at the HTML. Also, it is not restricted to a specific image size.

like image 170
Avi Avatar answered Sep 22 '22 17:09

Avi