整个网页内容怎么居中

怎么让整个网页内容居中!整个body里面的内容居中,body中的图片也居中

可以通过以下 CSS 样式来让整个网页内容居中:

body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  margin: 0;
}

其中,display: flex; 将 body 元素设置为 flex 布局,justify-content: center; 和 align-items: center; 分别将子元素在水平和垂直方向上居中对齐,height: 100vh; 将 body 元素的高度设置为整个视口的高度,margin: 0; 去除 body 元素的默认边距。

如果要让 body 中的图片也居中,可以在图片的父元素上设置以下 CSS 样式:

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
}

其中,.parent 是图片的父元素的类名,display: flex; 将父元素设置为 flex 布局,justify-content: center; 和 align-items: center; 分别将子元素在水平和垂直方向上居中对齐。这样就可以让图片在父元素中居中显示了。

该回答引用chatgpt:flex布局


<!DOCTYPE html>
<html>
<head>
  <style>
    html, body {
      height: 100%;
      margin: 0;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  </style>
</head>
<body>
  <!-- 网页内容 -->
  <div>
    <img src="image.jpg" alt="图片">
    <h1>网页标题</h1>
    <p>网页内容</p>
  </div>
</body>
</html>

给body一个text-align: center;

在你的HTML代码中,找到body标签组,使用center标签组将它包起来,可实现你想要的效果。
就像这样:

<center>
    <body></body>
</center>

介绍几种左右居中的方法:

  1. flex
  2. absolute + transform
  3. absolute + margin
  4. Grid
  5. text-align

下边是栗子:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>左右居中</title>
  <style>
    * {
      padding: 0;
      margin: 0;
    }
    
    body {
      height: 100vh;
    }
    
    /* flex方法 */
    /* .g-container {
      display: flex;
      justify-content: center;
      height: 100%;
    } 
    .g-content {
      width: 300px;
      height: 300px;
      text-align: center;
      line-height: 300px;
      background-color: pink;
    } */
   

    /* absolute + transform */
    /* .g-container {
      position: relative;
      height: 100%;
    }

    .g-content {
      position: absolute;
      left: 50%;
      width: 300px;
      height: 300px;
      text-align: center;
      line-height: 300px;
      background-color: pink;
      transform: translate(-50%, 0);
    } */

    /* absolute + margin */
    /* .g-container {
      position: relative;
      height: 100%;
    }

    .g-content {
      position: absolute;
      right: 0;
      left: 0;
      width: 300px;
      height: 300px;
      margin: 0 auto;
      text-align: center;
      line-height: 300px;
      background-color: pink;
    } */

    /* Grid */
    /* .g-container {
      display: grid;
      height: 100%;
    } 
    .g-content {
      justify-self: center;
      width: 300px;
      height: 300px;
      text-align: center;
      line-height: 300px;
      background-color: pink;
    } */

    /* text-align */
    .g-container {
      height: 100%;
      text-align: center;
    } 
    .g-content {
      display: inline-block;
      width: 300px;
      height: 300px;
      text-align: center;
      line-height: 300px;
      background-color: pink;
    }
  </style>
</head>
<body>
  <!-- 
    <center>:
      HTML Center 元素(<center>)是个块级元素,可以包含段落,以及其他块级和内联元素。这个元素的整个内容在它的上级元素中水平居中(通常是 <body>)。
      已弃用: 不再推荐使用该特性。虽然一些浏览器仍然支持它,但也许已从相关的 web 标准中移除,也许正准备移除或出于兼容性而保留。请尽量不要使用该特性,并更新现有的代码;参见本页面底部的兼容性表格以指导你作出决定。请注意,该特性随时可能无法正常工作。
    <center>
      <div class="g-container">
        <div class="g-content">忍冬</div>
      </div>
    </center> 
  -->
  <div class="g-container">
    <div class="g-content">忍冬</div>
  </div>
</body>
</html>

我找找我的笔记

4, 水平居中

<!--块级元素-->
<div class="box" style="border: #0aa66e solid 1px;height: 200px;width: 400px;">
      <div class="content" style="width:100px; background-color: #0A8CD2">
            我是块级元素,我要水平居中。或者还有垂直居中.我定宽不定高.
      </div>
</div>
<br><br><br>
<!--行内元素-->
<div class="box" style="border: #0aa66e solid 1px;height: 200px;width: 400px;">
      <span class="inlineContent" style="background-color: #0A246A;">我是行内元素,我要水平居中。</span>
</div>

行内元素 :只需要紧邻父级 text-align:center;

块级元素

  • (定宽)方式一:margin: 0 auto;

  • (定宽)方式四 绝对定位:父级:position: relative;当前元素:position:absolute; top:0; left: calc(50% - 50px);50% 减去自身宽度。

  • (不定宽)方式二inline-block: 父级 ,text-align: center; 当前元素:display: inline-block;

  • (不定宽)方式三flex:父级: display:flex; justify-content:center:

  • (不用定宽)transform:

    • 父级:

      position: relative;
      
    • 当前元素:

      position:absolute; left:50%;
      transform: translateX(- 50%);
      

5, 垂直居中

**单行行内元素:** `line-height`等于盒子 `height`",即行高等于盒子的高"

**多行行内元素:** 父元素设置 `display:table-cell`;和`vertical-align: middle`;

**块级元素:**

- (定高)绝对定位 : 父级:`position: relative;`当前元素:`position:absolute; top:calc(50% - 50px); left: 0`  50% 减去自身高度。
- (不用定高)**flex**:父级:`display: flex; align-item: center;`
- (不用定高)transform:父级:`position: relative;`当前元素:`position:absolute; top:50%; transform: translateY(- 50%)`