Understanding CSS Visual Formatting


Visual Formatting in CSS is based on the Box Model. The CSS Visual Formatting is a model corresponding to an algorithm which processes and transforms each element of the document to generate one or more boxes that conform to the CSS Box Model.

The layout of generated boxes depends on several properties such as −

  • Dimensions

  • Type − atomic inline-level, block, inline, or inline-block

  • Positioning − absolute, float, or normal

  • Relation with child and neighbour elements of document

  • External Information − viewport’s and image’s width - height, etc.

CSS Box Generation of processed elements −

  • Block level

    These elements force line break above and below themselves and take up the available whole width even though their content doesn’t require it.

    For example: Divisions (<div>), Heading (<h1> to <h6>), etc.

  • Inline level

    These elements do not force line break above and below themselves and take up only the required width for the content.

    For example − Spans (<span>), Strong element (<strong>)

Block Level box Generation

These elements force line break above and below themselves and take up the available whole width even though their content doesn’t require it. Let us see an example −

Example

In this example, we will work around the block level elements like <form>, <fieldset>, <div>, etc −

<!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>

Example

In this example, we will work around the block level element <form>, <div>, etc. −

<!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>

Inline Level box Generation

These elements do not force line break above and below themselves and take up only the required width for the content. Let’s see an example for inline level box generation −

Example

In this example, we will work around the inline level element <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>

Example

In this example, we will work around the inline level element <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>

Updated on: 27-Dec-2023

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements