Align divs horizontally

2020-04-11 03:21发布

I have the following Divs:

   <div class = "left">
     <div class = "custimage"><img src = "img/notch.jpg" alt = "notch"/><p>Notch</p></div>
     <div class = "custimage"><img src = "img/peak.jpg" alt = "peak"/><p>Peak</p></div>
     <div class = "custimage"><img src = "img/shawl.jpg" alt = "shawl"/><p>Shawl</p></div>
   </div>

The CSS:

.left {
    position: relative;
    float: left;
    width:600px;
    height:200px;
    background-color: #000;
    opacity: 0.7;
}

.custimage{
   position:relative;
   margin-top: 15px;
   margin-left: 15px;
   height: 170px;
   width: 130px;
   background-color: #999;
   text-align:left;
}

.custimage p{
    color: #fff;
    font:normal 60%/1.5 Helvetica, Arial, sans-serif;
    padding-left: 5px;
}

The images don't align horizontally though:

http://www.everry.com/new/customise/customisenow.html

What am I doing wrong?

标签: html css
3条回答
狗以群分
2楼-- · 2020-04-11 04:10

Add float: left to .custimage .

查看更多
可以哭但决不认输i
3楼-- · 2020-04-11 04:15

Apply a float:left on .custimage

.custimage{
   position:relative;
   margin-top: 15px;
   margin-left: 15px;
   height: 170px;
   width: 130px;
   background-color: #999;
   text-align:left;
   float: left;  // float all cust images to the left so they stack up against each other
}
查看更多
再贱就再见
4楼-- · 2020-04-11 04:21

two ways:

first: add float:left to your .custimage

second: instead of float, add display:inline-block to your .custimage.

The second one does not work in older Browsers, but is way more cleaner than let all float around.

查看更多
登录 后发表回答