Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply margins or css classes to Angular 2 component?

Tags:

css

angular

Suppose I have a component that I want to apply some margins to, preferably via Bootstrap helper classes, e.g. mt-3 to apply top margin. When I specify them on the component like this:

<my-custom-input 
   required 
   class="mt-3" 
   name="usr" 
   label="User" 
   placeholder="Please enter username" 
   [(ngModel)]="username">
</my-custom-input>

the class="mt-3" does not do anything! Setting the margin manually via Chrome dev tools is not possible either... I'm guessing because my-custom-input is not a predefined HTML element (like div), it cannot have defined margins?

This seems incredibly trivial thing to need in any application, but I'm surprised I was unable to find an answer thus far. How do we reposition such component? At the very least, specifying margins at the parent level (not inside components template) seems crucial thing to need?..

like image 680
Titan Avatar asked Jun 19 '17 10:06

Titan


People also ask

How we can set margins of an element by using CSS?

The CSS margin properties are used to create space around elements, outside of any defined borders. With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

How would you implement CSS logic in an angular application?

Angular components can be styled via global CSS the same as any other element in your application. Simply drop a `<link>` element on your page (typically in index. html) and you're good to go! However, Angular additional gives developers more options for scoping your styles.

Do margins add up CSS?

They will not add up. Whichever is larger will be used.

What are the two component decorator metadata properties used to set up CSS styles for a component?

You have used both styles and styleUrls metadata properties to style your component's HTML template.


Video Answer


1 Answers

Give it a display: block; in your css

like image 124
Meir Avatar answered Oct 08 '22 01:10

Meir