6
In the example below, see that the div naturally occupies the whole width of the body
, but when I click the button Fix to change the property position
for fixed
, it loses its full width, looking like an element inline, occupying only the width of the small text within it.
function fixar(e){
e.outerHTML = '';
document.querySelector("div").style.position = "fixed";
}
div{
background: yellow;
height: 100px;
}
<div>
texto
</div>
<section>
<button onClick="fixar(this)">Fixar</button>
</section>
Why by changing the position property of a div
for fixed
it ceases to occupy the entire width of the screen, being only the width of the internal content, as if it were an element inline?
I know using
width: 100%
will occupy the whole width, but I don’t want solve the problem, I want to understand the behavior.
tries to put width:100%; in css
– Zaffar
Obg @Zaffar, but I would like to understand the behavior.
– Sam
I think that this occurs because it happens to be based on the
viewport
and no longer in the elementpai
, another kind ofposition
which promises to be a middle ground betweenfixed
andrelative
is thesticky
I think this one keeps the behavior ofdisplay:block
– Icaro Martins
@Icaromartins For he is young. Strange that. If it stays as reference the viewport and this is the width of the window, it should stay in the width of the window also rs... Curious that.
– Sam