许多方法可以将HTML元素与CSS对齐,但是一起使用或单独使用它们并不是那么容易。开发人员所面临的困难之一就是试图将元素集中在页面中间。
因此,在本文中,我将展示一些最常用的方法,即通过使用不同的CSS属性在水平和垂直方向上居中图像。
让我们开始使用3个不同的CSS属性将图像水平居中。
使图像水平居中的第一种方法是使用text-align属性。但是,仅当图像位于块级容器(例如<div>)内时,此方法才有效:
使图像居中的另一种方法是使用margin:auto属性(用于左边距和右边距)。但是,单独使用margin:Auto将不适用于图像。如果需要使用margin:auto,则还必须使用2个其他属性。
margin-auto属性对内联级别的元素没有任何影响。由于<img>标签是一个内联元素,因此我们需要先将其转换为块级元素:
其次,我们还需要定义宽度。因此,左右边缘可以占用其余的空白空间并自动对齐,可以这样解决问题(除非我们给出100%的宽度):
将图像水平居中的第三种方法是使用display:flex。同样,我们对容器使用text-align属性,它也会使用display:flex。但是,仅使用display:flex是不够的。容器还必须具有一个称为justify-content的附加属性。
justify-content属性与display:flex一起使用,我们可以使用它水平放置图像的中心。最后,图像的宽度必须小于容器的宽度,否则,它会占用100%的空间,然后我们就无法对其进行集中化。
1、Display: Flex
对于垂直对齐,使用display:flex确实很有帮助。考虑到我们的容器的高度为800px,但图像的高度仅为500px:
现在,在这种情况下,向容器中添加一行代码(align-items:center)就可以了:
如果将align-items属性与display:flex一起使用,就会将元素垂直放置。
2、位置:绝对和变换属性
垂直对齐的另一种方法是一起使用position和transform属性。这个有点复杂,所以让我们一步一步地做。
步骤1:定义绝对位置
首先,我们将图像的定位行为从静态更改为绝对:
同样,它应该位于相对放置的容器内,因此我们添加一个位置:相对于其容器的div。
步骤2:定义顶部和左侧属性
其次,我们定义图像的顶部和左侧属性,并设置为50%。这会将图像的起点(左上角)移到容器的中心:
步骤3:定义变换属性
在第二步的时候已经将图像的一部分移出容器。因此,我们需要将其取回内部。在图像上定义转换属性,并在其X和Y轴上添加负50%可以达到目的:
还可以使用其他方法进行水平和垂直居中,我这里只尝试了最常用的方法。希望这篇文章可以帮助你了解如何在页面中心对齐图像。
、页面使用CSS的好处:
有效的传递页面信息。
使用CSS美化过的页面文本,使页面漂亮、美观,吸引用户。
可以很好的突出页面的主题内容,使用户第一眼可以看到页面主要内容。
具有良好的用户体验。
2、美化网页的一些基本样式
A:字体样式:
font-family设置字体类型font-family:"隶书";
font-size设置字体大小font-size:12px;
font-style设置字体风格font-style:italic;
font-weight设置字体的粗细font-weight:bold;
font在一个声明中设置所有字体属性font:italic bold 36px "宋体";
举例:
body{font-family: Times,"Times New Roman", "楷体";}
h1{font-size:24px;} 单位->px:像素
h2{font-style:oblique;}
p{font-weight:bold;}
p span{font:oblique bold 12px "楷体";}
B:文本属性:
color设置文本颜色color:#00C;
text-align设置元素水平对齐方式text-align:right;
text-indent设置首行文本的缩进text-indent:20px;
line-height设置文本的行高line-height:25px;
text-decoration设置文本的装饰text-decoration:underline;
C:排版文本段落:
水平对齐方式:text-align:left|right|center|justify;
left把文本排列到左边。默认值:由浏览器决定
right把文本排列到右边
center把文本排列到中间
justify实现两端对齐文本效果
首行缩进:text-indent:em或px
行高:line-height:px
文本修饰:text-decoration:none|underline|overline|line_through
值说明
none默认值,定义的标准文本
underline设置文本的下划线
overline设置文本的上划线
line-through设置文本的删除线
垂直对齐方式:vertical-align属性:middle、top、bottom
文本阴影:text-shadow : color x-offset y-offset blur-radius;
D:背景颜色和背景图片:
background-color:"red";
background-image:url(图片路径);
background-repeat:
沿水平和垂直两个方向平铺
no-repeat:不平铺,即只显示一次
repeat-x:只沿水平方向平铺
repeat-y:只沿垂直方向平铺
background-position:X、Y方向关键词 水平方向的关键词:left、center、right
垂直方向的关键词:top、center、bottom
举例:
.title {
font-size:18px;
font-weight:bold;
color:#FFF;
text-indent:1em;
line-height:35px;
background:#C00 url(../image/arrow-down.gif) 205px 10px no-repeat;
}
线性渐变:linear-gradient ( position, color1, color2,…)
3、举例:
<head>
<title>家用电器</title>
</head>
<link rel="stylesheet" href="jiayongdianqi.css">
<body>
<div class="container">
<h2>家用电器</h2>
<div class="type">
<h6><a href="#">大家电</a></h6>
<ul>
<li> <a href="#">平板电视</a> <a href="#">洗衣机</a> <a href="#">冰箱</a></li>
<li> <a href="#">空调</a> <a href="#">烟机/灶具</a> <a href="#"> 热水器</a></li>
<li> <a href="#">冷柜/酒柜</a> <a href="#">消毒柜</a> <a href="#">家庭影院</a></li>
</ul>
<h6><a href="#">生活电器</a></h6>
<ul>
<li> <a href="#">电风扇</a> <a href="#">净化器</a> <a href="#">吸尘器</a></li>
<li> <a href="#">净水设备</a> <a href="#">挂烫机</a> <a href="#"> 电话机</a></li>
</ul>
<h6><a href="#">厨房电器</a></h6>
<ul>
<li> <a href="#">榨汁机</a> <a href="#">电压力锅</a> <a href="#">电饭煲</a></li>
<li> <a href="#">豆浆机</a> <a href="#">微波炉</a> <a href="#"> 电磁炉</a></li>
</ul>
<h6><a href="#">五金家装</a></h6>
<ul>
<li> <a href="#">淋浴/水槽</a> <a href="#">电动工具</a> <a href="#">手动工具</a></li>
<li> <a href="#">仪器仪表 </a> <a href="#">浴霸/排气 </a> <a href="#"> 灯具</a></li>
</ul>
</div>
</div>
</body>
</html>
li{
/* 去圆点 */
list-style: none;
}
/* 分类页面总宽度 */
.container{
width: 500px;
}
a{
/* 去超链接下划线 */
text-decoration: none;
}
/* 大标题字体 */
.container>h2{
font-size: 18px;
color: white;
font-weight: bolder;
line-height: 50px;
text-indent: 2px;
/*从上到下,0%开始颜色。50%颜色。70%颜色 */
background: linear-gradient(to bottom,#0467ac,#63a7d6,#b6dbf5);
}
css代码:
/* 所有电器分类内容 */
.type{
/* 首行缩进 */
text-indent: 4.5em;
/* 左外边距40px */
margin-left: 40px;
}
/* 电器分类 */
.type>h6{
font-size: 14px;
font-weight: bolder;
line-height: 30px;
background: linear-gradient(to bottom,#e4f1fa,#bddff7,#e4f1fa);
}
/* 电器分类中的A标签 */
.type>h6>a{
color:#0467ac;
}
/* 电器分类中的A标签鼠标悬浮效果 */
.type>h6>a:hover{
text-decoration: underline;
}
/* 分类内容字体 */
.type>ul>li{
font-size: 12px;
line-height: 26px;
}
/* 分类内容字体里面的A标签 */
.type>ul>li>a{
color: #969696;
}
/* 分类内容字体里面的A标签悬浮效果 */
.type>ul>li>a:hover{
color: #804000;
text-decoration: underline;
}
4、效果图链接:file:///D:/ruanjian/VS/wenjianxiangmu/htmlFive/jiayongdianqi.html
SS 是前端里面的基础之一,也是非常重要的一部分,它往往决定了你所做出来的网页页面是否美观。在设计网页页面的过程中,总会有将元素或者文字进行水平垂直居中的要求。下面w3cschool编程狮就为大家介绍 CSS 中几种常用到的水平垂直居中的方法。
当元素有给定的高度以及宽度的时候,使用 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 来实现。但是,使用的同时还需要结合其他属性才完整实现。因为,单是设置 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资讯、技术干货~
*请认真填写需求信息,我们会在24小时内与您取得联系。