水平居中

  1. 如果它是一个行内元素 对其父元素使用 text-align:center 即可实现。
  <p style = " text-align:center; width:300px; background-color:pink;">妈妈再也不担心我的学习了</p>

2.如果它是一个块级元素:

A. margin 方法

  	<div style="background-color: greenyellow; ">
<div style="margin:0 auto; width: 300px;background-color: gold;">
我是一只小小鸟,飞啊飞啊,我的骄傲放纵。
</div>
</div>

B. table布局:水平布局,但也可以实现让某块级元素水平居中的效果。

	<table style="table-layout:fixed;border:2px solid black;width:100%; ">
<colgroup>
<col span="1" style="width: 25%;">
<col span="1" style="width: 50%;">
<col span="1" style="width: 25%;">
</colgroup>
<tbody>
<td style="background-color:green;">green</td>
<td style="background-color:red;">red</td>
<td style="background-color:blue;">blue</td>
</tbody>
</table>

C. flex 盒子:这里主要演示块级元素的情况,它也可在行内元素里使用。是现在比较流行的一种水平布局的方式。

行内元素中: display : inline-flex;

在webkit内核中: display : -webkit-flex;

<div style="display: flex; flex-flow: row nowrap; justify-content: center; background-color: darkgrey;  height: 50px;">
<div style="flex:0 1 auto; background-color: orange;">A</div>
<div style="flex:0 1 50px; background-color: blueviolet; ">B </div>
<div style="flex:0 1 auto; background-color: gray; ">C </div>
</div>



父元素:

flex-flow: flex-drection||flex-wrap;

flex-direction: row(左→右);

flex-wrap:nowrap(不折行);

justify-content(主轴对齐方式):center;

子集:

flex:flex-grow(项目放大比例) flex-shrink(项目缩小比例) flex-basis(为项目预留的空间,个人认为它可实现width的功能);

关于flex的详细学习,大家可以看这两篇文章,写的很赞哦。

https://demo.agektmr.com/flexbox/

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool

垂直居中

  1. 绝对定位的垂直居中的方法

    A.

    main {
    position: absolute;
    top: calc(50% - 4em);
    left: calc(50% - 6em);
    width: 12em;
    height: 8em;
    background-color: aqua;
    } <main>
    <h1>hello word!</h1>
    <p>Center </p>
    </main>

    这个方法固定了高度和宽度,不是由内容来决定的。现在我们对代码稍微改进一下。



    B.

    	main{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: yellow;
    } <main>
    <h1>hello word!</h1>
    <p>Center </p>
    </main>

    这里用transform:translate();巧妙的让main居中了,但是在过多会溢出页面。

  2. 不使用绝对定位时

    	main {
width: 12em;
padding: 1em 1.5em;
background-color: darkgoldenrod;
margin: 50vh auto 0;
transform: translateY(-50%);
} <main>
<h1>hello word!</h1>
<p>Center </p>
</main>

这个满足了我们让块级元素垂直居中的基本要求,但是这个技巧的实用性是有限的,它只适用于在视口居中的情况。

  1. flex
 .contain {
display: flex;
flex-flow: row nowrap;
justify-content: center;
align-content: center;
align-items: center;
height: 400px;
background-color: darkkhaki;
/*height: 100vh;*/
}
.son{
flex: 0 1 auto;
align-self: center;
background-color:darkviolet;
} <div class="contain">
<div class="son">
<h1>hello word!</h1>
<p>Center </p>
</div>
</div>

它不仅仅可以在视口中垂直居中,还可以在其他块级元素内垂直居中。

相对flex有更深的了解,可以去看一下前面的两个链接里的内容。

感谢阅读,喜欢请点赞,有问题可以留言。以上内容参考了《CSS揭秘》这本书,一本有趣的CSS书。全剧终,谢谢!

CSS 水平居中/布局 垂直居中 (月经问题)的更多相关文章

  1. CSS 水平居中与垂直居中

    前言 在CSS布局中,水平居中与垂直居中一直是用到比较多的,在本篇中将介绍水平居中.垂直居中的几种方式. 示例 HTML: <div class="parent"> & ...

  2. CSS水平居中和垂直居中解决方案

    一.CSS 居中 — 水平居中 DIV等标签本身没有定义自己居中的属性,网上很多的方法都是介绍用上级的text-align: center,然后嵌套一层DIV来解决问题. 可是这个方法有时候完全不起作 ...

  3. CSS网页布局垂直居中整理

    一.使用CSS3处理垂直居中方式 1.使用Flex布局处理(推荐),简单好用 body,html{ width:100%; height:100%; } .out { width: 20%; heig ...

  4. CSS水平居中与垂直居中的方法

    一.水平居中 1.行内元素水平居中 在父元素里添加text-align:center即可.代码如下: <style> .container-1 { height: 50px; border ...

  5. css水平居中和垂直居中

    水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display ...

  6. CSS 水平居中和垂直居中

    1.水平居中——行内元素 text-align: center; 2.水平居中——定宽块状元素 margin: auto,满足定宽和块状两个条件的元素是可以通过设置“左右margin”值为“auto” ...

  7. 主流 CSS 布局(水平居中、垂直居中、居中 )

    什么是布局 html 页面的整体结构或骨架 布局不是某个技术内容 而是一种设计思想 [ 布局方式 ] 水平居中布局 垂直居中布局 居中布局( 水平 + 垂直 ) 什么是水平居中布局 水平居中布局 元素 ...

  8. CSS的水平居中和垂直居中解决方案

    在写CSS样式的时候,有时为了美观,会添加水平居中和垂直居中,这时候你有可能会遇到很棘手的问题,有些水平居中和垂直居中的属性添加上去完全没反应,下面给大家列举一些CSS水平居中和垂直居中的终极解决方案 ...

  9. css之布局

    布局一直是页面制作很重要的部分,有个良好的布局不仅在页面上呈现很好的效果,还对后续功能扩展有重要的作用.本文主要讨论一下几种布局: 水平居中布局 垂直居中布局 多列布局 自适应布局 stracky-f ...

随机推荐

  1. 微服务配置内容《网上copy》=========》如何创建一个高可用的服务注册中心

    前言:首先要知道什么是一个高可用的服务注册中心,基于spring boot建成的服务注册中心是一个单节点的服务注册中心,这样一旦发生了故障,那么整个服务就会瘫痪,所以我们需要一个高可用的服务注册中心, ...

  2. 【html】01_html的介绍

    [HTML专修介绍] 定义: HTML(HypertextMarkup Language),超文本标记语言 如何理解: (意思就是超越了文本,还能兼容图片,视频,声音字节) 它的主要用处是什么? 就是 ...

  3. js学习笔记<拷贝传值,引用传址和匿名函数>

    拷贝传值:把一个变量的值拷贝一份,传给了另外一个变量拷贝传值中,两个变量之间没有任何联系,修改其中一个一个变量的值,原来的变量不变. 例: var arr1 = ["张三",24, ...

  4. TCP/IP协议栈 --- 网络层(IP 首部 和分片)

    IP 是TCP/IP协议栈中重要的层次, TCP UDP ICMP IGMP都是依赖IP层进行传输的.首先它是一种不可靠,无连接的协议.不可靠:它不保证IP包能正确到达目的地,无连接:表示IP并不会维 ...

  5. Laravel框架使用的一些注意细节(一)

    1.资源路由RESTful 当你不想编写太多的路由的时候,肯定会用到RESTful资源控制器.但当你使用资源控制器的时候,需要注意的是,你的资源路由的名字不能与public目录下的文件有重名,否则会导 ...

  6. Wannafly挑战赛5 补题

    A 珂朵莉与宇宙 题目链接: https://www.nowcoder.com/acm/contest/36/A 思路: 科学暴力:枚举前缀和,同时计算前缀和里面可能出现的完全平方数,匹配前缀和 与完 ...

  7. ACM HDU 1081 To The Max

     To The Max Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  8. HDU 1394 逆序数 线段树单点跟新 | 暴力

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. Java集合(5)一 HashMap与HashSet

    目录 Java集合(1)一 集合框架 Java集合(2)一 ArrayList 与 LinkList Java集合(3)一 红黑树.TreeMap与TreeSet(上) Java集合(4)一 红黑树. ...

  10. 选择客栈noip2011

    哈,没想到吧.今天居然有两篇(算什么,厕所读物吗 选择客栈 本题的更优解请跳转zt 这题11年,刚改2day. 对于30% 的数据,有 n ≤100: 对于50% 的数据,有 n ≤1,000: 对于 ...