Passa al contingut principal

CSS Things That Don’t Occupy Space

CSS Things That Don't Occupy SpaceIn most cases, when you place an element on the page in your markup, if you don’t specify any special styles, it will occupy exactly the same space that it appears to occupy visually.


In other words, if you place a box sized at 200px by 200px on your page, anything you place after it in the source order, with no further styles added, will occupy the space below or beside the green box, outside of those set boundaries.


But not everything on an HTML page occupies space that is honored by other elements. I thought it would be interesting to list and describe all the things in CSS that don’t occupy this kind of physical space in an HTML document.


Absolutely Positioned Elements


This means any element that is set to position: absolute or position: fixed. These elements are taken out of the document flow, so elements that appear after them in the source order will not flow around or below them. So basically, other elements behave as if the absolutely positioned elements are not even there.


Offset Values for Relatively Positioned Elements


Unlike absolutely positioned elements, elements with position: relative do occupy space. But the offsets that you specify on relatively positioned elements using top, right, bottom, or left, do not occupy space. This might seem a bit odd at first, but look at this example:


http://jsbin.com/areyus/1/edit


The pink box follows the blue box in the source order, so naturally, the blue box’s occupied space is not available. So the pink box drops below. Next, the fact that the blue box is relatively positioned and then offset, causes the blue box to move. But this offset (20px from the top and 20px from the left) does not affect the position of the pink box. This is because the offset space on a relatively positioned element does not occupy any space.


Outlines


The outline property is very much like the border property, the difference being the fact that outlines don’t add to the occupied space of the element on which the outline is applied. This is why web developer tools can use outlines to highlight elements for debugging purposes, without causing elements around it to move or reposition themselves.


Look at this demo:


http://jsbin.com/inesuz/1/edit


Notice that the blue box has a 50px solid yellow outline. The outline overlaps the pink box below it, rather than pushing it down, as would happen if you change the outline to a border.


Offsets Caused by Transforms


Transforms, in many ways, have similar behaviour to relatively positioned elements. The most obvious types of transforms that do this are translation transforms, declared using the translate() function.


Translating an element using the transform property will cause the transformed element to reposition itself, pretty much the same way discussed above under relative positioning. This means that the original space occupied by the element will remain intact, as if the element hasn’t moved. But the offset created by the translation does not occupy space.


Similarly, when an element is rotated, skewed, or scaled, the original space occupied by that element is honored, but any areas covered by offsets due to any of these transforms will not affect the flow of other elements around.


Shadows


Box shadows and text shadows both fall under the category of elements that don’t cause reflow around them, thus not occupying any actual space in the document. When shadows are applied, as is the case with all of the other features discussed in this post, they don’t affect the position of any surrounding elements.


In the case of shadows, this is actually significant when you’re trying to center an element either vertically or horizontally. If an element has a shadow on a specific side and not the opposite side, this would make it appear not to be centered. This is because something like “margin: 0 auto” does not take into consideration the size of the shadow when centering the element, so you might have to add an opposing margin or something if you think this affects the aesthetics.


Other Miscellaneous Items


Some other notable things that are created by means of CSS but that don’t occupy space include:



  • WebKit Reflections

  • IE-only proprietary filters

  • text-decoration

  • Elements with display: none, which is different from elements with visibility: hidden, the latter of which do occupy space


What About Overflows?


One important thing that should be pointed out here is that even though these features discussed above do not occupy space in the document, some of them will cause scrollbars to appear if they are positioned in such a way as to render outside a parent element.


Something like a text shadow would not do this, but a transformed or relatively positioned element would. So although the offsets created by transforms or positioned elements may not take up space or affect surrounding elements, they will cause their parent element, or even the window, to create necessary scrollbars — unless of course the parent element in question is set to overflow: hidden.


“Occupy Space” — Maybe Not The Right Phrase?


The final thing I’ll say here is that, technically speaking, the phrase “doesn’t occupy space” may not be the best choice here, even though I’ve used it throughout this post.


When the spec discusses this sort of thing, it usually uses a phrase like “doesn’t cause reflow”. But I’ve tried to write this primarily with understandability in mind. I think it’s easy to understand what I mean when I say that something “doesn’t occupy space”, so in no way am I implying that my discussion here is semantically correct. The concept is really more important than my choice of words.


Is there anything I’ve missed here? Comment and I’ll add/correct.



Related posts:



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

  2. Using Absolute Positioning in CSS

  3. The CSS Clip Property








via Impressive Webs http://www.impressivewebs.com/css-things-that-dont-occupy-space/

Comentaris

Entrades populars d'aquest blog

10 alternativas a Cuevana para ver películas online

10 alternativas a Cuevana para ver películas online : Durante este último tiempo, en Cuevana se sucedieron varios “problemas” por los cuales hubo que ajustar algunas cosas antes de tiempo (como el rediseño del sitio), que dejaron a algunos usuarios ciertos problemas para acceder a las películas o series del portal. Pero realmente esto es algo que no incumbe a los usuarios y, como sabemos, existen muchas otras alternativas a Cuevana dando vueltas por Internet, que intentaremos presentar aquí mismo. Los sitios que repasaremos funcionan del mismo modo que Cuevana, mediante la instalación de un plugin que permite visualizar los videos de Megaupload o WUShare, entre otros servicios, en una calidad de imágen realmente excelente. Tal como sucede con el más popular servicio, todos ellos tienen publicidad que en algunos casos resulta insoportable, pero como dice Federico en DotPod “a caballo regalado no se le miran los dientes”. Alternativas a Cuevana 1. Moviezet Posiblemente el mejor clon d...

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 ...

Más de 50 de las mejores herramientas online para profesores

No es una exageración afirmar que hay ciento de aplicaciones educativas por ahí por la red, para todos los gustos y de todos los colores, por lo que es difícil tratar de recogerlas todas en un listado. Sin embargo, algunas destacan más que otras por su innovación y por su capacidad para conseguir adeptos, y esas son las que protagonizan la presente recopilación. Son 50 interesantes herramientas online basadas en las recopilaciones de EduArea , las muchas ya tratadas en Wwwhat’s new y las destacadas por la experiencia. 1. Dropbox : Un disco duro virtual con varios GB gratuitos y al que se accede desde casi cualquier dispositivo. Sin embargo, es muchísimo más que eso, de hecho ya comentamos 20 razones . 2. Google Drive : La evolución de Google Docs que suma a sus múltiples herramientas de creación de documentos, un considerable espacio virtual gratuito. 3. CloudMagic : Una extensión y una app multidispositivo para buscar información simultáneamente en Gmail, Twitter, Facebook, Evernote ...