diff --git a/src/djlint/formatter/compress_html.py b/src/djlint/formatter/compress_html.py
index 65c1405..d707c4f 100644
--- a/src/djlint/formatter/compress_html.py
+++ b/src/djlint/formatter/compress_html.py
@@ -85,7 +85,7 @@ def compress_html(html: str, config: Config) -> str:
# put short single line tags on one line
html = re.sub(
re.compile(
- fr"(<({config.single_line_html_tags})(?:[^<>])*>)\s*([^<\n]{{,80}})\s*?((\2)>)",
+ fr"(<({config.single_line_html_tags})(?:\s(?:(?:{{%[^(?:%}}]*?%}})|(?:{{{{[^(?:}}}})]*?}}}})|[^<>])*)?>)\s*([^<\n]{{,80}})\s*?((\2)>)",
re.IGNORECASE | re.MULTILINE | re.DOTALL | re.VERBOSE,
),
r"\1\3\4",
diff --git a/src/djlint/formatter/expand_html.py b/src/djlint/formatter/expand_html.py
index 7fddeb5..8cf6ae9 100644
--- a/src/djlint/formatter/expand_html.py
+++ b/src/djlint/formatter/expand_html.py
@@ -69,7 +69,7 @@ def expand_html(html: str, config: Config) -> str:
# html tags - break before
html = re.sub(
re.compile(
- fr"{break_char}\K(?(?:{html_tags})(?:[^<>])*>)",
+ fr"{break_char}\K(?(?:{html_tags})(?:\s((?:{{%[^(?:%}}]*?%}})|(?:{{{{[^(?:}}}})]*?}}}})|[^<>])*)?>)",
flags=re.IGNORECASE | re.VERBOSE,
),
add_left,
@@ -79,7 +79,7 @@ def expand_html(html: str, config: Config) -> str:
# html tags - break after
html = re.sub(
re.compile(
- fr"(?(?:{html_tags})(?:[^<>])*>)(?=[^\n])",
+ fr"(?(?:{html_tags})(?:\s((?:{{%[^(?:%}}]*?%}})|(?:{{{{[^(?:}}}})]*?}}}})|[^<>])*)?>)(?=[^\n])",
flags=re.IGNORECASE | re.VERBOSE,
),
add_right,