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 ...
随机推荐
- 利用Needleman–Wunsch算法进行DNA序列全局比对
生物信息学原理作业第二弹:利用Needleman–Wunsch算法进行DNA序列全局比对. 具体原理:https://en.wikipedia.org/wiki/Needleman%E2%80%93W ...
- Linux 虚拟IP
虚拟IP Linux网卡上绑定另一个虚拟ip,即网卡上一个真实ip一个虚拟ip.当然通过这2个ip都可以连接到该主机. 实现原理主要是靠TCP/IP的ARP协议.因为ip地址只是一个逻辑 地址,在以太 ...
- Jenkins配置备份恢复插件ThinBackup
一.系统管理-管理插件-找到ThinBackup并安装 二.系统管理-找到ThinBackup-点击Setting进行设置 第一个参数备份目录是必选,其它可选,点保存. 三.保存后返回到ThinBac ...
- PHP中cookies跨目录无法调用解决办法
localhost/a/test.php 中写入: <?php setcookie("user","zhangsan",time()+3600); ?&g ...
- iOS 应用开发,用户密码存储技术--KeyChain
文/清雪飘香(简书作者)原文链接:http://www.jianshu.com/p/c41525172aee著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 这次的Xcode 事件,让我 ...
- PHPUnit-附录 C. XML 配置文件
[http://www.phpunit.cn/manual/5.7/zh_cn/appendixes.configuration.html] PHPUnit <phpunit> 元素的属性 ...
- android应用中去android市场去评分的功能实现(吐槽一波个人应用上线...)
一般的app可能会有这中功能,在应用中去android商店评分来提高排名,前段时间也把我的博客园上传到商店,这里不得不吐槽一些android商店的开放平台. 酷派,vivo,oppo,联想不支持个人开 ...
- Postfix+dovecot搭建简单邮箱服务器
实验环境: (1)修改主机名:hostnamectl set-hostname mail.meilintong.com 退出,重新登陆 (2)关闭selinux (3)关闭防火墙 1.安装postfi ...
- 教我徒弟Android开发入门(二)
前言: 上一期实现了简单的QQ登录效果,这一期继续对上一期进行扩展 本期的知识点: Toast弹窗,三种方法实现按钮的点击事件监听 正文: Toast弹窗其实很简单,在Android Studio ...
- ubuntu16.04~qt 5.8无法输入中文
编译fcitx-qt需要cmake,安装cmake命令,如果已经安装,请略过. sudo apt-get install cmake 安装 fcitx-libs-dev sudo apt-get in ...