kastaplastislife
kastaplastislife

Reputation: 295

CSS marquee only displaying text as wide as the page is

My marquee is only displaying my text as wide as the screen is. I'm not sure what options I have to make it display all the text, regardless of the screen width. I know my width: 100% style is the issue here.

.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>

Upvotes: 1

Views: 2441

Answers (2)

Aib Syed
Aib Syed

Reputation: 3196

If you want to change the width where the marquee runs, simply adjust the width of the parent div.

See below:

.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>

Upvotes: 2

Brhaka
Brhaka

Reputation: 1642

The problem is that your text is breaking into new lines. Add a white-space: nowrap to example1 class to fix that.

Here's the snippet:

.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>

Upvotes: 2

Related Questions