Position Absolute does not occupy 100% of the father

Asked

Viewed 149 times

1

Why does a div with Absolute position not occupy 100% of the father’s width if it fits, why breaks the line before? See example below:

.segura{
  width: 200px;
  height: 200px;
  display: block;
  position: relative;
  background-color: #eeeeee;
}
.caixa{
  width: auto;
  height: auto;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #000000;
  color: #ffffff;
}
<div class="segura">
   <div class="caixa">
       dfdf fsdfsd dsf d
   </div>
</div>

What was right was the black box without this line break, what to do to break the line only if you reach the width limit?

  • I don’t get it, you want the black square to be this big, but don’t break the line?

  • No, the html broke the line if I ask, even fitting the whole text in a single line. This break was automatic. And I didn’t want this break (only if I have to) @Leandroangelo

1 answer

3


One option to resolve this is alignment with flex, so you align the child element with margin:auto and not with top left, below I will explain the problem in more detail.

.segura{
  width: 200px;
  height: 200px;
  position: relative;
  background-color: #eeeeee;
  display: flex;
}
.caixa{
  margin: auto;
  width: auto;
  height: auto;
  background-color: #000000;
  color: #ffffff;
}
 <div class="segura">
   <div class="caixa">
       dfdf fsdfsd dsf d
   </div>
</div>


Now the explanation of your problem.

What happens is when you wear transform:translate you this adjusts the child element on its axis and not on the parent’s axis! Keeping this in mind realize that when you play the .caixa (which is wider than 50% of the father’s width), 50% left he breaks the line!

To better understand see in this Gif that when moving the .caixa in the left it breaks or not, already moving the axle X of transform:translate nothing happens, because these measures refer to the axis itself!

inserir a descrição da imagem aqui

In fact the .caixa is breaking the line on the right bank of the parent. After that you shift the element "virtually" on the axis itself, but the line remains broken by hitting the side of the parent.

inserir a descrição da imagem aqui


TIP: If your text is just a line you can put in white-space: nowrap; But it only works if the text has only one line!

See how it looks:

.segura{
  width: 200px;
  height: 200px;
  display: block;
  position: relative;
  background-color: #eeeeee;
}
.caixa{
  width: auto;
  height: auto;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #000000;
  color: #ffffff;
  white-space: nowrap;
}
<div class="segura">
   <div class="caixa">
       dfdf fsdfsd dsf d
   </div>
</div>

  • Understood, but this does not help me, because the . safe is 100% wide in my case (responsive), would have to think another way to solve....

  • @caiocafardo Cara vc can decrease the padding... or even the font-size! What happens when you throw the . box to the center of the . holds the word "dsf" does not fit, it breaks the line. See the code I changed now in editing

  • In fact it fits yes, I did a test, it is breaking for what I realized by the way I made it align to the center: Transform: Translate(-50%, -50%); But I can not change the measures (padding source) on my site, because it is responsive, may not break with the source that put in one resolution and another break, would have to find a solution even.

  • @caiocafardo I’m thinking of an alternative here...

  • Thanks, I even took the padding in my example not to confuse. There are other ways to align to the center, but then div Absolute gets 100% width, which can’t happen either.

  • @Caiocafardo Can be using flex?

  • Sure! Working can be any way!

  • white-space: nowrap; also not da, because the name comes from database, can have 5 characters or 300...

  • @caiocafardo da uma olhada no primeiro exemplo com flex que just editar. I’ll do a better explanation tb de pq o texto quebra acho que descobri!

  • 1

    Perfect with flex!

  • @caiocafardo now yes the answer is complete! If you are interested after a look at the explanation of the broken line

Show 6 more comments

Browser other questions tagged

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