【问题标题】:How to center text in a absolute positioned div?如何在绝对定位的 div 中居中文本?
【发布时间】:2016-09-20 03:01:45
【问题描述】:

您好,我正在寻求您的专业人士的帮助...我正在使用 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>

【问题讨论】:

    标签: html css text gallery centering


    【解决方案1】:

    在#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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-09
      • 2012-03-28
      • 2015-09-12
      • 2017-11-15
      • 2015-04-11
      • 2010-09-20
      相关资源
      最近更新 更多