13

How do I move text to a new line in an HTML input element with the type="button" attribute?

I have following code:

<input type="button" id="btnTexWrapped" value="I see this is a long sentence here." style="width: 200px;" />

I want the button's text value to be wrapped in two lines. I tried typing it into the next line in HTML. It didn't work as I expected:

<input type="button" id="btnTexWrapped" value="I see this is a long 
sentence here." style="width: 200px;" />

I also tried using all options of white-space with fixed width: 200px;, but still nothing works.

I am OK with statically fixing the length, width, or other values: as the control is not going to change.

alex
  • 4,689
  • 8
  • 43
  • 83
RMN
  • 708
  • 8
  • 24
  • 44

6 Answers6

23
white-space: normal;

should work

Marián Zeke Šedaj
  • 907
  • 3
  • 13
  • 24
17

Try this, you can see how it works instantly:

<input type="button" value="Carriage&#13;&#10;return&#13;&#10;separators" style="text-align:center;">
daniel
  • 1,435
  • 9
  • 16
14

You can use button tag

<button> I see this <br/>is a long <br/> sentence here.</button>

Lokesh
  • 811
  • 10
  • 8
6

For anyone reading this question 4 years later, I would like to add some clarifying details. Lokesh's answer is the correct one (not the accepted answer), but the initial question is based on a misunderstanding of how HTML works, which no one has addressed.

HTML is not a white-space significant language. That is, any new lines are completely ignored by the browser. While you can (and should!) put new lines in your HTML code in order to write readable, maintainable HTML, it will (almost) never affect the end result (I won't get into exceptions here).

As Lokesh indicated, you can use the <br /> tag to force a new line. Another common way is to use block level elements, such as div or section.

A number of elements are set to block by the browser UA stylesheet. They are usually container elements, like <div>, <section>, and <ul>. Also text "blocks" like <p> and <h1>. Block level elements do not sit inline but break past them. By default (without setting a width) they take up as much horizontal space as they can.

This is a quote from https://css-tricks.com/almanac/properties/d/display/, which is a very good reference on the different properties for the display attribute.

Additionally, I don't recommend trying to use any of the other suggested solutions which require putting encoded newline symbols inside the value attribute. This is pushing the input tag beyond was it was designed for. If you need more complex content for a button, the button tag is more appropriate. In fact, I generally don't think there's ever good reason to use <input type="button"> instead the button tag. It's much more semantic, easier to read, and is infinitely more flexible - for example, in allowing breaks and other elements (such as images!) inside it.

Just a bit of knowledge and a few recommendations from a professional web developer... :)

corinnaerin
  • 349
  • 5
  • 12
  • Completely agreed with you. However, what if I need an `` and writing custom logic for submitting a form because of a *single* linebreak... oh, I think it's overingeneering. – Andre Polykanine Sep 25 '16 at 10:36
  • I don't see how using a ` – corinnaerin Sep 25 '16 at 19:54
2

I had come cross type of requirement in my one of the project,i resolved like given below.

use html encoding string

<input button type="submit" value="This is button &#x00A; two line text" />

&#x00A;

for splitting the text in value attribute of the button tag

Hope this will help you..

Skomma
  • 29
  • 2
-12

Just press enter in the middle of the value, like this:

<input type="button" id="btnTexWrapped" value="I see this is a long 
sentence here." style="width:200px;"/>

I

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
Abdallah Barghouti
  • 53
  • 1
  • 2
  • 12