如何在绝对定位 div 中居中文本?

How to center text in a absolute positioned div?

您好,我正在寻求专业人士的帮助...我正在使用 HTML 和 CSS 创建流体画廊。我需要在图像悬停时在图像上添加文本,我希望该文本水平和垂直居中。到目前为止,我尝试的一切都失败了。目前,文字在绝对图像上,但文字位于左上角。请帮忙!

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="http://placehold.it/600x400/c69/?text=%20" class="hover">
      <p class="text">text</p>
    </li>
    <li id="wrapper">
      <img src="http://placehold.it/600x400/9c6/?text=%20" class="hover">
      <p class="text">text</p>
    </li>
  </ul>
  
  
</section>

#wrapper .text

中将 display:flex;justify-content: center;align-items: center; 结合使用

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>