Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align a div inside td element using CSS class

It is working with this way

<td align="center"> 

But I want to use CSS class.

I defined class like this way but no luck

td {     vertical-align: middle;     text-align: center;     margin-left: auto;     margin-right: auto;     align: center; } 

Vertical align is working and text align is working for text. But it does not align div inside td with this way. I want to align div inside td.

like image 719
MonsterMMORPG Avatar asked Aug 15 '11 08:08

MonsterMMORPG


People also ask

How do I align something inside a div?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do you align an element in a table cell?

To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.

How do I center a TD in CSS?

To center align text in table cells, use the CSS property text-align. The <table> tag align attribute was used before, but HTML5 deprecated the attribute. Do not use it. So, use CSS to align text in table cells.


2 Answers

div { margin: auto; } 

This will center your div.

Div by itself is a blockelement. Therefor you need to define the style to the div how to behave.

like image 137
Luuk Avatar answered Oct 03 '22 20:10

Luuk


I cannot help you much without a small (possibly reduced) snippit of the problem. If the problem is what I think it is then it's because a div by default takes up 100% width, and as such cannot be aligned.

What you may be after is to align the inline elements inside the div (such as text) with text-align:center; otherwise you may consider setting the div to display:inline-block;

If you do go down the inline-block route then you may have to consider my favorite IE hack.

width:100px; display:inline-block; zoom:1; //IE only *display:inline; //IE only 

Happy Coding :)

like image 25
martin Avatar answered Oct 03 '22 22:10

martin