可以使用以下方法将一个div居中,但不影响其内部元素:

  1. 使用flex布局

将父元素设置为flex布局,然后使用justify-content和align-items属性将子元素居中。

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

.child {
  /* 子元素样式 */
}
  1. 使用绝对定位

将父元素设置为相对定位,然后使用绝对定位将子元素居中。

.parent {
  position: relative;
}

.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* 子元素样式 */
}
  1. 使用margin:auto

将父元素设置为块级元素,并使用margin:auto将其居中。

.parent {
  display: block;
  margin: auto;
}

.child {
  /* 子元素样式 */
}
css将div居中但是不影响里面的元素

原文地址: https://www.cveoy.top/t/topic/bFOH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录