如何纵向和横向中心网页?

时间:2017-04-23 06:27:59

标签: html css

我想让我的网页垂直和水平居中。我尝试了很多选择,但没有成功。帮我。基本上它是一个缩略图图库页面,我正在使用photoswipe。一切都很好,除了我不能使页面居中。

我的HTML代码

<div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp1-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp1.jpg" itemprop="thumbnail"></a>
        <figcaption>1</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp2-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp2.jpg" itemprop="thumbnail"></a>
        <figcaption>2</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp3-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp3.jpg" itemprop="thumbnail"></a>
        <figcaption>3</figcaption>
    </figure>



    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp4-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp4.jpg" itemprop="thumbnail"></a>
        <figcaption>4</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp5-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp5.jpg" itemprop="thumbnail"></a>
        <figcaption>5</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp6-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp6.jpg" itemprop="thumbnail"></a>
        <figcaption>6</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp7-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp7.jpg" itemprop="thumbnail"></a>
        <figcaption>7</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp8-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp8.jpg" itemprop="thumbnail"></a>
        <figcaption>8</figcaption>
    </figure>



    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp9-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp9.jpg" itemprop="thumbnail"></a>
        <figcaption>9</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp10-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp10.jpg" itemprop="thumbnail"></a>
        <figcaption>10</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp11-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp11.jpg" itemprop="thumbnail"></a>
        <figcaption>11</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp12-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp12.jpg" itemprop="thumbnail"></a>
        <figcaption>12</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp13-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp13.jpg" itemprop="thumbnail"></a>
        <figcaption>13</figcaption>
    </figure>



    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp14-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp14.jpg" itemprop="thumbnail"></a>
        <figcaption>14</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp15-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp15.jpg" itemprop="thumbnail"></a>
        <figcaption>15</figcaption>
    </figure>

</div>

我的CSS代码:

.my-gallery {
margin-left: auto;
margin-right: auto; 
width:100%;

}
.my-gallery img {
width: 100%; 
height: auto;
}
.my-gallery figure {
display: block;
float: left;
width: auto;
margin: 0 auto;
text-align: center;
font: 15px Arial, sans-serif;
border: thin silver solid;
margin: 5px 5px;
padding: 0.1em;

}
.my-gallery figcaption {
padding-top: 1px;      
padding-bottom: 1px;       

}

如何让它居中?

我的完整代码

<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
<title>Multiple PhotoSwipe galleries on page</title>

<link rel="stylesheet" href="css/photoswipe.min.css">
<link rel="stylesheet" href="css/default-skin/default-skin.min.css">

<link rel="stylesheet" href="css/style.css">

</head>

<body> 



<div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp1-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp1.jpg" itemprop="thumbnail"></a>
        <figcaption>1</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp2-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp2.jpg" itemprop="thumbnail"></a>
        <figcaption>2</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp3-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp3.jpg" itemprop="thumbnail"></a>
        <figcaption>3</figcaption>
    </figure>



    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp4-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp4.jpg" itemprop="thumbnail"></a>
        <figcaption>4</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp5-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp5.jpg" itemprop="thumbnail"></a>
        <figcaption>5</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp6-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp6.jpg" itemprop="thumbnail"></a>
        <figcaption>6</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp7-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp7.jpg" itemprop="thumbnail"></a>
        <figcaption>7</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp8-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp8.jpg" itemprop="thumbnail"></a>
        <figcaption>8</figcaption>
    </figure>



    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp9-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp9.jpg" itemprop="thumbnail"></a>
        <figcaption>9</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp10-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp10.jpg" itemprop="thumbnail"></a>
        <figcaption>10</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp11-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp11.jpg" itemprop="thumbnail"></a>
        <figcaption>11</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp12-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp12.jpg" itemprop="thumbnail"></a>
        <figcaption>12</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp13-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp13.jpg" itemprop="thumbnail"></a>
        <figcaption>13</figcaption>
    </figure>



    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp14-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp14.jpg" itemprop="thumbnail"></a>
        <figcaption>14</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp15-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp15.jpg" itemprop="thumbnail"></a>
        <figcaption>15</figcaption>
    </figure>

</div>



<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
     It's a separate element, as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. PhotoSwipe keeps only 3 slides in DOM to save memory. -->
        <!-- don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>
<script src="js/photoswipe.min.js"></script>
<script src="js/photoswipe-ui-default.min.js"></script>

<script src="js/index.js"></script>

</body>

</html>

我希望班级“my-gallery”居中。我可以使用section标签来制作身体中心。

4 个答案:

答案 0 :(得分:1)

使用“my-gallery”类将此代码添加到div中。

.my-gallery
{
    position:relative;
    top:50%;
    left:50%;
    -ms-transform: translateX(-50%) translateY(-50%);
    -webkit-transform: translate(-50%,-50%);
    transform: translate(-50%,-50%);
}

答案 1 :(得分:1)

要对齐数字HORIZONTALLY您可以将显示属性从“block”更改为“inline-block”,并删除“float:left”(在“.my-gallery figure”规则中),并添加“text-align:center” “在”.my-gallery“规则:

.my-gallery {
    text-align: center;
}

.my-gallery figure {
    display: inline-block;
}

答案 2 :(得分:0)

.my-gallery img {
width: 100%; 
height: auto;
}
.my-gallery figure {
display: block;
float: left;
width: auto;
margin: 0 auto;
text-align: center;
font: 15px Arial, sans-serif;
border: thin silver solid;
margin: 5px 5px;
padding: 0.1em;

}
.my-gallery figcaption {
padding-top: 1px;      
padding-bottom: 1px;       

}

div {
 position: absolute;
    top:50%;
    left: 50%;
    margin-right: -50%;
    transform: translate(-50%, -50%) }
   
 }
<div class="my-gallery" itemscope itemtype="http://schema.org/ImageGallery">

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp1-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp1.jpg" itemprop="thumbnail"></a>
        <figcaption>1</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp2-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp2.jpg" itemprop="thumbnail"></a>
        <figcaption>2</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp3-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp3.jpg" itemprop="thumbnail"></a>
        <figcaption>3</figcaption>
    </figure>



    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp4-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp4.jpg" itemprop="thumbnail"></a>
        <figcaption>4</figcaption>
    </figure>


    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp5-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp5.jpg" itemprop="thumbnail"></a>
        <figcaption>5</figcaption>
    </figure>

     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp6-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp6.jpg" itemprop="thumbnail"></a>
        <figcaption>6</figcaption>
    </figure>
    
    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp7-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp7.jpg" itemprop="thumbnail"></a>
        <figcaption>7</figcaption>
    </figure>
    
      <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp8-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp8.jpg" itemprop="thumbnail"></a>
        <figcaption>8</figcaption>
    </figure>
     
     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp9-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp9.jpg" itemprop="thumbnail"></a>
        <figcaption>9</figcaption>
    </figure>
     
     <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp10-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp5.jpg" itemprop="thumbnail"></a>
        <figcaption>10</figcaption>
    </figure>

   <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp11-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp5.jpg" itemprop="thumbnail"></a>
        <figcaption>11</figcaption>
    </figure>
    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp12-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp12.jpg" itemprop="thumbnail"></a>
        <figcaption>12</figcaption>
    </figure>
    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp13-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp13.jpg" itemprop="thumbnail"></a>
        <figcaption>13</figcaption>
    </figure>
    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp14-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp14.jpg" itemprop="thumbnail"></a>
        <figcaption>14</figcaption>
    </figure>

    <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
              <a href="images/rp15-1.jpg" itemprop="contentUrl" data-size="1400x2100"> 
          <img src="images/rp15.jpg" itemprop="thumbnail"></a>
        <figcaption>15</figcaption>
    </figure>
</div>

答案 3 :(得分:0)

将此添加到您的CSS并完成

body, html
{
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    display:table;
}

body 
{
    display:table-cell;
    vertical-align:middle;
}

.my-gallery 
{
    text-align: center;
}

.my-gallery figure 
{
    display: inline-block;
}