Is it possible to set the right margin to something other than 0 for a div element of class ="container-fluid"?

container-fluid has width: 100%; which causes the issue. You can add width: auto, i.e, w-auto to solve the issue.

<link 
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 
  rel="stylesheet" />
<div class="container-fluid m-2 bg-dark text-white w-auto">
  This is a test
</div>