1

I am working on a TV application with JavaScript, Html and CSS.

I want to generate buttons where the label(title) is not always the same: sometimes so long or quiet short. The labels should be dynamically generated from xml files. So the problem is to display all the generated buttons with the same width.

Here is how I am creating the buttons with javascript:

    var a = document.createElement("a");    
    var span2 = document.createElement("span");
    span2.setAttribute("class", "span1");
    span2.appendChild(document.createTextNode(text));
    a.appendChild(span2);

How we can do it?

6
  • 3
    Why not then have a class on all the buttons with a defined width? Commented Nov 17, 2014 at 14:23
  • 1
    So basically, all you want is having your buttons the same width? A fixed width, more precisely? Commented Nov 17, 2014 at 14:23
  • 1
    If you are ok by doing: set a limit like 55 characters if text exceeds 55 characters, append (...) text after trimming 55 characters and make the full text in the title/tooltip. Which makes uniform design with good look... Commented Nov 17, 2014 at 14:37
  • the problem is the butoons should be displayed in a beautiful view. In other words, they should be displayed with the same width! I tried to make the width fixed but it doesn't fit to all of them Commented Nov 17, 2014 at 14:38
  • 2
    Show us what you have done and we can help you more :) Commented Nov 17, 2014 at 14:39

3 Answers 3

1

Here's an option:

  1. When you get your set of button labels, get the length of each string.
  2. Determine how wide the buttons need to be based on the number of characters in the longest string.
  3. When you build each span2, give it a width attribute equal to that largest size.

However, be aware that if you have some very long labels and some very short, it may look weird having the really short labels on very wide buttons.

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

Comments

1

I don't know exactly what you are trying to do but here is one example how to make the buttons with same width and having different character length :)

var words = ['Lorem', 'Lorem ipsum', 'Lorem ipsum dolor', 'Lorem ipsum dolor sit', 'Lorem ipsum dolor sit amet', 'Lorem ipsum dolor sit amet consectetur', 'Lorem ipsum dolor sit amet consectetur adipisicing', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit fuga', 'Lorem ipsum dolor sit amet, consectetur adipisicing elit fuga animi'];

for (var i = 0; i < 10; i++) {
  var a = document.createElement("a");
  var span2 = document.createElement("span");
  span2.setAttribute("class", "span1");
  span2.style.width = 150 + "px";
  span2.appendChild(document.createTextNode(words[i]));
  a.appendChild(span2);
  document.body.appendChild(a);
}
.span1 {
  display: inline-block;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 12px;
  text-align: center;
  background: lightblue;
  margin: 5px;
  vertical-align: middle;
  padding: 5px;
  border-radius: 5px;
  color: #fff;
}

Comments

0

Thanks a lot for your ideas and comments :)

I decided to make some thumbnails and I will query the buttons title :) At least I am sure now that I will get a list of buttons with fixed width.

Thanks a lot for your kind help :)

Comments

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.