Angular 4 - how to make a mat-card fill entire parent component area
Hope you have figured this out by now but I'll provide this solution to others who might be seeing similar issues.
The reason height:100%;
extends past the parent container is the mat-card
box-sizing
property is content-box
by default. If you set it to border-box
it will subtract the padding while determining the size to fill the parent.
The following should work for you:
app-window mat-card{
width: 100%;
height: 100%;
box-sizing: border-box;
}
Related videos on Youtube
GreatHam
Updated on September 16, 2022Comments
-
GreatHam over 1 year
I have a
mat-grid-tile
(parent) which contains a componentapp-window
(child), which contains amat-card
at its root.The
app-window
fills themat-grid-tile
as desired and is center-aligned both vertically and horizontally. Now I want theapp-window
'smat-card
to do the same so that I can have a grid of equally spacedmat-card
s.How can I get the
mat-card
contained withinapp-window
to fill the entireapp-window
? I have tried some ideas but none were successful; more details below.Component HTML:
<mat-grid-list cols="3" rowHeight="4:3"> <mat-grid-tile> <app-window></app-window> </mat-grid-tile> </mat-grid-list>
Component CSS:
app-window { width: calc(100% - 10px); height: calc(100% - 10px); }
Attempted solutions and their results:
- Setting the
app-window
'smat-card
's width to awidth
andheight
of 100%. This makes themat-card
slightly larger than themat-grid-tile
that contains it. The desired behaviour would have been to match the size of theapp-window
. - Not assigning any extra CSS properties to the
mat-card
. As a result, themat-card
occupies theapp-window
's entire width but is only as tall as its content. I want it to always be the same height as theapp-window
. - Assigning the property
flex-grow: 1;
to themat-card
. Based on my understanding, this should make themat-card
fill the entire space of theapp-window
, i.e. its parent container. However, there is no effect and the result is the same as the second solution's.
-
tao over 6 yearsYou seem to need CSS help. CSS doesn't care much what you use to generate the final markup. Right now you require someone who knows well both Angular 4 and CSS to answer your question. Also, they will need to test it by creating a minimal reproducible example. If you create it yourself, linking required resources to reproduce the issue, anyone with sufficient CSS knowledge could help by inspecting the result featured in the snippet, even if they didn't know a thing about Angular 4 and how it builds up that markup.
- Setting the