Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Div overlay other divs in a flex container [duplicate]

Tags:

html

css

flexbox

There is a container in which there is a flex container.

HTML

<div class="container">
  <div class="content">
    <div class="over">Over</div>
    <div>Test</div>
    <div>Test</div>
    <div>Test</div>
  </div>
</div>

CSS

.container {
  background-color: yellow;
  width: 500px;
}
.content {
  display: flex;
}
.over {
  background-color: rgba(255, 0, 0, 0.8);
}

Now it looks like this:

enter image description here

Can the over div somehow be placed over all other divs, and the width of the content should be?

enter image description here

like image 626
Harrix Avatar asked May 05 '18 14:05

Harrix


People also ask

How do I overlay a div over another div?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

How do I bring a div in front of another div?

Use the CSS z-index property. Elements with a greater z-index value are positioned in front of elements with smaller z-index values. Note that for this to work, you also need to set a position style ( position:absolute , position:relative , or position:fixed ) on both/all of the elements you want to order.

Can an element be both a flex item and a flex container?

Flex items within a flex container can be laid out either horizontally or vertically, but not both. If you want to lay out items in both dimensions, you'll need to nest a flex container inside another one. In this example we apply display: flex to both the outer container and to the red flex item.


1 Answers

You can make the element position:absolute and stretch it to take the full width of .content. You may also use inline-flex so that the width of .content is equal to the width of its content:

.container {
  background-color: yellow;
  width: 500px;
}

.content {
  display: inline-flex;
  position:relative;
}

.over {
  position:absolute;
  top:0;
  left:0;
  right:0;
  bottom:0;
  background-color: rgba(255, 0, 0, 0.8);
}
<div class="container">
  <div class="content">
    <div class="over">Over</div>
    <div>Test</div>
    <div>Test</div>
    <div>Test</div>
  </div>
</div>
like image 177
Temani Afif Avatar answered Sep 30 '22 18:09

Temani Afif