Hey.

One question is not clear to me. The example will be clear. I create a block element <div></div> given size and throw in it other <div> </div> elements of such a specified size so that they are closely inside the parent. And all padding and margin are reset to zero, I want the objects to be "very tight". I get this in Figure 1.

Here are the styles:

 .images{ margin:0px; padding:0px; width:200px; height:200px; display:inline-block; border-radius:10px; } #container{ margin:0px; padding:0px; width:600px; height:600px; border:5px solid orange; background-color:orange; position:absolute; border-radius:10px;; } 

I delete text DOM node objects that the browser arbitrarily stuffed between my divas. If they are removed, then the divas will be located "side by side", as in Figure 2. But there remains a gap between the top and bottom "line".

Questions:

  1. What is this gap? I removed all unnecessary text nodes. How to remove this gap between the "lines"?

  2. Why does the browser cram extra text nodes between my nodes? For some reason, the developers came up with it.

enter image description here

  • It is also worth adding markup and styles. On the sidebar - all consecutive whitespace characters in html go one space, which is shown between inline elements. - Grundy
  • For block elements such a problem should not be. Most likely a mistake in the styles used - Grundy
  • there is no specific markup and there is no <div> </ div> element and ten <div> </ div> elements inside it. A common question, this always happens - this is not a flaw in a particular code. indents set zero everywhere to see how objects behave. I wanted to cram the squares into a big square “zapadlitso” - so that there were no gaps at all - Dimon
  • Without styles, they would not look like that. There is some kind of code. Would show. We would help - Mihanik71
  • need a specific example of markup and styles in which the view is obtained as in the provided pictures. - Grundy

4 answers 4

Problem in using display:inline-block .

An alternative solution is to use display:flex on a container, with the flex-wrap:wrap setting.

 .images { margin: 0px; padding: 0px; width: 190px; height: 190px; display: inline-block; border-radius: 10px; border: 5px solid red; background-color: green; } #container { margin: 0px; padding: 0px; width: 400px; height: 400px; border: 5px solid orange; background-color: orange; position: absolute; border-radius: 10px; display: flex; flex-wrap: wrap; } 
 <div id="container"> <div class="images"></div> <div class="images"></div> <div class="images"></div> <div class="images"></div> </div> 

    To disappear indent, you need to reset the line-height.

    • Thank you, I was told the same thing at the toaster at the same time) - Dimon
    • Usually, the parent sets the font-size: 0; and line-height: 0, and then override them if necessary. - dmitryshishkin

    Often faced with this inline-block behavior, my solution is not very pleasant visually, all the divs with inline-block are written in one line. namely, the closing tag must be in one line and without a space followed by the opening one.

     <div id="container"> <div class="images"> </div><div class="images"> </div> <div class="images"> </div><div class="images"> </div> </div> 
    • this only solves the side pass. interline remains - Grundy
    • I didn’t even notice the amusement, thanks for a note on the future - Anser
    • one
      vertical-align: top; for .images and indent disappears - Anser

    I wrote a mini-game in javascript and ran into this problem. I wanted the cells of the playing field adjoined to each other (so that there was no playing field in the form of “cells”, where the cells are separated by borders). When I create block elements with inline-block elements, they have both the properties of inline elements and block ones. And the lowercase elements have the concept of "distance between the lines." Plus, even the lower-case elements have a line break, this is a space, which can also visually indent. It turns out that there are special line spacing in CSS, which is governed by the CSS line-height property. Never heard of them. Applied line-height: 0px, and the problem was solved - the gaps between the lines "collapsed."

    On the second part of the question, namely

    2. Why does the browser cram extra text nodes between my nodes? For some reason, the developers came up with it. I will answer myself.

    The browser regards any characters (numbers, letters, space, line breaks, tabs ...) that are not in the element as a text node. In my HTML document * everything is displayed on the screen: (except for service elements), and when I enter some characters outside the service elements, the browser thinks “Aha, he wants me to display them. Stop, but they are not inside the element, then I will display them as a plain text node (for which the prototype object is the Text prototype object) ”. I used to face a similar situation, but did not understand why. For example, I write code:

     … <body> Привет <div>123</div> … 

    This text “Hello” was just stuffed into such an object as a text node. Therefore, “Hello” was displayed on the screen, although I thought “Why is it output, is it not in the element?”. But remember that text and text are different (this is written in the next JavaScript course). This hello node object has a Text prototype object. If I create an object variable using a javascript using the String () constructor function, then this object has a prototype object (and therefore, the properties and methods are completely different) is completely different - String {...}. That is, the entire markup of the unofficial part of my HTML document, along with the characters between the markup elements themselves (space, tabulation, line breaks ...), is regarded by the browser as what I want to display on the screen , converted into DOM tree node objects (emphasize it is exactly the NODE objects - they are displayed on the screen , and not just the objects of the BOM tree) and are woven into the BOM. I wrote “intertwined”, since intermediate container objects (“knots”) are also created - for example, if I write an element div with id = ”first”, and throw two more div elements into it, then in BOM there are two corresponding div objects will not lie on the first level of nesting, but on the second in relation to the first div object - in the “more ordered” mediation object referenced by the property

     document.getElementById(“first”).childNodes; 

    . Plus, the stylistic part of the content of my HTML document forms another tree — the CSSOM tree, which also “weaves” (embeds) into the BOM. This is a tree of objects that are not displayed on the screen (and help the browser understand how to display DOM node objects that are displayed on the screen). DOM and CSSOM trees are part of a common BOM tree. When I write markup, I format it for readability — I make tabs, I can also add spaces and line breaks between elements . These tabs, spaces, and line breaks are the browser and surpasses DOM text nodes. For example, if I write this code:

     <!DOCTYPE HTML><html><head><style>.qq{width:100px;height:100px;border:1px solid black;}</style></head><body><div class="qq" id="first">1</div><div class="qq" id="second">2</div></body></html> 

    That is, the code without tabs and line breaks , these mysterious text objects-nodes of the DOM tree will disappear (if you take, for example, and see the “more ordered” object-list of objects-nodes-children of the body-node from the code above, then there will be only two div objects, and there will be no more text node objects). But this code is unreadable at all, so you need to either delete them or set them font-size: 0px. Plus, it is also necessary to remember that if my node object is an inline-block, then it obeys the laws of lines — this object is treated as a letter . For example, it appears upper line indent, and to remove it, you need to write line-height: 0px.

    • Try to describe more concisely and without water and caps. - user207618 8:55 pm
    • I'm not a writer. how it happened - it happened. there is no water. explained on the fingers, so that any person would understand, and not just specials. and the specialists have nothing to do here - they already know everything - Dimon
    • one
      I do not care. you do not like what is written - do not read. No one forces you to read this article - there are a lot of others. do not judge others by yourself. if a person does not understand something, he will read any text, if the information is sensible. guidance marafet (so that everything looks like a needle) on the site let those who earn money on it do it. - Dimon
    • 3
      If you don't care how to write an answer, you have nothing to do in this field. You write for readers. - user207618
    • one
      I write so that people understand, and not to admire. if you like - you can bring your answers to the ideal in terms of appearance. I am registered on a heap of forums and I will not waste time in order to bring my question to the perfect look at every forum. if you like to spend time on it - this is your choice. there is no "field" - it's just a forum. came, received information, suggested something himself and left - Dimon