CSS syntax and xml association 1

title: Hello World

CSS syntax and xml association 1

Article directory

  • CSS syntax and xml association 1
    • CSS selector
      • element selector
      • ID selector
      • class selector
      • attribute selector
    • CSS properties
      • display properties
      • Setting the font attribute
      • Border property settings
      • Layout property settings
      • Background attribute settings
      • Text attribute settings
    • CSS float
      • Float
      • margin
      • clear float
    • CSS application styles
      • external style sheet
      • internal style sheet

XML is
Storing structured data provides a powerful method, but it does not provide information about how the data is displayed (the structure of the data has nothing to do with the data representation). You can use CSS to control the presentation of elements in an XML document.

It consists of three parts: selector, property, and value. The basic syntax is as follows:

selector {<!-- -->
    property1: value;
    property2: value;
    property3: value;
}

Although CSS is not case-sensitive, in XML, names such as Name, NAME, and name will be treated as the same element by CSS, so it is impossible to set different attributes for these elements. Therefore, completely different names should be set to distinguish them.

CSS selector

Element selector

Selects all matching elements given a node name.

Syntax:elementname

Example:input matches any element.

element {<!-- --> style declaration }

ID selector

Selects a matching element based on the id attribute. It should be noted that each ID attribute in a document should be unique.

Syntax:#idname

Example:#toc matches the element with the ID “toc”.

#id attribute value {<!-- --> style declaration }

Class selector

Selects all matching elements based on the value of the given class attribute.

Syntax:.classname

Example:.index matches any element containing the class “index” in the class attribute.

.Class name {<!-- --> Style declaration }

Attribute selector

Selects all matching elements according to the given attribute.

Syntax:[attr] [attr=value] [attr~=value] [attr| =value] [attr^=value] [attr$=value] [attr*=value]

Example:[autoplay] Selects all elements with the autoplay attribute (regardless of the value of this attribute).

ID selector: chrome, firefox normal, IE does not support

Class selector: chrome, IE does not support it, firefox supports it.

Attribute selector: all supported

Element selector: all supported

CSS properties

Display properties

In CSS, the display attribute is usually used to control the text display effect of browser elements.

display attribute value Description
block Display the element in a block and display it separately from other elements through line breaks
inline Display elements on the same line
none Hide elements so that they are on the page Invisible
list-item Display elements in a list

CSS stipulates that the display attribute cannot be inherited by other child elements, so if the display attribute of the parent element is set to block, it does not mean that it has set this attribute for the child element. But there are two special cases:

  • If the display attribute of the parent element is set to none, the child elements actually inherit the attribute setting of none, because when the parent element is hidden, all child elements will be hidden at the same time;
  • The inline attribute value of the parent element will also be inherited by the child element, because the IE browser displays element content inline by default.

Example:

  1. Create a new xml file: employee.xml, with the following content:

    <?xml version="1.0" encoding="GB2312"?>
    <?xml-stylesheet type="text/css" href="test01.css"?>
    <Employees>
        <Employee>
            <name>Zhang Tao</name>
            <sex>Male</sex>
            <age>18</age>
            <department>Development Department</department>
        </Employee>
        <Employee>
            <name>Li Xueping</name>
            <sex>Female</sex>
            <age>34</age>
            <department>Sales Department</department>
        </Employee>
    </Employees>
    
  2. Create a new css document: employee.css, with the following content:

    Employee {<!-- -->
        display: block;
        margin-top: 16pt;
    }
    name {<!-- -->
        font-weight: bold;
    }
    Employee#1 {<!-- -->
        font-size: 25pt;
        font-style: italic;
    }
    Employee#2 {<!-- -->
        font-size: 20pt;
        font-style: bold;
    }
    department {<!-- -->
        display: none;
    }
    

    image-20231106223031509

Setting the font attribute

< /table>

Setting border attributes

  • border-style: Set the style around the border;
  • none, dotted, dashed (the border line is dotted), solid, double, groove (the border line is set to have a 3D effect), inset (the border line is set to have a sinking effect), outset (the border line is set to have a floating effect).
  • border-color:
  • The value is the English name or RGB value of the color. By default, the border and element have the same color;
  • border-width:
  • thin, medium, thick

Setting layout attributes

  • Positioning attributes: The position, width and height of elements can be set through the positioning attributes of CSS;
  • top, bottom, left, width, height, right;
  • margin attribute: By default, the margin of an element in CSS is 0;
  • margin (set the distance between the upper, lower, left and right boundaries of the element at the same time), margin_top, margin_bottom, margin_left, margin_right;
  • padding attribute: After the border attribute is set, the distance between the element and the border may be too close. By setting the padding attribute, the distance between the border and the element will not be too close;
  • padding, padding_top, padding_bottom, padding_left, padding_right;

Setting background attributes

font sub-property Description Value
font-family Set the text font Song, Hei, Kai_gb2312
font-style Set the font style of the text normal, italic, oblique
font-weight Set the thickness of the text normal, bold, bolder, lighter
font-variant Set the capitalization of text letters normal, small-caps
font-size Set text size small, medium, large / pt, cm, in…
Background sub-property Description Value
backgroung-color Set the background color of the element The English name or RGB value of the color
background-image Set the background image of the element none, URL, file name (in the same directory as CSS)
background-repeat Set the repeating style of the element background image repeat, repeat-x (repeated tiles in the horizontal direction), no-repeat
background-attachment Set whether the element background image scrolls with the element content scroll , fixed
background-position Set the position of the element background image relative to the text background-position-horizontal, background-position-vertical(top, center, bottom, left, right)

Setting text attributes

In CSS, the spacing and line height of element text, converting letters to upper and lower cases, and adding various modifications to text, etc.

Set text attributes Instructions Value
letter-spacing Settings Character spacing of elements None
vertical-align Set the vertical alignment of element text baseline, sub (text displayed with subscripts), super, top, middle, bottom
text-align Set the horizontal alignment of element text left, center, right, justify (align both ends)
text-transform Set the case conversion method of element text capitalize (the first letter is capitalized), uppercase, lowercase, none
text-decoration Set the decoration method of element text underline, overline, line-through, blink (text flashing), none

CSS float

Float

CSS Float will move an element to the left or right, and its surrounding elements will also be rearranged.

Float is often used for images, but it is also very useful in layout.

xml:

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/css" href="test01.css"?>
<Employees>
    <div></div>
    <p>
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
    </p>
</Employees>

css:

div{<!-- -->
    display: block;
    width: 150px;
    height: 100px;
    border-radius: 5px;
    background-color: rgb(207, 232, 220);
    padding: 1em;
}
p{<!-- -->
    font-size: 20px;
}

image-20231106225002064

To make the box float, add the float and margin-right attributes below the rule div:

div{<!-- -->
    float: left;
    margin-right: 15px;
    width: 150px;
    height: 100px;
    border-radius: 5px;
    background-color: rgb(207, 232, 220);
    padding: 1em;
}

image-20231106225137872

Let’s consider how floats work – a floated element (a

element in this example) breaks out of the normal document layout flow and snaps to the left of its parent container. Content that would be below the floated element in a normal layout will now surround the floated element, filling the space to its right.

margin

Visualize floating effects

We can apply margin on floating elements to push text away, but we cannot apply margin on text to push floating elements away. This is because the floated element breaks away from the normal document flow, and the elements immediately following it are arranged “behind” it. You can change the sample code to observe this phenomenon.

Add the t2 class to the first paragraph of text immediately following the floating box, and then add the following rule to your CSS file, which will give a background color to the paragraphs that follow it.

div{<!-- -->
    float: left;
    margin: 10px;
    width: 150px;
    height: 200px;
    border-radius: 5px;
    background-color: rgb(207, 232, 220);
    padding: 1em;
}
#t1{<!-- -->
    font-size: 20px;
}

#t2{<!-- -->
    display: block;
    background-color: rgb(79, 185, 227);
    color: #fff;
}
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/css" href="test01.css"?>
<Employees>
    <div></div>
    <p id="t2">
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
    </p>
    <p id="t1">
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
        Here is some text. Here is some text. Here is some text.
    </p>
</Employees>

To see the effect more clearly, change the floating margin-left to margin to leave all the surrounding space empty. As shown in this code effect, you can see that the background color of the paragraph is under the floating box.

image-20231106230352238

Clear float

We see that a floated element is moved out of the normal document flow and other elements are displayed below it. If we don’t want the remaining elements to be affected by the floated element, we need to stop it; this is done by adding the clear attribute.

In the HTML code of the previous example, add the cleared class to the second paragraph below the floated element, and then add the following style to the CSS file:

#t1{<!-- -->
    font-size: 20px;
    display: block;
    clear: both;
}

image-20231106231436306

You should see that the second paragraph has stopped floating and will no longer be arranged following the floating elements. The clear attribute accepts the following values:

  • left: Stop any active left floating
  • right: Stop any active right floating
  • both: Stop any active left and right floating

CSS application style

If the created CSS is not linked to the XML file, the elements in the document cannot be displayed in the browser according to the rules of the style sheet.

There are three types of HTML application styles (inline, inline, external reference). XML application styles should only fully support external references at present.

External style sheet

<?xml:stylesheet type="text/css" href="***.css"?>

Example:

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/css" href="test01.css"?>
<Employees>
    <Employee>
        <name>fwt</name>
        <sex>Male</sex>
        <age>21</age>
        <department>Development Department</department>
    </Employee>
</Employees>

image-20231106215253207

You can use multiple xml:stylesheet statements in an XML document to link multiple stylesheet files.

Internal style sheet

<?xml-stylesheet type="text/css"?>
<Root element xmlns:HTML="URL">
    <HTML:STYLE>
        <!--CSS content-->
    </HTML:STYLE>
    <!--XML sub-element-->
</root element>

Example:

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/css"?>
<Employees xmlns:HTML="http://www.w3.org/Profiles/XHTML-transitional"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.w3.org/Profiles/XHTML-transitional ">
    <HTML:STYLE>
        name{
        font-weight:bold
        }
        sex{
        font-size:200px
        }
        age{
        font-style:italic
        }
    </HTML:STYLE>
    <Employee>
        <name>fwt</name>
        <sex>Male</sex>
        <age>21</age>
        <department>Development Department</department>
    </Employee>
</Employees>

This method of testing is no longer suitable for Google and IE browsers.

image-20231106214940187