Inline-level Elements and Inline Boxes in CSS


Inline-level elements have their CSS display property set to either ‘inline, ‘inline-table’, or ‘inline-block’ and these elements do not force a line break above and below themselves. Inline-level boxes are generated by each inline-level element which is a part of the positioning scheme as well as contains child boxes.

Inline boxes are boxes which have their content follow inline formatting context. Inline boxes are split into a number of inline boxes whilst those inline boxes that are never split are called atomic inline-level boxes.

Anonymous inline boxes are those boxes over which developer has no control. If a block box contains some bare text and other inline-level boxes, then the bare text around inline-level boxes follows inline formatting context and is enclosed in anonymous inline boxes.

Inline-level vs block-level

The inline- level 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>)

The block-level 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.

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: 21-Dec-2023

274 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements