I coded based on the documents in this link. But my code doesn't work in SCSS.
this is my main.scss.
@import "node_modules/bootstrap/scss/bootstrap";
body {
background: red #{"/* rtl:blue */"};
}
and this is my html
<!doctype html>
<html lang="ar" dir="rtl">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="compiled.css">
<title>Test</title>
</head>
<body>
<h1>Test Test Test</h1>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js" integrity="sha384-SR1sx49pcuLnqZUnnPwx6FCym0wLsk5JZuNx2bPPENzswTNFaQU1RDvt3wT4gWFG" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-j0CNLUeiqtyaRmlzUHCPZ+Gy5fQu0dQ6eZ/xAww941Ai1SxSY+0EQqNXNE6DZiVc" crossorigin="anonymous"></script>
</body>
</html>
It may not be clear in the Bootstrap 5 docs, but RTLCSS is a JS module that is used compile special RTLCSS formatted CSS to CSS. For example,
RTLCSS with value directive:
CSS result (after processing with RTLCSS):
You can try this on this RTLCSS playground
Browsers don't understand the RTLCSS, they only understand CSS
RTLCSS is also different than using
dir="rtl"
. Using "dir=rtl" simply tells the browser the "directionality of the element's text".How Bootstrap 5 implements RTL support
Looking at Bootstrap 5 compiled CSS files you'll see there is now a
bootstrap.rtl.min.css
file. Bootstrap is using RTLCSS to process/compile the SASS generated LTR CSS. Here's how thebootstrap.rtl.(min.)css
files are generated by Bootstrap...bootstrap.css
) that contains comments with special RTLCSS directivesbootstrap.css
using RTLCSS modulebootstrap.rtl.css
which contains CSS rules that re-orient components like breadcrumbs, dropdowns, carousel, etc... for RTL support. The generatedbootstrap.rtl.css
also reverses the direction of "start" and "end" for padding, margins, floats, etc...So the build process is basically...
SASS files
>compile with SASS
>boostrap.css
(contains special RTLCSS directives in comments) >compile with RTLCSS
>boostrap.rtl.css
As you can see here, you can use SASS to change the generated CSS, but that generated CSS would still need to be post processed using RTLCSS in order to generate CSS rules that will work in the browser...
SASS:
Which would output to the following for our default CSS and RTL CSS:
Therefore, you need to post process using RTLCSS to get what you expect in
compiled.css
. If you didn't want to use RTLCSS like Bootstrap does, you could simply add CSS rules with thedir=rtl
selectors..Demo