【问题标题】:How to inline two image Divs inside a Div?如何在一个 Div 中内联两个图像 Div?
【发布时间】:2016-04-20 17:58:11
【问题描述】:

我正在努力将两个 div 彼此内联,有什么建议吗?我在 CSS 中尝试的一切都不像 Float(img-1): Left 和 Float (img-2): Right, display: inline-block 等工作。

<div class="multimedia-img"> 
  <div class="img-1">  
    <a href="https://www.youtube.com/">
    <img src="images/youtube.png" style="width: 200px; height: 140px;">
    </a>
  </div>
  <div class="img-2">
    <a href="https://www.ted.com/talks">
    <img src="images/ted.png" style="width: 200px; height: 160px;">
    </a>
  </div>
</div>

编辑:

在下面添加了我的 CSS 以向您展示我到目前为止所做的...

body{
    margin: 0;
    padding: 0;
    text-align: center;
    background-color: #505050;
    height: auto;
    min-height: 100%;
}

.container{

    width: 1000px;
    margin: 0 auto;
    background-color: white;
    display: block;
    overflow: auto;
}

.content{
    width: 100%;
    margin: auto;
    padding-bottom: 5px;
    color: #1B0C0C;
    height: 114px;
    text-align: center;
    padding-top: 40px;
    font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
    white-space: pre-line;
}

.hobbies{
    margin: auto;
    width: 100%;
}

.img-hobby {
    width: 50%;
    height:100%;
    float: left;
    margin-top: 47px;
    margin-right: -295px;
}

.text-hobby {
    height: 100%;
    margin-top: 8%;
    margin-left: 20%;

}

.friends{
    margin: auto;
    width: 100%;
}

.img-friends{
    width: 50%;
    height:100%;
    float: left;
}

.text-friends{
    height: 100%;
    margin-top: 8%;
    margin-left: 20%;

}


.multimedia-img{

    display: inline-block;
}

.img-1{
    width: 50%;
    float: left;
}

.img-2{
    width: 50%;
    float: right;
}


img{
    padding-top: 17px;
    border-radius: 125px;
}

p{
    font-weight: bold;
}


ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    background-color: #333;
    top: 0;
    width: 100%;
    overflow: hidden;
    font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;


}

li {
    float: left;
}

li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}


li a:hover {
    background-color: #61C8ED;
}

li a:hover:not(.header) {
    background-color: #000000;
}

.header{
    background-color: #61C8ED;
}

【问题讨论】:

  • 如果你能发布你的 CSS 标记将会很有用。谢谢。
  • 我已经发布了我的 CSS,希望对您有所帮助。

标签: html css inline


【解决方案1】:

尝试添加 display: inline-block

 .multimedia-img{
   ....
   display: inline-block;
   ....
 }

【讨论】:

    【解决方案2】:

    浮动

    你浮动两个 div 并清除父元素,例如overflow:hidden;

    .multimedia-img {
      overflow: hidden;
    }
    
    .img-1, .img-2 {
      float: left;
    }
    

    弹性盒

    将父级设置为display: flex;

    .multimedia-img {
       display: flex;
    }
    

    【讨论】:

      【解决方案3】:

      我想你想这样做

      https://jsfiddle.net/yec8p3vt/

      html

      <div class="multimedia-img"> 
        <div class="img">  
          <a href="https://www.youtube.com/">
          <img src="images/youtube.png" style="width: 200px; height: 140px;">
          </a>
        </div>
        <div class="img">
          <a href="https://www.ted.com/talks">
          <img src="images/ted.png" style="width: 200px; height: 160px;">
          </a>
        </div>
      </div>
      

      CSS

      .img {
          display: inline-block;
      }
      

      【讨论】:

        【解决方案4】:

        你可以同时做:

        .multimedia-img {
          display: inline;
        }
        .img-1,
        .img-2 {
          display: inherit;
        }
        

        或者:

        .img-1,
        .img-2 {
          display: inline;
        }
        

        【讨论】:

          【解决方案5】:

          我会推荐 Flexbox。这么简单:)

          http://caniuse.com/#feat=flexbox

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-01-03
            • 1970-01-01
            • 2016-05-27
            • 2019-10-07
            • 2016-11-23
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多