Six ways:
Absolute plus margin solution fixed plus margin solution display:table solution line-height solution flex layout solution transform unknown element width and height solution
Absolute + margin scheme
div{
position: absolute;
width: 100px;
height: 100px;
left: 50%;
top: 50%:
margin-top: -50px;
margin-left: -50px;
}
Fixed plus margin scheme
div{
position: fixed;
width: 100px;
height: 100px;
top: 0;
right:0;
left: 0;
bottom: 0;
margin: auto;
}
Display: table scheme
div{
display: table-cell;
vertical-align: middle;
text-align: center;
width: 100px;
height: 100px;
}
The line-height scheme for the inline element
div{
text-align: center;
line-height: 100px;
}
Flex Flexible Layout Solution
div{
display: flex;
align-items: center;
justify-content:center
}
Transform unknown element width and height solution
div{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
Read More:
- CSS: several ways to center the box vertically and horizontally
- How to center the box horizontally and vertically in HTML
- Selenium: 8 Ways to Locate Elements
- How to center your HTML button itself? ——Use the align attribute of center or Div
- valueError: Length mismatch: Expected axis has 40 elements, new values have 38 elements
- Latex: How to Set text Center
- Introduction to total phase data center
- 11. Center text
- V-for styles individual elements
- window.open () several ways to open windows
- Several ways for Ubuntu to open command line terminal window
- Learn English together | three ways to realize digital factorial with JavaScript
- Python uses the priority queue to get the maximum k elements
- Several ways to view spark task log
- JQuery is a solution to the disappearance of listening events after adding elements with append
- Several ways to check the IP address of raspberry pie
- Four ways to get Django parameters in request
- Three ways to get form data in struct2
- There are three ways to deal with the problem of vs (Visual Studio) 2017 flashback. I feel that none of them is the fundamental solution.
- One of the ways to solve the problem of internal server error in flash learning