Abstract We consider the functional ℱ ( u ) ≔ ∫ Ω f ( x , D u ( x ) ) d x , {\mathcal{ {\mathcal F} }}\left(u):= \mathop{\int }\limits_{\Omega }f\left(x,Du\left(x)){\rm{d}}x, where f ( x , z ) f\left(x,z) satisfies a ( p , q ) \left(p,q) -growth condition with respect to z z and can be approximated by means of a suitable sequence of functions. We consider B R ⋐ Ω {B}_{R}\hspace{0.33em}\Subset \hspace{0.33em}\Omega and the spaces X = W 1 , p ( B R , R N ) and Y = W 1 , p ( B R , R N ) ∩ W loc 1 , q ( B R , R N ) . X={W}^{1,p}\left({B}_{R},{{\mathbb{R}}}^{N})\hspace{1.0em}\hspace{0.1em}\text{and}\hspace{0.1em}\hspace{1.0em}Y={W}^{1,p}\left({B}_{R},{{\mathbb{R}}}^{N})\cap {W}_{\hspace{0.1em}\text{loc}\hspace{0.1em}}^{1,q}\left({B}_{R},{{\mathbb{R}}}^{N}). We prove that the lower semicontinuous envelope of ℱ ∣ Y {\mathcal{ {\mathcal F} }}{| }_{Y} coincides with ℱ {\mathcal{ {\mathcal F} }} or, in other words, that the Lavrentiev term is equal to zero for any admissible function u ∈ W 1 , p ( B R , R N ) u\in {W}^{1,p}\left({B}_{R},{{\mathbb{R}}}^{N}) . We perform the approximations by means of functions preserving the values on the boundary of B R {B}_{R} .