Angular Material - Why is my md-button full-width?
Solution 1
Just wrap it in another div
if this is undesired. The flex-direction of <div layout="column">
seems to be responsible for the behavior.
Solution 2
An elements width defaults to 100% of it's container, unless specified otherwise.
germainelol
Updated on July 05, 2022Comments
-
germainelol almost 2 years
Simple issue, I've made a really basic page to play around with Angular Material. I have a button which is visible on medium-sized devices which toggles the side navigation, and for some reason it is full width. There is no CSS which is making it full width, and there are no Material directives which are making it full width. It seems to have inherited the width of the container it's in, but I can't see why.
Can anyone with a bit of Angular Material knowledge see why? (Just resize the window pane to see the button)
Here's my CodePen:
http://codepen.io/anon/pen/jPYNzy
And the code:
<!-- Container --> <div layout="column" layout-fill> <div layout="row" flex> <!-- Content --> <md-content flex> <md-toolbar> <div class="md-toolbar-tools"> <h1>Title</h1> </div> </md-toolbar> <div layout="column" class="md-padding"> <p> The left sidenav will 'lock open' on a medium (>=960px wide) device. </p> <md-button ng-click="toggleLeft()" hide-gt-md> Toggle left </md-button> </div> </md-content><!-- ./Content --> </div> </div><!-- ./Container -->
-
Sajeetharan almost 9 yearsI dont see any button
-
germainelol almost 9 years@Sajeetharan The button is viewable on
md
devices as I mentioned in my post. -
miyamoto almost 9 yearsThat's the reason why it's full-width.
hide-gt-md
sets a min-width and max-width. -
germainelol almost 9 years@miyamoto Yes, I saw that that the
min-width
is88px
, but there is nomax-width
anywhere, just awidth
setting. -
miyamoto almost 9 yearsI misspoke. It seems to be from the
<div layout="column">
flex. Removing-webkit-flex-direction: column
(I'm on Chrome) displays it inline with the text and of a "regular" size.
-
-
germainelol almost 9 yearsI don't think this is a good solution, this is very dependent on the text inside the button and not reusable.
-
germainelol almost 9 yearsAwesome, thanks! So when it comes inside the
div column
it treats the button almost as an entire column, but when it's wrapped inside another div, it treats the div as a column and the button as a regular button...right? I hope I've made myself clear! -
miyamoto almost 9 yearsYeah, basically I think. I'm a little fuzzy on it myself but the
layout
attribute turnsdiv
into a flex container and its immediate children into flex items. Theflex-direction
fromlayout="column"
means that they are being stacked (and then "flexed" in the horizontal direction??). Wrapping the button in anotherdiv
means that thatdiv
is the immediate child and the flex item. -
germainelol almost 9 yearsGreat explanation dude. I understood what you said, but Angular Material definitely needs a dictionary of all of these terms with 1 sentence descriptions. A lot of the examples on the docs are just images or CodePens which don't explain the theory very well.
-
miyamoto almost 9 yearsIt's not unique to angular-material, it's just css. I had to read these though, and still not a 100% what's going on behind the scenes. scotch.io/tutorials/a-visual-guide-to-css3-flexbox-properties css-tricks.com/snippets/css/a-guide-to-flexbox
-
germainelol almost 9 yearsI've read through this before and tried having a play with flexbox (not in angular-material), but it's a very different logic to standard bootstrap/foundation style grid systems and the way they work. I'll take another read of that article I think.
-
vijay kani almost 9 yearsOk for dependency of text inside. add attribute as "min-width:100px" and Width in Percentage. it does the trick... hope its helpful
-
germainelol almost 9 yearsIt works, but it isn't a good solution. By default the button should be dependent on it's content. You can see the above answer that I chose for a more reusable solution.