html - 尽管有 "display:inline-block",为什么这些 div 没有显示在同一行中?

标签 html css

JSFiddle

在接下来的 SSCCE 中,我将 display:inline-block 应用于 div.content-textdiv.content-image。但它们仍然没有显示在同一行中。相反,它们显示如下:

enter image description here

我希望它们显示在同一行的蓝色框中。尽管有 display:inline-block;,为什么他们不呢?我应该如何让它发挥作用?

.content-box {
  display: inline-block;
  width: 350px;
  height: 250px;
  border: 5px solid blue;
}
.content-box .content-image {
  width: 35%;
  height: 100%;
  background-size: cover;
  /*border: 5px solid orange;*/
}
.content-box .content-text {
  background-color: rgb(255, 255, 255);
  color: #1c1a1a;
  font-family: sans-serif;
  font-size: 15px;
  font-weight: 100;
  overflow-y: auto;
  /*border: 5px solid #52e007;*/
  height: 100%;
  width: 65%;
}
<!DOCTYPE>

<html>

<head>
  <meta charset="utf-8" />
  <title></title>
  <link rel="stylesheet" type="text/css" href="styles.css" />
</head>

<body>

  <div class="content-box">


    <div class="content-text">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It
      has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop
      publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>


    <div class="content-image" style="background-image:url(http://www.penttila-gardens.com/activities/walking/countryside/images/02-harvest.jpg)"></div>


  </div>

</body>

</html>

最佳答案

您应该将此 inline-block 布局应用于内部元素 .content-box .content-image.content-box .content-text,而不是容器 .content-box。之后,您应该删除元素之间的空格(它们由于内联布局而出现),例如,通过将 font-size: 0 (或 word-spacing: -100% )应用于 .content -box.

JSFiddle

关于html - 尽管有 "display:inline-block",为什么这些 div 没有显示在同一行中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31095098/

相关文章:

html - 使用 HTML Tidy 来缩进 HTML 代码?

css 专注于一个完整的表格

html - 如何使 flexbox 子行居中?

html - 单选按钮的边框

javascript - `element.next().insertBefore(element)` 是做什么的?

php - 为什么 php 在我的 html 末尾添加一个 <pre> 标签?

html - 内部容器在 IE11 上浮出父级顶部

javascript - 如何防止 document.write 超出 Div 框?

html - 将 h2 垂直底部对齐

javascript - 如何让浏览器提前下载隐藏的div内容