1

really simple question and cant figure it out. I am trying to add a line break here on my table header

var text = "<table id='test' class='test'>";
    text += "<tr><th> style='font-size:16px'>Cool \nStuff</th></tr>";

I have tried to add a line break using <br> and \n and no luck, can anyone help me

5
  • 1
    \n should work fine Commented May 1, 2015 at 15:20
  • <br> should work. \n will not make a difference Commented May 1, 2015 at 15:20
  • yeah its suppose to work but is not.. im using datatables plugin and im building my table dynamically Commented May 1, 2015 at 15:24
  • @user3547086 It would help if you put the exact code into your question that does not work! Commented May 1, 2015 at 15:24
  • you have a typo at <th> .. the style tag is outside the tag.. And please see my answer. The <br> tag works perfectly Commented May 1, 2015 at 15:28

6 Answers 6

4

\n puts a newline in the string, but HTML treats newlines as spaces. To put in a line break, you:

  1. Use a <br> ("break") element:

    var text = "<table id='test' class='test'>";
        text += "<tr><th style='font-size:16px'>Cool<br>Stuff</th></tr>";
    

    or,

  2. Put the first line in one block element, the second in another:

    var text = "<table id='test' class='test'>";
        text += "<tr><th style='font-size:16px'>" +
                "<div>Cool</div>" +
                "<div>Stuff</div>" +
                "</th></tr>";
    

That sort of thing.

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

1 Comment

The first suggestion didn't work but the second one did. Thank you, I will accept your answer whenever it lets me
2

Use <br> should work fine. Note you had a typo with the opening th

var text = "<table id='test' class='test'>";
    text +=  "<thead>";
    text += "<tr><th style='font-size:16px'>Cool <br/>Stuff</th></tr>";
    text +=  "</thead>";
    text +=  "</table>";


document.getElementById("z").innerHTML = text;
<div id="z"></div>

Comments

0

Separate each line in the header into <span>Cool</span><span>Stuff</span> and add the following styles to the span:

span {
    width: 100%;
    display: block;
}

Example:

// using your javascript example...

var text = "<table id='test' class='test'>";
    text += "<tr><th> style='font-size:16px'><span style='width:100%; display:block;'>Cool</span><span style='width:100%; display:block;>Stuff</span></th></tr>";
.table {
    width: 100%;
    display: block;
}
.table > thead > tr > th {
    padding: 15px;
    text-align: center;
    border: solid 2px #d2d2d2;
    background-color: #000000;
    color: #ffffff;
}
.table > thead > tr > th > span {
  width: 100%;
  display: block;
}
<table class="table"><!-- START TABLE -->
    <thead>
        <tr>
            <th><span>Cool</span><span>Stuff</span></th>
            <th>Column 2</th>
            <th>Column 3</th>
            <th>Column 4</th>
        </tr>
    </thead>
    <tbody class="tbody">
    </tbody>
</table><!-- /END TABLE -->

Comments

0

Using <br> works perfectly.

(function () {
    text += '<th>Cool<br>Stuff</th>';
})();

Relevant JSFiddle

Comments

0

http://jsfiddle.net/68Lc0d7t/1/

You missed the closing tag in the <th> tag.. Above is a simple working fiddle with <br> tag.

Comments

0

Adding </br> should work, see the code snippet below. Please note that there is an error in your HTML. <th> style='font-size:16px'> contains two closing > tags, you should remove the first one.

var text = "<table id='test' class='test'>";
text += "<tr><th style='font-size:16px'> Cool </br> Stuff</th></tr>";
document.write(text)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.