CSS 选取框仅显示与页面一样宽的文本

CSS marquee only displaying text as wide as the page is

我的选取框只显示与屏幕一样宽的文本。我不确定我有什么选项可以让它显示所有文本,无论屏幕宽度如何。我知道我的 width: 100% 风格是问题所在。

.example1 {
    height: 50px;
    overflow: hidden;
    position: relative;
}

.example1 h3 {
    position: absolute;
    width: auto;
    height: 100%;
    margin: 0;
    line-height: 50px;
    text-align: center;
    /* Starting position */
    -moz-transform: translateX(100%);
    -webkit-transform: translateX(100%);
    transform: translateX(100%);
    /* Apply animation to this element */
    -moz-animation: example1 15s linear infinite;
    -webkit-animation: example1 15s linear infinite;
    animation: example1 15s linear infinite;
}


/* Move it (define the animation) */

@-moz-keyframes example1 {
    0% {
        -moz-transform: translateX(100%);
    }
    100% {
        -moz-transform: translateX(-100%);
    }
}

@-webkit-keyframes example1 {
    0% {
        -webkit-transform: translateX(100%);
    }
    100% {
        -webkit-transform: translateX(-100%);
    }
}

@keyframes example1 {
    0% {
        -moz-transform: translateX(100%);
        /* Firefox bug fix */
        -webkit-transform: translateX(100%);
        /* Firefox bug fix */
        transform: translateX(100%);
    }
    100% {
        -moz-transform: translateX(-100%);
        /* Firefox bug fix */
        -webkit-transform: translateX(-100%);
        /* Firefox bug fix */
        transform: translateX(-100%);
    }
}

.example1 h3:hover {
    -moz-animation-play-state: paused;
    -webkit-animation-play-state: paused;
    animation-play-state: paused;
}
<div class="example1">
    <h3>LeBron sets tone at Cavs media day | Rajon Rondo breaks hand in fall | LeBron: Patience is key for Cavs | Jackson: Knicks can make playoffs | Grizzlies sign F Michael Beasley |
    </h3>
</div>

问题是您的文本换行了。添加一个 white-space: nowrapexample1 class 来解决这个问题。

这是片段:

.example1 {
    height: 50px;
    overflow: hidden;
    position: relative;
    white-space: nowrap; /* <--- FIX */
}

.example1 h3 {
    position: absolute;
    width: auto;
    height: 100%;
    margin: 0;
    line-height: 50px;
    text-align: center;
    /* Starting position */
    -moz-transform: translateX(100%);
    -webkit-transform: translateX(100%);
    transform: translateX(100%);
    /* Apply animation to this element */
    -moz-animation: example1 15s linear infinite;
    -webkit-animation: example1 15s linear infinite;
    animation: example1 15s linear infinite;
}


/* Move it (define the animation) */

@-moz-keyframes example1 {
    0% {
        -moz-transform: translateX(100%);
    }
    100% {
        -moz-transform: translateX(-100%);
    }
}

@-webkit-keyframes example1 {
    0% {
        -webkit-transform: translateX(100%);
    }
    100% {
        -webkit-transform: translateX(-100%);
    }
}

@keyframes example1 {
    0% {
        -moz-transform: translateX(100%);
        /* Firefox bug fix */
        -webkit-transform: translateX(100%);
        /* Firefox bug fix */
        transform: translateX(100%);
    }
    100% {
        -moz-transform: translateX(-100%);
        /* Firefox bug fix */
        -webkit-transform: translateX(-100%);
        /* Firefox bug fix */
        transform: translateX(-100%);
    }
}

.example1 h3:hover {
    -moz-animation-play-state: paused;
    -webkit-animation-play-state: paused;
    animation-play-state: paused;
}
<div class="example1">
    <h3>LeBron sets tone at Cavs media day | Rajon Rondo breaks hand in fall | LeBron: Patience is key for Cavs | Jackson: Knicks can make playoffs | Grizzlies sign F Michael Beasley |
    </h3>
</div>

如果要更改选取框所在的宽度,只需调整父级的宽度即可 div。

见下文:

.example1 {
  height: 50px;
  overflow: hidden;
  position: relative;
  /* change your width on this parent div */ 
  width: 200px;
  /* add white-space: no-wrap to display all text */ 
  white-space: nowrap;
}

.example1 h3 {
  position: absolute;
  width: auto;
  height: 100%;
  margin: 0;
  line-height: 50px;
  text-align: center;
  /* Starting position */
  -moz-transform: translateX(100%);
  -webkit-transform: translateX(100%);
  transform: translateX(100%);
  /* Apply animation to this element */
  -moz-animation: example1 15s linear infinite;
  -webkit-animation: example1 15s linear infinite;
  animation: example1 15s linear infinite;
}


/* Move it (define the animation) */

@-moz-keyframes example1 {
  0% {
    -moz-transform: translateX(100%);
  }
  100% {
    -moz-transform: translateX(-100%);
  }
}

@-webkit-keyframes example1 {
  0% {
    -webkit-transform: translateX(100%);
  }
  100% {
    -webkit-transform: translateX(-100%);
  }
}

@keyframes example1 {
  0% {
    -moz-transform: translateX(100%);
    /* Firefox bug fix */
    -webkit-transform: translateX(100%);
    /* Firefox bug fix */
    transform: translateX(100%);
  }
  100% {
    -moz-transform: translateX(-100%);
    /* Firefox bug fix */
    -webkit-transform: translateX(-100%);
    /* Firefox bug fix */
    transform: translateX(-100%);
  }
}

.example1 h3:hover {
  -moz-animation-play-state: paused;
  -webkit-animation-play-state: paused;
  animation-play-state: paused;
}
<div class="example1">
  <h3>LeBron sets tone at Cavs media day | Rajon Rondo breaks hand in fall | LeBron: Patience is key for Cavs | Jackson: Knicks can make playoffs | Grizzlies sign F Michael Beasley |
  </h3>
</div>