And┼żelika And┼żelika - 6 months ago 12
CSS Question

How to center text in a absolute positioned div?

Hello I am asking for your professionals help... I am creating fluid gallery with HTML and CSS. I need to add text on images while they are hovered and I want that text to be centered horizontally and vertically. Everything I tried failed as far as now. At the moment text is on images with absolute, but text is on top left side. Please help!



body {
margin: auto;
}
.portfolio-container,
.portfolio-list {
width: 100%;
}
.portfolio-list {
margin: 0;
padding: 0;
list-style: none;
}
.portfolio-list li {
position: relative;
display: block;
float: left;
width: 25%;
}
.portfolio-list li img {
display: block;
width: 100%;
margin: 0;
vertical-align: top;
}
.portfolio-list a:after {
width: 100%;
}
.portfolio-list li img {
width: 100%;
margin: 0 auto;
}
h1,
h2 {
font-weight: normal;
}
p {
line-height: 1.5;
}
#description {
max-width: 40em;
margin: 0 auto 4.125em;
padding: 0 3%;
}
.group:before,
.group:after {
content: "";
display: table;
}
.group:after {
clear: both
}
.group {
zoom: 1
}
#wrapper .text {
left: 0;
right: 0;
margin: 0 auto;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0;
transition: all 0.5s;
-webkit-transition: all 0.5s;
background: rgba(47, 193, 108, 0.6);
}
#wrapper:hover .text {
opacity: 1;
}

<section class="portfolio-container group">
<ul class="portfolio-list group">
<li id="wrapper">
<img src="" class="hover">
<p class="text">text</p>
</li>
<li id="wrapper">
<img src="" class="hover">
<p class="text">text</p>
</li>
</ul>
</section>




Answer

Use display:flex; with justify-content: center; and align-items: center; inside #wrapper .text

body {
   margin: auto;
 }
 .portfolio-container,
 .portfolio-list {
   width: 100%;
 }
 .portfolio-list {
   margin: 0;
   padding: 0;
   list-style: none;
 }
 .portfolio-list li {
   position: relative;
   display: block;
   float: left;
   width: 25%;
 }
 .portfolio-list li img {
   display: block;
   width: 100%;
   margin: 0;
   vertical-align: top;
 }
 .portfolio-list a:after {
   width: 100%;
 }
 .portfolio-list li img {
   width: 100%;
   margin: 0 auto;
 }
 h1,
 h2 {
   font-weight: normal;
 }
 p {
   line-height: 1.5;
 }
 #description {
   max-width: 40em;
   margin: 0 auto 4.125em;
   padding: 0 3%;
 }
 .group:before,
 .group:after {
   content: "";
   display: table;
 }
 .group:after {
   clear: both
 }
 .group {
   zoom: 1
 }
 #wrapper .text {
   left: 0;
   right: 0;
   margin: 0 auto;
   position: absolute;
   width: 100%;
   height: 100%;
   top: 0;
   left: 0;
   opacity: 0;
   transition: all 0.5s;
   -webkit-transition: all 0.5s;
   background: rgba(47, 193, 108, 0.6);
   display:flex;
   justify-content: center;
   align-items: center;
   
 }
 #wrapper:hover .text {
   opacity: 1;
 }
<section class="portfolio-container group">
  <ul class="portfolio-list group">
    <li id="wrapper">
      <img src="" class="hover">
      <p class="text">text</p>
    </li>
    <li id="wrapper">
      <img src="" class="hover">
      <p class="text">text</p>
    </li>
  </ul>
</section>

Comments