css文字居中、图片居中、div居中解决方案
一、文字居中
若文字只有一行
<!--html代码-->
<div class="box">
<p class="text">文本只有一行</p>
</div>
<!--css代码-->
.box {
width: 200px;
height: 200px;
background-color: red;
}
.text {
line-height: 200px;
text-align: center;
}
若文字行数未知
<!--html代码-->
<div class="box">
<div class="box_inner">
<p>文本行数未知</p>
</div>
</div>
<!--css代码-->
.box {
width: 200px;
height: 200px;
background-color: red;
display: table;
}
.box_inner {
display: table-cell;
vertical-align: middle;
text-align: center;
}
二、图片居中
表格法,跟上面文本行数未知居中一样,只是把<p>标签换为<img>标签
背景法
<!--html代码-->
<div class="box"></div>
<!--css代码-->
.box {
width: 200px;
height: 200px;
background: url(...) no-repeat center center;
}
三、div居中
若div只是水平方向居中,可以设置margin:0 auto实现
div水平垂直居中(方法一)
<!--html代码-->
<div class="box">
<div class="box_inner"></div>
</div>
<!--css代码-->
.box {
width: 200px;
height: 200px;
background-color: red;
position: relative;
}
.box_inner {
width: 50px;
height: 50px;
background-color: green;
position: absolute;
top: 50%;
left: 50%;
margin-left: -25px;
margin-top: -25px;
}
div水平垂直居中(方法二)
<!--html代码-->
<div class="box">
<div class="box_inner"></div>
</div>
<!--css代码-->
.box {
width: 200px;
height: 200px;
background-color: red;
position: relative;
}
.box_inner {
width: 50px;
height: 50px;
background-color: green;
position: absolute;
top:0;
left:0;
right:0;
bottom:0;
margin:auto;
}
div水平垂直居中(方法三)
<!--html代码-->
<div class="box">
<div class="box_inner"></div>
</div>
<!--css代码-->
.box {
width: 200px;
height: 200px;
background-color: red;
position: relative;
}
.box_inner {
width: 50px;
height: 50px;
background-color: green;
position: absolute;
left:50%;
top:50%;
transform:translate(-50%,-50%);
}
以上三种方法思路都是通过设置div绝对定位,先居中左上角,再补齐。
div水平垂直居中(方法四)
<!--html-->
<div class="box">
<div></div>
</div>
<!--css-->
.box {
justify-content:center;//子元素水平居中
align-items:center;//子元素垂直居中
display:flex;
}
css文字居中、图片居中、div居中解决方案的更多相关文章
- css怎么让图片垂直左右居中?(外层div是浮动且按照百分比排列)
一.原始的居中方法是把div换成table <div style="width: 500px; height: 200px; border: solid 1px red; text-a ...
- css文字环绕图片--遇到的问题及解决方法
一.前言 需要实现一个文字环绕图片的效果,心想so easy嘛. 1)代码部分 <style> .img-left { border: 3px solid #005588; width:3 ...
- css中position:fixed实现div居中
上下左右 居中 代码如下 复制代码 div{ position:fixed; margin:auto; left:0; right:0; top:0; bottom:0; width:200px; h ...
- HTML/CSS实现文字环绕图片布局
原文: https://blog.csdn.net/yiyelanxin/article/details/75006925 在一个图文并茂的网页上,文字环绕图片可以使布局美观紧凑,如何实现呢?有两种办 ...
- 关于div居中
margin : 100px; margin-left: auto; margin-right: auto; 这样子设置css样式就可以实现一个div居中
- <转载>使CSS文字图片div元素居中方法之水平居中的几个方法
文字居中,文字垂直居中水平居中,图片居中,图片水平居中垂直居中,块元素垂直居中?当我们在做前端开发是时候关于css居中的问题是很常见的.情 况有很多种,不同的情况又有不同的解决方式.水平居中的方式解决 ...
- div+css文字垂直居中 解决左侧头像右侧姓名,姓名多换行后相对于头像仍居中显示
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中 ...
- [转]div 让文字或图片居中
本文转自:http://qsfwy.iteye.com/blog/250206 在div 中让 文字或图片居中,请参考以下代码1: - - - - - - - - - - - - - - - - - ...
- CSS布局,div居中,文字居中
.main { width: 100%; margin: 0 auto; .banner { img { width: 100%; } } .article { margin-bottom: 100p ...
随机推荐
- 01 Java 搭建环境
刚接触 Java 语言,为了方便以后查阅,将学习过程中遇到的知识点记录下来,作为学习笔记使用.在学习过程中 JDK 使用版本为 1.8.0_73,操作系统为 macOS Sierra 10.12.6. ...
- 小甲鱼OD学习第12讲
这次我们的任务是破解这个需要特定的注册码的软件,如下图 我们从字符串入手,输入register,搜索 我们点击 查找下一个,看看有什么有用的字符串,如下图 然后,在下方,我们发现了 Regis ...
- sublime插件AndyJS2安装教程
1.下载AndyJS2包,已整理上传,下载AndyJS2.rar,附上网址.(https://github.com/jiaoxueyan/AndyJS2) 2.点击首选项(preference)=&g ...
- 【Java】多线程初探
参考书籍:<Java核心技术 卷Ⅰ > Java的线程状态 从操作系统的角度看,线程有5种状态:创建, 就绪, 运行, 阻塞, 终止(结束).如下图所示 而Java定义的 ...
- Java中excel与对象的互相转换的通用工具类编写与使用(基于apache-poi-ooxml)
通用excel与对象相互转换的工具类 前言:最近开发需要一个Excel批量导入或者导出的功能,之前用过poi-ooxml开发过一个导入的工具类,正好蹭着这次机会,把工具类的功能进行完善. 使用说明: ...
- apache 改变文档根目录www的位置
1.找到apache的安装目录,找到config/httpd.conf,找到DocumentRoot "D:/wamp/www/" 改成你想要的目录,例如:改成 DocumentR ...
- 哪些CSS是可以被继承的--简单整理
那些CSS是可以被继承的--简单整理1.文本相关属性是继承的:font-size,font-family,line-height,text-index等2.列表相关属性是继承的:list-style- ...
- laypage 使用
最近发现一个特别好用的前端分页插件,分享一下 <!doctype html> <html> <head> <meta charset="utf-8& ...
- 高可用的MongoDB集群
1.序言 MongoDB 是一个可扩展的高性能,开源,模式自由,面向文档的数据库. 它使用 C++编写.MongoDB 包含一下特点: l 面向集合的存储:适合存储对象及JSON形式的数据. l ...
- python爬虫循环导入MySql数据库
1.开发环境 操作系统:win10 Python 版本:Python 3.5.2 MySQL:5.5.53 2.用到的模块 没有的话使用pip进行安装:pip install xxx ...