整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

工作中常用且容易遗忘的 CSS 样式清单整理(一)

工作中常用且容易遗忘的 CSS 样式清单整理(一)

大家精心准备了工作中常用的CSS样式,都是自己工作中常用到的记录发,分享给大家,如果觉得可以希望点赞关注和评论。

废话不多说先上图:




最后把源码奉献给大家

SS 是前端里面的基础之一,也是非常重要的一部分,它往往决定了你所做出来的网页页面是否美观。在设计网页页面的过程中,总会有将元素或者文字进行水平垂直居中的要求。下面w3cschool编程狮就为大家介绍 CSS 中几种常用到的水平垂直居中的方法。


一、使用 margin:auto

当元素有给定的高度以及宽度的时候,使用 margin: auto; 元素仅会水平居中,并不会进行垂直居中。此时就需要设置元素的 position 为 absolute,父级元素的 position 为 relative,同时元素的上下左右都需要设置为 0。

HTML 代码

<div class="box">
  <div class="center1"></div>
</div>

CSS 代码

.box{
  width: 200px;
  height: 200px;
  background-color: #eee;
  position: relative;
  margin-top: 20px;
}
.center1{
  width: 50px;
  height: 50px;
  background-color: #00ACED;
  margin: auto;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
}

效果展示:



二、使用 position:absolute

当已经知道了要进行水平垂直居中的元素的宽高时,就可以通过设置 position: absolute 来实现。但是,使用的同时还需要结合其他属性才完整实现。因为,单是设置 absolute,上左距离均为一半,就会出现下面这种情况。很显然可以看到,元素并不是完全居中,仅只有左上角的位置在中心点

概念图:

因此想要实现元素完全水平垂直居中,在设置了 absolute 定位后,可以设置 margin 值为负,或者使用 calc 来计算,上左距离在 50% 的基础上还要减去元素本身一半的宽高。

margin 值为负或者 calc 计算均是在已知元素宽高的情况下,假设不知道元素的宽高,那么怎么实现水平垂直居中呢?这里就可以使用 transform 属性,通过坐标位移来实现居中。

CSS 代码

/* 结合 margin */
.center2{
  width: 50px;
  height: 50px;
  background-color: #7FFFD4;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -25px;
  margin-top: -25px;
}
/* 结合 calc 计算*/
.center2{
  width: 50px;
  height: 50px;
  background-color: #7FFFD4;
  position: absolute;
  left: calc(50% - 25px)
  top: calc(50% - 25px);
}
/* 结合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}

效果展示



03

PART

三、使用弹性布局

可以通过弹性布局来设置水平垂直居中,这里需要设置父级元素 display:flex; 还需要设置两个属性,水平布局 justify-content 以及垂直布局 align-items。

HTML代码

<div class="box2">
  <div class="center4"></div>
</div>

CSS代码:

.box2{
  background-color: #eee;
  width: 200px;
  height: 200px;
  position: relative;
  margin-top: 20px ;
  display: flex;
  justify-content: center;
  align-items: center;
}
.center4{
  width: 50px;
  height: 50px;
  background-color: #B39873;
}

效果展示:


四、文本水平对齐和行高

前面介绍的是元素如何实现水平垂直居中,下面介绍的是如何将文字进行水平垂直居中。这第一个方法也是最经常用的,使用文本水平对齐 text-align 和行高 line-height 来实现的。

HTML 代码

<div class="box3">
  <div class="center5">文字居中</div>
</div>

CSS 代码

.box3{
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
}
.center5{
  text-align: center;
  line-height: 200px;
}

效果展示


05

PART

五、使用网格布局

第二个方法可以通过网格布局 grid 来实现。而这里通过 grid 有两种方式实现,一种对元素本身属性进行设置,另一种在元素的父级元素中设置。两者看上去内容似乎差不多,不同的是在元素中设置的是 align-self 还要多了一个 margin,父级元素中是 align-items。

相关代码:

/* grid 元素中设置 */
.box4{
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
  display: grid;
}
.center6{
  align-self: center;
  justify-content: center;
  margin: auto;
}
/* grid 父级元素中设置 */
.box5{
  background-color: #eee;
  width: 200px;
  height: 200px;
  margin-top: 20px;
  display: grid;
  align-items: center;
  justify-content: center;
}

效果展示:


六、总结

以上就是关于 CSS 如何将元素或者文字进行水平垂直居中的几种常用方法,大家还其他关于 CSS 实现水平垂直居中的方法吗?请在评论区留下你的想法。

关注w3cschool编程狮订阅更多IT资讯、技术干货~

天学习下css文本几个常用相关属性,包括文本颜色(color)这个是非常常见的、文本间距(这里主要指letter-spacing和word-spacing)、文本对齐(text-align)以及文本修饰(text-decoration),以下是相应的CSS和HTML代码示例。

1. 文本颜色(color)

使用color属性可以设置文本的颜色。颜色值可以是颜色名(如red)、十六进制(如#FF0000)、RGB(如rgb(255, 0, 0))、RGBA(如rgba(255, 0, 0, 0.5))、HSL或HSLA等。

HTML:

<p class="text-color">这是一段红色文本。</p>

CSS:

.text-color {
color: red; /* 或者使用其他颜色值,如 #FF0000, rgb(255, 0, 0) 等 */
}

2. 文本间距

  • 字母间距(letter-spacing):设置文本中字母之间的间距。
  • 单词间距(word-spacing):设置文本中单词之间的间距。

HTML:

<p class="letter-spacing">这是一个字母间距示例。</p>
<p class="word-spacing">这是 一个单词间距 示例。</p>

CSS:

.letter-spacing {
letter-spacing: 2px; /* 字母间距增加2px */
}

.word-spacing {
word-spacing: 5px; /* 单词间距增加5px */
}

3. 文本对齐(text-align)

使用text-align属性可以设置文本的对齐方式,如左对齐(left)、右对齐(right)、居中对齐(center)或两端对齐(justify)。

HTML:

<p class="text-left">左对齐文本。</p>
<p class="text-center">居中对齐文本。</p>
<p class="text-right">右对齐文本。</p>
<p class="text-justify">两端对齐文本。这是一个较长的句子,用于展示两端对齐的效果。</p>

CSS:

.text-left {
text-align: left;
}

.text-center {
text-align: center;
}

.text-right {
text-align: right;
}

.text-justify {
text-align: justify;
}

4. 文本修饰(text-decoration)

text-decoration属性用于设置文本的装饰线,如下划线(underline)、上划线(overline)、删除线(line-through)或无装饰线(none)。

HTML:

<p class="underline">下划线文本。</p>
<p class="overline">上划线文本。</p>
<p class="line-through">删除线文本。</p>
<p class="no-decoration">无装饰线文本。</p>

CSS:

.underline {
text-decoration: underline;
}

.overline {
text-decoration: overline;
}

.line-through {
text-decoration: line-through;
}

.no-decoration {
text-decoration: none;
}

以上就是关于CSS中文本相关属性的详细解释及代码示例。

演示效果