Background-color does not track content

Asked

Viewed 369 times

1

Well, I have the code below and I have a question:

inserir a descrição da imagem aqui

Code

.um {
  background-color:#000;
  width:600px;
  height:280px;
  margin:0 auto; /*centralizando horizontalmente esta div*/
}

.dois {		
  background-color:#CCC;
  width:300px;
  height:240px;	
  margin:0 auto; /*centralizando horizontalmente esta div*/
  line-height:240px;
  vertical-align:middle;
}
<div class="um">

  <div class="dois">

    <div style="border:red 1px solid;">Teste 1!</div>
    <div style="border:red 1px solid;">Teste 2!</div>

  </div>

</div>

As div's daughters of div.dois are having the same height as line-height of div.dois and I can’t center vertically div.dois in the center of div.um.

  • 1

    I don’t quite understand what you want to happen! I could explain better.

2 answers

2

I’ll explain the first problems, first vertical-align is used for siblings element and which are inline or inline-* (text type), div by default are block which is different from "text type".

In other words, you applied vertical-align expecting to affect block objects and that are father and son and not siblings, one way to use vertical-align would be this (this is not the solution is just to explain the vertical-align):

.irmao-1 {
    background: #fc0;
    height: 160px;
}

.irmao-2 {
    background: #ccc;
    height: 240px;
}

.irmao-1, .irmao-2 {
    display: inline-block;
    vertical-align: middle;
}
<div class="irmao-1">
  Irmão 1
</div>
<div class="irmao-2">
  Irmão 2
</div>

Another thing that seems to be confusing is the term background, the background only affects the element itself, each element will have its own background and the background property will never leak the element.

One thing, you don’t need to "fix" the height of all elements, you could use padding for example:

.pai {
   background-color: #000;
   padding-top: 20px;
   padding-bottom: 20px;
}

.filho {
   background-color: #ccc;
   width:300px;
   height: 240px;
}
<div class="pai">
    <div class="filho">
       oi
    </div>
</div>

The sum of height: 240px with the padding-top: 20px and padding-bottom: 20px will make the parent element have 280px

An example with two Ivs:

.pai {
   background-color: #000;
   padding-top: 20px;
   padding-bottom: 20px;
}

.filho-1 {
   margin: 0 auto; /*centraliza horizontal*/
   background-color: #ccc;
   width:300px;
   height: 240px;
}

.filho-2 {
   margin: 0 auto; /*centraliza horizontal*/
   background-color: #fc0;
   width:300px;
   height: 240px;
}
<div class="pai">
    <div class="filho-1">
       oi
    </div>
    <div class="filho-2">
       oi
    </div>
</div>

  • Thank you for the answer. Perhaps I have not been quite objective in the question. I have the div’s father and son. The father div is 280px high and the son div is 240px high. The child div has a background-color and the goal is for the child div to have its content centered vertically and horizontally. But that the child div (all, including its background) is aligned in relation to the parent horizontally and vertically. I will reread your answer more carefully but if I may add to the answer an example of this kind I would be grateful

  • @Carlosrocha my answer gives a brief solution on this using padding, since the height is fixed, see that the padding-top and padding-bottom are 20px in the parent element, if the child has 240px then the parent will total 280px, please test.

  • @Carlosrocha please read the part that I wrote this in the answer: One thing, you don’t need to "fix" the height of all elements, you could use padding for example and see the example, I edited to be clearer.

  • I changed the question based on what I learned from what you wrote. Now look what’s really wrong! I’m going to reread the detail of the heights

  • @Carlosrocha the second example (where it is written One thing, you don’t have to "fix") perfectly solves the problem of putting two Divs in the parent element. If you fix the height you will have problems. Test the code, hope it works if you don’t warn me =)

  • 1

    1+ great explanation and organization congratulations on the response

  • @Victorgomes thank you!

  • 1

    Yes, now yes. The problem was mai with postion than proper with valign. Thank you

Show 3 more comments

1


Your "background" of div father does not accompany the div's daughters by the fact that you have a fixed height on her, in case the height:240px; take it and accompany.

And to align horizontally the child elements just put in them a margin: 0 auto;!

#pai {
  background-color:#000;
  width:600px;
  line-height:280px;
  text-align:center;      
}

.filho {
  margin: 0 auto;
  background-color:#CCC;
  width:300px;
  height:240px;   
}
<div id="pai">

  <div class="filho">

    teste 2!

  </div>

  <div class="filho">

    teste 3!

  </div>

</div>

Now if you really have to fixar a height in the parent element background accompany the children.

  • In the last example, how to get the two children to stand next to each other using float: left and float:right and the parent background follow?

  • @Carlosrocha Is this what you want to do? Look here

  • Yes. Now a piece of advice: I managed to arrange another way. I added overflow: auto to (in my case) div 'um'. And the background followed nicely. Which way is more correct? What here demonstrated or using overflow? Remembering that in my case I have a div still above the div one that encompasses it. (and has only her inside)

  • @Carlosrocha With the overflow: auto; the internal content of div one will stay in its proper place, but if you exceed the dimensions will create a scrollbar to view the rest of the content. It would not affirm a correct form, it depends on what its purpose!

  • I got it. I did this test and it actually gave me the scrollbar. But you determined in div.um line-height:200px; But it is not possible to determine its height because the content of the children is dynamic, that is, return of query to the bank can be any time, That way, what the output?

  • @Carlosrocha Can add another element inside the div.um for example a tag p and use a vertical-align so no matter the size of the text that will be inserted there. Take a look at my Jsfiddle that I updated, if that’s the way you want it?

  • So my need is this one: https://jsfiddle.net/rght8n6z/4/

  • gotten: missed after loop add a <div style='clear:Both;'></div>

  • @Carlosrocha OK!

  • thanks. I need to study more css

  • @Carlosrock of nothing, we are here for this!

  • @Carlosrocha and Igor It is not a negative criticism, it is just something that I disagree, in fact what you answered already contained in my reply, about the height being fixed, the AP only chose his answer because in fact the code he had a problem was totally different from what he posted in the question (he was using float), He chose his answer more for the welfare than for what he was asking, if he edit now the question will invalidate everything. In other words, it was almost a duplicate response (however it is allowed). However, I really disagree with how it followed.

  • Guilherme Nascimento I accept your criticism, if it is because the solution (correct answer comes out of yours to come to me) feel the will @Carlosrocha to reverse again, I am not here to "dispute votes" but to help, disseminate and learn more and more knowledge!

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.