如何居中和左对齐图像?

时间:2018-02-07 21:10:36

标签: html css

我正在处理图片库,并希望图片的容器完全位于页面的中心位置,但图像保持对齐。

这是我想要的输出:

example1

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

example 2

我尝试使用之前的堆栈溢出问题跟随诉讼:CSS: Center block, but align contents to the left 并将图像包装在另一个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;   
}

4 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

#gallery div max-widthtext-align: centermargin:auto,然后将标题放在#gallery内的另一个div中,但在#images之外{1}}。然后将text-align: left放在#images div。

见下面的例子:

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

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

.container {
  display: inline-block;
}

#images {
  text-align: left
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这是有效的

&#13;
&#13;
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;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 3 :(得分:-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%;
}