This is the 17th day of my participation in Gwen Challenge
Indented text
One of the most common text formatting effects is to indent the first line of a paragraph on a Web page.
text-indent:[ <length> | <percentage> ] && hanging? && each-line?
Copy the code
- Applies to: Block containers
<length>
: Specifies the indentation of the text with the length value. It can be negative.<percentage>
: Specifies the indentation of the text as a percentage (relative to the parent element width). It can be negative.- Each -line: defines the indentation applied to the first line of the block container or to the first line of every forced newline inside the block container. Soft newlines are not affected. (CSS3, browser not yet supported)
- Hanging: Reverse all indent rows. (CSS3, browser not yet supported)
<! DOCTYPEhtml>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.parent {
width: 400px;
background-color: yellow;
}
.text-indent {
width: 200px;
text-indent:100%;
font-size: 16px;
background-color: gray;
}
</style>
</head>
<body>
<div class="parent">
<div class="text-indent">
Text-indent.
</div>
</div>
</body>
</html>
Copy the code
When the parent element is 400px, the child element text-indent:100%; It has reached 400px, so the percentage is relative to the parent block element.
Horizontal alignment
text-align: start | | endleft | right | center | justify | match-parent | justify-all
Copy the code
- Applies to: Block containers
- Left: left-align content.
- Center: align the content in the center.
- Right: Align content to the right.
- Justify: The content is aligned at both ends, but does not justify forced breaks (the broken line) or the last line (including only one line of text, because it is both the first and last line). (range)
- Start: Align the content with the start boundary. (range)
- “End” : align the content with the end boundary. (range)
- Match-parent: this value is the same as inherit, except that the start or end keyword is calculated for the parent’s <‘ direction ‘> value. The calculated values can be left and right. (range)
- Context-all: The effect is equivalent to justify, but also aligns the last line. (range)
/ / test the justify.justify {
text-align: justify;
}
<div class="justify">
WASHINGTON - US President Donald Trump and the top leader of the Democratic People's Republic of Korea (DPRK), Kim Jong Un, are scheduled to meet at 9:00 am Singapore time on June 12, the White House said on Monday. At a press briefing, White House spokesperson Sarah Sanders also said that the U.S. advance team now in Singapore "is finalizing logistical preparations and will remain in place until the summit begins." Noting that the US side is "actively" preparing for the meeting, she said that the US delegation in the demilitarized zone (DMZ) along the inter-Korean border continues diplomatic negotiations with the DPRK delegation. "The discussions have been very positive and significant progress has been made," she said. Besides the exchanges in Singapore and the DMZ, US Secretary of State Mike Pompeo last week met with Kim Yong Chol, vice chairman of the DPRK's ruling Workers' Party of Korea Central Committee, in New York.
</div>
Copy the code
Let’s take a look at the current results:
Currently, the text on the page is aligned at both ends, but it can be seen from the content in the red box of the picture that word islands appear. When the text is aligned at both ends, the word spacing needs to be adjusted, and the “word islands” phenomenon occurs. The result not only looks bad, it damages readability. There are several ways to implement character line breaking on web pages, but the results are pretty poor. The diagram below:
.justify {
text-align: justify;
word-break:break-all;
}
Copy the code
CSS Text (version 3) introduces a new property 登 记. It accepts three values: None, manual, and auto. Manual is its initial value, and its behavior corresponds exactly to the existing way of working: we can manually insert a soft hyphen at any time to achieve the effect of hyphenation. Obviously 登 记 : None; Disables this behavior. However, the Chorme browser does not support the current properties, the specific properties can be queriedcaniuse. And we have to addword-wrap:break-word;
Use together.
.justify {
text-align: justify;
word-wrap:break-word;
-moz-hyphens: auto;
-ms-hyphens: auto;
-webkit-hyphens: auto;
hyphens: auto;
}
Copy the code
We can see that there are multiple link characters “-” in the red box.
The vertical alignment
Extension: Where do the heights of block-level elements and inline elements come from?
Block-level elements: In cases where height is set, height is used. If height is not set, the content of the block-level element prevails. Content is eventually directed to inline elements (text content in block-level elements is surrounded by anonymous block-level boxes).
Inline elements: Inline elements are complicated, why? The larger the font, the larger the text. It is important to remember that the size of the font does not necessarily affect the height of the contained block-level elements. A specific example of this has already been tested by Daniel, please see the article. The conclusion is that line-height determines the height of the block-level element (if no height is set).
Extension: The background of the text content does not affect the half-space
<style>
.block {
background-color: gray;
line-height: 70px;
}
</style>
<divClass = "block" > test <font style="background-color: red;padding: 10px;margin: 10px;" > background of text content </font> does not affect half-space. </div>
Copy the code
In the image below, the background color applies to the text box plus the padding and margin. But it doesn’t apply to half space.
Extension: The content area of the text may be larger than font size
* {
font-family: "Comic Sans MS";
}
.inline {
font-size: 80px;
line-height: 1em;
background-color:grey;
opacity: 0.7;
padding: 0;
}
Copy the code
<div style="background-color: teal;">
<span class="inline">Test content area draw.</span>
</div>
Copy the code
The code here can see that the current span tag’s line-height is 1em, or 80px. So the height of the parent element is 80px. But the background of the SPAN element is actually larger than 80px, and the actual height tested here is 111px, and the font height changes as the font changes. What’s the reason here?
There may be differences between the EM box and character glyphs of fonts. For most fonts, the height of the EM box is inconsistent with the height of the character glyph.
CSS2.1 states: “The height of the content area should be based on the font, but this GUI fen does not specify how to determine the height of the content area based on the font. Use em boxes or font glyphs “. So there’s a lot of room for the browser to manipulate. The browser controls the height of the content area of the current font.
line-height
Line-height refers to the distance between the baseline lines of text, not the font size, and determines how much to increase or decrease the height of each element box. The difference between the calculated value of line-height and font size (called “line spacing” in CSS) is divided in half and added to the top and bottom of a line of text, respectively. The smallest box that can contain this is the line box.
- The normal default. Set reasonable line spacing.
- Number Sets the number that is multiplied by the current font size to set the line spacing.
- Length sets a fixed line spacing.
- % The percentage of line spacing based on the current font size.
- Inherit specifies that the value of the line-height property should be inherited from the parent element.
Align vertically – vertical-align
Start with the div content being vertically centered
Let’s start with an example:
.block-parent{
background-color: teal;
height: 200px;
}
.block-child {
background-color: gray;
height: 100px;
}
Copy the code
<h3>Test the vertical-align environment</h3>
<hr/>
<h5>The parent element is a block</h5>
<div class="block-parent">
<div class="block-child">
block child
</div>
</div>
Copy the code
First we use line-height to center the child div, and modify the CSS file as follows:
.block-parent{
background-color: teal;
height: 200px;
line-height: 200px;
}
.block-child {
background-color: gray;
height: 100px;
line-height: 100px;
display: inline-block;
}
Copy the code
Here’s how it works: First of all, this example is just for use with line-height.
From what we learned earlier, we know that line-height is the sum of a text box and a half-space. The height of the text box is determined by the font size and the size of the font that the browser draws. Regardless of the height of the text box, when the height of the line-height increases, the spacing between the top and bottom will only increase without changing the font size. And the upper and lower half distances are equal.
When the height of the parent element is the same as the height of the parent element, the content of the parent element is naturally treated as an inline element with line-height of 200px, but this does not apply to the child element of the block. So the child element is set to inline-block and line-height is set to the height of the child element because line-height is inheritable.
Vertical-align implements center
The above example uses line-height to center the text box. Recall that this is done by equalizing the upper and lower halves and placing the text box in the vertical center. Again, the baseline of our line model.
Why do I mention baselines? Because the baseline is the initial position of the vertical position of the text. X is the height of the letter x. X is the height of the letter x. X is the height of the letter x. X is the height of the letter x. Remember the four lines and three grids, the center is the baseline + 0.5ex.
At this point we seem to have found the key, which is to move the center line of the current inline content to the baseline + 0.5ex of the parent element’s text content.
Vertical-align :middle aligns the vertical midpoint of the inline element box with 0.5ex above the parent element baseline.
.block-parent{
background-color: teal;
height: 200px;
}
.first-block-child {
background-color: gray;
height: 100px;
display: inline-block;
line-height: 100px;
vertical-align: middle;
}
.second-block-child {
background-color: gray;
height: 200px;
display: inline-block;
line-height: 200px;
vertical-align: middle;
}
Copy the code
<h3>Test the vertical-align environment</h3>
<hr/>
<h5>The parent element is a block</h5>
<div class="block-parent">
<div class="first-block-child">
first block child
</div>
<div class="second-block-child">
</div>
</div>
Copy the code
As shown, I currently center first-block-child based on class as second-block-child.
Conclusion: In fact, the core idea of vertical-align is the inner line frame, which uses different positions of the inner line frame as the baseline to align with the elements we want to adjust the position.
Vertical – align profile
The vertical-align attribute applies to inline elements (input, image) and table-cell elements.
Formal syntax: baseline | sub | super | text-top | text-bottom | middle | top | bottom | <percentage> | <length>
Copy the code
Not inheritable
- Baseline Element Baseline is aligned with the baseline of the parent element. For some replaceable elements, for example, the HTML standard does not specify its baseline, which means that browsers may behave differently when using this keyword.
- The sub element baseline is aligned with the subscript baseline of the parent element.
- The super element baseline is aligned with the superscript baseline of the parent element.
- The top of the text-top element is aligned with the top of the font of the parent element.
- The bottom of the text-bottom element is aligned with the bottom of the font of the parent element.
- The vertical line in the middle element is aligned with half the height of the parent element’s baseline plus lower case X.
- The element baseline exceeds the specified height of the parent element baseline. I can take negative values.
- Again, the percentage is relative to line-height.
The following two values are relative to the entire line:
- The top of the top element and its descendants are aligned with the top of the entire line.
- The bottom of the bottom element and its descendants are aligned with the bottom of the entire line. If the element does not have a baseline baseline, the lower edge of its margin is used as the baseline.
Baseline rules
Baseline is the most important baseline for text position. If we find it, we can handle the position layout of the current text line.
- The baseline of an inline-table element is the baseline of the first row of its table.
- The baseline of the parent element line box is the baseline of the last inline box.
- The baseline determines the rule for the inline-block element
- Rule 1: The baseline of an inline-block element is the baseline of the last element that exists as a content element [inline box], if there is a line box.
- Rule 2: For an inline-block element, if there is no line box inside it or its overflow property is not visible, then the baseline will be the bottom margin of the inline-block element.
Example: Rule 2
<h3>The baseline of vertical-align is 2</h3>
<hr/>
<div>
<img src="./img/bg.jpg" alt="link"/>
</div>
Copy the code
Figure: This is a normal image with no other CSS styles, and we can see a little white space (not white space because of the background) in the current red box. Why rule two? There seems to be no matching project, so let’s modify the code a little bit and add a few XXX.
<h3>The baseline of vertical-align is 2</h3>
<hr/>
<div>
<img src="./img/bg.jpg" alt="link"/>
<font>xxxxx</font>
</div>
Copy the code
We can now see that the current image is at the same level as the baseline of the text content XXXX. Here you may have a question, does the picture not affect the text line height?
The size of text is determined by font size, so the size of XXXX here will not be affected. The second question is the location of XXXX. It affects the position of the text in the line. As you know, linge-height affects the position of the text. But there’s no impact here, right? Here’s why:
The concept of the box model for inline elements has a phrase (more on that next time) : The inner margins, borders, and margins of non-replacement elements have no vertical effect on the formation of the box (that is, they do not affect the height of the element’s inner box), that is, they have no effect in the vertical direction. But the background has an effect.
What does that mean? Non-replacement elements do not affect most of the style presentation of the line of text, but they do affect the background, which is the line height, but not line-height. If the text is displayed without an image, the height of the image increases the height of the text line, but does not affect the line-height of the text line. So the text moves around the img height, and it’s understandable that the space above the line of text expands because of the img height. Take a look at the image below and see that the current background is consistent with the image when I photograph the text.
Eliminate the white space in the lower part of the above example
To eliminate the white space in the example, we can think of several ways:
- Change the image to a block, which is not affected by the line.
- The space below the text is due to the example between baseline and baseline, so we can consider not showing the example between baseline and baseline in the current text box, that is, reducing line-height. Since the value of line-height is smaller than the value of em, the distance will be reduced in reverse, but the value cannot be negative, otherwise it will not work. This approach is not recommended because distance will still exist from the current document.
- Font size is set to 0 so that lines of text are removed directly and the default base-line is not affected.
- Use other alignment: vertical – align: bottom | vertical – align: middle | vertical – align: top
Word spacing and letter spacing
Words between word spacing
word-spacing:normal | <length> | <percentage>
Copy the code
- Inheritance: Yes
- Normal: default interval
<length>
: Specifies the interval with a length value. It can be negative.<percentage>
: Specifies the interval as a percentage. It can be negative. (range)
.percentage {
word-spacing: 0.5 em;
}
Copy the code
Compatibility requires attention:
Values | IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
---|---|---|---|---|---|---|---|---|
Basic Support | 6.0 + | 2.0 + | 4.0 + | 6.0 + | 15.0 + | 6.0 + | 2.1 + | 18.0 + |
6.0-11.0 | 2.0-37.0 | 4.0-41.0 | 6.0-8.0 | 15.0-27.0 | 6.0-8.3 | 2.1-4.4.4 | 18.0-40.0 |
Letter-spacing
letter-spacing:normal | <length> | <percentage>
Copy the code
- Inheritance: Yes
- Normal: default interval
<length>
: Specifies the interval with a length value. It can be negative.<percentage>
: Specifies the interval as a percentage. It can be negative. (range)
The compatibility is as follows:
Values | IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
---|---|---|---|---|---|---|---|---|
Basic Support | 6.0 + | 2.0 + | 4.0 + | 6.0 + | 15.0 + | 6.0 + | 2.1 + | 18.0 + |
6.0-11.0 | 2.0-37.0 | 4.0-41.0 | 6.0-8.0 | 15.0-27.0 | 6.0-8.3 | 2.1-4.4.4 | 18.0-40.0 |
The text conversion
text-transform:none | capitalize | uppercase | lowercase | full-width
Copy the code
- Inheritance: Yes
- None: No conversion
- Capitalize: Capitalize the first letter of each word
- Uppercase: Converts each word to uppercase
- Lowercase: Convert each word to lowercase
- Full-width: Converts all characters to fullwidth. If a character does not have a corresponding fullwidth form, it is left as is. This value is commonly used in typesetting ideograms such as Latin characters and numbers. (range)
The compatibility is as follows:
Values | IE | Firefox | Chrome | Safari | Opera | iOS Safari | Android Browser | Android Chrome |
---|---|---|---|---|---|---|---|---|
Basic Support | 6.0 + | 2.0 + | 4.0 + | 6.0 + | 15.0 + | 6.0 + | 2.1 + | 18.0 + |
full-width | 6.0-11.0 | 2.0-18.0 | 4.0-45.0 | 6.0-8.0 | 15.0-29.0 | 6.0-8.3 | 2.1-4.4.4 | 18.0-42.0 |
Text is just a little bit more, I’ll talk about it next time, and the concept of inline elements.