#CSS text properties define the appearance of text.
Indent text
Indent the first line of a paragraph on a Web page. This is one of the most commonly used text formatting effects. (Recommended learning: CSS Getting Started Tutorial)
CSS provides the text-indent attribute, which can easily implement text indentation.
By using the text-indent attribute, the first line of all elements can be indented by a given length, even a negative value.
The most common use of this attribute is to indent the first line of a paragraph. The following rules will indent the first line of all paragraphs by 5 em:
p {text-indent: 5em;}
Note: Generally Say, text-indent can be applied to all block-level elements, but the attribute cannot be applied to inline elements, nor can it be applied to replacement elements such as images. However, if a block-level element (such as a paragraph) has an image in the first line, it will move with the rest of the text in that line.
Tip: If you want to "indent" the first line of an inline element, you can use left padding or margin to create this effect.
Use negative values
text-indent can also be set to negative values. Using this technique, you can achieve many interesting effects, such as "hanging indent", where the first line hangs to the left of the rest of the element:
p {text-indent: -5em;}
But when setting a negative value for text-indent, you need to Be careful, if you set a negative value for a paragraph, some of the text in the first line may extend beyond the left edge of the browser window. In order to avoid this display problem, it is recommended to set an additional margin or some padding for negative indentation:
p {text-indent: -5em; padding-left: 5em;}
Use a percentage value
text-indent OK Use all length units, including percentage values.
The percentage is relative to the width of the indented element's parent element. In other words, if you set the indent value to 20%, the first line of the affected element will be indented by 20% of the width of its parent element.
In the following example, the indent value is 20% of the parent element, which is 100 pixels:
div {width: 500px;} p {text-indent: 20%;} <div> <p>this is a paragragh</p> </div>
Inherited
text-indent attribute can be inherited, please consider the following markup:
div#outer {width: 500px;} div#inner {text-indent: 10%;} p {width: 200px;} <div id="outer"> <div id="inner">some text. some text. some text. <p>this is a paragragh.</p> </div> </div>
The paragraph in the above markup will also be indented by 50 pixels. This is because this paragraph inherits the div with the id of inner The indent value of the element.
The above is the detailed content of How to indent text in CSS?. For more information, please follow other related articles on the PHP Chinese website!

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)
