How to align the radio buttons horizontally in angular material?
Asked Answered
P

3

47

I was expecting a in-built directive or a tag for this but probably not according to their documentation.

This is the example.

<div class="radioButtondemoBasicUsage" ng-app="MyApp">
<form ng-submit="submit()" ng-controller="AppCtrl">
<p>Selected Value: <span class="radioValue">{{ data.group1 }}</span> </p>

<md-radio-group ng-model="data.group1">

  <md-radio-button value="Apple" class="md-primary">Apple</md-radio-button>
  <md-radio-button value="Banana"> Banana </md-radio-button>
  <md-radio-button value="Mango">Mango</md-radio-button>

</md-radio-group>

Plasmagel answered 29/6, 2015 at 11:51 Comment(0)
S
137

You do not need to override any default CSS:

<md-radio-group layout="row">
  <md-radio-button value=0 class="md-primary">On</md-radio-button>
  <md-radio-button value=1> Off </md-radio-button>
</md-radio-group>

enter image description here

Just put in md-radio-group tag a layout="row" attribute.

Sabulous answered 2/10, 2015 at 14:56 Comment(0)
S
8

Better not override any existing class.

Use the directive ng-style and give value for display parameter.

<md-radio-group ng-model="selvalue">
  <md-radio-button value="A" class="md-primary" ng-style="{'display':'inline'}"> Apple</md-radio-button>
  <md-radio-button value="R" class="md-primary" ng-style="{'display':'inline'}"> Orange </md-radio-button>
</md-radio-group>
Sokil answered 12/8, 2015 at 11:56 Comment(2)
Works perfectly for Angular 5 (Material 2 v5.0).Rescript
To make it look little more accurate add md-radio-group[layout="row"] md-radio-button { margin: 10px; }Corazoncorban
S
0

If you use layout row and your radio group is in a row layout parent the text vertical aligin will break. The soulution is putting it in a empty wrapper element.

enter image description here

<div layout="row">
     ...
     <div>
        <md-radio-group ng-model="data.group1" aria-labelledby="favoriteFruit" layout="row">
          <md-radio-button value="Apple" class="md-primary">Apple</md-radio-button>
          <md-radio-button value="Banana">Banana</md-radio-button>
          <md-radio-button value="Mango">Mango</md-radio-button>
        </md-radio-group>
     </div>
     ...
</div>
Sialkot answered 4/8, 2020 at 9:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.