How to align the Left fields of an Html table?

2

I can not align my labels (Razor) to the left of my table in html with Visual Studio!

<table cellspacing="6">        
    <tr >
        <td >
            @Html.Label("Cep: ")
        </td>            
          <td  >
            @Html.Label("Endereco: ")

          </td>
        <td >
            @Html.Label("Número: ")
        </td>
    </tr>

    <tr>
        <td >
            @Html.TextBoxFor(e => e.CEP, new { maxlength = "9", id = "Cep", name = "Cep", onchange = "findCEP()" })
        </td>

        <td >
            @Html.TextBoxFor(e => e.DescricaoEndereco, new { maxlength = "50", id = "Endereco", name = "Endereco" })
        </td>

        <td>
            @Html.TextBoxFor(e => e.Numero, new { maxlength = "50", id = "Numero", name = "Numero" })
        </td>
    </tr>
    <tr>
        <td >
            @Html.Label("Complemento: ")
        </td>
    </tr>
    <tr>
        <td >
            @Html.TextBoxFor(e => e.Complemento, new { maxlength = "50", id = "Complemento", name = "Complemento" })
        </td>
    </tr>
    <tr>
        <td >
            @Html.Label("UF: ")
        </td>

        <td >
            @Html.Label("Cidade: ")

        </td>
        <td >
            @Html.Label("Bairro: ")
        </td>
    </tr>
    <tr>
        <td>
            @Html.DropDownListFor(e => e.UF, Model.UFList, new { id = "UF", name = "UF" })
        </td>
        <td>
            @Html.TextBoxFor(e => e.Cidade, new { maxlength = "40", id = "Cidade", name = "Cidade" })
        </td>
        <td>
            @Html.TextBoxFor(e => e.Bairro, new { maxlength = "50", id = "Bairro", name = "Bairro" })
        </td>
    </tr>

</table>
    
asked by anonymous 02.03.2015 / 17:42

5 answers

1

Text is already left-aligned by default. However, if you want to align all the text inside the table to left you can use the following:

<table cellspacing="6" style="text-align:left;"> ... </table>

To align all of the table text to right , use:

<table cellspacing="6" style="text-align:right;"> ... </table>

To align only certain% of% of the table, create a td/tr or a class with the name you want, and implement it in the CSS code by applying the style id and also in the markup code as in the example below:

<!-- Código CSS -->
<style>
.alinhadoEsquerda {text-align:left;}
.alinhadoCentro {text-align:center;}
.alinhadoDireita {text-align:right;}

.textoRealce-E {color:#2C55FF;}
.textoRealce-C {color:green;}
.textoRealce-D {color:red;}

td {padding: 8px; border: 1px solid #E4E4E4;}
</style>

<!-- Código da Tabela -->
<table cellspacing="6">        
    <tr>
        <td class="alinhadoDireita">
            <span class="textoRealce-D">Alinhado à direita</span>
            @Html.Label("Cep: ")
        </td>            
        <td class="alinhadoEsquerda">
            <span class="textoRealce-E">Alinhado à esquerda</span>
            @Html.Label("Endereco: ")
            
        </td>
        <td class="alinhadoCentro">
            <span class="textoRealce-C">Alinhado ao centro</span>
            @Html.Label("Número: ")
        </td>
    </tr>
</table>
  

Note: In the above example I put the CSS and HTML code all together purposely because I have a hunch and something tells me that you're   create this without using a style sheet.

     

But the best way to do this would be to place the CSS code   within a stylesheet to separate the HTML code from CSS because    that's what style sheets were created for .

    
05.07.2015 / 09:04
0

You can create a CSS class, as follows:

.alinhamento {
  text-align: left;
}

And then add this class to td elements, either manually via HTML or asp.NET-MVC (which I am not aware of and I do not know if I can do such a task).

    
02.03.2015 / 17:47
0

One way would look like this:

<td class="left-align">@Html.Label("Cep: ")</td>

You need a class css :

.left-align { text-align: left; }

But you do not need to use a label in this case. Consider using @Html.DisplayFor(m => m.Field) that uses the value of the [Display] attribute of the class. That way, if you need to change the model display your view will reflect this change.

If you consider strictly necessary to use a label, then you can use @Html.LabelFor(m => m.Field)

In class:

public class Cliente 
{
    [Display(Name = "Número")]
    public string Numero { get; set; }
...

And your cshtml:

<table cellspacing="6">        
    <tr>
        <td class="left-align">
            @Html.DisplayFor(m => m.CEP)
        </td>            
         <td class="left-align">
            @Html.DisplayFor(m => m.DescricaoEndereco)    
          </td>
        <td class="left-align">
            @Html.DisplayFor(m => m.Numero)
        </td>
    </tr>
...
    
02.03.2015 / 19:08
0

Instead of having to add a class to each td tag, you can put a class inside the table tag

<table cellspacing="6" class="align-left">

and in css

.align-left td{
    text-align: left;
}

This means that every td tag that is inside a table tag that has the align-left class will receive the left-aligned text alignment.

    
02.06.2015 / 09:33
0

Have you tried to create a classe in CSS and put these classes in labels ? For example:

.alinha_a_esquerda{
    text-align:left;
}

And put it in HTML:

<label class="alinha_a_esquerda">...</label>
    
02.03.2015 / 17:47