need help CSS centering with absolute positioning

后端 未结 9 1197
庸人自扰
庸人自扰 2021-02-14 07:41

I\'ve never been totally satisfied with my various solutions to centering content in a web page. the

tag has been deprecated back in the middle of th
9条回答
  •  后悔当初
    2021-02-14 08:17

    body { text-align: center; }
    #wrapper { width: 960px; margin: 0 auto; text-align: left; position: relative; }
    #wrapper .child { position: absolute; left: 0; top: 0; }
    

    Just example code, but any .child element would be at 0,0 of the wrapper

    Any absolutely positioned element will absolutely position to it's nearest position-relative parent. If an element doesn't have a parent with position relative, it just uses the document. Below is an example without classes (some color and width styles added for clarity).

    
        
            
    This will absolutely position relative to the container div.
    This will absolutely position relative to the document

提交回复
热议问题