-4

When you have to specify the same .css class for multiple headers <h3>, is it better to use the <br> tag or is it better to repeat the header while specifying the same style class over and over again? In this specific scenario, I can't put styling on the surrounding <div>

Which one is best practice and why?

Code example while using <br> tags:

    <div class="more_room_left | more_room_top">
        <h5 class="no_margin_top_bottom">
            *location?.streeth* *location?.number*
            <br> *location?.postcode* *location?.city*
            <br> *location?.country*
            <br>
        </h5>
    </div>

Code example using different headers:

    <div class="more_room_left | more_room_top">
        <h5 class="no_margin_top_bottom">*location?.streeth* *location?.number*</h5>
        <h5 class="no_margin_top_bottom">*location?.postcode* *location?.city*</h5>
        <h5 class="no_margin_top_bottom">*location?.country*</h5>
    </div>

Visual example:

Visual example

EDIT:

After reading more about when to use the <br> tag, I've concluded that in my case the <br> tag has no value and is considered a hack. I reworked the way I'm styling the headers <h3>, which are now paragraphs <p>, to fit the styling I need without using the <br> tag. This solution was handed to me through an already removed answer, so I can't give credit.

HTML

    <div class="address_container | more_room_left | more_room_top">
        <p>*location?.streeth* *location?.number*</p>
        <p>*location?.postcode* *location?.city*</p>
        <p>*location?.country*</p>
    </div>

CSS

.address_container > p {
    margin-top: 0;
    margin-bottom: 0;
}
Wouter Vanherck
  • 2,070
  • 3
  • 27
  • 41
  • I'd use multiple `

    ` tags but that's just me.

    – apokryfos Mar 23 '17 at 08:58
  • @apokryfos Then that will not have header styling and OP will have to add that manually – Rajesh Mar 23 '17 at 08:59
  • @Rajesh he already is doing manual styling on the header so using the same class will probably achieve the same result. Again, it's just how I would do it. I reserve using headers as headers to sections (which is not the case here). Note that HTML tags are mostly for semantic purposes nowdays and not for styling. – apokryfos Mar 23 '17 at 09:01
  • @apokryfos Thats correct. – Rajesh Mar 23 '17 at 09:09
  • @apokryfos I have added a wiki answer and have tried to make it subjective enough. Please feel free to add if anything is missing – Rajesh Mar 23 '17 at 09:14

2 Answers2

5

There are various factors that would define which one should be used.

For example, if you wish to retrieve this header text, how you want value. You want entire text or individual values. If individual values are expected, I'd suggest using multiple headers.

On the contrary, if you want to receive entire value together, use single header with brs. But using <br/> is not considered good practice and you should refer: Is it sometimes bad to use <BR />?


Another such case would be for adaptive design. On smaller screen how should they wrap? If you are fine with *location?.streeth* *location?. on one line and number* on next, you can use single header. But if you want *location?.number to wrap in next line, using multiple elements would suit better.


If values are not going to be retrieved and its just for view, then either one would do. But in such cases, I'd go for <p> (as suggested by apokryfos) and add custom styling for view.


It is worth noting that HTML 5 has put a great deal of effort into making web pages semantic by adding a number of tags which are visually identical but differ in name and therefore purpose. For that reason, you should use a header tag (like e.g. `) to contain a 5 level header, e.g., when you need to specify a 5 level subsection to a document. This means that you should not use a header at all for the elements in question because the sole purpose of the header being used is for styling rather than offering semantic significance.

Community
  • 1
  • 1
Rajesh
  • 24,354
  • 5
  • 48
  • 79
-1

Based on your code, I would personally use different header's or paragraph's to achieve the spacing as well as allow for styling. If in the future you wish to style one of these parts, it will be much easier to do so at the block level.

    <div class="more_room_left | more_room_top">
      <p class="no_margin_top_bottom">*location?.streeth* *location?.number*</p>
      <p class="no_margin_top_bottom">*location?.postcode* *location?.city*</p>
      <p class="no_margin_top_bottom">*location?.country*</p>
    </div>
Arjan Knol
  • 942
  • 5
  • 20
  • Doesn't it matter that I keep using the same class in different headers while at the same time, I could do it in one header? – Wouter Vanherck Mar 23 '17 at 09:02
  • 1
    This is true but, If in the future you wish to style one of these parts, it will be much easier to do this. So for me this would be better practice . – Arjan Knol Mar 23 '17 at 09:04