CSS Positioning Margin Trouble

前端 未结 4 1784
[愿得一人]
[愿得一人] 2020-12-10 00:21

I am unsure how to position elements using css, as when I use methods like the following, whenever I resize the browser, the elements stay in the same place instead of where

4条回答
  •  长情又很酷
    2020-12-10 01:05

    Defining position: absolute in CSS takes the element in question out of the flow of the document.

    Think of this as layers: the bottom most layer is the document (though not always, depending on z-index!), and the top most layer is your element which you have defined as absolutely positioned.

    By setting position: absolute, you have told the browser that you will be responsible for positioning the element relative to the top left corner of the document (screen). Above, you have told the browser to position #logo 20px from the left and 20px from the top of the document. When you resize your browser viewport, that element will remain in that position.

    I think what you want is to position your element within the document flow, without using absolute positioning. This can be achieved with a combination of floats, margins, and padding.

提交回复
热议问题