HTML--元素居中各种处理方法

前端开发 作者: 2024-08-25 22:10:01
对于行内元素可以使用: 对于块元素,你可以设置其左右外边距为:auto;同时你还应该设置该元素的宽度,不然的话,元素的宽度会撑满整个浏览器或者一种是没反应(不过你设置背景就会看到了)。 如果你想让多个

1、水平居中

.center-children {
  text-align: center;
}
如果你想让多个块元素在一行当中显示,首先你得设置display的属性,inline-block;在使用上面说的方法。还有一种方式是设置display:flex;justify-content: center;
 1 body {
 2   background: #f06d06;
 3   font-size: 80%;
 4 }
 5 
 6 main {
 7  white;
 8   margin: 20px 0;
 9   padding: 10px;
10 }
11 
12 main div {
13  black;
14   color:15  15px;
16   max-width: 125px;
17  5px;
18 }
19 
20 .inline-block-center {
21   text-align: center;
22 }
23 .inline-block-center div {
24   display: inline-block;
25  left;
26 }
27 
28 .flex-center {
29  flex;
30   justify-content:31 }
View Code
</
    I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
  >
main>

="flex-center">

    I'm a centered line.
  >
View Code
body {
  background: #f06d06;
  font-size: 80%;
}

main { white; 20px 0;
  padding: 40px;
}

main div { black;
  color:
  height: 100px;
  line-height: 20px;
  width: 50%;
  white-space: nowrap;
}
View Code
<table>
 2   <tr>
 3     <td>
 4       I'm vertically centered multiple lines of text in a real table cell.
 5     </td>
  </tr>
 7 </table>
 8 
 9 <div class="center-table">
10   <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
11 </div>
12 
13 16 }
17 
18 table {
19 20   width: 240px;
  border-collapse: separate;
22  20px;
23   height: 250px;
24 }
25 
26 table td {
27 28   border: 10px solid white;
31   /* default is vertical-align: middle; */
32 }
33 
34 .center-table {
35  table;
36 37 38 39 40 }
41 .center-table p {
42  table-cell;
43  0;
44 45 46 47 48   vertical-align: middle;
49 }
View Code
<div class="flex-center">
  <p>I'm vertically centered multiple lines of text in a flexbox container.</p>
 4 
 6  8 }
 9 
div {
11 12 14 }
15 
16 18   flex-direction: column;
 200px;
  resize: vertical;
  overflow: auto;
27 .flex-center p {
30 }
View Code
<div class="ghost-center">
  <p>I'm vertically centered multiple lines of text in a container. Centered with a ghost pseudo element</p>
 9 }
10 
20 }
21 
22 .ghost-center {
  position: relative;
25 .ghost-center::before {
26   content: " ";
 100%;
 1%;
31 }
32 .ghost-center p {
33 34  190px;
39 }
View Code
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_68626.html