I've got a fixed-width container element that contains several variable-width child elements. I'd like to distribute extra space evenly between those elements. This is easy to do if none of the elements include word-wrapped text. But if the total content is wider than will fit in the container without wrapping, I'm not sure how to distribute space between elements anymore.
Here's a repro showing it working great when there's no wrapping, but not if there's wrapped text: https://codepen.io/justingrant/pen/bGKKJje
If I use flex-basis: auto (the default) then wrapped items have wide padding and non-wrapped items have no padding.
If I use flex-basis: 1px (or any identical width) then items are identical width, so items with wider text have less padding.
Instead, I want to distribute extra space evenly between all items, regardless of whether they're wrapped or not. Is this possible?
Note that what I don't want to do is assign a fixed padding to all items, because that will overflow the container and tie up lots of extra space. I just want to allocate extra space evenly.
.container {
display: flex;
flex-direction: column;
}
.bad {
width: 600px;
}
.good {
width: 800px;
}
ul {
display: flex;
flex-grow: 1;
width: 100%;
gap: 20px;
padding: 20px;
border: 1px solid gray;
list-style: none;
}
li {
flex-grow: 1;
flex-basis: auto;
padding: 10px 0;
border: 1px solid red;
text-align: center;
}
ul.basis li {
flex-basis: 1px;
}
<div class='container good'>
<label>Works as expected (consistent padding) if nothing wraps</label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: auto</code></label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: 1px</code></label>
<ul class="basis">
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
I guess in your .bad cases the problem comes from the restricted width of your flex box. The list items already have the maximum space they can get and so they CAN'T grow any padding.
You can see the problem if you add some padding yourself to the list items.
I'm adding 10px of padding left and right padding: 10px 10px; to your examples <li> elements. This way it WOULD have consistent padding for all lis titems, but the items are growing to large and are sticking outside of the flex box.
.container {
display: flex;
flex-direction: column;
}
.bad {
width: 600px;
}
.good {
width: 800px;
}
ul {
display: flex;
flex-grow: 1;
width: 100%;
gap: 20px;
padding: 20px;
border: 1px solid gray;
list-style: none;
}
li {
flex-grow: 1;
flex-basis: auto;
padding: 10px 10px;
border: 1px solid red;
text-align: center;
}
ul.basis li {
flex-basis: 1px;
}
<div class='container good'>
<label>Works as expected (consistent padding) if nothing wraps</label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: auto</code></label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: 1px</code></label>
<ul class="basis">
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
So I only see two options in order to have equal paddings for all items.
Case one, let the words break anywhere:
You can add the 10px left and right padding as in above example and in addition, also add word-break: break-all; This way the items will not grow larger as they can, have equal paddings BUT it's hard to read because you allow words to break at any position.
.container {
display: flex;
flex-direction: column;
}
.bad {
width: 600px;
}
.good {
width: 800px;
}
ul {
display: flex;
flex-grow: 1;
width: 100%;
gap: 20px;
padding: 20px;
border: 1px solid gray;
list-style: none;
}
li {
flex-grow: 1;
flex-basis: auto;
padding: 10px 10px;
border: 1px solid red;
text-align: center;
word-break: break-all;
}
ul.basis li {
flex-basis: 1px;
}
<div class='container good'>
<label>Works as expected (consistent padding) if nothing wraps</label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: auto</code></label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: 1px</code></label>
<ul class="basis">
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
Or case two, let the flex box wrap it's items:
Again, add the 10px left and right. Also, instead of breaking the text at any random position, you can let the items itself wrap down into the next row with flex-wrap: wrap; on your <ul> element.You probably also want to remove the flex-grow: 1; from your <li> elements, or some items grow to the full flex box width.
.container {
display: flex;
flex-direction: column;
}
.bad {
width: 600px;
}
.good {
width: 800px;
}
ul {
display: flex;
flex-wrap: wrap;
flex-grow: 1;
width: 100%;
gap: 20px;
padding: 20px;
border: 1px solid gray;
list-style: none;
}
li {
flex-basis: auto;
padding: 10px 10px;
border: 1px solid red;
text-align: center;
}
ul.basis li {
flex-basis: 1px;
}
<div class='container good'>
<label>Works as expected (consistent padding) if nothing wraps</label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: auto</code></label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not desired (padding varies) using <code>flex-basis: 1px</code></label>
<ul class="basis">
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
And just to wrap it up again, I wouldn't be aware of any method to not do either of them (breaking words anywhere or wrapping items to next row) AND keep the items inside your flex-box.Of course you could always play around with your gap and other properties as well in order to fit your items into a specific container width. But as soon as you have more items in your container (for example they are not hardcoded but pulled from a database), you will always hit a limit where it's no longer possible to display your items in an uniform way.
Still WAY too much CSS here but you can decide how you wish the list text to wrap internally and which is visually most appealing to you.
First block as you had it, the other two I wrapped in a container to better control the flow IMHO. Basic concept is to actually say what padding you DO want.
.container {
display: flex;
flex-direction: column;
}
.bad {
width: 400px;
}
.good {
width: 800px;
}
.list-container {
width: 100%;
}
.list-container>.list-group {
display: flex;
flex-direction: row;
flex-wrap: wrap;
list-style: none;
align-items: center;
justify-content: space-around;
}
.list-container>.list-group>* {
padding: 0.25rem;
}
ul {
display: flex;
flex-grow: 1;
gap: 1rem;
padding: 20px;
border: 1px solid gray;
list-style: none;
}
li {
flex-grow: 1;
flex-basis: auto;
padding: 10px 0;
border: 1px solid red;
text-align: center;
}
ul.basis li {
flex-basis: 1px;
}
<div class='container good'>
<label>Works as expected (consistent padding) if nothing wraps</label>
<ul>
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
<div class="container bad">
<label>Not wrapped text!</label>
<div class="list-container">
<ul class="list-group">
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
</div>
<div class="container bad">
<label>Wrapped text <code>flex-basis: 1px</code></label>
<div class="list-container">
<ul class="list-group basis">
<li>South Carolina</li>
<li>North Carolina</li>
<li>Virginia</li>
<li>Alaska</li>
<li>District of Columbia</li>
<li>California</li>
<li>Arizona</li>
</ul>
</div>
</div>
Related
I have a sidebar that is set to flex with direction column. I am trying to get my menu ul to be vertically centered, and my .logo-container to be on the top of the page.
Is there any way to get one child to flex-start and another one centered?
Code:
<aside class="side-bar">
<nav class="navigation">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
</nav>
</aside>
CSS:
html {
box-sizing: border-box;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
}
.navigation {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
}
.logoname {
display: inline-block;
}
* {
color: black;
}
ul {
list-style: none;
}
Codepen
Many thanks!
What you can do is to create an empty/invisible element as a third flex item inside the flex parent (in my example below it's the divwith class xxx) and apply justify-content: space-between to the flex parent (instead of center).
Depending on your actual code and content you should make sure that that additional element has the same height as the nav element (30px in your and my example). And again, depending on the situation you might want to add visibility: hidden; to the additional element (xxx) to make it invisible but still have its height included in the flex position calculations:
html {
box-sizing: border-box;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
}
.navigation {
display: flex;
flex-direction: column;
justify-content: space-between;
align-items: center;
height: 100%;
}
.logoname {
display: inline-block;
}
* {
color: black;
}
ul {
list-style: none;
}
.xxx {
height: 30px;
visibility: hidden;
}
<aside class="side-bar">
<nav class="navigation">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
<div class="xxx"></div>
</nav>
</aside>
You can try this approach.
html {
box-sizing: border-box;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
}
.navigation {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100%;
}
* {
color: black;
}
ul {
list-style: none;
}
.logo-container {
display:grid;
justify-content:space-around;
margin:0 auto;
padding-top: 20px;
}
.logo-container img {
text-align:center;
padding:5px;
}
<aside class="side-bar">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<nav class="navigation">
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
</nav>
</aside>
All you have to do is to have the logo and ul in separate divs within the parent div that has the column direction styling, apply flex-shrink:0 to the div containing the logo and flex-grow: 1 to the other div.
That will allow the logo to be at the top and the other div to take the rest of the space - then you can apply flex styling in the navigation -container to center the ul within that div.
UPDATE - the OP wanted the ul centered into the height of the viewport - as noted in the comments this is as simple as offsetting the position of the ul in the bottom div by half the height of the top div - so in this case - moving it up by 20px) because the top div is 40px in height. This allows centering of the ul into the viewport height without resorting to adding empty divs just to get the alignment.
html {
box-sizing: border-box;
color: white;
}
* {
margin: 0;
padding: 0;
}
.side-bar {
width: 35%;
height: 100vh;
background-color: blue;
padding: 8px;
}
.navigation {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
align-items: center;
}
.logo-container {
flex-shrink:0
}
.logoname {
display: inline-block;
padding : 8px;
color: lime;
}
.navigation-container {
flex-grow:1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
ul {
list-style: none;
position: relative;
top: -20px
}
li a{ color: white; }
<aside class="side-bar">
<nav class="navigation">
<div class="logo-container">
<a href="index.html" class="link">
<img src="http://unsplash.it/30/30" class="logoimg" alt="">
<h6 class="logoname">My<span class="lastname">Name</span></h6>
</a>
</div>
<div class="navigation-container">
<ul class="nav-list">
<li class="item">Menuitem1</li>
<li class="item">Menuitem2</li>
<li class="item">Menuitem3</li>
<li class="item">Menuitem4</li>
</ul>
</div>
</nav>
</aside>
I'm confronted with the issue of IE11 that the text doesn't wrap as in other browsers. I've set up a codepen with the issue.
https://codepen.io/johnny_gruber/pen/QmBePd
The HTML
<header>
<div class="logo">
<img src="https://upload.wikimedia.org/wikipedia/commons/c/ce/Coca-Cola_logo.svg" alt="">
</div>
<div class="search">
search
</div>
<div class="language">
DE v
</div>
<nav>
<ul>
<li>Home</li>
<li>Product & Solutions</li>
<li>Industries</li>
<li>Facilities</li>
<li>About Us</li>
<li>Contact Us</li>
<li>Group Website</li>
</ul>
</nav>
<div class="login">
login v
</div>
</header>
The css
header {
display: flex;
flex-direction: row wrap;
max-width: 100%;
}
.logo,
.search,
.language,
.login {
flex-shrink: 0;
}
ul {
display: flex;
float: right;
}
li {
padding: 25px 15px;
}
nav {
flex-grow: 1;
flex-shrink: 1;
}
In chrome you can see the expected behaviour. The text wraps if possible. In IE11 it just stays on one line. Is there a chance to get the same behaviour on IE11?
Thanks!
IE 11 requires a unit to be added to the third argument, the flex-basis property
c/o CanIuse.com
From there , just set the ul to display:flex and the li to say:
li {
padding: 25px 15px;
flex: 1 1 50px;
}
body {
font-family: Arial;
font-size: 16px;
}
header {
display: flex;
flex-direction: row wrap;
max-width: 100%;
}
.logo {
height: 72px;
}
.search,
.language,
.login {
width: 50px;
height: 50px;
padding: 10px;
border: 1px solid silver;
}
.logo,
.search,
.language,
.login {
flex-shrink: 0;
}
ul {
display: flex;
list-style:none;
}
li {
padding: 25px 15px;
flex: 1 1 50px;
}
nav {
flex-grow: 1;
flex-shrink: 1;
}
<header>
<div class="logo">
<img src="https://upload.wikimedia.org/wikipedia/commons/c/ce/Coca-Cola_logo.svg" alt="">
</div>
<div class="search">
search
</div>
<div class="language">
DE v
</div>
<nav>
<ul>
<li>Home</li>
<li>Product & Solutions</li>
<li>Industries</li>
<li>Facilities</li>
<li>About Us</li>
<li>Contact Us</li>
<li>Group Website</li>
</ul>
</nav>
<div class="login">
login v
</div>
</header>
Codepen.io Demo
I am currently trying to create a grid-layout with HTML/CSS only for various of reasons (I know Bootstrap etc. but that's no option in this context & and I can not add markup elements).
I have the following code (container div with each time a title which has an ul with li's):
<div>
<h3>title here</h3>
<ul>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
</ul>
<h3>title 2 here</h3>
<ul>
<li>list-item</li>
<li>list-item</li>
</ul>
<h3>title 3 here</h3>
<ul>
<li>list-item</li>
</ul>
</div>
Now I'd like to be able to create a grid-layout. Meaning e.g. each title is 33% width so I can have 3 next to eachother.
Problem is that there is an ul in between each time. So is there a possible floating solution so I can have a grid layout as result.
TITLE - TITLE - TITLE
ul ul ul
-
h3 {
width: 33%;
float: left;
display: inline-block;
}
ul{
float: left;
width: 33%;
display: inline-block;
}
and all of this without a framework.
Thanks in advance
Here is a sample using the existing markup
Do note they flow from top to bottom, not left to right.
div {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 220px; /* 30px + 80px times 2 row */
}
div > * {
width: 33.33%;
box-sizing: border-box;
}
h3 {
margin: 0;
height: 30px;
}
ul {
margin: 0;
height: 80px;
}
<div>
<h3>title here</h3>
<ul>
<li>list-item</li>
<li>list-item</li>
<li>list-item</li>
</ul>
<h3>title 2 here</h3>
<ul>
<li>list-item</li>
<li>list-item</li>
</ul>
<h3>title 3 here</h3>
<ul>
<li>list-item</li>
</ul>
<h3>title 2 here</h3>
<ul>
<li>list-item</li>
<li>list-item</li>
</ul>
<h3>title 3 here</h3>
<ul>
<li>list-item</li>
</ul>
</div>
Updated based on a comment
There is one possibility to do a left-to-right build, visually, by using the order property
div {
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 220px; /* 30px + 80px times 2 row */
}
div > * {
width: 33.33%;
box-sizing: border-box;
}
h3 {
margin: 0;
height: 30px;
}
ul {
margin: 0;
height: 80px;
}
div :nth-child(3),
div :nth-child(4) {
order: 2;
}
div :nth-child(5),
div :nth-child(6) {
order: 4;
}
div :nth-child(7),
div :nth-child(8) {
order: 1;
}
div :nth-child(9),
div :nth-child(10) {
order: 3;
}
<div>
<h3>title 1 here</h3>
<ul>
<li>1 list-item</li>
<li>1 list-item</li>
<li>1 list-item</li>
</ul>
<h3>title 2 here</h3>
<ul>
<li>2 list-item</li>
<li>2 list-item</li>
</ul>
<h3>title 3 here</h3>
<ul>
<li>3 list-item</li>
</ul>
<h3>title 4 here</h3>
<ul>
<li>4 list-item</li>
<li>4 list-item</li>
</ul>
<h3>title 5 here</h3>
<ul>
<li>5 list-item</li>
</ul>
</div>
You mean something like this?
https://jsfiddle.net/jw22Lqgo/
h3 {
display: inline-block;
}
ul{
display: block;
}
.col {
width: 32%;
border: 1px solid red;
display: inline-block;
}
.container {
width: 960px;
}
I have a navigation bar as shown here: http://i.imgur.com/4rxkS2K.jpg
I am using foundation to build a website, the way I have built the nav bar is as follows:
HTML:
<nav class="top-bar">
<ul>
<li>About</li>
<li id="menu-divider">|</li>
<li>Testimonials</li>
<li><img src="images/logo.png" alt=""></li>
<li>Services</li>
<li id="menu-divider">|</li>
<li>Contact</li>
</ul>
</nav>
CSS:
.top-bar { font-family: 'bebas_neueregular';
height: 150px;
line-height: 100px;
padding: 18px;
width: 100%;
position: relative;
text-align:center;
margin-bottom:10px; }
.top-bar ul { display:inline-block;
margin-left: auto ;
margin-right: auto ;}
.top-bar ul > li { display:inline-block;
margin-left: auto ;
margin-right: auto ;}
#menu-divider { color:#ffffff;
font-size: 24px;}
As you can see in the picture, the way I have built it means that my center li element (my logo picture) is not in exact center as the other li elements are of different widths meaning they are all centered collectively. What I'm after is the logo in the dead center then the other li elements as they are centered around the logo.
Thanks in advance for any help!
You can play around but I'm pretty sure this does the trick:
http://codepen.io/anon/pen/dYXQpz
Use 3 containers (that means you lose your nav as a ul). Flex them and inside of the left and right one, flex the elements (end for the first, start for the other)
<div class="nav-bar">
<div class="sideNav leftNav">
<div class="menu">
MENU 1
</div>
<div class="split"></div>
<div class="menu">
MENU 2
</div>
</div>
<div class="logo">
<img src="https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcSN9qhGx6NftAepiMOjdGXkcW-UxkO9dtQ4VGRlepyzNC2S8xQCcA" />
</div>
<div class="sideNav rightNav">
<div class="menu">
MENU 3
</div>
<div class="split"></div>
<div class="menu">
MENU 4
</div>
</div>
</div>
Then apply the css. It can be improved but it can help you get started.
.nav-bar {
background: pink;
display: flex;
}
.sideNav {
flex: 1 0 auto;
background: red;
display: flex;
}
.leftNav {
justify-content: flex-end;
}
.rightNav {
justify-content: flex-start;
}
.sideNav > div {
margin: 100px 20px 0 20px;
}
.split{width: 2px;background: white;height: 16px}
Hope that helps. I loves flexbox.
The picture below shows what I would like to get.
It is a menu within a container, where the menu may wrap to multiple lines when the window/screen gets too narrow for all menu items to fit in. At the same time I would like the menu to have a background which expands to full screen in width, while expanding in height with the menu when it gets wrapped to multiple lines. Currently I think this is not possible with CSS, but I am also just a CSS amateur. My current solution involves #media queries to set the height of the menu background for resolutions where wrapping appears. This does not take into account that font-size could change, thus making each line of menu higher.
Here is a jsFiddle with a basic setup, which does NOT what I want:
https://jsfiddle.net/n3jmyq2f/3/ (Edited, was not the final version)
Here is the code:
<div class="container">
<div class="menu_wrap">
<div class="menu_bg"></div>
<div class="menu">
<ul>
<li>item 1</li>
<li>item2</li>
<li>item3</li>
<li>item4</li>
<li>item5</li>
<li>item6</li>
</ul>
</div>
</div>
<div class="content">It's me, Mario!</div>
CSS:
.container {
width:50%;
margin: 0 auto;
background:lightgreen;
height:300px;
}
.menu_bg{
position: absolute;
background: #afafaf;
width: 100%;
left:0;
height:30px;
z-index: -1;
}
ul {
height:30px;
background: #afafaf;
}
li {
display:inline-block;
}
The first option is the simplest.
Stop thinking of the .container as something that must contain everything. It's just a class that can be reused as and when required.
If you take the menu div out of the "container" but put a .container div inside you get the effect you are looking for.
JSfiddle Demo
*,
body {
padding: 0;
margin: 0;
}
.container {
width: 50%;
margin: 0 auto;
background: lightgreen;
}
.menu {
background: #afafaf;
}
ul {
border: 1px solid green;
}
li {
display: inline-block;
}
.content {
height: 300px;
}
<div class="menu">
<div class="container">
<ul>
<li>item 1
</li>
<li>item2
</li>
<li>item3
</li>
<li>item4
</li>
<li>item5
</li>
<li>item6
</li>
</ul>
</div>
</div>
<div class="container">
<div class="content">It's me, Mario!</div>
</div>
2nd Option
Use a pseudo-element
*,
body {
margin: 0;
padding: 0;
}
.container {
width: 50%;
margin: 0 auto;
background: lightgreen;
height: 300px;
}
ul {
background: #afafaf;
position: relative;
border: 1px solid green;
}
ul:before {
content: '';
position: absolute;
height: 100%;
background: inherit;
width: 100vw;
left: 50%;
transform: translateX(-50%);
z-index: -1
}
li {
display: inline-block;
}
<div class="container">
<div class="menu">
<ul>
<li>item 1
</li>
<li>item2
</li>
<li>item3
</li>
<li>item4
</li>
<li>item5
</li>
<li>item6
</li>
</ul>
</div>
<div class="content">It's me, Mario!</div>
</div>
JSfiddle Demo
if in .container you change
width:50%;
to
width:100%;
it will do it
fiddle
you could also use the .menu-wrap class (which I've seen in your markup) to do this