3

Here is what I want to do:

  1. Keep some text and a button at the same line and align center
  2. When reduce the screen size, always keep the button showing up while text-overflow: ellipsis taking effect

What I did so far can't keep the button showing up. Ellipsis only starts working when the window edge reaches the text.

HTML:

<div class="wrapper">
  <span>
    foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar 
  </span>
  <button type="button">Download</button>
</div>

CSS:

.wrapper {
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  background-color: #b3ffcb;
}

JSFiddle Demo

Anyone know how to fix this?

1

1 Answer 1

4

You can use Flexbox on wrapper and apply text-overflow: ellipsis on span Fiddle

.wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #b3ffcb;
}
span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div class="wrapper">
  <span>foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar </span>
  <button type="button">Download</button>
</div>

Sign up to request clarification or add additional context in comments.

3 Comments

When I continually reduce the screen, the button size somehow got reduced gradually too. What is this happening?
@Li' I can't see that in my demo, can you create fiddle and show what you mean by that?
Nvm, I added button min-width to fix button size issue. Flexbox is what I need to solve my problem. Thx!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.