Center the td of html in a table [duplicated]

0
    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        tr{
            text-align: center;
            background-color: red;
        }
        td{
            padding: 50px;
            border: 1px black;
        }
        table{
            border: 5px solid green;
        }
    </style>
</head>
<body>
<table >
    <script type="text/javascript">
        var num = prompt("Introduce el numero: ")*1;
        if (isNaN(num)==false && num % 2 != 0) {
            for(var i=1; i<=num;i+=2){
                document.write('<tr>');
                for(var x = i; x>=1 ;x--){

                    document.write('<td>');

                    document.write('</td>');        
                }
                document.write('</tr>');
            }
        }else{
            document.write("Numero incorrecto!")
        }
//      for (var i = num; i > 0; i-=2) {
    </script>
</table>
</body>
</html>

In fact, what I'm trying to do is a pyramid with odd numbers. Where the entry would be odd numbers and this is drawing pyramid. What I want to do is center all the TDs. In such a way that a pyramid remains. Thanks ... Although I doubt that the exercise has it well done ...

    
asked by Abdullah 04.10.2017 в 01:07
source

2 answers

0

I think the best alternative you have is to create a table with the following proportions:

Each time you enter 1 number you will add a <td> blank ahead, except in the last number of that floor, after the next level you add n <td> at the beginning to go forming the pyramid and at the end you can remove the edges of the cells so that they look like in the image or just put borders to the cells that contain numbers.

I hope this can help you.

    
answered by 04.10.2017 / 23:33
source
0

If you're looking to center the content of a td tag, you can access the style property of the tag and apply the text-align and its centering value:

<table>
  <tr><td style="text-align: center;"> Valor </td></tr>
</table>
    
answered by 04.10.2017 в 03:40