Does adding a position: absolute to a block element make it behave like an inline? [closed]

跟風遠走 提交于 2019-12-01 21:13:43

问题


Please refer - https://jsfiddle.net/6nyh5p40/

position: absolute;

On the div is making it behave like an inline element. Remove the property and we see that the div behaves like it should, a block element.

My question - Does just adding a position: absolute to a block element make it behave like an inline?


回答1:


Yes, the block element feature of having the full width of the parent's content area will not be honored when an element is absolutely positioned.

If you want to retain the width (100% of the container) of a block element, then add postion: relative; to the parent of the absolute element, then set the width of the absolute element to 100%.




回答2:


Here is an excerpt from the Mozila Developer Network page:

Most of the time, absolutely positioned elements have auto values of height and width computed to fit the contents of the element. However, non-replaced absolutely positioned elements can be made to fill the available space by specifying (as other than auto) both top and bottom and leaving height unspecified (that is, auto). Likewise for left, right, and width.

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/position#Notes

So, as others have specified, it does not make it an inline element. It just adjusts it's height and width to take up only as much space as it requires.




回答3:


No Position absolute is basically to adjust the position of particular area Because position absolute remove that element from its current div.

To display in inline we usually use

display:inline-block;

OR

float:left;



回答4:


That does not mean it is like inline element.

absolute and fixed positioned elements loses the dimension. We need to give width, height.

Actually an inline element with position:absolute behaves like a block element.

https://jsfiddle.net/6nyh5p40/1/ - See how the span gets the height.

#x {
background: red;
height: 100px;
position: absolute;
}
span {
background: green;
height: 100px;
position: absolute;
}
<div id = "x">div 1</div>abcd

<span>Testing span</span>



回答5:


I did not get your question. If you want it to behave like an inline element then you should use

display:inline-block;

First, we should differentiate each other what position:absolute means. It means that it would be positioned absolutely in relative of the parent element. On the other hand, display:block; functions the same as <p> tag. It will occupy the entire line. Do not use position:absolute to line up the elements. You can use either display:inline-block or you can use float:left;



来源:https://stackoverflow.com/questions/31398209/does-adding-a-position-absolute-to-a-block-element-make-it-behave-like-an-inlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!