中,是我們編碼過程中最常見的,那么,我們平時常見的居中方式,下面一一羅列出來,有錯誤的地方,望碼友多多包涵并加以矯正。
水平居中
1、多塊級元素,設置display:inline-block;使之在一行排列,在父級樣式里,設置text-align:center;就可以實現水平居中的效果
body {
text-align: center;
}
div{
width: 100px;
height: 100px;
border: 1px solid;
display: inline-block;
}
2、內聯元素,利用text-align:center;可以實現塊級元素內部的內聯元素的水平居中
div {
border: 1px solid red;
width: 100px;
height: 100px;
text-align: center;
}
<div>
<span>塊級元素中的行內元素的水平居中</span>
</div>
3、塊級元素,通過把固定寬高的塊級元素的margin-left和margin-right設置為auto,方可實現
div{
width: 100px;
height: 100px;
border: 1px solid;
margin: 0 auto;
}
<div></div>
4、利用彈性盒子(display: flex;)
給父級定寬定高,然后設置display: flex;以及justify-content: center;方可達到水平居中效果
body {
width: 500px;
height: 500px;
display: flex;
justify-content: center;
border: 1px solid red;
}
div {
width: 100px;
height: 100px;
border: 1px solid;
}
<body>
<div></div>
</body>
垂直居中
1、內聯元素(單行)
通過設置元素的height和line-height,方可達到居中效果
2、多行元素,利用表布局(table)
通過給想要居中的元素的父級設置display: talbe-cell;以及vertical-align:enter;方可居中
3、彈性盒子(flex)
給父級設置display: flex;變成彈性盒子。
分兩種,
(1),主軸方向為水平,直接設置 align-items: center;
(2),主軸方向為垂直,設置flex-direction: column;改變主軸方向,然后設置justify-content: center;
彈性盒模型主軸不同,居中的方式也不同,靈活應用。
4、固定寬高的塊級元素
利用父相子絕的定位原理,實現垂直居中
position: absolute;
left: 50%;
top: 50%;
margin-left: (自身高度的一半);
5,未知寬高的塊級元素
利用transform: translateY(-50%);方可實現
position: absolute;
top: 50%;
transform: translateY(-50%);
水平垂直方向的居中
1、固定寬高
通過margin平移整體寬高的一半,實現水平垂直居中
{
position: absolute;
width: 100px;
height: 100px;
border: 1px solid;
left: 50%;
top: 50%;
margin-top: -50px;
margin-left: -50px;
}
2、未知寬高
利用transform中的translate()屬性實現
{
position: absolute;
border: 1px solid;
left: 50%;
top: 50%;
transform: translateY(-50%);
transform: translateX(-50%);
}
3、彈性盒子(flex)
通過display:flex,把父級變成彈性盒模型,利用align-items: center;justify-content: center;方可實現居中。
注意:彈性盒子容器中,多行項目的居中方式另加計算。
body {
border: 1px solid;
width: 300px;
height: 300px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
div {
border: 1px solid;
width: 100px;
height: 100px;
}
隨著學習的不斷深入,居中方式可以有很多種,我們要善于利用,更加明確什么情況下用怎樣的居中方式。
、盒子里的字,默認是位于盒子內的左上角,如何讓它水平居中,垂直居中呢。
圖1
圖2
以下是我在vscode下做的代碼:
圖3
本文主要介紹H5新增內容以及CSS3中的新特性。在H5方面主要介紹拓展了哪些內容,CSS3方面介紹動畫及轉換。
「1. 什么是HTML5」
「2. HTML5拓展了哪些內容」
「3. HTML5的現狀」
絕大多數新的屬性,都已經被瀏覽器所支持,最新版本的瀏覽器已經開始陸續支持最新的特性,總的來說:HTML5已經是大勢所趨。
「1. 什么是語義化」
語義化是指用HTML寫出符合內容的結構化(內容語義化),選擇合適的標簽(代碼語義化),能夠便于開發者閱讀和寫出更優雅的代碼的同時讓瀏覽器的爬蟲和機器很好地解析。
「2. 新增了哪些語義化標簽」
「3. 新增多媒體音頻標簽」
<audio controls>
<!-- 注意:在 chrome 瀏覽器中已經禁用了 autoplay 屬性 -->
<!-- <audio src="./media/snow.mp3" controls autoplay></audio> -->
<!--
因為不同瀏覽器支持不同的格式,所以我們采取的方案是這個音頻準備多個文件 -->
<source src="myAudio.mp3" type="audio/mpeg">
<source src="myAudio.ogg" type="audio/ogg">
<p>Your browser doesn't support HTML5 audio. Here is
a <a href="myAudio.mp4">link to the audio</a> instead.</p>
</audio>
「4. 新增多媒體視頻標簽」
<video src="./media/video.mp4" controls="controls"></video>
<body>
<!-- <video src="./media/video.mp4" controls="controls"></video> -->
<!-- 谷歌瀏覽器禁用了自動播放功能,如果想自動播放,需要添加 muted 屬性 -->
<video controls="controls" autoplay muted loop poster="./media/pig.jpg">
<source src="./media/video.mp4" type="video/mp4">
<source src="./media/video.ogg" type="video/ogg">
</video>
</body>
「5. 新增input標簽」
「6. 新增表單屬性」
「1. CSS3屬性選擇器」
button {
cursor: pointer;
}
button[disabled] {
cursor: default;
}
input[type=search] {
color: skyblue;
}
span[class^=black] {
color: lightgreen;
}
span[class$=black] {
color: lightsalmon;
}
span[class*=black] {
color: lightseagreen;
}
「2. 結構偽類選擇器」
ul li:first-child {
background-color: lightseagreen;
}
ul li:last-child {
background-color: lightcoral;
}
ul li:nth-child(3) {
background-color: aqua;
}
<style>
/* 偶數 */
ul li:nth-child(even) {
background-color: aquamarine;
}
/* 奇數 */
ul li:nth-child(odd) {
background-color: blueviolet;
}
/*n 是公式,從 0 開始計算 */
ul li:nth-child(n) {
background-color: lightcoral;
}
/* 偶數 */
ul li:nth-child(2n) {
background-color: lightskyblue;
}
/* 奇數 */
ul li:nth-child(2n + 1) {
background-color: lightsalmon;
}
/* 選擇第 0 5 10 15, 應該怎么選 */
ul li:nth-child(5n) {
background-color: orangered;
}
/* n + 5 就是從第5個開始往后選擇 */
ul li:nth-child(n + 5) {
background-color: peru;
}
/* -n + 5 前五個 */
ul li:nth-child(-n + 5) {
background-color: tan;
}
</style>
<style>
div :nth-child(1) {
background-color: lightblue;
}
div :nth-child(2) {
background-color: lightpink;
}
div span:nth-of-type(2) {
background-color: lightseagreen;
}
div span:nth-of-type(3) {
background-color: #fff;
}
</style>
「3. 偽元素選擇器」
<style>
div {
width: 100px;
height: 100px;
border: 1px solid lightcoral;
}
div::after,
div::before {
width: 20px;
height: 50px;
text-align: center;
display: inline-block;
}
div::after {
content: '德';
background-color: lightskyblue;
}
div::before {
content: '道';
background-color: mediumaquamarine;
}
</style>
p {
position: relative;
width: 220px;
height: 22px;
border: 1px solid lightseagreen;
margin: 60px;
}
p::after {
content: '\ea50';
font-family: 'icomoon';
position: absolute;
top: -1px;
right: 10px;
}
「4. 2D 轉換之translate」
transform: translate(x, y)
transform: translateX(n)
transfrom: translateY(n)
div {
background-color: lightseagreen;
width: 200px;
height: 100px;
/* 平移 */
/* 水平垂直移動 100px */
/* transform: translate(100px, 100px); */
/* 水平移動 100px */
/* transform: translate(100px, 0) */
/* 垂直移動 100px */
/* transform: translate(0, 100px) */
/* 水平移動 100px */
/* transform: translateX(100px); */
/* 垂直移動 100px */
transform: translateY(100px);
/*百分比用法*/
transform: translateY(100%);
}
div {
position: relative;
width: 500px;
height: 500px;
background-color: pink;
/* 1. 我們tranlate里面的參數是可以用 % */
/* 2. 如果里面的參數是 % 移動的距離是 盒子自身的寬度或者高度來對比的 */
/* 這里的 50% 就是 50px 因為盒子的寬度是 100px */
/* transform: translateX(50%); */
}
p {
position: absolute;
top: 50%;
left: 50%;
width: 200px;
height: 200px;
background-color: purple;
/1.* margin-top: -100px;
margin-left: -100px; */
/2.* translate(-50%, -50%)
盒子往上走自己高度的一半 */
transform: translate(-50%, -50%);
}
span {
/* translate 對于行內元素是無效的 */
transform: translate(300px, 300px);
}
「5. 2D 轉換之rotate」
/* 單位是:deg */
img:hover {
transform: rotate(360deg)
}
transform-origin: x y;
「6. 2D 轉換之scale」
transform: scale(x, y)
div:hover {
/* 注意,數字是倍數的含義,所以不需要加單位 */
/* transform: scale(2, 2) */
/* 實現等比縮放,同時修改寬與高 */
/* transform: scale(2) */
/* 小于 1 就等于縮放*/
transform: scale(0.5, 0.5)
}
「7. 2D 轉換綜合寫法以及順序問題」
div:hover {
transform: translate(200px, 0) rotate(360deg) scale(1.2)
}
「動畫」是CSS3中最具顛覆性的特征之一,可通過設置多個節點來精確的控制一個或者一組動畫,從而實現復雜的動畫效果。
「動畫的使用」
/*1. 定義動畫*/
@keyframes 動畫名稱 {
0% {
width: 100px;
}
100% {
width: 200px
}
}
div {
/* 調用動畫 */
animation-name: 動畫名稱;
/* 持續時間 */
animation-duration: 持續時間;
}
「動畫序列」
<style>
div {
width: 100px;
height: 100px;
background-color: aquamarine;
animation-name: move;
animation-duration: 0.5s;
}
@keyframes move{
0% {
transform: translate(0px)
}
100% {
transform: translate(500px, 0)
}
}
</style>
「動畫常見屬性」
div {
width: 100px;
height: 100px;
background-color: aquamarine;
/* 動畫名稱 */
animation-name: move;
/* 動畫花費時長 */
animation-duration: 2s;
/* 動畫速度曲線 */
animation-timing-function: ease-in-out;
/* 動畫等待多長時間執行 */
animation-delay: 2s;
/* 規定動畫播放次數 infinite: 無限循環 */
animation-iteration-count: infinite;
/* 是否逆行播放 */
animation-direction: alternate;
/* 動畫結束之后的狀態 */
animation-fill-mode: forwards;
}
div:hover {
/* 規定動畫是否暫停或者播放 */
animation-play-state: paused;
}
「動畫簡寫方式」
/* animation: 動畫名稱 持續時間 運動曲線 何時開始 播放次數 是否反方向 起始與結束狀態 */
animation: name duration timing-function delay iteration-count direction fill-mode
知識要點
animation: move 2s linear 1s infinite alternate forwards;
「速度曲線細節」
animation-timing-function: 規定動畫的速度曲線,默認是ease
/*打字機效果*/
div {
width: 0px;
height: 50px;
line-height: 50px;
white-space: nowrap;
overflow: hidden;
background-color: aquamarine;
animation: move 4s steps(24) forwards;
}
@keyframes move {
0% {
width: 0px;
}
100% {
width: 480px;
}
}
通過過渡transition,可以讓web前端開發人員不需要javascript就可以實現簡單的動畫交互效果。
深入理解CSS過渡transition
https://www.cnblogs.com/xiaohuochai/p/5347930.html
「定義」過渡transition是一個復合屬性,包括transition-property、transition-duration、transition-timing-function、transition-delay這四個子屬性。通過這四個子屬性的配合來完成一個完整的過渡效果。
transition-property: 過渡屬性(默認值為all)
transition-duration: 過渡持續時間(默認值為0s)
transiton-timing-function: 過渡函數(默認值為ease函數)
transition-delay: 過渡延遲時間(默認值為0s)
.test{
height: 100px;
width: 100px;
background-color: pink;
transition-duration: 3s;
/* 以下三值為默認值,稍后會詳細介紹 */
transition-property: all;
transition-timing-function: ease;
transition-delay: 0s;
}
.test:hover{
width: 500px;
}
~~~html
<div class="test"></div>
「復合屬性」過渡transition的這四個子屬性只有<transition-duration>是必需且不能為0。其中,<transition-duration>和<transition-delay>都是時間。當兩個時間同時出現時,第一個是<transition-duration>,第二個是<transition-delay>;當只有一個時間時,它是<transition-duration>,而<transition-delay>為默認值0s
.test{
height: 100px;
width: 100px;
background-color: pink;
/*代表持續時間為2s,延遲時間為默認值0s*/
transition;2s;
}
.test:hover{
width: 500px;
}
<div class="test"></div>
延遲時間delay 案例
.test{
height: 100px;
width: 100px;
background-color: pink;
/*代表持續時間為1s,延遲時間為2s*/
transition: 1s 2s;
}
.test:hover{
width: 500px;
}
<div class="test"></div>
「過渡屬性」
none: 沒有指定任何樣式
all: 默認值,表示指定元素所有支持transition-property屬性的樣式
<transition-property>: 可過渡的樣式,可用逗號分開寫多個樣式
「過渡持續時間」
/*DEMO中的過渡屬性值*/
transition-property: width,background;
「過渡時間函數」
過渡時間函數用于定義元素過渡屬性隨時間變化的過渡速度變化效果
「取值」 過渡時間函數共三種取值,分別是關鍵字、steps函數和bezier函數
「關鍵字」其實是bezier函數或steps函數的特殊值
ease: 開始和結束慢,中間快。
linear: 勻速。
ease-in: 開始慢。
ease-out: 結束慢。
ease-in-out: 和ease類似,但比ease幅度大。
「3D的特點」近大遠小,物體和面遮擋不可見
「三維坐標系」
1. 3D 轉換知識要點
2. 3D 移動translate3d
transform: translate3d(100px, 100px, 100px)
/* 注意:x, y, z 對應的值不能省略,不需要填寫用 0 進行填充 */
transform: translate3d(100px, 100px, 0)
代碼演示
body {
/*透視需要寫在被視察元素的父盒子上面 */
perspective: 1000px;
}
translateZ與perspective的區別
3D 旋轉指可以讓元素在三維平面內沿著 x 軸、y 軸、z 軸 或者自定義軸進行旋轉
div {
/*透視寫在被視察元素的父盒子上面 */
perspective: 300px;
}
/*被觀察元素*/
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotateX(-45deg)
}
div {
perspective: 500px;
}
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotateY(180deg)
}
div {
perspective: 500px;
}
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotateZ(180deg)
}
「rotate3d」
div {
perspective: 500px;
}
img {
display: block;
margin: 100px auto;
transition: all 1s;
}
img:hover {
transform: rotate3d(1, 1, 0, 180deg)
}
*請認真填寫需求信息,我們會在24小時內與您取得聯系。