The simple answer to your question is that such $X$ exist for certain $B$, but it is not entirely clear what $B$ are possible. There are some restrictions that we know.
The plus side
As Piotr commented, there is a very simple answer to the way you phrased your question: Take any smooth projective $C$ and then let $X=B\times C$ with the obvious projection to $B$.
You could ask for non-trivial families, but then you can just make fiber bundles such as a ruled surface.
The interesting question is if you require that the family of curves $X\to B$ is non-isotrivial, that is, any (general) fiber is only isomorphic to at most finitely many others or equivalently the induced map to moduli $B\to \mathsf M_g$ is (generically) finite. Such a family is also known as a Kodaira fibration.
If $g\geq 3$, then the Satake compactification has a boundary of codimension $2$, so the intersection of the right number of general hyperplanes gives a projective curve in $\mathsf M_g$ and hence after possibly taking a finite cover and resolution a family as required, but then you have absolutely no control over what $B$ is and this is not something one can write down explicitly. (See $g\leq 2$ below).
There is a known explicit construction (I am not sure if this is due to Kodaira, but possible) to produce such families: Suppose you have smooth projective curves $C,B$ such that there exists a morphism $\alpha:B\to C$ and a finite group $\Sigma\subseteq \mathrm{Aut} C$ that acts without fixed points on $C$. Let $\Gamma_\sigma\subseteq B\times C$ denote the graph of the morphism $\sigma\circ\alpha$ and $\Gamma=\displaystyle\bigcup_{\sigma\in\Sigma}\Gamma_\sigma$. Further assume that for some $r\geq 2$ there exists an $r$-fold cyclic cover of $B\times C$ that is ramified exactly along $\Gamma$. Let $X$ be this cover with the induced morphism to $B$.
I leave it for you to verify that this actually works: This gives a smooth projective family that moves in moduli. One should also verify that there exist curves with the required properties. This is not too hard, but the curves are somewhat special. I don't know of any other construction that is essentially different than this (take a trivial family and find an appropriate finite cover which is no longer isotrivial).
For more details see Chapter V of [Barth-Peters-Van de Ven].
The minus side
One would expect that this cannot always be done.
Clearly (see Olivier's comment) this cannot happen if the moduli space of the would-be fibers does not contain a projective curve. This means that $g\leq 2$ is out. (See $g\geq 3$ above).
On the other hand it turns out that we can rule out certain curves to appear in place of $B$. In fact Shafarevich's conjecture states the following:
Let $B$ be a fixed smooth projective curve and $g\geq 2$ an integer. Let $\Delta\subset B$ be a finite set of (closed) points. Then there exist only finitely many isomorphism classes of projective families of curves of genus $g$ that are smooth over $B\setminus\Delta$. Furthermore, if $2g(B)- 2 + \#\Delta < 0$, then there exist no such families.
This was proven by Arakelov and Parshin, so this is a theorem. In particular this means that one cannot have a Kodaira fibration over a curve of genus $0$ or $1$. (For connections with the Mordell conjecture see this MO answer).
This conjecture has far reaching generalizations to higher dimensions both in the direction of the base and the fiber. A survey article where some of this development is explained is
S.K., Subvarieties of moduli stacks of canonically polarized varieties: generalizations of Shafarevich's conjecture. Algebraic geometry—Seattle 2005. Part 2, 685–709,
Proc. Sympos. Pure Math., 80, Part 2, Amer. Math. Soc., Providence, RI, 2009.