# How to Center a BeamerColorBox

The question is, how to horizontally center a beamercolorbox on the page?

I put it in the center environment, but that doesn't work.

\setbeamercolor{bgcolor}{fg=black,bg=blue}
\begin{center}
Some text goes here...
\end{beamercolorbox}
\end{center}

-
Glad you liked my answer. Next time it might be better to hold off clicking the check mark in case someone else comes by with a better one. I usually give it a day so I can determine which of several answers is best. – Matthew Leingang Dec 2 '10 at 1:06
@Matthew: I was so excited with you clever and succinct answer that I couldn't help but to accept it! Yet your advice is wise and I'll stick to it next time. – M.S. Dousti Dec 2 '10 at 1:10

wd=<dimen> not only sets the width of the beamercolorbox to <dimen>, it sets the \hsize to <dimen>. So you're centering a box in a line which has the same width as the box, resulting in nothing.

So to force centering, use some \hfills:

\documentclass{beamer}
\setbeamercolor{bgcolor}{fg=black,bg=blue}

\begin{document}

\begin{frame}
\hfill
Some text goes here...
\end{beamercolorbox}
\hfill\hfill
\end{frame}

\end{document}


I'm not sure why you need two at the end—it seems like one at the front and back would do the centering. But instead you just get a box flush right. So an extra one does the trick.

-
Well, you are more creative than me. I used one \hfill before and another after beamercolorbox, and got the flush right effect. I'd never imagine of using two \hfill! – M.S. Dousti Dec 2 '10 at 1:05
@Sadeq @Matthew: \hspace*{\fill} at the end of the frame forces centering. – Stefan Kottwitz Dec 2 '10 at 1:11
@Stefan: Thanks! – M.S. Dousti Dec 2 '10 at 1:19
\hfill followed by \par (or equivalent) does nothing. Doubling the \hfill means that the first is no longer followed by \par so it does something. The second does nothing as it is followed by (something equivalent to) \par. So \hfill{} would work just as well. – Loop Space Dec 20 '10 at 10:55
That ought to be a slogan for something! It's almost worth setting up a site just so that the tag-line could be "\par eats glue". – Loop Space Dec 20 '10 at 14:18

Here's an answer that keeps the center environment:

\documentclass{beamer}
\setbeamercolor{bgcolor}{fg=black,bg=blue}

\begin{document}

\begin{frame}

\begin{center}
~
Some text goes here...
\end{beamercolorbox}
~
\end{center}
\end{frame}

\end{document}


Matthew's answer gave me the hint. The trick is to ensure that the colour box is not the only thing on the line that is being centred. Then it isn't treated as a box by itself, but as a component of something larger. So then the search is on for something to put on the line that won't be seen in the final output. If it has non-zero width, then it needs to be put before and after to ensure that the centring is still central. A non-breaking space (~) seemed the simplest thing to do.

-

A simple solution, which is slightly less messy for my taste, is to create a single column, which is automatically centered:

\begin{frame}
\begin{columns}
\column{.7\linewidth}