I would like to have an element aligned to the top right of the window. I wrote this:
<div class="container-fluid fixed-top">
<div class="row">
<div class="col-8"><!-- dummy --></div>
<div class="col-4 alert alert-primary">Here I am!</div>
</div>
</div>
The problem is that if I resize the window, the dummy column give an additional vertical space.
I tried to use float-right
, but my div
goes out from its container.
Is there a way just have my alert alert-primary
have a minimum text-width (let's say 20 chars) and being right aligned?
Bootstrap allows us to align elements by using the utility class float. As we want to align the button to the right side of the text box, we have to use the float-right class. Example: html.
Float Class The float classes in the bootstrap are used to float the element to the left or right of the containing block. The . float-end class is used to right-align the elements.
You can do this (no extra CSS)...
<div class="container-fluid fixed-top">
<div class="row">
<div class="ml-auto col-auto alert alert-primary">Here I am!</div>
</div>
</div>
https://www.codeply.com/go/KeorPCu7AR
The col-auto
class minimizes the column width to fit its' content, and then ml-auto
will push it to the right. This way you won't need the dummy column.
Edit
Also, it would be better to put the alert inside the column...
<div class="container-fluid fixed-top p-0">
<div class="row">
<div class="ml-auto col-auto">
<div class="alert alert-primary">Here I am!</div>
</div>
</div>
</div>
Also see: Right aligned Bootstrap container which also aligns with default container
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With