html - 如何使网页在垂直和水平方向上居中?

标签 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>

我希望“我的画廊”类居中。我可以使用部分标签使 body 居中。

最佳答案

将此代码添加到类为“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%);
}

关于html - 如何使网页在垂直和水平方向上居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43568086/

相关文章:

html - li 元素内的 div 内的响应式 img?

html - 我应该在 <li> 或 <ul> 上定义列表样式吗?

javascript - 如何将所选元素的标签添加到 jquery 自动完成?

javascript - window.scrollTo 在 phonegap 中不起作用 - 替代解决方案或解决方法?

javascript - 文本调整大小在 div 中不起作用

html - 如何使用 flex 创建两个高度不相等的并排列?

javascript - 在 Mouseout 上停止 slider (Jquery)

html - html css中的图像文字

css - 使选择元素中的文本太长时换行?

html - 如何在 HTML 中设置 <table>/<td> 样式?