理解 CSS 視覺化格式化


CSS 中的視覺化格式化基於盒模型。CSS 視覺化格式化是一個與演算法相對應的模型,該演算法處理並轉換文件的每個元素以生成一個或多個符合 CSS 盒模型的框。

生成的框的佈局取決於幾個屬性,例如:

  • 尺寸

  • 型別 - 原子內聯級、塊級、內聯或內聯塊級

  • 定位 - 絕對、浮動或普通

  • 與文件的子元素和相鄰元素的關係

  • 外部資訊 - 視口和影像的寬度-高度等。

CSS 處理元素的盒生成 -

  • 塊級

    這些元素在其自身的上方和下方強制換行,並佔用可用整個寬度,即使其內容不需要它。

    例如:分隔符 (<div>)、標題 (<h1> 到 <h6>) 等。

  • 內聯級

    這些元素在其自身的上方和下方不強制換行,並且僅佔用內容所需的寬度。

    例如 - 跨度 (<span>)、強元素 (<strong>)

塊級盒生成

這些元素在其自身的上方和下方強制換行,並佔用可用整個寬度,即使其內容不需要它。讓我們看一個例子 -

示例

在這個例子中,我們將圍繞塊級元素,如 <form>、<fieldset>、<div> 等進行操作 -

<!DOCTYPE html>
<html>
<head>
   <title>HTML Heading</title>
   <style>
      form {
         width:70%;
         margin: 0 auto;
         text-align: center;
      }
      * {
         padding: 2px;
         margin:5px;
      }
      input[type="button"] {
         border-radius: 10px;
      }
      #headingPreview h2{
         background-color: #DC3545;
      }
   </style>
</head>
<body>
   <form>
      <fieldset>
         <legend>HTML Heading</legend>
         <input type="text" id="textSelect" placeholder="Type Heading Here...">
         <div id="radioBut">
            <label>H1</label><input class="radio" type="radio" name="heading" value="h1" checked >
            <label>H2</label><input class="radio" type="radio" name="heading" value="h2">
            <label>H3</label><input class="radio" type="radio" name="heading" value="h3">
            <label>H4</label><input class="radio" type="radio" name="heading" value="h4">
            <label>H5</label><input class="radio" type="radio" name="heading" value="h5">
            <label>H6</label><input class="radio" type="radio" name="heading" value="h6">
         </div>
         <div>Heading Preview: <span id="headingPreview">Output will show up here</span></div>
         <input type="button" onclick="changeHeading()" value="Preview">
      </fieldset>
   </form>
   <script>
      var textSelect = document.getElementById("textSelect");
      var headingPreview = document.getElementById("headingPreview");
      function changeHeading() {
         if(textSelect.value === '')
         headingPreview.innerHTML = 'Write a Heading';
         else{
            for(var i=0; i<6; i++){
               var radInp = document.getElementsByClassName('radio')[i];
               if(radInp.checked === true && textSelect.value !== ''){
                  headingPreview.innerHTML = '<'+radInp.value+'>'+textSelect.value+'</'+radInp.value+'>';
                  headingPreview.innerHTML += '<'+radInp.value+'>'+textSelect.value+'</'+radInp.value+'>';
               }
            }
         }
      }
   </script>
</body>
</html>

示例

在這個例子中,我們將圍繞塊級元素 <form>、<div> 等進行操作 -

<!DOCTYPE html>
<html>
<head>
   <title>CSS Block-level Elements and Block Boxes</title>
   <style>
      form {
         width:70%;
         margin: 0 auto;
         text-align: center;
      }
      * {
         padding: 2px;
         box-sizing: border-box;
         /*margin:5px;*/
      }
      input[type="button"] {
         border-radius: 10px;
      }
      .child{
         height: 40px;
         width: 100%;
         color: white;
         border: 4px solid black;
      }
      .child:nth-of-type(1){
         background-color: #FF8A00;
      }
      .child:nth-of-type(2){
         background-color: #F44336;
      }
      .child:nth-of-type(3){
         background-color: #C303C3;
      }
   </style>
</head>
<body>
   <form>
      <fieldset>
         <legend>CSS Block-level Elements and Block Boxes</legend>
         <div id="container">Color Orange
            <div class="child"></div>Color Red
            <div class="child"></div>Color Violet
            <div class="child"></div>
         </div><br>
      </fieldset>
   </form>
</body>
</html>

內聯級盒生成

這些元素在其自身的上方和下方不強制換行,並且僅佔用內容所需的寬度。讓我們看一個內聯級盒生成的例子 -

示例

在這個例子中,我們將圍繞內聯級元素 <em> 進行操作 -

<!DOCTYPE html>
<html>
<head>
   <title>em element</title>
   <style>
      form {
         width:70%;
         margin: 0 auto;
         text-align: center;
      }
      * {
         padding: 2px;
         margin:5px;
      }
      input[type="button"] {
         border-radius: 10px;
      }
      em{
         background-color: #FF8A00;
      }
   </style>
</head>
<body>
   <form>
      <fieldset>
         <legend>em-element</legend>
         <label for="textSelect">Formatter: </label>
         <input id="textSelect" type="text" placeholder="John Doe">
         <input type="button" onclick="convertItalic()" value="Check">
         <div id="divDisplay"></div>
      </fieldset>
   </form>
   <script>
      var divDisplay = document.getElementById("divDisplay");
      var textSelect = document.getElementById("textSelect");
      function convertItalic() {
         for(i=0; i<2; i++){
            var italicObject = document.createElement("EM");
            var italicText = document.createTextNode(textSelect.value);
            italicObject.appendChild(italicText);
            divDisplay.appendChild(italicObject);
         }
      }
   </script>
</body>
</html>

示例

在這個例子中,我們將圍繞內聯級元素 <span> 進行操作 -

<!DOCTYPE html>
<html>
<head>
   <title>CSS Inline-level Elements and Inline Boxes</title>
   <style>
      form {
         width:70%;
         margin: 0 auto;
         text-align: center;
      }
      * {
         padding: 2px;
      }
      input[type="button"] {
         border-radius: 10px;
      }
      .child{
         color: white;
         border: 4px solid black;
      }
      .child:nth-of-type(1){
         background-color: #FF8A00;
      }
      .child:nth-of-type(2){
         background-color: #F44336;
      }
      .child:nth-of-type(3){
         background-color: #C303C3;
      }
   </style>
</head>
<body>
   <form>
      <fieldset>
         <legend>CSS Inline-level Elements and Inline Boxes</legend>
         <div><span class="child">Orange</span> Color<span class="child">Red</span> Color<span class="child">Violet</span> Color</div><br>
      </fieldset>
   </form>
</body>
</html>

更新於: 2023-12-27

173 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告