CSS【详解】对齐 (含文本垂直对齐,文本水平对齐、单行文本垂直居中、多行文本垂直居中、6 种方案块级元素水平垂直居中 、7 种方案图片水平垂直居中、文本自适应对齐、图标和文本对齐,图片和文本对齐等)

简介: CSS【详解】对齐 (含文本垂直对齐,文本水平对齐、单行文本垂直居中、多行文本垂直居中、6 种方案块级元素水平垂直居中 、7 种方案图片水平垂直居中、文本自适应对齐、图标和文本对齐,图片和文本对齐等)

vertical-align 文本垂直对齐

只对 display 的计算值为 inline、inline-block,inline-table 或 table-cell 的内联元素有效。( 浮动和绝对定位会让元素块状化,从而导致 vertical-align 失效)


属性值


线类,如baseline(默认值-基线对齐)、top(顶部对齐)、middle(居中对齐)、bottom(底部对齐);

基线对齐

在文本之类的内联元素,基线是字母x的下边缘

对于图片等替换元素,基线为元素本身的下边缘

一个inline-block元素,如果里面没有内联元素,或者overflow不是visible,则该元素的基线就是其margin底边缘;如果里面有内联元素,则其基线是元素里最后一行内联元素的基线。

文本类,如text-top、text-bottom;

上标下标类,如sub、super;

数值类,正值或负值,如-80px、20px、2em等。

负值全部都是往下偏移,正值全部都是往上偏移,而且数值大小全部都是相对于基线位置计算的,因此,从这一点来看,vertical-align:baseline等同于vertical-align:0。

由于是相对字母x的下边缘对齐,而中文和部分英文字形的下边缘要低于字母x的下边缘,因此,会给人感觉文字是明显偏下的,一般都会进行调整。比方说,我们给文字内容设置vertical-align:10px,则文字内容就会在当前基线位置再往上精确偏移10px

百分比类,正值或负值,如20%等,vertical-align属性的百分比值则是相对于line-height的计算值计算的,但很少使用。

【演示】文本和图片垂直方向对齐

要点:在图片上设置 vertical-align 样式

https://www.runoob.com/try/try.php?filename=trycss_vertical-align

text-align 文本水平对齐

  • 在容器上添加样式

属性值

描述
left 【默认】 把文本排列到左边。
right 把文本排列到右边。
center 把文本排列到中间。
justify 文本两端对齐
<div style="border: 1px solid red; text-align: center">你好</div>

【实战】单行文本垂直居中

行高 = 容器高度 
<div style="border: 1px solid red;height: 50px;line-height:50px;" >
你好
</div>

【实战】多行文本垂直居中

借助 CSS 的 行距上下等分机制 ,利用行高实现。

<template>
  <div class="box">
    <div class="content">
      很久很久以前,门口有座山,山上有座庙,庙里有一老一少两个和尚。有一天,老和尚给小和尚讲了一个故事。
    </div>
  </div>
</template>

<style scoped>
.box {
  /* 使用的line-height,而不是 height */
  line-height: 120px;
  background-color: greenyellow;
}
.content {
  /* 将内容变为行内块元素 */
  display: inline-block;
  /* 行内垂直居中 */
  vertical-align: middle;
  line-height: 20px;
  margin: 0 20px;
}
</style>

实战】块级元素水平居中

左右外边距都设置为 auto 

<template>
  <div class="parent">
    <div class="child"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 100px;
  width: 100px;
}

.child {
  background-color: green;
  height: 60px;
  width: 60px;
  margin: 0 auto;
}
</style>

【实战】块级元素水平垂直居中

方案:flex布局

<template>
  <div class="parent">
    <div class="child"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  /* 样式都写在容器-父元素上 */
  display: flex;
  justify-content: center;
  align-items: center;
}

.child {
  background-color: green;
  height: 60px;
  width: 60px;
}
</style>

多行块级元素的水平垂直居中

<template>
  <div class="parent">
    <div class="child1"></div>
    <div class="child2"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 200px;
  width: 100px;
  /* 样式都写在容器-父元素上 */
  display: flex;
  justify-content: center;
  align-content: center;
  flex-wrap: wrap;
}

.child1 {
  background-color: blue;
  height: 60px;
  width: 60px;
}

.child2 {
  background-color: green;
  height: 60px;
  width: 60px;
}
</style>

方案:flex布局 + 自动外边距

<template>
  <div class="parent">
    <div class="child"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  display: flex;
}

.child {
  background-color: green;
  height: 60px;
  width: 60px;
  margin: auto;
}
</style>

方案:单元格布局 + 自动水平外边距

<template>
  <div class="parent">
    <div class="child"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  /* 父元素内采用单元格布局 */
  display: table-cell;
  /* 子元素-垂直居中 */
  vertical-align: middle;
}

.child {
  background-color: green;
  height: 60px;
  width: 60px;
  /* 子元素-水平居中 */
  margin: 0 auto;
}
</style>


方案:子绝父相 + 上下左右0 + 自动外边距

<template>
  <div class="parent">
    <div class="child"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  /* 容器-父元素相对定位 */
  position: relative;
}

.child {
  background-color: green;
  height: 60px;
  width: 60px;
  /* 子元素绝对定位 */
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}
</style>

方案:子绝父相 + transform: translate

<template>
  <div class="parent">
    <div class="child"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  /* 容器-父元素相对定位 */
  position: relative;
}

.child {
  background-color: green;
  height: 60px;
  width: 60px;
  /* 子元素-绝对定位 */
  position: absolute;
  /* 子元素的左上角下移到垂直方向的中点 */
  top: 50%;
  /* 子元素的左上角右移到水平方向的中点 */
  left: 50%;
  /* 子元素向左,向上移动自身宽高的一半 */
  transform: translate(-50%, -50%);
}
</style>

方案:子绝父相 + margin 偏移

仅适用于子元素有明确宽高的情况,因需手动计算偏移距离,不推荐使用。

<template>
  <div class="parent">
    <div class="child"></div>
  </div>
</template>

<style scoped>
.parent {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  /* 容器-父元素相对定位 */
  position: relative;
}

.child {
  background-color: green;
  height: 60px;
  width: 60px;
  /* 子元素-绝对定位 */
  position: absolute;
  /* 子元素的左上角下移到垂直方向的中点 */
  top: 50%;
  /* 子元素的左上角右移到水平方向的中点 */
  left: 50%;
  /* 子元素向上移动自身高度的一半 */
  margin-top: -30px;
  /* 子元素向左移动自身宽度的一半 */
  margin-left: -30px;
}
</style>

【实战】图片在 div 内水平垂直居中

方案:flex布局

父元素采用 flex布局后,img 的 display 变成了 block

<template>
  <div class="box">
    <img src="./ecLogo.jpg" height="60" />
  </div>
</template>

<style scoped>
.box {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

方案:flex布局 + 自动外边距

<template>
  <div class="box">
    <img src="./ecLogo.jpg" height="60" />
  </div>
</template>

<style scoped>
.box {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  display: flex;
}

img {
  margin: auto;
}
</style>

方案:单元格布局 + 文本水平居中

<template>
  <div class="box">
    <img src="./ecLogo.jpg" height="60" />
  </div>
</template>

<style scoped>
.box {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}
</style>

方案:子绝父相 + 上下左右0 + 自动外边距

<template>
  <div class="box">
    <img src="./ecLogo.jpg" height="60" />
  </div>
</template>

<style scoped>
.box {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  position: relative;
}

img {
  position: absolute;
  /* 子元素绝对定位 */
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
}
</style>

方案:子绝父相 + transform: translate

<template>
  <div class="box">
    <img src="./ecLogo.jpg" height="60" />
  </div>
</template>

<style scoped>
.box {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  position: relative;
}

img {
  position: absolute;
  /* 子元素绝对定位 */
  position: absolute;
  /* 子元素的左上角下移到垂直方向的中点 */
  top: 50%;
  /* 子元素的左上角右移到水平方向的中点 */
  left: 50%;
  /* 子元素向左,向上移动自身宽高的一半 */
  transform: translate(-50%, -50%);
}
</style>

方案:子绝父相 + margin 偏移

仅适用于子元素有明确宽高的情况,因需手动计算偏移距离,不推荐使用。

<template>
  <div class="box">
    <img src="./ecLogo.jpg" height="60" />
  </div>
</template>

<style scoped>
.box {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  position: relative;
}

img {
  position: absolute;
  /* 子元素绝对定位 */
  position: absolute;
  /* 子元素的左上角下移到垂直方向的中点 */
  top: 50%;
  /* 子元素的左上角右移到水平方向的中点 */
  left: 50%;
  /* 子元素向上移动自身高度的一半 */
  margin-top: -30px;
  /* 子元素向左移动自身宽度的一半 */
  margin-left: -30px;
}
</style>

方案:行高与容器等高 + 文本垂直居中对齐

<template>
  <div class="box">
    <img src="./ecLogo.jpg" height="60" />
  </div>
</template>

<style scoped>
.box {
  border: 1px solid red;
  height: 100px;
  width: 100px;
  text-align: center;
  line-height: 100px;
}

img {
  vertical-align: middle;
}
</style>

【实战】文本自适应对齐

文本宽度小于容器宽度时居中对齐,文本宽度大于容器宽度时居左对齐

https://blog.csdn.net/weixin_41192489/article/details/114647286

【实战】图标和文本对齐

https://blog.csdn.net/weixin_41192489/article/details/112384315

  • 文字和图标垂直居中对齐(四种方法)
  • https://blog.csdn.net/weixin_41192489/article/details/115218875
  • 使用ex实现文本与图标对齐
    https://blog.csdn.net/weixin_41192489/article/details/115187975

【实战】图片和文本对齐

https://blog.csdn.net/weixin_41192489/article/details/112467616

目录
相关文章
|
1月前
|
存储 缓存 前端开发
常见的css优化方案都有那些
【7月更文挑战第7天】 - 合并压缩CSS减少HTTP请求,提高加载速度。 - 精简选择器,避免复杂嵌套和通配符,提升渲染效率。 - 利用继承和公共样式减少重复代码。 - 减少浮动和定位,使用Flexbox或Grid优化布局。 - 避免CSS表达式,使用CSS Sprites减少请求。 - 控制重排重绘,减少性能消耗。 - 利用CSS变量和现代布局技术提高灵活性。 - 服务器端启用GZIP压缩,客户端缓存CSS。 综合优化可显著提升网页性能。
23 4
|
4天前
|
前端开发 JavaScript
使用Vue+CSS实现汉堡图标过渡为叉号图标,有点意思
本文分享了如何使用Vue和CSS实现一个汉堡图标在点击时过渡为叉号图标的动画效果,并提供了详细的实现代码和效果演示。
11 0
使用Vue+CSS实现汉堡图标过渡为叉号图标,有点意思
|
5天前
|
前端开发
如何使用 CSS object-fit 进行图片的缩放和裁剪
如何使用 CSS object-fit 进行图片的缩放和裁剪
12 0
如何使用 CSS object-fit 进行图片的缩放和裁剪
|
1月前
|
前端开发 C++ 容器
CSS【详解】居中对齐 (水平居中 vs 垂直居中)
CSS【详解】居中对齐 (水平居中 vs 垂直居中)
15 0
|
1月前
|
前端开发 C++
css 鼠标悬浮显示放大图片 vs 鼠标点击显示放大图片
css 鼠标悬浮显示放大图片 vs 鼠标点击显示放大图片
25 0
|
3月前
|
前端开发
CSS水平居中与垂直居中的方法
CSS水平居中与垂直居中的方法
|
3月前
|
前端开发
css flex布局两个元素水平居中垂直居中
css flex布局两个元素水平居中垂直居中
50 1
|
前端开发
CSS水平居中+垂直居中+水平/垂直居中的方法总结
CSS水平居中+垂直居中+水平/垂直居中的方法总结
200 0
CSS水平居中+垂直居中+水平/垂直居中的方法总结
|
前端开发 容器
必须知道的css水平、垂直居中
  在网页制作中,有很多种水平、垂直居中方法,本文就归纳如下几种:   水平居中   1. 不建议用了。   2. text-align:center 在父容器里水平居中 inline 文字,或 inline 元素   3.margin:0 auto;   4.用 position 加 translate translate(-50%,-50%) 比较奇特,百分比计算不是以父元素为基准,而是以自己为基准。
127 0
|
前端开发
CSS水平和垂直居中技巧大梳理
水平居中   行内元素的水平居中 text-align:center(在父元素中设置) 只对内联元素或行内块元素有效 需要放置于父元素中   块级元素的水平居中 margin: 0 auto; 只对块级元素有效 auto指的是自适应宽度。
6538 0