CSS is all called "Cascading style Sheets (Cascadingstyle Sheets)",
CSS styles consist of selectors and declarations , and declarations are made up of attributes and values
Just like in HTML annotations, there are comment statements in CSS: Used /* 注释语句*/ to mark (use in HTML <!-- 注释语句--> ).
Inline CSS style, written directly in existing HTML tags
<p > here the text is red. </p>
CSS style code to be written in double quotes, if there are more than one CSS style code settings can be written together, separated by semicolons. The following code:
<p > here the text is red. </p>
Embedded CSS style, written in the current file
Write CSS style code between <styletype= "text/css" ></style> tags.
External CSS style, written in a separate file
<linkhref="Base.css" rel="stylesheet"type="text/css" />
? E5?? is to set the div as the block element horizontally centered:
HTML code:
<body>
<div> I am a fixed width block element, haha, I want to horizontally center the display. </div>
</body>
CSS code:
<style>
div{
BORDER:1PX solid red;/* in order to show that the center effect is clearly set the div */
width:500px;/* Fixed Width */
margin:20px auto;/* Margin-left and margin-right set to Auto */
}
</style>
Horizontal Center Summary-variable width block element method (i)
In the actual work we will encounter the need to set the "block element of the indefinite width" center, such as page navigation on the pages, because the number of paging is indeterminate, so we can not set the width to limit its elasticity.
There are three ways to center the indeterminate width of a block element (these three methods are currently used in more than one):
- Add Table Label
- Set the Display;inline method
- Set position:relative and left:50%;
In this section we'll take a look at the first method:
First step: Add a table label (including <tbody>, <tr>, <td>) to the center of the element you want to set.
Step two: Set the "left and right margin center" for this table (this is the same as for a fixed-width block element).
HTML code:
<div>
<table>
<tbody>
<tr><td>
<ul>
<li><a href= "#" >1</a></li>
<li><a href= "#" >2</a></li>
<li><a href= "#" >3</a></li>
</ul>
</td></tr>
</tbody>
</table>
</div>
CSS code:
<style>
table{
margin:0 Auto;
}
ul{list-style:none;margin:0;padding:0;}
li{float:left;display:inline;margin-right:8px;}
</style>
Horizontal Center Summary-variable width block element method (ii)
The second method: Change the display of the block-level element to the inline type, and then use the text-align:center to achieve the centering effect. Here's an example:
HTML code:
<body>
<div class= "Container" >
<ul>
<li><a href= "#" >1</a></li>
<li><a href= "#" >2</a></li>
<li><a href= "#" >3</a></li>
</ul>
</div>
</body>
CSS code:
<style>
. container{
Text-align:center;
}
. Container ul{
List-style:none;
margin:0;
padding:0;
Display:inline;
}
. Container li{
margin-right:8px;
Display:inline;
}
</style>
This approach has the advantage of not adding no semantic tags and simplifying the nesting depth of the tags, but there are also some problems: it changes the display type of the block element to inline, so it has fewer features, such as setting the length value.
Horizontal Center Summary-variable width block element method (iii)
Method Three: By setting float for the parent element and then setting position:relative and left:50% for the parent element, the child elements are set position:relative and left:-50% to achieve horizontal centering.
The code is as follows:
<body>
<div class= "Container" >
<ul>
<li><a href= "#" >1</a></li>
<li><a href= "#" >2</a></li>
<li><a href= "#" >3</a></li>
</ul>
</div>
</body>
CSS code:
<style>
. container{
Float:left;
position:relative;
left:50%
}
. Container ul{
List-style:none;
margin:0;
padding:0;
position:relative;
left:-50%;
}
. container li{float:left;display:inline;margin-right:8px;}
</style>
Center vertically-single line of text with parent element height determination
The height of the parent element determines the vertical centering of a single line of text by setting the height of the parent element and the line-height to a high consistency. The following code:
<div class= "Container" >
hi,imooc!
</div>
CSS code:
<style>
. container{
height:100px;
line-height:100px;
Background: #999;
}
</style>
Center vertically-multi-line text with parent element height determination (method one)
There are two ways to align the height of a parent element with multiple lines of text, a picture, and a block element vertically:
Method One: Use the Insert table (including TBODY, TR, TD) labels, and set the Vertical-align:middle.
When it comes to vertical centering, the CSS has a property vertical-align for vertical centering, but this style only takes effect if the parent element is TD or TH. So we have to insert the table tag again. Take a look at the example below:
HTML code:
<body>
<TABLE><TBODY><TR><TD class= "Wrap" >
<div>
<p> See if I can center. </p>
<p> See if I can center. </p>
<p> See if I can center. </p>
<p> See if I can center. </p>
<p> See if I can center. </p>
</div>
</td></tr></tbody></table>
</body>
CSS code:
Table Td{height:500px;background: #ccc}
Because the TD label defaults to Vertical-align as middle by default , we don't need to set it explicitly.
Center vertically-Multiple lines of text (method two) of the parent element height determination
In Chrome, Firefox and IE8 above the browser can be set block-level element display for Table-cell, activating vertical-align properties, but note IE6, 7 does not support this style.
HTML code:
<div class= "Container" >
<div>
<p> See if I can center. </p>
<p> See if I can center. </p>
<p> See if I can center. </p>
<p> See if I can center. </p>
<p> See if I can center. </p>
</div>
</div>
CSS code:
<style>
. container{
height:300px;
Background: #ccc;
Display:table-cell;/*ie8 above and Chrome, firefox*/
Vertical-align:middle;/*ie8 above and Chrome, firefox*/
}
</style>
The advantage of this method is not to add redundant meaningless tags, but the disadvantage is also obvious, its compatibility is not very good, incompatible with IE6, 7.
Implicitly changing the display type
One interesting phenomenon is that when you set one of the following 2 sentences for an element (regardless of what type of element was previously, Display:none):
- Position:absolute
- Float:left or Float:right
The element automatically becomes Display:inline-block , and of course you can set the width and height of the element and the default width does not fill the parent element.
As the following code, the small partners know that a tag is an inline element, so setting its width is not effective, but set to Position:absolute later, it is OK.
<div class= "Container" >
<a href= "#" title= "" > Enter course click here </a>
</div>
CSS Code
<style>
. Container a{
Position:absolute;
width:200px;
Background: #ccc;
}
</style>
CSS Learning Notes