【问题标题】:How can I center and left align images?如何居中和左对齐图像?
【发布时间】:2018-07-18 07:17:18
【问题描述】:

我正在处理图片库,希望图片的容器完全居中在页面上,但图片是左对齐的。

这是我想要的输出:

但是,当我尝试在容器上执行 text-align: center 时(id:gallery),我得到的图像显示如下:

我尝试使用之前的堆栈溢出问题:CSS: Center block, but align contents to the left 并将图像包装在另一个 div 中,然后将其与 display: inline-block;text-align: left; 对齐,但图像似乎在整个页面上左对齐:

我可以做些什么来完成我想要的输出?

HTML

 <div id="gallery">
     <div id="images">
        <div class="container">
            <a href="images/gallery/image1.jpg" data-lightbox="mygallery">
                <img src="images/gallery/image1.jpg">           
            <div class="overlay">
                <img src="images/magnify.png">
            </div>
               </a>
        </div>
        <div class="container">
            <a href="images/gallery/image2.jpg" data-lightbox="mygallery">
            <img src="images/gallery/image2.jpg">
            <div class="overlay">
                <img src="images/magnify.png">
            </div>
            </a>
          </div>
        </div>
    </div>

CSS

#gallery{
    text-align: center;
}
#images{
    display: inline-block;
    text-align: left;
}
img{
    width: 300px;
    cursor: pointer;
}
.overlay  {
    position: absolute;
    right: 0;
    left: 0;
    cursor: pointer;
    visibility: hidden;
    color: transparent;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    transition: all ease-in .3s;
}
.overlay > img{
    height: 50%;
    width: 50%;
    top: 50%; 
    visibility: hidden;
    left: 50%;
    transform: translate(-50%,-50%);
    position: absolute;   
}
.overlay:hover > img{
    visibility: visible; 
}
.container {
    position: relative;
    display: inline-block;
    margin: 5px;
}
.container:hover .overlay  {
    visibility: visible;
    opacity: .6;
    background: black;
    color: white;   
}

【问题讨论】:

  • 将最大宽度赋予#gallery 并添加margin:auto

标签: html css


【解决方案1】:

给你的#gallery div 一个max-widthtext-align: centermargin:auto,然后将你的标题放在#gallery 内部但#images 之外的另一个div 中。然后将text-align: left 放在您的#images div 上。

请看下面的例子:

#gallery {
  text-align: center;
  max-width: 420px;
  margin: auto;
}

img {
  width: 100px;
  cursor: pointer;
}

.container {
  display: inline-block;
}

#images {
  text-align: left
}
<div id="gallery">
  <div id="header">
    <h1>Header</h1>
  </div>
  <div id="images">
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=d42">
        <img src="http://thecatapi.com/api/images/get?id=d42">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=21o">
        <img src="http://thecatapi.com/api/images/get?id=21o">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=49e">
        <img src="http://thecatapi.com/api/images/get?id=49e">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=13v">
        <img src="http://thecatapi.com/api/images/get?id=13v">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=6e6">
        <img src="http://thecatapi.com/api/images/get?id=6e6">
      </a>
    </div>
    <div class="container">
      <a href="http://thecatapi.com/api/images/get?id=4bf">
        <img src="http://thecatapi.com/api/images/get?id=4bf">
      </a>
    </div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    这行得通

    body{
                display: flex;
                flex-flow: row nowrap;  
                justify-content: center;
                align-content: center;
                align-items: center;
    }
    section{
    height:400px;
    width:400px;
    background:grey;
    }
    
    img {
      margin:48px;
    }
    <section>
      <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
      <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
      <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
      <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
      <img src="https://telecomputingarchitects.com/media/logo.png" height="24"/>
     
    </section>

    【讨论】:

      【解决方案3】:

      HTML

      <h2>HEADER</h2>
      <div class="container">
        <img src=""/>
        <img src=""/>
        <img src=""/>
        <img src=""/>
        <img src=""/>
        <img src=""/>
      </div>
      

      CSS

      h2 {
        text-align: center;
      }
      .container {
        float: left;
      }
      
      img {
        border: medium solid black;
        width: 200px;
        height: 350px;
        margin: 5% 2%;
      }
      

      【讨论】:

        【解决方案4】:

        如何为图像包装器.images 设置样式

        .images {
            width:80%;
            margin:0 auto;
            text-align:left;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-12
          • 1970-01-01
          • 2015-07-18
          • 1970-01-01
          • 1970-01-01
          • 2018-05-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多