---
title: "containerBackground(_:for:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/containerbackground(_:for:)"
---

# containerBackground(_:for:)

Sets the container background of the enclosing container using a view.

## Declaration

```swift
nonisolated func containerBackground<S>(_ style: S, for container: ContainerBackgroundPlacement) -> some View where S : ShapeStyle

```

## Discussion

Discussion The following example uses a LinearGradient as a background: struct ContentView: View {     var body: some View {         NavigationStack {             List {                 NavigationLink("Blue") {                     Text("Blue")                     .containerBackground(.blue.gradient, for: .navigation)                 }                 NavigationLink("Red") {                     Text("Red")                     .containerBackground(.red.gradient, for: .navigation)                 }             }         }     } } The .containerBackground(_:for:) modifier differs from the background(_:ignoresSafeAreaEdges:) modifier by automatically filling an entire parent container. ContainerBackgroundPlacement describes the available containers. Parameters style: The shape style to use as the container background. container: The container that will use the background.

## See Also

### Layering views

- [Adding a background to your view](swiftui/adding-a-background-to-your-view.md)
- [ZStack](swiftui/zstack.md)
- [zIndex(_:)](swiftui/view/zindex(_:).md)
- [background(alignment:content:)](swiftui/view/background(alignment:content:).md)
- [background(_:ignoresSafeAreaEdges:)](swiftui/view/background(_:ignoressafeareaedges:).md)
- [background(ignoresSafeAreaEdges:)](swiftui/view/background(ignoressafeareaedges:).md)
- [background(_:in:fillStyle:)](swiftui/view/background(_:in:fillstyle:).md)
- [background(in:fillStyle:)](swiftui/view/background(in:fillstyle:).md)
- [overlay(alignment:content:)](swiftui/view/overlay(alignment:content:).md)
- [overlay(_:ignoresSafeAreaEdges:)](swiftui/view/overlay(_:ignoressafeareaedges:).md)
- [overlay(_:in:fillStyle:)](swiftui/view/overlay(_:in:fillstyle:).md)
- [backgroundMaterial](swiftui/environmentvalues/backgroundmaterial.md)
- [containerBackground(for:alignment:content:)](swiftui/view/containerbackground(for:alignment:content:).md)
- [ContainerBackgroundPlacement](swiftui/containerbackgroundplacement.md)
