Vertical alignment in tabular cells with variable height

I have the code:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}
\usepackage{lipsum}
\begin{document}
\begin{tabularx}{\textwidth}{ | X | c | }
\hline
\lipsum[1] & top\\
\hline
\lipsum[1] & center\\
\hline
\lipsum[1] & bottom\\
\hline
\end{tabularx}
\end{document}


Gives this:

The top row is already aligned top. How can I vertically align the center row at the center? And the bottom row at the bottom?

The vertical adjustment of the row "c" is related to the definition of the columntype X which uses the specifier p.

You need m for a centered adjustment and b for bottom. This can be achieved by \multicolumn, whereby the line width must be saved (I don't know a good solution).

Here is an example:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}
\usepackage{lipsum}
\begin{document}

\begin{tabularx}{\textwidth}{ | X | c | }
\hline
\lipsum*[1]\xdef\tempwidth{\the\linewidth} & top\\\hline
\multicolumn{1}{|m{\tempwidth}|}{\lipsum*[1]} & center\\\hline
\multicolumn{1}{|b{\tempwidth}|}{\lipsum*[1]} & bottom\\\hline
\end{tabularx}
\end{document}


• @MarcoDaniel Why column c vertical alignment is defined by changing column X? May 7, 2013 at 19:24
• @user4035: Because the column type X is responsible for the baseline. May 7, 2013 at 19:26
• @MarcoDaniel What if we have 3 columns: X|c|c. And want the 1-st c to be top aligned, and 2-nd c to be bottom aligned? May 7, 2013 at 19:31
• @user4035: you can use the same trick or use David's trick May 7, 2013 at 19:49
• @Marco May this changed over they year but based on my tests you are wrong. The the column is of type X is irrelevant. The important fact is that it contains the large multi-line text. Sep 5, 2019 at 15:02

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularx}
\usepackage{lipsum}
\begin{document}

\noindent
\begin{tabularx}{\textwidth}{ | X | c | }
\hline
\lipsum[1] & top\\
\hline
\noindent\parbox[c]{\hsize}{\lipsum[1]} & center\\
\hline
\noindent\parbox[b]{\hsize}{\lipsum[1]} & bottom\\
\hline
\end{tabularx}

\end{document}

• I absolutely love this simple solution, but for my liking there should be a bit of empty space between the cells. When I applied this to my table the cells are so close to each other that it looks kinda awkward. Is there any way to add a bit of empty space on the top and bottom of the cell? Apr 4, 2015 at 14:43
• @Cadbon \renewcommand\arraystretch{2} or whatever number works for you Apr 4, 2015 at 15:02
• Unfortunately I couldn't solve my issues with that method as my top row has only one row of text and the arraystretch command stretches all cells. This means that my top row only has a single row of text but a very large cell which looks silly. Apr 6, 2015 at 10:22
• Any idea how I can center the lipsum text horizontally with this? I tried \parbox[c][][c] but that doesn't seem to help... Oct 5, 2015 at 2:05
• @gghuffer as in any parbox or p or X column,, just use \centering Oct 5, 2015 at 6:58

Another way, using a minipage environment instead of a tabularx package:

\documentclass{article}
\usepackage{lipsum}
\usepackage[a4paper]{geometry}
\begin{document}

\begin{tabular}{|l|c|}
\hline
\begin{minipage}[t]{0.85\textwidth}\lipsum[1]\end{minipage} & top\\
\hline
\begin{minipage}{0.85\textwidth}\lipsum[1]\end{minipage} & center\\
\hline
\begin{minipage}[b]{0.85\textwidth}\lipsum[1]\end{minipage} & bottom\\
\hline
\end{tabular}

\end{document}


My new LaTeX3 package tabularray has two new h and f alignment types for putting cell text at the head and foot of the row respectively:

\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\noindent
\begin{tblr}{
colspec = {|X|c|}, rowspec = {Q[h]Q[m]Q[f]},
}
\hline
\lipsum[1] & top\\
\hline
\lipsum[1] & center\\
\hline
\lipsum[1] & bottom\\
\hline
\end{tblr}
\end{document}


• While this package is very new (might not be in TeXLive/etc. by default), it has several nice features: h/f valign option (which is closer to top/bottom vertical align in word processors), individual cell alignment with \SetCell (however, unfortunately there's no corresponding for middle-align i.e. halfway between the head and the foot, you mostly have to middle-align the whole row). For other features see the doc Nov 16, 2021 at 15:18