Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a flex item act like display block [duplicate]

Tags:

html

css

flexbox

So I have a container which uses display flex, then 4 divs inside which the first one needs to act like it is used display block and the 3 others need to be as they are.

Code snippet demonstration :

#container {
  display: flex;
}

#menu {
  display: flex;
}

#menu p {
  margin: 0;
  padding: 8px;
  padding-bottom: 0;
}

.otherDivs {
  height: 700px;
  width: 10%;
  background-color: grey;
  margin-right: 5px;
}
<div id="container">

  <div id="menu">

    <p>Btn</p>
    <p>Btn</p>
    <p>Btn</p>

  </div>

  <div class="otherDivs"></div>
  <div class="otherDivs"></div>
  <div class="otherDivs"></div>

</div>

How do I get the menu to display above those other divs?

I know I could just put the menu div out of the container but I need to keep it within the container where it is because I am using it with Jquery tabs.

like image 991
Erdss4 Avatar asked Dec 15 '17 22:12

Erdss4


People also ask

Can you display block in Flex?

Block: Displays an element as a block element. It starts on a new line and takes up take up as much horizontal space as they can. Block-level elements do not appear in the same line, but breaks the existing line and appear in the next line. Flex: Flex displays an element as a flexible structure.

How do you turn an element into a flex item?

To use the Flexbox model, you must make a parent element a flex container (AKA flexible container). You do this by setting display: flex or display: inline-flex for the inline variation. It's that simple, and from there you're all set to use the Flexbox model.

How do you overlap 2 Flex items?

Flexbox is not designed for z-axis alignment (overlapping). Any overlapping would have to come from negative margins, absolute positioning, CSS Grid Layout, JavaScript or something else. The z-index property may also need to play a role. Cards are made to overlap using line-based placement.


2 Answers

You should add a flex-basis: 100% to #menu, and allow the gray items to go to a new line by applying flex-wrap: wrap; to #container:

#container {
  display: flex;
  flex-wrap: wrap;
}

#menu {
  display: flex;
  flex-basis: 100%;
}

#menu p {
  margin: 0;
  padding: 8px;
  padding-bottom: 0;
}

.otherDivs {
  height: 700px;
  width: 10%;
  background-color: grey;
  margin-right: 5px;
}
<div id="container">

  <div id="menu">

    <p>Btn</p>
    <p>Btn</p>
    <p>Btn</p>

  </div>

  <div class="otherDivs"></div>
  <div class="otherDivs"></div>
  <div class="otherDivs"></div>

</div>
like image 155
Itay Ganor Avatar answered Sep 21 '22 08:09

Itay Ganor


If the goal is to make a flex item occupy an entire row, then set it to flex-basis: 100%, and enable wrap on the container. This causes the full width item to force subsequent items to the next line.

#container {
  display: flex;
  flex-wrap: wrap;   /* NEW */
}

#menu {
  flex: 0 0 100%;    /* NEW */
  display: flex;
}

#menu p {
  margin: 0;
  padding: 8px;
  padding-bottom: 0;
}

.otherDivs {
  height: 700px;
  width: 10%;
  background-color: grey;
  margin-right: 5px;
}
<div id="container">
  <div id="menu">
    <p>Btn</p>
    <p>Btn</p>
    <p>Btn</p>
  </div>
  <div class="otherDivs"></div>
  <div class="otherDivs"></div>
  <div class="otherDivs"></div>
</div>
like image 25
Michael Benjamin Avatar answered Sep 18 '22 08:09

Michael Benjamin