When you include ng-href in a button using AngularJS, it causes a shift in the alignment of the text within the button

Recently, I've been delving into Angularjs with angular-material and encountered a slight issue with ng-href. I created a Toolbar at the top of my webpage, but the moment I include the "ng-href" attribute to a button, the text inside the Button loses its center alignment:

See Example Image

The first two Buttons have the ng-href tag applied. However, the third one does not. Besides that, they are identical.

angular.module('angular2',['ngRoute','ngMaterial','ngMessages'])
    .config(function ($mdThemingProvider,$routeProvider) {
        $mdThemingProvider.theme('default')
            .primaryPalette('blue')
            .accentPalette('blue');
        $routeProvider
            .when("/", {
                templateUrl : "main.html"
            })
            .when("/test1", {
                templateUrl : "test1.html"
            })
            .when("/test2", {
                templateUrl : "test2.html"
            })
    })
    .controller('mainCtrl',function ($scope, $http, $mdDialog) {

    })
    ;
.navButton{
    font-weight: bold;
    margin: 0px;
    height: inherit;
    width: 150px;
    border-radius: 0px;
    text-transform: none;
    border: solid;
}
<!DOCTYPE html>
<html lang="en" ng-app="angular2">
<head>
    <meta charset="UTF-8>
    <title>Angular 2</title>

    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic">
    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="app.css">


    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-messages.min.js></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angular_material/1.1.0/angular-material.min.js></script>

    <script src="app.js"></script>
</head>

<body ng-controller="mainCtrl>
<div layout="column>
    <md-toolbar class="md-menu-toolbar" style="height: 64px">
        <div layout="row" style="height: inherit">
            <md-toogle-filler flex="5" style="height: inherit"" layout layout-align="center center">
                <md-icon class="material-icons md" style="font-size: 48px; height: 48px; width: 48px">>mail_outline
                </md-icon>
            </md-toogle-filler>
            <div flex layout="row" layout-align="start center" style="height: inherit">
                <md-button class="md-primary navButton" ng-href="#/">Main</md-button>
                <md-button class="md-primary navButton" ng-href="#/test1">(First Test)</md-button>
                <md-button class="md-primary navButton">(Second Test)</md-button>
            </div>
        </div>
    </md-toolbar>
    <div ng-view></div>
</div>


</body>

</html>

I'm having trouble centering the text within the buttons after adding the ng-href attribute.

Answer №1

I attempted to recreate this issue with a fiddle, but was unsuccessful. However, I did experiment with the md-button directive and it typically functions well in this scenario. It is possible that there may be a conflict in your app.css file.

When md-button detects an href or ng-href attribute, it will generate an <a> tag and place your button contents within a <span>.

<md-button class="md-primary navButton" ng-href="#/test1">Test 1</md-button>

transforms into:

<a class="md-primary navButton md-button md-ink-ripple" 
        ng-transclude="" ng-href="#/test1" href="#/test1">
    <span class="ng-scope">Test 1</span>
</a>

angular-material.css applies text-align:center to the <a> tag, so be sure to review your CSS for any styles affecting the attributes of the <span> when nested within an <a> tag.

Although not identical to your situation, here is the Fiddle I worked on: https://jsfiddle.net/phanxo14/

Answer №2

I was able to solve the issue by making a simple adjustment: Instead of utilizing the md-button that was later converted into an <a> tag by angularjs, I opted to copy the <a> tag produced by angular and used it directly:

<a class="md-primary navButton md-button md-ink-ripple" ng-transclude="" ng-href="#/" style="text-align; center; height:inherit" href="#/" layout>
    <span class="ng-scope" flex style="text-align: center">Main</span>
</a>

Additionally, I included the "layout" attribute to the surrounding <a> tag and the "flex" attribute to the inner <span>. Although I am unsure why this solution works, the text is now centered.

Update: A simpler and more effective fix would have been to just add the layout and layout-align="center center" attributes to the button.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Implementing a Callback Function when Jquery Countdown Reaches

Implementing this plugin will result in a live countdown displayed on the webpage. I recently reviewed the on.finish callback documentation found on the Official website. The main objective is to hide the span#limit once the timer completes its countdown ...

Adjust the size of the canvas element based on changes to its parent's dimensions

I am working with an application that includes a div containing a canvas element to display an image. Additionally, there is a sidebar that can be hidden with the click of a button, causing all other elements to resize and adjust to the remaining space. W ...

Unable to show the input's value

Need help in taking user input to display calculated values //html <div class="empty"> <h5> Enter Empty Seats </h5> <ion-item> <ion-input placeholder="Enter Number of Empties.." type="number" name="emptySeats" [( ...

What is the process of transforming an xhr request into an angular $http request?

I've been successfully loading images using ajax with the following code. However, when trying to convert it into Angular and use $http, it's not working as expected. Original ajax code var xhr = new XMLHttpRequest(); xhr.open('GET', ...

managing the focus and blur events within an Angular 1.5 component

While working on an angular js project recently, I encountered a situation involving handling focus and blur events in a textbox. My specific scenario required adding the $ sign when focus is lost from the textbox and removing it when the textbox is focuse ...

Guide to incorporating all template partials dynamically while leveraging a module in AngularJS

For several modules I have in my application, there is a need to load template partials whenever the module is used. These files are stored in a folder named partials inside each module subfolder. Starting from meanjs, I store these partials in public/modu ...

Taking data input from three textboxes and storing them in a PHP array

In my HTML form, I have 3 text input fields and one submit button. Each text field is intended for entering the name of an animal, which should then be saved into a PHP array. Here is the structure of my HTML form: <form action="Test.php" method="post ...

Creating a color-changing checkbox toggle in Bootstrap 5

Is it possible to customize the color of a Bootstrap 5 switch without changing it site-wide? I want some checkbox switches to have a unique color. Most tutorials online only cover how to change colors globally or use images, but I'm hoping to achieve ...

Setting the minimum width for Cards in Bootstrap: A How-To Guide

I'm facing an issue with Bootstrap cards. When I set a minimum width for the cards, they end up overlapping as I reduce the window width. How can I ensure that the third card moves to the next row to prevent this overlapping? https://i.sstatic.net/XA ...

Is it possible to utilize a route path in a JavaScript AJAX request?

So I have a situation with an ajax call that is currently functioning in a .js file, utilizing: ... update: function(){ $.ajax({ url: '/groups/order_links', ... However, my preference would be to use the route path instead. To achieve ...

What is the best way to integrate new entries into the data source of a Kendo UI grid?

After successfully creating a kendo.data.dataSource, I managed to bind it to the KendoUI Grid on my page. However, when attempting dataSource.insert(0, [a : "b"]);, it surprisingly removes the existing data. The code snippet that illustrates this issue i ...

Guide to building a hierarchical data object with JavaScript

Prior This object consists of multiple rows: { "functions": [ { "package_id": "2", "module_id": "2", "data_id": "2" }, { "package_id": ...

issue with AngularJS model not initially binding to select dropdown

I am facing an issue with a select dropdown in my code. I am using ng-repeat to populate the dropdown like this: <select ng-model="tstCtrl.model.value" required> <option ng-repeat="option in tstCtrl.myOptions" value="{{option.a}}">{{option.b ...

How about implementing a 'thumbs up' feature on every post?

I'm relatively new to SQL and PHP and have successfully set up a database where I can input entries through an HTML form. However, I am struggling with implementing a 'like button' feature that will increment a counter for each specific post ...

A stylish and versatile Bootstrap Modal is the perfect

Just dipping my toes into Bootstrap 4.5, but struggling with the Modal component. I've gone through the documentation multiple times and simply copied and pasted from the website. The code is so basic, yet I can't seem to figure out what's ...

Are you harnessing the power of ng-view for your highly dynamic content?

I'm currently in the process of developing a website that offers users the ability to search for various products, such as laptops. Below is the code snippet from my main index div: <div class="content" id="main"> <div id="search-wrap" ...

How can background wait for executescript in a Chrome Extension?

I'm currently encountering an issue while developing my first Google Chrome Extension. In my background.js script, I have a scenario where I call script.js every second. Here's a simplified version of the code: script.js: /* Some code */ if (co ...

AngularJS | Using ngAnimate to Display Form Validation Errors

My form errors are currently being displayed successfully using CSS and conditional classes, but I recently discovered ng-message and ng-animate. Would it be possible to use ng-message to contain the error messages? Also, I have been unable to find any tu ...

Incorporating jquery.prettyPhoto results in the script being displayed on the webpage

When I relocate the following script: <script src="js/jquery.prettyPhoto.js"></script> To this location: <script type="text/javascript"> ... </script> The script starts displaying on the page starting from the bold section on ...

The concept of selective importing within JavaScript

Seeking guidance on configuring conditional imports for a native library that is built for both node and electron. The challenge arises when one project requires the node implementation for testing, while another project needs the electron version. Projec ...