Coder Social home page Coder Social logo

codingstartup's Introduction

CodingStartup

上下左右置中对其的三种方法

左右置中的两种方法

  • display: inline / inline-block
    • 将父元素(容器)设定 text-align: center;
    • 就可左右置中
  • display: block
    • 将元素本身的 margin-leftmargin-right 设定为 auto,就可以左右置中。

三个上下左右置中的方法

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Centering Elements</title>
  <link rel="stylesheet" href="./style.css">
</head>
<body>
  <div id="black"></div>
</body>
/* -- Graphics For Demo -- */
html {
  background: url(https://codingstartup.com/assets/centering-elements/dart.png) no-repeat center center;
  background-size: 460px 460px;
  min-height: 100vh;
  margin: 0;
}

#black {
  background: url(https://codingstartup.com/assets/centering-elements/black.png) no-repeat center;
  background-size: cover;
  width: 160px;
  height: 160px;
}
/* -- Graphics For Demo -- */

Position Absolute

  • 对齐元素本身
#black {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Flexbox

  • 对齐元素内容
body {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

Display Table

  • 对齐元素内容
<div class="cell">
  <div id="black"></div>
</div>
/* -- Graphics For Demo -- */
html {
  background: url(https://codingstartup.com/assets/centering-elements/dart.png) no-repeat center center;
  background-size: 460px 460px;
  min-height: 100vh;
  margin: 0;
}

#black {
  background: url(https://codingstartup.com/assets/centering-elements/black.png) no-repeat center;
  background-size: cover;
  width: 160px;
  height: 160px;
  display: inline-block;
}
/* -- Graphics For Demo -- */

body {
  display: table;
  width: 100%;
  min-height: 100vh;
  margin: 0;
}

.cell {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

codingstartup's People

Contributors

douruxin avatar

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.