Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align and center images in a fixed div

Tags:

html

css

i have a very simple page with a fixed footer. On this footer, i'd like to place 5 images centered with equals distances between them.

Here is my HTML/CSS :
Footer div :

div.fixed {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 9%;
    background-color: pink;
}

And the HTML :

<div class="fixed">
    <img style="max-width: 80%; margin-left: auto; margin-right: auto; max-height: 80%;" src="images/icons/icon.png">
    <img style="max-width: 80%; margin-left: auto; margin-right: auto; max-height: 80%;" src="images/icons/icon.png">
    <img style="max-width: 80%; margin-left: auto; margin-right: auto; max-height: 80%;" src="images/icons/icon.png">
    <img style="max-width: 80%; margin-left: auto; margin-right: auto; max-height: 80%;" src="images/icons/icon.png">
    <img style="max-width: 80%; margin-left: auto; margin-right: auto; max-height: 80%;" src="images/icons/icon.png">
</div>

Here is what i obtain :

screenshot of mobile browser display

I would like to display the red icons at the center. It should be responsive depending on the display resolution with the use of '%' values. I thought about doing a grid in the footer. But is there an easier way to do it ? Couldn't find anything about this on the web, hope it's not a duplicate ! Thank you in advance for any help !

like image 410
saperlipopette Avatar asked Jan 17 '17 15:01

saperlipopette


People also ask

How do you center a fixed element?

You basically need to set top and left to 50% to center the left-top corner of the div. You also need to set the margin-top and margin-left to the negative half of the div's height and width to shift the center towards the middle of the div.

How do I align an image to a div?

Aligning an image means to position the image at center, left and right. We can use the float property and text-align property for the alignment of images. If the image is in the div element, then we can use the text-align property for aligning the image in the div.


5 Answers

You can just add text-align: center on fixed div.

div.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 9%;
  background-color: pink;
  text-align: center;
}
<div class="fixed">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
</div>
like image 59
Nenad Vracar Avatar answered Oct 23 '22 02:10

Nenad Vracar


div.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 9%;
  background-color: pink;
  text-align: center;
}
div.fixed >img{
  max-width: 80%;
  margin-left: auto;
  margin-right: auto;
  max-height: 80%;
}
<div class="fixed">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
  <img src="images/icons/icon.png">
</div>
like image 44
Vixed Avatar answered Oct 23 '22 02:10

Vixed


you can use flexbox to align child items vertically center and horizontally equally spaced.

div.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 100%;
  height: 9%;
  background-color: pink;
  display: flex; //Set display to flex
  justify-content : space-around; //space equally horizontally
  align-items: center; //place at center vertically
}

No need to give any extra styling to child elements i.e. <img> in your case. Flexbox will take care of alignments and is very well supported by most of the browsers.

like image 33
Jagajit Prusty Avatar answered Oct 23 '22 01:10

Jagajit Prusty


div.fixed {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 100%;
    height: 9%;
    background-color: pink;
    text-align: center

}


img
{ 
  display:inline-block; 
}
like image 34
Indesejavel Coisa Avatar answered Oct 23 '22 01:10

Indesejavel Coisa


Just add text-align: center; to the parent div.fixed

like image 34
Yousif Al-Raheem Avatar answered Oct 23 '22 01:10

Yousif Al-Raheem