div下的多个span标签会添加额外的空格

时间:2018-06-25 10:10:31

标签: html css

我正在尝试实现一种排版动画,在这方面,我将文本的所有字符都放在<span>标记中,如下所示。

但是,html的输出如下图所示,我的span元素之间有多余的空格。 (请注意,每个跨度之间显示一个额外的元素)

.container {
  max-width: 1200px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.text {
  font-size: 120px;
  font-weight: 900;
}
   

<body>
  <div class="container">
    <div class="text">
      <div class="text-row">
        <span>W</span>
        <span>e</span>
        <span>&nbsp;</span>
        <span>P</span>
        <span>r</span>
        <span>o</span>
        <span>v</span>
        <span>i</span>
        <span>d</span>
        <span>e</span>
      </div>
    </div>
  </div>
</body>

enter image description here

谁能解释为什么会这样?

3 个答案:

答案 0 :(得分:1)

这是因为span是内联元素。如果您想摆脱空白区域是否将float属性用于跨度项目

span {
    float: left;
}

浏览以下资源以探索有关显示属性的更多信息- https://css-tricks.com/fighting-the-space-between-inline-block-elements/

答案 1 :(得分:1)

Male
Female

答案 2 :(得分:1)

.text span {
    font-size: 60px;
    font-weight: 900;
    display: block;
    float: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

 <div class="text">
    <div class="text-row">
      <span>W</span>
      <span>e</span>
      <span>&nbsp;</span>
      <span>P</span>
      <span>r</span>
      <span>o</span>
      <span>v</span>
      <span>i</span>
      <span>d</span>
      <span>e</span>
    </div>
  </div>

</body>
</html>

希望这可以解决您的问题