天來說下HTML語言CSS樣式字體的文本縮進 text-indent
text-indent屬性用來指定文本的第一行的縮進,通常是將段落的首行縮進。來看下語法使用:
div {
text-indent:10px;
}
來看下使用效果,使用前:
每一行沒有縮進
使用后:
每個段落的第一行都縮進了20px,具體代碼如下:
<!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.0">
<title>CSS樣式之文本縮進</title>
<style>
p {
text-indent: 20px;
}
</style>
</head>
<body>
<p>第一年,我考上了大學,來到了北京,冬天的雪就像冷冷的冰雨,
在臉上胡亂的拍.搖啊搖,搖啊搖,搖到外婆橋,從此世人眼中的你就像超人一樣屹立于天地間。
</p>
<p>一片冰心在玉壺,我勸天公重抖擻,不拘一格降人才。一片冰心在玉壺,我勸天公重抖擻,不拘一格降人才。</p>
<p>只有聆聽你的聲音,才能撫平我寂寞無聊的心,HTML5。只有聆聽你的聲音,才能撫平我寂寞無聊的心,HTML5</p>
</body>
</html>
需要縮進的像素可以是任意值,除了正的px,還可以是負的值,可以看下效果:
這不是我的瀏覽器壞了,展示不了段落首部,是將px修改為-20px
具體代碼如下:
<!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.0">
<title>CSS樣式之文本縮進</title>
<style>
p {
text-indent: -20px;
}
</style>
</head>
<body>
<p>第一年,我考上了大學,來到了北京,冬天的雪就像冷冷的冰雨,
在臉上胡亂的拍.搖啊搖,搖啊搖,搖到外婆橋,從此世人眼中的你就像超人一樣屹立于天地間。
</p>
<p>一片冰心在玉壺,我勸天公重抖擻,不拘一格降人才。一片冰心在玉壺,我勸天公重抖擻,不拘一格降人才。</p>
<p>只有聆聽你的聲音,才能撫平我寂寞無聊的心,HTML5。只有聆聽你的聲音,才能撫平我寂寞無聊的心,HTML5</p>
</body>
</html>
根據大家寫文章段落的習慣,一般都是縮進兩個字,但是20px或者10px是不是2個字的長度呢,回答不是的??s進字數長度有專門的單位: em
通過設置該屬性,所有元素的第一行都可以縮進一個給定的長度,甚至該長度可以是負值。
em是一個相對單位,就是當前元素 font-size 1個文字的大小,如果當前元素沒有設置大小,則會按照1個父元素文字大小。
我們來看下效果:
確實是縮進了2個字的長度,看下對應代碼:
<!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.0">
<title>CSS樣式之文本縮進</title>
<style>
p {
text-indent: 2em;
}
</style>
</head>
<body>
<p>第一年,我考上了大學,來到了北京,冬天的雪就像冷冷的冰雨,
在臉上胡亂的拍.搖啊搖,搖啊搖,搖到外婆橋,從此世人眼中的你就像超人一樣屹立于天地間。
</p>
<p>一片冰心在玉壺,我勸天公重抖擻,不拘一格降人才。一片冰心在玉壺,我勸天公重抖擻,不拘一格降人才。</p>
<p>只有聆聽你的聲音,才能撫平我寂寞無聊的心,HTML5。只有聆聽你的聲音,才能撫平我寂寞無聊的心,HTML5</p>
</body>
</html>
每個段落第一行如果想要三個字間距,就是3em
今天就先到這里,大家周末快樂~
于具有很多屬性的標簽,我希望在新行上分割每個屬性,當按Enter鍵調用新行時,它應該只縮進一個標簽。
以下示例輸出我想如何縮進我的屬性:
<svg width="300px" height="150px"> <ellipse class="fill-current" cx="150" cy="75" rx="100" ry="75" /> </svg>
但PHPStorm會自動嘗試將我所有的行縮進到當前屬性:
<svg width="300px" height="150px"> <ellipse class="fill-current" cx="150" cy="75" rx="100" ry="75" /> </svg>
我無法在設置(編輯器 - >代碼樣式 - > HTML)中找到任何選項來更改此行為。有誰知道這個問題的解決方案?
這背后的原因是因為我經常在我的HTML模板中使用自定義標簽。有時候我自己的標簽很長,并且是2個或(罕見的情況下)3個單詞的組合,并且在新行上啟動所有額外屬性并且它不應該在標簽的末尾對齊。自己縮進屬性是很麻煩的。我想自動化它。
原圖樣式)
今天我們講一下幾個文本標簽<code>text-indent</code>,<code>letter-spacing</code>和<code>line-height</code>,并附加講一下<code>first-line</code>和<code>first-letter</code>的使用方法。
先上源碼:
<!DOCTYPE html>
<html>
<head>
<title>css文本標簽介紹</title>
<style>
.wenben{
width:300px;
height: 150px;
background-color: #f0f0f0;
padding: 20px;
border: 1px solid #ccc;
margin:0 auto;
}
</style>
</head>
<body>
<div class="wenben">
今天我們來測試一下自己間距的設置方法,主要標簽有<code>text-indent</code>、<code>letter-spacing</code>和<code>line-height</code>,附加講一下<code>first-line</code>和<code>first-letter</code>的樣式。
</div>
</body>
</html>
第一個標簽:text-indent(設置抬頭距離css縮進)
css樣式:
<style>
.wenben{
width:300px;
height: 150px;
background-color: #f0f0f0;
padding: 20px;
border: 1px solid #ccc;
margin:0 auto;
text-indent: 23px;
}
</style>
第二個標簽:letter-spacing(設置字與字之間的間距)
css樣式:
<style>
.wenben{
width:300px;
height: 150px;
background-color: #f0f0f0;
padding: 20px;
border: 1px solid #ccc;
margin:0 auto;
text-indent: 23px;
letter-spacing: 3px;
}
</style>
第三個標簽: line-height(設置行高,就是每一行的高度)
css樣式:
<style>
.wenben{
width:300px;
height: 150px;
background-color: #f0f0f0;
padding: 20px;
border: 1px solid #ccc;
margin:0 auto;
text-indent: 23px;
letter-spacing: 3px;
line-height: 30px;
}
</style>
第四個標簽,其實叫做選擇器: ::first-line(設置第一行的樣式)
<style>
.wenben{
width:300px;
height: 150px;
background-color: #f0f0f0;
padding: 20px;
border: 1px solid #ccc;
margin:0 auto;
text-indent: 23px;
letter-spacing: 3px;
line-height: 30px;
}
.wenben::first-line{
color:blue;
font-weight: bold/*字體加粗*/;
}
</style>
第五個選擇器: ::first-letter(設置第一行的第一個字的樣式)
<style>
.wenben{
width:300px;
height: 150px;
background-color: #f0f0f0;
padding: 20px;
border: 1px solid #ccc;
margin:0 auto;
text-indent: 23px;
letter-spacing: 3px;
line-height: 30px;
}
.wenben::first-line{
color:blue;
font-weight: bold/*字體加粗*/;
}
.wenben::first-letter{
font-size: 24px;
color: white;
font-weight: normal;
border: 1px solid red/*設置文字邊框*/;
background-color:blue/*設置文字背景*/;
padding: 2px;
}
</style>
你學會了嗎?有問題可以私聊我哦!
*請認真填寫需求信息,我們會在24小時內與您取得聯系。