Passa al contingut principal

Default CSS Display Values for Different HTML Elements

Every element in an HTML document accepts a value for the CSS display property. The possible values you can use for display are many.


The three most commonly used values are none, block, and inline. But what if you don’t define a display value for an element? Well, all elements have an initial or default state for their display value. Let’s consider some of these and see some interesting things you might not have known.


Blocks and Text-flow Content


This one is pretty straightforward. I’ve covered block vs. inline before, but here’s a quick review.


Container-type elements (like <div>, <article>, <p>, etc.) will start out as block-level elements, so the computed value of the display property for these is “block”. All headers (<h1>, <h2>, etc.), even though they generally only contain text content, are likewise block elements by default.


On the other hand, elements that flow with text content are inline elements. That is, their computed display value is “inline”. These are elements like <span>, <em>,

and <cite>.


With both block and inline elements, you can (if you choose to) redefine their display value to something other than their default. Generally this is done in order to give an element a value of “none” or one of the alternate values (e.g. table). You can also change an inline element to block and vice-versa. But in most cases, this really is not the right choice. Instead, you’re better off using a different element altogether or otherwise rethinking what you’re trying to do.


Replaced Elements


In HTML, in addition to the elements already mentioned, there are also what are referred to as replaced elements. These include <img>, <input>, <select>, and <video>.


Although these are different from block and inline elements, they still have computed values for their display property. And they’re not always the same. For example, an <img> element, by default, is inline. Also, an <input> element is inline-block (which I’ve covered before).


Unknown Elements


Standards and accessibility advocates will hate me for telling you this, but the fact is, you don’t have to use any valid HTML5 elements in your documents. You could build an entire web page using your own made-up tags like <cookieMonster>, <potato>, and <sarcasm>.


So how would the browser read these foreign elements? Well, here’s a clue. Do you remember seeing this chunk of code in your CSS reset?



article,aside,details,figcaption,figure,
footer,header,hgroup,nav,section,summary {
display: block;
}

That’s added for older browsers (like IE6-8) that don’t recognize the new HTML5 elements. By default, in all browsers, all unknown elements have a computed display value of “inline”. Here’s a JS Bin that demonstrates this using a made-up element and then logging the display value of the element in the console.


And here’s another example with the same element given “block” status.


Invisible Elements


Here’s something you may not know is possible. HTML5 has a category of elements called document metadata. These include elements like <head>, <title>, and <style>. These elements, by default, are computed to display: none, as you can see by viewing the console in this JS Bin.


But, believe it or not, you can make them visible. Look at the screen shot taken below from the online version of Jeremy Keith’s HTML5 book:


Blocked title element on HTML5 for Web Designers


As shown in Chrome’s developer tools, the header for the site is styled by making the <title> element inside the <head> visible by means of display: block. In this case, both the <title> and <head> elements need to be blocked, not just the <title>.


This can also be done to make an element’s stylesheet editable by the user, as demonstrated in this JS Bin. We do this using the contenteditable attribute on the <style> element. In supporting browsers, with that attribute set to “true”, the user can make changes to the CSS and watch them take effect instantly.


Conclusion


Most of what I’ve discussed here has been covered, to some degree, before, so this might not all be new to everyone. If you know of any other interesting things about the default display values for various elements, or have any corrections/additions to anything I’ve said, I’d love to hear them.



Related posts:



  1. How is the DOM Affected by Improperly Nested HTML Elements?

  2. CSS3′s ‘space’ and ‘round’ Values for background-repeat

  3. How Do Browsers Render the Different CSS Border Style Values?








via Impressive Webs http://www.impressivewebs.com/default-css-display-values-html-elements/

Comentaris

Entrades populars d'aquest blog

Learn Composition from the Photography of Henri Cartier-Bresson

“Do you see it?” This question is a photographic mantra. Myron Barnstone , my mentor, repeats this question every day with the hopes that we do “see it.” This obvious question reminds me that even though I have seen Cartier-Bresson’s prints and read his books, there are major parts of his work which remain hidden from public view. Beneath the surface of perfectly timed snap shots is a design sensibility that is rarely challenged by contemporary photographers. Henri Cartier-Bresson. © Martine Franck Words To Know 1:1.5 Ratio: The 35mm negative measures 36mm x 24mm. Mathematically it can be reduced to a 3:2 ratio. Reduced even further it will be referred to as the 1:1.5 Ratio or the 1.5 Rectangle. Eyes: The frame of an image is created by two vertical lines and two horizontal lines. The intersection of these lines is called an eye. The four corners of a negative can be called the “eyes.” This is extremely important because the diagonals connecting these lines will form the breakdown ...

El meu editor de codi preferit el 2024, que això ja se sap que va canviant 😄

Visual Code Visual Code és un editor de codi font lleuger, però potent que s’executa al teu escriptori i està disponible per a Windows, macOS i Linux. Compta amb suport integrat per a JavaScript, TypeScript i Node.js i té un ric ecosistema d’extensions per a altres llenguatges i entorns d’execució (com C++, C#, Java, Python, PHP, Go, .NET).  És una eina ideal per a desenvolupar i depurar aplicacions web i en el núvol. Per què Visual Code? Visual Code té molts avantatges com a editor de codi font, com per exemple: És gratuït, ràpid i fàcil d’instal·lar i actualitzar. Té un ampli ecosistema d’extensions que et permeten afegir funcionalitats i personalitzar la teva experiència de desenvolupament. Té un suport integrat per a molts llenguatges i entorns d’execució, i et permet depurar i executar el teu codi des del mateix editor. Té una interfície senzilla i elegant, amb diferents temes i modes de visualització. Té un sistema de sincronització de configuracions que et permet guardar les...

Las Mejores Aplicaciones Gratis para iPad de 2012

Las Mejores Aplicaciones Gratis para iPad de 2012 : ¿No tienes ni un duro? No te preocupes, pues hoy os traemos una extensa selección de las mejores apps gratuitas que puedes conseguir en la App Store para que llenes tu iPad de calidad, sin gastar nada de nada.   ¿Estás buscando juegos o apps gratis para tu iPad? En la App Store hay más de 500,000 apps y juegos, y una gran cantidad de ellos está disponible de forma totalmente gratuita. Aquí vamos con la selección de las mejores Apps gratis para iPad (todos los modelos), organizada por categoría. ¿Estás preparado? Las Mejores Apps Gratis de Redes Sociales para iPad Nombre Facebook Gratis Categoría Redes sociales Facebook es la red social más famosa del mundo , con casi mil millones de usuarios. Su app para iPad ha tardado, pero aquí está. Nombre Twitter Gratis Categoría Redes sociales Twitter es la red de microblogging por excelencia. La forma más rápida y directa de informar y mantenerse informado de las cosa...