Bootstrap 4 provides an easy way to create predefined alert messages: × Success! This alert box indicates a successful or positive action.
To close the alert message, add a . alert-dismissible class to the alert container. Then add class="close" and data-dismiss="alert" to a link or a button element (when you click on this the alert box will disappear).
If you want an alert that is fixed to the top and you are using bootstrap navbar navbar-fixed-top the following style class will overlay they alert on top of the nav:
.alert-fixed {
position:fixed;
top: 0px;
left: 0px;
width: 100%;
z-index:9999;
border-radius:0px
}
This worked well for me to provide alerts even when the user is scrolled down in the page.
Just wrap your inner message inside a div on which you apply your padding : http://jsfiddle.net/Ez9C4/
<div id="message">
<div style="padding: 5px;">
<div id="inner-message" class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
test error message
</div>
</div>
</div>
The simplest approach would be to use any of these class utilities that Bootstrap provides:
<div class="position-static">...</div>
<div class="position-relative">...</div>
<div class="position-absolute">...</div>
<div class="position-fixed">...</div>
<div class="position-sticky">...</div>
https://getbootstrap.com/docs/5.0/utilities/position/
<div class="position-static">...</div>
<div class="position-relative">...</div>
<div class="position-absolute">...</div>
<div class="position-fixed">...</div>
<div class="position-sticky">...</div>
<!-- Deprecated classnames -->
<div class="fixed-top">...</div>
<div class="fixed-bottom">...</div>
<div class="sticky-top">...</div>
https://getbootstrap.com/docs/4.0/utilities/position
I think the issue is that you need to wrap your div
in a container and/or row.
This should achieve a similar look as what you are looking for:
<div class="container">
<div class="row" id="error-container">
<div class="span12">
<div class="alert alert-error">
<button type="button" class="close" data-dismiss="alert">×</button>
test error message
</div>
</div>
</div>
</div>
CSS:
#error-container {
margin-top:10px;
position: fixed;
}
Bootply demo
Others are suggesting a wrapping div
but you should be able to do this without adding complexity to your html...
check this out:
#message {
box-sizing: border-box;
padding: 8px;
}
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