Align vertically using CSS 3

后端 未结 8 1205
忘了有多久
忘了有多久 2020-11-30 19:21

With CSS 3, are there any way to vertically align an block element? Do you have an example? Thank you.

8条回答
  •  悲&欢浪女
    2020-11-30 19:54

    I always using tutorial from this article to center things. It's great

    div.container3 {
       height: 10em;
       position: relative }              /* 1 */
    div.container3 p {
       margin: 0;
       position: absolute;               /* 2 */
       top: 50%;                         /* 3 */
       transform: translate(0, -50%) }   /* 4 */
    

    The essential rules are:

    1. Make the container relatively positioned, which declares it to be a container for absolutely positioned elements.
    2. Make the element itself absolutely positioned.
    3. Place it halfway down the container with 'top: 50%'. (Note that 50%' here means 50% of the height of the container.)
    4. Use a translation to move the element up by half its own height. (The '50%' in 'translate(0, -50%)' refers to the height of the element itself.)

提交回复
热议问题