14

TL;DR Is it a bad practice to change default display property in my CSS?

Issue

Recently, in our project we had to position 2 header tags so they would look like one. They had the same font size and similar styling so the only issue was how to place one next to another. We had 2 different ideas on that and it le do a discussion on whether or not is a good practice to change default display property

So, our very basic code

<div class="container">
    <h1>Header:</h1>
    <h2>my header</h2>
</div>

The outcome we would like to have:

Header: my header

Note: The code needs to consists of 2 different headings because on mobile version we want to display them in in separate lines (so leaving default display: block).

Approach #1: Use display: inline

This is pretty stright forward. Block elements became inline so they are positioned in the same line. The disadvantage of this approach is that default display properties of both h1 and h2 were changed.

Approach #2: Use float

H1 can be positioned on the left using float: left property. This approach leaves the default display property intact, but will requires some hacks if the .container is not long enough to fit both headers in single line.

The question

It all leads to a simple question: Is it a bad practice to change the default display property of HTML elements? Is it breaking the standard and should be avoided if possible? Or is it our bread and butter and it does not really matter, as long as code is semantically correct (so headers are placed in h1, articles are placed in article etc...)

dippas
  • 58,591
  • 15
  • 114
  • 126
dotintegral
  • 918
  • 1
  • 10
  • 23
  • "The code needs to consists of 2 different headings because on mobile version we want to display them in in separate lines" Your problem is not in changing the default display type, but in misusing headings. Just because a heading can span multiple lines doesn't mean it becomes two separate headings (of different ranks no less!). – BoltClock Jun 16 '16 at 08:07
  • Let's assume for the sake of the question, that we need to have two headings. Or let's forget about the headings for the time being. – dotintegral Jun 16 '16 at 08:12
  • Just a comment to the original problem you had. A better way, semantically and code-wise, is to simply add a span element inside your primary heading. [Here is an example.](https://jsfiddle.net/7Lstz0fc/). The second part will go onto a new line on window size < 640px. – Bram Vanroy Jun 19 '16 at 13:25
  • What ever happended to just targeting the structures you want and using good old CSS to override default properties with selectors? I see no need for this question. I have no clue how it got so many up votes. – Anthony Rutledge Jun 21 '16 at 20:38
  • @dotintegral Is it me, or are some of these answers trying to justify globally changing major CSS property defaults (display, visibility, etc..) when, except for CSS resets, there is nothing I can think of in the cannon that would suggest doing this is a best practice? – Anthony Rutledge Jun 21 '16 at 20:43
  • 3
    it is just you indeed. – dippas Jun 21 '16 at 22:32
  • 1
    @AnthonyRutledge, how to properly select elements in CSS is out of scope for this question. Don't read too much into the formatting. – Nils Kaspersson Jun 22 '16 at 07:04
  • 4
    This question is not about resetting the display value globally. Using selectors to target only the specific elements is implied. The question is what we should do with these elements once selected. – Mchl Jun 22 '16 at 07:18
  • @Mchl "It all leads to a simple question: Is it a bad practice to change the default display property of HTML elements?" – Anthony Rutledge Jun 22 '16 at 10:45

9 Answers9

16

Answering your main question:

tl;dr is it a bad practice to change default display property in my CSS?

NO


WHY?

A: Because it is all about semantics

Elements, attributes, and attribute values in HTML are defined (by this specification) to have certain meanings (semantics). For example, the ol element represents an ordered list, and the lang attribute represents the language of the content.

These definitions allow HTML processors, such as Web browsers or search engines, to present and use documents and applications in a wide variety of contexts that the author might not have considered.


So, in your case if you really need to have 2 headings semantically then you can change their styles, including the display property.

However If you don't need to have 2 headings semantically, but only for purely cosmetics/design (responsive code), then you are doing it incorrectly.

Look at this example:

<h1>Welcome to my page</h1>
<p>I like cars and lorries and have a big Jeep!</p>
<h2>Where I live</h2>
<p>I live in a small hut on a mountain!</p>

Because HTML conveys meaning, rather than presentation, the same page can also be used by a small browser on a mobile phone, without any change to the page. Instead of headings being in large letters as on the desktop, for example, the browser on the mobile phone might use the same size text for the whole the page, but with the headings in bold.

This example has focused on headings, but the same principle applies to all of the semantics in HTML.

** Emphasis in the quote above is mine **

P.S - Remember that headings h1h6 must not be used to markup subheadings (or subtitles), unless they are supposed to be the heading for a new section or subsection.


With all this above in mind, here is a few (good) approaches:

If you're doing the two headings purely for design then:

  • add a span inside of the h1, using a media query either using mobile first approach (min-width) or the non-mobile approach (max-width).

PROs - easily manageable through CSS, changing only properties.

CONs - adding extra HTML markup, using media queries as well.

h1 {
  /* demo only */
  background: red;
  margin:0
}
@media (max-width: 640px) {
  span {
    display: block
  }
}
<div class="container">
  <h1>Header:<span> my header</span></h1>
</div>

If you need to use the two headings semantically then:

  • use flexbox layout.

PROs - no need to add extra HTML markup or the use of media queries, being the most flexible currently in CSS (basically the cons from option above mentioned).

CONs - IE10 and below has partial or none support, Can I use flexbox ? (fallback for IE10 and below would be CSS TABLES)

.container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  /*demo only*/
  background: red;
}

h1,
h2 {
  /*demo only*/
  margin: 0;
}

h2 {
  /*640px will be flex-basis value - can be changed as prefered */
  flex: 0 640px;
}
<div class="container">
  <h1>Header:</h1>
  <h2>my header</h2>
</div>

Sources:

dippas
  • 58,591
  • 15
  • 114
  • 126
  • Target and override. – Anthony Rutledge Jun 21 '16 at 20:29
  • ids, classes, spans, and divs exists for a reason. Target the structure you want, then use selectors to override. Target and override. – Anthony Rutledge Jun 21 '16 at 20:33
  • Really you are going to use that argument? I'm just asking for a detail explanation on your comment..I do know the meaning of target and then override it. But i didn't considered a better approach given two headings.. than the flexbox layout. With only one heading that is being done with span tag. Again care to explain the comment and the downvote? – dippas Jun 21 '16 at 20:38
  • I explained it. Refresh your browser. – Anthony Rutledge Jun 21 '16 at 20:39
  • Then read my explanation about using flexbox. It is all a matter of using less HTML markup.. Why using ID or classes here? No need.. That's a over kill. If you see my 1st option I do use a span. – dippas Jun 21 '16 at 20:42
  • Heck, use pure CSS selectors if that floats your boat. The basic answer to the general question, especially about the display property, is no. – Anthony Rutledge Jun 21 '16 at 20:45
  • I don't see a reason for a downvote just because I didn't present an option with IDs and or classes then. – dippas Jun 21 '16 at 20:46
  • regarding using classes and IDs, it is that so hard to see this is just a simple demo, even so there is a class `.container` wrapping the headings, so they are restricted per say to that class if you target them in CSS like `.container h1` , but again **this is just a demo**, why the need to target with classes/ids when that's not the point of the question? – dippas Jun 21 '16 at 22:37
  • @dippas, Anthony Rutledge downvoted me as well. He commented vaguely with that "Target and override" malarkey, and I have class toggling in my answer. So obviously he either hasn't bothered to actually read the answers or cannot grasp the question and/or answers. – zer00ne Jun 22 '16 at 05:33
  • @dippas I understand the question better than you. The phrase "change the default display property of HTML elements" is the broadest CSS language possbile, implying the global selector. You can read the example and base your answer on that, or you can read the actual, fundamental question and give a sound answer to that. – Anthony Rutledge Jun 22 '16 at 10:44
  • 2
    apparently you didn't understand this question better than me ;) although I got your point of view. But not the whole point for this question. And that's why I think it is unfair the serial downvotes in (almost every) answer given here – dippas Jun 22 '16 at 10:49
  • @AnthonyRutledge it is just my impression, or you took the downvote, but regret it and then quickly downvote it again ? just wondering... – dippas Jun 22 '16 at 16:51
  • @Sound like wishful thinking. It was a mis-click. – Anthony Rutledge Jun 22 '16 at 16:54
  • so after the all the explanations you still to intend to keep the downvote? Ok, just to know – dippas Jun 22 '16 at 16:56
  • 1
    Thank you for your effort dippas. A boat of reputation is headed your way :) – Mchl Jun 24 '16 at 17:49
  • Well, i don't think this answer deserves +500 indeed, anyway congrats – Medet Tleukabiluly Jun 24 '16 at 19:17
  • @MedetTleukabiluly your answer definitely doesn't deserve it at all but hey thanks :) – dippas Jun 24 '16 at 19:19
  • 1
    lol I really had a good time in this question. Anyway @dippas nice answer. I also looked at some other answers you wrote and they were really good. I took some notes. Anyway congrats! :) – Jaqen H'ghar Jun 24 '16 at 21:29
  • Thanks @JaqenH'ghar `:)` – dippas Jun 24 '16 at 21:31
7

tl;dr is it a bad practice to change default display property in my CSS?

No. As expressed by W3C themselves; HTML conveys meaning, not presentation.

As an HTML author, it's your job to structure a page so that every section of the page carries the intended semantics as described by the documentation, so that software (browsers, screen readers, robots...) can correctly interpret your content.

As a CSS author, it's your job to alter the default styling of correct markup to present it the way you want to. This includes changing the default display properties just as much as changing the default color.

Any software can, however, decide that certain usage of CSS properties changes the way they interpret your page. For instance, a search engine could decide that text that has the same color as their parent's background should carry no weight for their ranking system.

In regards to subheadings, it's considered incorrect to markup a subheading with an <hX> element. What you should do is to decide on one <hX> element, wrap it in a <header> and wrap subheading-type text in <p>, <span> or similar.

The following is an example of proper subheadings, taken from the W3C documentation:

<header>
  <h1>HTML 5.1 Nightly</h1>
  <p>A vocabulary and associated APIs for HTML and XHTML</p>
  <p>Editor's Draft 9 May 2013</p>
</header>

Note that there's a discrepancy between the W3C specification and the WHATWG specification where the latter uses the <hgroup> element for this specific purpose, while the former has deprecated it. I personally go with W3C's example, but most software will still understand hgroup, likely for many, many years to come, if you prefer the WHATWG approach. In fact, some argue that WHATWG should be followed over W3C when the specs differ.

In your particular example, however, I'm not sure why you chose to split the <h1> into two elements in the first place. If what you marked up as an <h1> is actually supposed to be a generic "label" for the heading, then it should probably be considered a subheading instead. If you need to split it for styling purposes, wrap the two parts of text in <span> as such:

<h1>
  <span>Header:</span>
  <span>my header</span>
</h1>
Nils Kaspersson
  • 8,816
  • 3
  • 29
  • 30
  • Thank you for your answer Nils. I really wish I could split the bounty between you and @dippas somehow. – Mchl Jun 24 '16 at 17:45
4

tl;dr is it a bad practice to change default display property in my CSS?

Its a good practice but choose carefully when to use it because it can cause some critical structure mistakes.

Why is it a good practice

The display property is open for changes. It makes HTML simple and generic. HTML elements come with a default display value that match the general behavior - what you would usually want. But they dont have to be kept and manipulated around to imitate another display property. Think about <div> for example. Obviously most of the times you want it to have display: block;, but display: flex; is much more suitable once in a while.

Lets look at a really common example of lists. <li> comes with the display property of list-item that breaks the lines for every new item.

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

But horizontal lists are very common too. So why there is no special element for horizontal list items? Writing a special element for every common display behavior adds complexity. Instead, the convention, as also suggested by W3C is to set the <li> display property to inline.

ul li {
  display:inline;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

display: inline-block; as an alternative to float

float has been used massively in page layout for many years. The problem is that it wasnt created for this task and was originally designed to wrap text around elements. A well-known float issue is that non floated elements dont recognize floated children because they are being removed from the normal flow of the document. You also cannot centrally float an element. you are limited to left or right floats only.

display is much more suitable for layout many times. display: inline-block; tells browsers to place that element inline, but to treat it as though it were a block level element. This means that we can use inline-block instead of floats to have a series of elements side by side. It is more intuitive and eliminates floats <div class="clearfix"></div> which is an additional non semantic element in your HTML.

Floats are useful when there is a need to float an element so that other page content flows around it. But there is no need to always press them into the service of a complicated layout.

Things to avoid when changing display

When you change the display property remember:

Setting the display property of an element only changes how the element is displayed, NOT what kind of element it is.

<span> test case:

In HTML early versions <span> is considered an inline-level element and <div> is block-level. Inline-level elements cannot have block-level elements inside them. Giving the <span> a display:block; doesn't change his category. It is still an inline-level element, and still cannot have <div> inside.

HTML5 introduced content models. Each HTML element has a content model: a description of the element's expected contents. An HTML element must have contents that match the requirements described in the element's content model. <span> can contain only phrasing content. It means that still you cannot nest a <div> (flow content) inside a <span>. Giving <span> a display:block; still doesn't change it.

Avoid:

span {
  display:block;
}

<span>
  <div>
    Still Illegal!
  </div>
<span>

In conclusion, changing the default display property is certainly our bread and butter. Remember that it only changes how the element is displayed, NOT what kind of element it is and use it correctly.

Now about the original two heading issue:

With respect to the comments:

Let's assume for the sake of the question, that we need to have two headings. Or let's forget about the headings for the time being. - by the author

And also to the comment:

This question is not about resetting the display value globally. Using selectors to target only the specific elements is implied. The question is what we should do with these elements once selected. - by the person who set the bounty

Two headings side by side not only to handle mobile layout changes, can be done in many ways. The original example is simple and correct so its actually a good way.

h1, h2 {
  display: inline;
}
<div class="container">
    <h1>Header:</h1>
    <h2>my header</h2>
</div>

It follows HTML rules and doesnt require any additional hacks.

Jaqen H'ghar
  • 16,186
  • 8
  • 49
  • 52
  • Target and override. – Anthony Rutledge Jun 21 '16 at 20:28
  • 1
    @AnthonyRutledge Why downvoting a 0 score answer that someone clearly put a lot of effort in writing it? Dont you think the comment is enough? Anyway can you please fully explain your comment? I tried to understand it, I swear I did... And I read all your comments on dippas answer as well but didnt get it... Please dont be sarcastic and explain it in the context of my answer with an example if you can. Thank you I will appreciate it. – Jaqen H'ghar Jun 21 '16 at 21:54
  • 1
    @Effort does not negate the wrongness of your answer. I have explained, targeting and overriding is part of the elegance of CSS. The display property is basically universal across browsers (the browser style sheet) for the various structural HTML elements. Padding and margins are different because they can be very different in a cross browser sort of way. That is why the CSS reset was invented. Media queries were created to solve many of the issues this question brings up. There is no need to change the default value of a display property for a given element. It is just dumb. – Anthony Rutledge Jun 21 '16 at 22:09
  • @Jaquen This site has a Darwinian philosophy. The best answers rise to the top, and the worst tend to die off. It's just how it is. – Anthony Rutledge Jun 21 '16 at 22:12
  • @AnthonyRutledge thank you for explaining. Can you please re-read the end of my answer? The code snippet in the end is clearly not an advice. Its the OPs code which I pasted there only for the sake of the readers convenience. I also clearly stated that I answer it as if the original question was not about mobile layout adjustments. I answered it for a general case of a real need of two headings side by side (desktop and mobile alike) so it has nothing to do with media queries (and again - its not even a suggestion of how to do it). Please read the end of the answer again my friend thanks – Jaqen H'ghar Jun 21 '16 at 22:32
  • @JaqenH'ghar http://stackoverflow.com/questions/37853141/is-changing-default-display-value-a-good-practice#comment63359635_37908116 – dippas Jun 21 '16 at 22:39
  • @dippas sorry I dont understand my friend. I read all the comments in your answer before and also the new one now. can you explain the context a bit? Thank you – Jaqen H'ghar Jun 21 '16 at 22:44
  • Allegedly The "problem" is that it should've be done something like `

    Header:

    my header

    ` and then target/override default values using in CSS `.h1-class { properties}` . So given it is a demo, there is no point in doing this, plus it has a parent with class `.container`, so would be a overkill in markup html and extra CSS. And that's the reason for our downvotes
    – dippas Jun 21 '16 at 22:54
  • @dippas thank you for clarifying. Now I'm certain it has nothing to do with my answer... – Jaqen H'ghar Jun 21 '16 at 23:07
  • You are putting way too much thought into this. – Melinda Jun 22 '16 at 00:12
  • @Melinda yeah I guess you're right... I just wanted to know if there's a problem with my answer – Jaqen H'ghar Jun 22 '16 at 07:15
  • Question: "It all leads to a simple question: Is it a bad practice to change the default display property of HTML elements? " – Anthony Rutledge Jun 22 '16 at 10:48
  • After reading your comments here and on other answers I understand the problem. You clearly didnt understand the question... or the answers.. The question (and answers) are NOT about using html elements instead of classes as css selectors. they are about changing the display property of elements (with any selector class/id/...). I dont really know how you understood the question this way but what really puzzles me is how you thought the answers (not only mine) are suggesting it is a good practice to use this selectors technique. I guess you didnt read the answers... well they _are_ long... – Jaqen H'ghar Jun 22 '16 at 14:29
  • @JaqenH'ghar Venom is so useless on the Internet. – Anthony Rutledge Jun 22 '16 at 16:44
  • @AnthonyRutledge we can agree on one thing then :) – Jaqen H'ghar Jun 22 '16 at 17:35
  • 1
    I really like this answer JaqenH'ghar. It mentions some interesting topics which other answers didn't address. However I feel that @dippas' answer answers the question in the most direct and comprehensive way. – Mchl Jun 24 '16 at 17:42
  • 1
    @Mchl thank you! I liked dippas answer as well so I tried to cover other topics. He deserves the bounty so I'm happy he got it :) Thanks for the positive feedback! – Jaqen H'ghar Jun 24 '16 at 18:07
2

Sure changing the default behaviour is redundant and even can hit performance. As a subjective solution, would recommend to use flex (but i'm not sure about performance of it, altho you can google it), it's broadly supported, and doesn't change any element css properties, it's just a layout thing, check this out

.container {
  display: flex;
  justify-content: flex-start;
  flex-direction: column;
  align-items: baseline;
}
.container.mobile {
  flex-direction: row;
}
web
<div class="container">
  <h1>Header:</h1>
  <h2>my header</h2>
</div>
<hr />
mobile
<div class="container mobile">
  <h1>Header:</h1>
  <h2>my header</h2>
</div>

Notice that h1 styles stay the same

Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69
1

Changing default css properties is not a good idea, and should be avoided to prevent unwanted shortcomings in your markup. Instead, you should give "id" or better "class" to all html elements you want to customize and do the styling for those.

Besides, using css like "h1", "div" etc. is the slowest way as the engine try to find all those elements in the page.

In your example, it doesnt matter to use display or float as long as you give your h1 elements a css class.

Also, using correct html elements for better semantics can be useful for things such as SEO etc.

abeyaz
  • 3,034
  • 1
  • 16
  • 20
  • That was a simple example. But if I add classes, is changing the display based on classes regardless the default item display is still a good practice? – dotintegral Jun 16 '16 at 08:13
  • 1
    Yes, it is the best practise. Actually, this is how it's supposed to be. Classes are there to distinguish same elements in the same page with different look. – abeyaz Jun 16 '16 at 09:21
  • 5
    Why would changing default CSS properties be a bad idea? All elements have default values assigned to all properties, so any CSS you write _will_ change the default. – Nils Kaspersson Jun 19 '16 at 14:40
  • The point there is, is that changing the properties on `tags` could be a bad idea. Instead, change specifically what you want to change. For example, `span { display: block; }` might not be a good idea but `span.blockspan { display: block; }` or `#myspan { display: block; }` or just `.blockspan { display: block; }` could do a better job and not screw anything else on the rest of the page. – Nikhil Girraj Jun 22 '16 at 06:32
1

UPDATE

It seems that I might've obscured the Plunker, since Anthony Rutledge obviously failed to see (or neglected to review) it. I have provided a screen shot with a few tips on how to use the Plunker.

PLUNKER - Embed

PLUNKER - iNFO

PLUNKER - Preview

Plunker iNFO

Q & A

It all leads to a simple question: Is it a bad practice to change the default display property of HTML elements?

No, not at all. Matter of fact it's a very common practice of web developers (myself included), to alter not only properties of an element, but also attributes, and it's contents to name a few.

Is it breaking the standard and should be avoided if possible?

No, but perhaps the way one goes about doing it may break the code itself which IMO is a greater concern than standards. Standards of course plays an important role but not an essential one. If that were the case, then web browsers should comply under one common set of standards (I'm talking to you IE :P). Off the top of my head, here's things that should be avoided:

  • Using the table element for a layout

     <table>
       <tbody>
           <tr>
            <td><img></td>
            <td><input type="button"/></td>
           </tr>
     ...
    
  • Using inline styles

      <div style="display: inline-block"></div>
    
  • Using inline event handlers

      <div onclick='makeASandwich();'></div>
    

Or is it our bread and butter and it does not really matter, as long as code is semantically correct (so headers are placed in h1, articles are placed in article etc...)

Changing an element's display property is a very small yet fundamentally essential aspect of web developing. So yes I suppose it can be considered bread and butter, which would make semantics the parsley that's used as garnish and never eaten. Semantics is subjective, a way of thinking, it is not a standard. I believe a novice should be aware of it's importance (or at least how it's important to others), but should not be pontificating between an <article> and a <section> being semantically better than using a <main> and an <aside>. In due time, semantics will just feel right.


Approach #1: Use display: inline

I have never found a good reason to use display: inline because display: inline-block is a far better choice.

Approach #2: Use float

Floats are fragile antiques. Just like handling Grandma's bone china dinner plates, you must take certain precautions if you plan on using them. Be mindful of how to clear floats and don't throw them in the dishwasher.

Basically, if given only these 2 options, Approach #1 is a better choice, especially if using inline-block. I'd stay away from floats, they are counter-intuitive and break easily. I recall only using them once because a client wanted text wrapping around an image.


CSS & CSS/JS

Provided is a Snippet comprising of 3 demos:

  1. Pure CSS solution utilizing display: flex.

  2. Pure CSS solution utilizing display: table-row/table-cell.

  3. CSS and minimal JavaScript solution utilizing display: inline-block and the classList API

Each of these demos are identical on the surface:

HTML

  <section id="demo1" class="area">
    <!--==Pure CSS Demo #1==-->
    <!--======Flexbox=======-->
    <header class="titles">
      <h1>Demo 1 -&nbsp;</h1>
      <h2>display: flex</h2>
    </header>
   </section>

This is the original markup with the following changes:

  • div.container is now header.titles

  • h1 text is: "Demo #n"

  • h2 text is: "prop:value"

  • section#demo#n.area is wrapped around everything.

This is a good example of semantics: Everything has meaning

You'll notice at the bottom of the viewport, are buttons. Each button corresponds to a demo.

Details on how each demo works as well as pros and cons are in the following files located in the leftside menu of the Plunker (see screenshot):

  • demo1.md flexbox
  • demo2.md disply: table
  • demo3.md classList

PLUNKER


These notes are not for the purpose of informing the OP of anything relevant to the question. Rather they are observations that I would like to address later on.

Further Notes

  • Demo 1 and demo 2 are powered by the pseudo-class :target. Clicking either one of them will trigger the click event It resembles an event because it's invoked by a click, but there's no way of controlling, or knowing the capture or bubbling phase if it actually exists. Upon further clicking of the first and second button, it will exhibit odd behavior such as: toggling of the other button then eventually becoming non-functional. I suspect the shortcomings of :target is that CSS handles events in a completely different way with little or no interaction with the user.
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • Target and override. – Anthony Rutledge Jun 21 '16 at 20:28
  • Um...yeah, take a look at my [Plunker](http://plnkr.co/edit/g9mTYt4g7a5ppxi3pR2u?p=preview), third demo is toggling classes. My Plunker has 3 demos covering 3 different ways to manipulate `display`. 2 are pure CSS solutions since there are CSS tags but no JS. And I threw in another solution with some JS as a better alternative. When you downvote, please read the answer thoroughly. Better yet, leave a comment first, wait for a reply and if the reply wasn't satisfactory, then by all means voice your disapproval of a bad answer by downvoting. If I do not understand your statement, please expound. – zer00ne Jun 22 '16 at 03:38
  • Q: "It all leads to a simple question: Is it a bad practice to change the default display property of HTML elements?" – Anthony Rutledge Jun 22 '16 at 10:47
  • What theory? What is bad or good practice is merely an opinion, it is not backed by any specs, technical requirements, syntax--nothing that prevents, prohibits, or penalizes any person, code, organization from doing what you consider bad or good. There are real world situations when defaults are turned 180 degrees and are not edge cases. I never downvote based on opinion only what is wrong and what is right. My solution has nothing to do with that part of the question. There are several parts that required answers in the form of opinion and technical advice. My solution pertains to the latter – zer00ne Jun 22 '16 at 12:40
  • 1
    `inline-block` is not always a better choice than `inline`. Sometimes you want an inline-level element to generate an inline box instead of a block container. For example, you want to empathize some text inside a paragraph with ``, and you want to allow it to break among multiple lines. – Oriol Jun 22 '16 at 22:39
  • Agreed, `inline` definitely has it's uses, I meant I never had to *change* an element's `display` to `inline`. Adding the appropriate element in those circumstances is a better choice. – zer00ne Jun 23 '16 at 14:00
  • Thanks for your effort @zer00ne. I feel your answer is a bit chaotic and lacking sources. I really liked the plunker examples, but I think your answer would benefit from putting the pros/cons lists for each approach on SO as well. Thanks again! – Mchl Jun 24 '16 at 18:04
  • I'm more about results than talk. Your'e welcome @Mchl , it was fun. Lacking resources? I had 14 links, SO should really consider underlining anchors. – zer00ne Jun 24 '16 at 21:09
  • By 'sources' I meant links to official specs or articles like the one on hongkiat blog. Links to MDN or to other SO questions, while undoubtly useful, do not (in my personal opinion) bring that much value to answering this question. I said it before, and I'll say it again - it's a shame one can only accept/bounty a single answer. – Mchl Jun 24 '16 at 21:33
1

best Practice is to group the two heading in hgroup and change the display property for mobile and other views using @media query.

<hgroup class="headingContainer">
  <h1>Main title</h1>
  <h2>Secondary title</h2>
</hgroup>

The HTML Element (HTML Headings Group Element) represents the heading of a section. It defines a single title that participates in the outline of the document as the heading of the implicit or explicit section that it belongs to.

As hgroup defines a single title for a section ,therefore changing display property within hgroup is not bed practice.

Muhammad Nasir
  • 2,126
  • 4
  • 35
  • 63
  • `hgroup` is [obsolete](https://www.w3.org/TR/html5/obsolete.html#hgroup) and is no longer officially supported by any browser. It is recommended that you use `header` or `div`. – zer00ne Jun 22 '16 at 06:26
0

You should use:

$('element').css('display','');

That will set display to whatever is the default for element according to the current CSS cascade.

For example:

<span></span>

$('span').css('display','none');

$('span').css('display','');

will result in a span with display: inline.

But:

span { display: block }

<span></span>

$('span').css('display','none');
$('span').css('display','');
dippas
  • 58,591
  • 15
  • 114
  • 126
as7
  • 1
  • 1
  • This isn't a question on how to do it (with jQuery no less). It is about what should be done. – Mchl Jun 24 '16 at 17:36
-1

You can use flex box to arrange elements also, like this

<div class="container" style="display: flex;">
    <h1>Header:</h1>
    <h2>my header</h2>
</div>

Try to read this tutorial about flex, it is really great and easy to use https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Alongkorn
  • 3,968
  • 1
  • 24
  • 41