css - 如何正确使用 inline-block 定位元素?

标签 css sass

我有一张图片、一个标题和一段描述。 我想在左侧显示图像,然后在其旁边显示标题和描述。

这对于 float 来说很容易,但是使用内联 block 有可能吗?不知道如何在不使用 float 的情况下正确显示它。

执行此操作的正确方法是什么? float 是否“不好”使用?

JSFiddle:https://jsfiddle.net/g1euvcbx

.path-team {
        text-align: left;
        .item-list {
            width: 80%;
            margin: 0 auto;
        }
        .list-item {
            margin: 0;
            padding: 0;
        }
        .field__item {
            font-weight: bold;
            padding-bottom: 5px;
            p {
                font-weight: normal;
                padding-bottom: 25px;
            }
            img {
                margin: 0 auto;
                margin-bottom: 10px;
                padding: 0;
            }
        }
        /* positioning */
        ul {
            list-style: none;
            margin: 0;
            padding: 0;
            li {}
            ul {
                margin: 0;
                padding: 0;
            }
        }
    }
<div class="path-team">

<ul class="item-list">
  <li class="list-item">
    <div class="field-collection-item">
      <div class="content">
        <div class="field field--name-field-picture">
          <div class="field__item">
            <img src="https://i.gyazo.com/e201976d7a4fd5745d9cc1af713943b4.png" alt="Sofie">
          </div>
        </div>
        
        <div class="field field--name-team-name">
          <div class="field__item">
            Sofie
          </div>
        </div>
        
        <div class="field field--name-team-short-desc">
          <div class="field__item">
            <p>
              Bunch of text goes in here.
              Could be multiple lines of text.
              It's a description of the person.
              To the left we want the persons picture.
              At the top to the right of the picture we want the persons name in bold.
              Below that we want this text.
            </p>
          </div>
        </div>
        
      </div>
    </div>
  </li>
</ul>

</div>

这是我现在拥有的:

start

我想要的最终结果:

result

最佳答案

有多种方法可以实现这种布局。通常我会推荐使用 display: inline-blockdisplay: flex。如果您不能决定走哪条路,请始终检查支持,在您的情况下为 display: inline-blockdisplay: flex .

您也可以将您的 HTML 结构简化为这样。这将适用于两种解决方案。

HTML

<div class="field">
  <img src="https://images.pexels.com/photos/192651/pexels-photo-192651.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" alt="Title">
  <div class="content">
    <h1>Title</h1>
    <p> Bunch of text goes in here. Could be multiple lines of text. It's a description of the person. To the left we want the persons picture. At the top to the right of the picture we want the persons name in bold. Below that we want this text. </p>
  </div>
</div>

显示:内联 block

CSS

* {
  box-sizing: border-box;
}

.field {
  width: calc(50% - 4px);
  background: #eee;
  font-size: 0;
  position: relative;
}

.field img,
.field .content {
  display: inline-block;
}

.field img {
  vertical-align: top;
  width: 30%;
}

.field .content {
   width: 70%;
   font-size: initial;
   padding: 10px 20px;
}

.content h1 {
  margin: 0;
}

* {
  box-sizing: border-box;
}
.field {
  width: calc(50% - 4px);
  background: #eee;
  font-size: 0;
  position: relative;
}
.field img,
.field .content {
  display: inline-block;
}
.field img {
  vertical-align: top;
  width: 30%;
}
.field .content {
  width: 70%;
  font-size: initial;
  padding: 10px 20px;
}
.content h1 {
  margin: 0;
}
<div class="field">
  <img src="https://images.pexels.com/photos/192651/pexels-photo-192651.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" alt="Title">
  <div class="content">
    <h1>Title</h1>
    <p>
      Bunch of text goes in here. Could be multiple lines of text. It's a description of the person. To the left we want the persons picture. At the top to the right of the picture we want the persons name in bold. Below that we want this text.
    </p>
  </div>
</div>

显示: flex

CSS

.field {
  width: 50%;
  background: #eee;
  display: flex;
  align-items: flex-start;
}

img {
  max-width: 200px;
}

.field .content {
  padding: 10px 20px;
}

.content h1 {
  margin: 0;
}

.field {
  width: 50%;
  background: #eee;
  display: flex;
  align-items: flex-start;
}
img {
  max-width: 100px;
}
.field .content {
  padding: 10px 20px;
}
.content h1 {
  margin: 0;
}
<div class="field">
  <img src="https://images.pexels.com/photos/192651/pexels-photo-192651.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" alt="Title">
  <div class="content">
    <h1>Title</h1>
    <p>
      Bunch of text goes in here. Could be multiple lines of text. It's a description of the person. To the left we want the persons picture. At the top to the right of the picture we want the persons name in bold. Below that we want this text.
    </p>
  </div>
</div>

关于css - 如何正确使用 inline-block 定位元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41180488/

相关文章:

javascript - 使用 JS 在主列上响应宽度

javascript - 将鼠标悬停在 li 上时更改 div 的颜色

html - 如何显示 JPG 图像以便首先显示非常低的分辨率?

ios - Ionic2页脚工具栏文本在IOS上不占全宽

sass - polymer 1.1 和 SASS

javascript - 仅在悬停时通过 x 和 y 绘图线将文本添加到生成的象限区域,highcharts

html - 子菜单未显示。

sass - 在 Plunker 中使用 sass?

css - SASS:如何忽略循环

angular - 如何在库中声明/包含导入的 scss 文件夹,以便可以在应用程序的 scss 文件中调用 @import?