绝对定位+transform(translate)

  1. .outer{
  2. width: 300px;
  3. height: 300px;
  4. border: 1px solid #000;
  5. position: relative;
  6. }
  7. .inner{
  8. width: 50px;
  9. height: 50px;
  10. background-color: #ff0;
  11. position: absolute;
  12. top: 50%;
  13. left: 50%;
  14. transform: translate(-50%, -50%)
  15. }

绝对定位+trbl0+margin: auto

  1. .outer{
  2. width: 300px;
  3. height: 300px;
  4. border: 1px solid #000;
  5. position: relative;
  6. }
  7. .inner{
  8. width: 50px;
  9. height: 50px;
  10. background-color: #ff0;
  11. position: absolute;
  12. top: 0;
  13. right: 0;
  14. bottom: 0;
  15. left: 0;
  16. margin:auto;
  17. }

flex+jcc+aic

  1. .outer{
  2. width: 300px;
  3. height: 300px;
  4. border: 1px solid #000;
  5. display: flex;
  6. justify-content: center;
  7. align-items: center;
  8. }
  9. .inner{
  10. width: 50px;
  11. height: 50px;
  12. background-color: #ff0;
  13. }

flex+margin:auto

  1. .outer{
  2. width: 300px;
  3. height: 300px;
  4. border: 1px solid #000;
  5. display: flex;
  6. }
  7. .inner{
  8. width: 50px;
  9. height: 50px;
  10. background-color: #ff0;
  11. margin: auto;
  12. }

grid + margin:auto

  1. .outer{
  2. width: 300px;
  3. height: 300px;
  4. border: 1px solid #000;
  5. display: grid;
  6. }
  7. .inner{
  8. width: 50px;
  9. height: 50px;
  10. background-color: #ff0;
  11. margin: auto;
  12. }

grid + jsc + asc

  1. .outer{
  2. width: 300px;
  3. height: 300px;
  4. border: 1px solid #000;
  5. display: grid;
  6. }
  7. .inner{
  8. width: 50px;
  9. height: 50px;
  10. background-color: #ff0;
  11. justify-self: center;
  12. align-self: center;
  13. }