1. 程式人生 > >Day49:CSS屬性操作(文本、背景、邊框、列表、display、邊距)

Day49:CSS屬性操作(文本、背景、邊框、列表、display、邊距)

tro 驗證 介紹 lec ica 基本 next eat 敬畏

一、CSS屬性操作

1、CSS text

文本顏色:color

顏色屬性被用來設置文字的顏色。

顏色是通過CSS最經常的指定:

  • 十六進制值 - 如: #FF0000
  • 一個RGB值 - 如: RGB(255,0,0)
  • 顏色的名稱 - 如: red
p { color: rebeccapurple;  }

水平對齊方式

text-align 屬性規定元素中的文本的水平對齊方式。

  • left   把文本排列到左邊。默認值:由瀏覽器決定。
  • right   把文本排列到右邊。
  • center   把文本排列到中間。
  • justify   實現兩端對齊文本效果。
技術分享
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>css</title>
<style>
        h2 
{text-align:center;} p.publish_time {text-align:right;} p.content {text-align:justify;} </style> </head> <body> <h1>CSS text-align 水平居中</h1> <p class="publish_time">2017 年 5 月 17 號</p> <p class="content"> 有個落拓不得誌的中年人每隔三兩天就到教堂祈禱,而且他的禱告詞幾乎每次都相同。第一次他到教堂時, 跪在聖壇前,虔誠地低語:“上帝啊,請念在我多年來敬畏您的份上。讓我中一次彩票吧!阿門。” 幾天後,他又垂頭喪氣回到教堂,同樣跪著祈禱:“上帝啊,為何不讓我中彩票?我願意更謙卑地來 服侍你,求您讓我中一次彩票吧!阿門。”又過了幾天,他再次出現在教堂,同樣重復他的祈禱。如此周而 復始,不間斷地祈求著。到了最後一次,他跪著:“我的上帝,為何您不垂聽我的祈求?讓我中一次彩票吧! 只要一次,讓我解決所有困難,我願終身奉獻,專心侍奉您……”就在這時,聖壇上發出一陣宏偉莊嚴的聲 音:“我一直垂聽你的禱告。可是最起碼?你也該先去買一張彩票吧!”</p> <p><b>註意:</b> 重置瀏覽器窗口大小查看 &quot;justify&quot; 是如何工作的。</p> </body> </html>
示例

文本其他屬性

font-size: 10px;

line-height: 200px;   文本行高 通俗的講,文字高度加上文字上下的空白區域的高度 50%:基於字體大小的百分比

vertical-align:-4px  設置元素內容的垂直對齊方式 ,只對行內元素有效,對塊級元素無效


text-decoration:none  text-decoration 屬性用來設置或刪除文本的裝飾。主要是用來刪除鏈接的下劃線

font-family: ‘Lucida Bright‘   字體

font-weight: lighter/bold/border/   字體粗細

font-style: oblique   

text-indent: 150px;      首行縮進150px

letter-spacing: 10px;  字母間距

word-spacing: 20px;  單詞間距

text-transform: capitalize/uppercase/lowercase ; 文本轉換,用於所有字句變成大寫或小寫字母,或每個單詞的首字母大寫

2、背景屬性

屬性介紹

  • background-color   背景顏色
  • background-image   背景圖片
  • background-repeat   背景圖片填充方式
  • background-position  位置
background-color: cornflowerblue
 
background-image: url(‘1.jpg‘);
 
background-repeat: no-repeat;(repeat:平鋪滿)
 
background-position: right top(20px 20px);

簡寫

background:#ffffff url(‘1.png‘) no-repeat right top;

3、邊框屬性

屬性介紹

  • border-width
  • border-style (required)
  • border-color
border-style: solid;
  
border-color: chartreuse;
  
border-width: 20px;

簡寫

border: 30px rebeccapurple solid;

邊框-單獨設置各邊

border-top-style:dotted;
border-right-style:solid;
border-bottom-style:dotted;
border-left-style:none;

4、列表屬性

list-style-type         設置列表項標誌的類型。
list-style-image    將圖象設置為列表項標誌。
list-style-position 設置列表中列表項標誌的位置。
 
list-style          簡寫屬性。用於把所有用於列表的屬性設置於一個聲明中

ist-style-type屬性指定列表項標記的類型:

ul { list-style-type: square; }

使用圖像來替換列表項的標記:

ul { list-style-image: url(‘‘); }

5、display屬性

  • none
  • block
  • inline
  • inline-block

none(隱藏某標簽)

p{display:none;}

註意與visibility:hidden的區別:

visibility:hidden可以隱藏某個元素,但隱藏的元素仍需占用與未隱藏之前一樣的空間。也就是說,該元素雖然被隱藏了,但仍然會影響布局。

display:none可以隱藏某個元素,且隱藏的元素不會占用任何空間。也就是說,該元素不但被隱藏了,而且該元素原本占用的空間也會從頁面布局中消失。

block(內聯標簽設置為塊級標簽)

span {display:block;}

註意:一個內聯元素設置為display:block是不允許有它內部的嵌套塊元素。

inline(塊級標簽設置為內聯標簽)

li {display:inline;}

inline-block

display:inline-block可做列表布局,其中的類似於圖片間的間隙小bug可以通過如下設置解決:

#outer{
            border: 3px dashed;
            word-spacing: -5px;
        }

6、外邊距(margine)和內邊距(padding)

盒子模型

技術分享

  • margin: 用於控制元素與元素之間的距離;margin的最基本用途就是控制元素周圍空間的間隔,從視覺角度上達到相互隔開的目的。
  • padding: 用於控制內容與邊框之間的距離;
  • Border(邊框): 圍繞在內邊距和內容外的邊框。
  • Content(內容): 盒子的內容,顯示文本和圖像。

margine(外邊距)

單邊外邊距屬性:

margin-top:100px;
margin-bottom:100px;
margin-right:50px;
margin-left:50px;

簡寫屬性:

技術分享
margin:10px 20px 20px 10px;

        上邊距為10px
        右邊距為20px
        下邊距為20px
        左邊距為10px

margin:10px 20px 10px;

        上邊距為10px
        左右邊距為20px
        下邊距為10px

margin:10px 20px;

        上下邊距為10px
        左右邊距為20px

margin:25px;

        所有的4個邊距都是25px
簡寫

居中應用:

margin: 0 auto;

padding(內邊距)

單獨使用填充屬性可以改變上下左右的填充。縮寫填充屬性也可以使用,一旦改變一切都改變。

設置同margine;

頁碼實例:

技術分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .outer{
            margin: 0 auto;
            width: 80%;

        }

        .content{
            background-color: darkgrey;
            height: 500px;

        }

        a{
            text-decoration: none;
        }

        .page-area{

            text-align: center;
            padding-top: 30px;
            padding-bottom: 30px;
            background-color: #f0ad4e;

        }

        .page-area ul li{
            display: inline-block;
        }


       .page-area ul li a ,.page-area ul li span{

            display: inline-block;
            color: #369;
            height: 25px;
            width: 25px;
            text-align: center;
            line-height: 25px;

            padding: 8px;
            margin-left: 8px;

            border: 1px solid #e1e1e1;
            border-radius: 15%;

        }

       .page-area ul li .page-next{
           width: 70px;
           border-radius:0
       }


       .page-area ul li span.current_page{
           border: none;
           color: black;
           font-weight:900;
       }

       .page-area ul li a:hover{

           color: #fff;
           background-color: #2459a2;
       }


    </style>
</head>
<body>

<div class="outer">

<div class="content"></div>

<div class="page-area">

             <ul>

                 <li><span class="current_page">1</span></li>
                 <li><a href="#" class="page-a">2</a></li>
                 <li><a href="#" class="page-a">3</a></li>
                 <li><a href="#" class="page-a">4</a></li>
                 <li><a href="#" class="page-a">5</a></li>
                 <li><a href="#" class="page-a">6</a></li>
                 <li><a href="#" class="page-a">7</a></li>
                 <li><a href="#" class="page-a">8</a></li>
                 <li><a href="#" class="page-a">9</a></li>
                 <li><a href="#" class="page-a">10</a></li>
                 <li><a href="#" class="page-a page-next">下一頁</a></li>

             </ul>

</div>

</div>


</body>
</html>
頁碼、下一頁

思考1:body的外邊距

邊框在默認情況下會定位於瀏覽器窗口的左上角,但是並沒有緊貼著瀏覽器的窗口的邊框,這是因為body本身也是一個盒子(外層還有html),在默認情況下,body距離html會有若幹像素的margin,具體數值因各個瀏覽器不盡相同,所以body中的盒子不會緊貼瀏覽器窗口的邊框了,為了驗證這一點,加上:

body{
    border: 1px solid;
    background-color: cadetblue;
}

>>>>解決方法:

body{
    margin: 0;
}

思考2:margin collapse(邊界塌陷或者說邊界重疊)

1、兄弟div:
上面div的margin-bottom和下面div的margin-top會塌陷,也就是會取上下兩者margin裏最大值作為顯示值

2、父子div:
if 父級div中沒有border,padding,inlinecontent,子級div的margin會一直向上找,直到找到某個標簽包括border,padding,inline content中的其中一個,然後按此div 進行margin;

技術分享
<!DOCTYPE html>
<html lang="en" style="padding: 0px">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>

        body{
            margin: 0px;
        }

        .div1{
            background-color: rebeccapurple;
            width: 300px;
            height: 300px;
            overflow: hidden;

        }
        .div2{
            background-color: green;
            width: 100px;
            height: 100px;
            margin-bottom: 40px;
            margin-top: 20px;
        }
        .div3{
            background-color:teal;
            width: 100px;
            height: 100px;
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div style="background-color: bisque;width: 300px;height: 300px"></div>

<div class="div1">

   <div class="div2"></div>
   <div class="div3"></div>
</div>

</body>

</html>
View Code

>>>> 解決方法:

overflow: hidden;  

二、課後作業

1、

技術分享

技術分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <style>
        .a li{
            width: 25px;
            height: 25px;
            text-align: center;
            line-height: 25px;
            margin: 5px;
            padding: 5px 5px;
            list-style: none;
            border: 1px solid darkorchid;
            border-radius: 15%;
            float: left;
            user-select: none;
        }
        #next{
            width: 60px;
            border-radius: 10%;
        }
        .a li:hover{
            background: deepskyblue;
            color: white;
        }
        a {
            text-decoration: none;
        }
    </style>
</head>
<body>
<ul class="a">
    <li><a href="">1</a></li>
    <li><a href="">2</a></li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
    <li id="next">下一頁</li>
</ul>
</body>
</html>
參考答案

Day49:CSS屬性操作(文本、背景、邊框、列表、display、邊距)