如何将图片居中并左对齐?

3

我正在制作一个图库,并希望图像容器完全居中于页面,但是图像左对齐。

这是我的期望输出:

example1

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

example 2

我尝试跟随以前的堆栈溢出问题:CSS:中心块,但将内容对齐到左侧,然后将图像包装在另一个div中,然后使用display:inline-block;text-align:left;对其进行对齐,但是图像似乎只能在整个页面左对齐:

example 3

我该怎么做才能实现我的期望输出?

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;   
}

1
#gallery添加max-width并且加上margin:auto - undefined
4个回答

1

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

示例如下:

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


0
怎么样给图像包装器 .images 添加样式呢?
.images {
    width:80%;
    margin:0 auto;
    text-align:left;
}

0

这个有效

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>


-2

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%;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接