Answers.Body: <p>There's nothing in Craft core that would cause this.</p>
  <p>Have you inspected the actual rendered HTML source, or are you looking at it in your browser's dev tools' DOM/elements panel? Typically, browsers will do their best to (auto)correct obvious HTML errors (like an unclosed tag).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-20T00:24:57.247Z
Answers.Text: You nailed it. Had no idea DevTools did that, especially because the source code shows the closing tags. Thanks!
Answers.UserDisplayName: user5965
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-19T21:51:32.757Z
Answers.LastActivityDate: 2022-10-19T21:51:32.757Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>Your question has already been answered, but I think I can guess what you're trying to do and there's a much cleaner way. Try something like this:</p>
  <pre><code>{% set innards %}
      &lt;h3&gt;Lots of lengthy markup here&lt;/h3&gt;
      &lt;p&gt;Lorem ipsum dolor&lt;/p&gt;
      &lt;p&gt;etc&lt;/p&gt;
  {% endset %}
  
  {% if condition %}
      &lt;a href=&quot;{{ thing.url }}&quot;&gt;{{ innards|raw }}&lt;/a&gt;
  {% else %}
      {{ innards }}
  {% endif %}
  </code></pre>
  <p>This is similar to Roman's answer above, but I prefer this way a bit using set...endset as you don't need to worry so much about escaping characters.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-20T22:05:16.877Z
Answers.Text: That's a great solution. Thanks!
Answers.UserDisplayName: user5965
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-20T08:07:06.263Z
Answers.LastActivityDate: 2022-10-20T08:07:06.263Z
Answers.UserDisplayName: James Smith
Answers.Body: <p>The browser will always try to close unclosed tags, that's the point.</p>
  <p>You can change your output logic to like this, but that's really a bad way:</p>
  <pre><code>  {% set condition = true %}
    {% set html = '' %}
    {% if condition %}
      {% set html = '&lt;a href=&quot;#&quot;&gt;My link' %}
    {% endif %}
    {# ... #}
    {% if condition %}
      {% set html = html ~ '&lt;/a&gt;' %}
     {% endif %}
    {{ html|raw }}
  </code></pre>
  <p>You must change the logic so that you do not use such &quot;crutches&quot;</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-19T21:58:41.560Z
Answers.LastActivityDate: 2022-10-19T21:58:41.560Z
Answers.UserDisplayName: RomanAvr
Body: <p>When an <code>&lt;a&gt;</code> is used within a conditional, the output of the <code>&lt;a&gt;</code> adds a self-closing a <code>&lt;/a&gt;</code>. Is there a way to prevent this from happening? I didn't realize this occurred.</p>
  <pre><code>{% if true %}
      &lt;a href=&quot;#&quot;&gt;
  {% endif %}
  
  EXPECTED OUTPUT: &lt;a href=&quot;#&quot;&gt;
  ACTUAL OUTPUT: &lt;a href=&quot;#&quot;&gt; &lt;/a&gt;
  </code></pre>
  
Tags: <craft3>
Title: Preventing <a> from self closing within conditional
ThreadText: # Preventing <a> from self closing within conditional
  
  (Asked by: username_0 on 2022-10-19)
  
  When an `<a>` is used within a conditional, the output of the `<a>` adds a self-closing a `</a>`. Is there a way to prevent this from happening? I didn't realize this occurred.
  
  ```
  {% if true %}
      <a href="#">
  {% endif %}
  
  EXPECTED OUTPUT: <a href="#">
  ACTUAL OUTPUT: <a href="#"> </a>
  
  ```
  
  **Tags:** craft3
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2022-10-19. Score: 0 | Accepted answer
  
  There's nothing in Craft core that would cause this.
  
  Have you inspected the actual rendered HTML source, or are you looking at it in your browser's dev tools' DOM/elements panel? Typically, browsers will do their best to (auto)correct obvious HTML errors (like an unclosed tag).
  
  > **Comments:**
  > **username_0**: You nailed it. Had no idea DevTools did that, especially because the source code shows the closing tags. Thanks!  
  
  ---
  
  ### Answer by username_2 on 2022-10-20. Score: 0
  
  Your question has already been answered, but I think I can guess what you're trying to do and there's a much cleaner way. Try something like this:
  
  ```
  {% set innards %}
      <h3>Lots of lengthy markup here</h3>
      <p>Lorem ipsum dolor</p>
      <p>etc</p>
  {% endset %}
  
  {% if condition %}
      <a href="{{ thing.url }}">{{ innards|raw }}</a>
  {% else %}
      {{ innards }}
  {% endif %}
  
  ```
  
  This is similar to Roman's answer above, but I prefer this way a bit using set...endset as you don't need to worry so much about escaping characters.
  
  > **Comments:**
  > **username_0**: That's a great solution. Thanks!  
  
  ---
  
  ### Answer by username_3 on 2022-10-19. Score: 0
  
  The browser will always try to close unclosed tags, that's the point.
  
  You can change your output logic to like this, but that's really a bad way:
  
  ```
    {% set condition = true %}
    {% set html = '' %}
    {% if condition %}
      {% set html = '<a href="#">My link' %}
    {% endif %}
    {# ... #}
    {% if condition %}
      {% set html = html ~ '</a>' %}
     {% endif %}
    {{ html|raw }}
  
  ```
  
  You must change the logic so that you do not use such "crutches"
  
  
  ---
  
  