Giter Site home page Giter Site logo

水平垂直居中 about you-need-to-know-css HOT 6 OPEN

l-hammer avatar l-hammer commented on July 29, 2024
水平垂直居中

from you-need-to-know-css.

Comments (6)

zhangyalins avatar zhangyalins commented on July 29, 2024

绝对定位 position: absolute 限定宽高 这个要给父元素添加相对定位position:relative,要不然都跑出去了,flex好像没啥作用

from you-need-to-know-css.

zhangyalins avatar zhangyalins commented on July 29, 2024

我想知道top: calc(50% - 16px); left: calc(50% - 72px); 为啥是减去16px 和72px呢,有什么原理吗

from you-need-to-know-css.

l-hammer avatar l-hammer commented on July 29, 2024

我想知道top: calc(50% - 16px); left: calc(50% - 72px); 为啥是减去16px 和72px呢,有什么原理吗

这里减去的是元素自身宽/高的一半

from you-need-to-know-css.

l-hammer avatar l-hammer commented on July 29, 2024

绝对定位 position: absolute 限定宽高 这个要给父元素添加相对定位position:relative,要不然都跑出去了,flex好像没啥作用

是需要加relative的。绝对定位元素相对于最近的非 static 祖先元素定位。当这样的祖先元素不存在时,则相对于ICB(inital container block, 初始包含块)

from you-need-to-know-css.

lcl-101 avatar lcl-101 commented on July 29, 2024

这是在移动端经常使用的方式

display: flex no limit to width & height

main {
    width:100%;
    height:100%;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 152px;
}
main > div {
    flex: 0 1 auto;
    background: #b4a078;
    color: white;
    padding: .3em 1em .5em;
    border-radius: 3px;
    box-shadow: 0 0 0.5em #b4a078;
}
<main>
  <div>Center me, please!</div>
</main>

image

from you-need-to-know-css.

wlll122 avatar wlll122 commented on July 29, 2024

定位居中,应该在父元素加一个position:absolute;更加容易懂吧!

from you-need-to-know-css.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.