How to get a base url in Magento 2

- Magento 2

Magento 2 Development

To get a URL from your Magento root directory, use getUrl. Since it inherits from the AbstractBlock class (Magento\Framework\View\Element\AbstractBlock), you are able to use it with any of your blocks. Check the below example:

The first parameter is the path you need, while the second one sets the _secure option if the user is browsing over https. You can add to the path by concatenating a specific filename onto the getUrl call or you could add it to the first parameter as well. Please note that the path is relative to the root directory of your Magento install.

More tips from Magento 2 Developer’s Cookbook

Source (Magento Stackexchange Question) (Nathan Toombs)