Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the max-width of a container with flexbox?

Tags:

html

css

flexbox

I would like to have services-bar__title and services-bar__services to have a max-width of 1024px so that I get the normal flexbox behaviour for the container as formatted below, but the list doesn't expand further than 1024px if the viewport is wider. I tried setting the container widths and max-widths, but it doesn't seem to work as expected as the container doesn't stay centered anymore, any ideas?

.services-bar {
  display: flex;
  flex-direction: column;
  height: 20rem;
  width: 100%;
  box-shadow: 0 -3rem 8rem rgba(#000, 0.5);
}

.services-bar__title {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.services-bar__services {
  display: flex;
  flex-direction: row;
  justify-content: center;
}
<div class="services-bar">
  <div class="services-bar__title">
    test title
  </div>
  <div class="services-bar__services">
    <div class="services-bar__services_1"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
    <div class="services-bar__services_2"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
    <div class="services-bar__services_3"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
    <div class="services-bar__services_4"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
  </div>
</div>
like image 393
user1088793 Avatar asked Jun 22 '18 09:06

user1088793


People also ask

How do you set the max width on a Flex container?

Change the width of the flex-container to 810px and the flex-basis of each flex-item to 300px . As you can notice, the flex-basis property refers to an ideal size of the flex-items, only if there is enough space available in the flex-container.

How do I change the width on my flexbox?

A flexbox item can be set to a fixed width by setting 3 CSS properties — flex-basis, flex-grow & flex-shrink. flex-basis : This property specifies the initial length of the flex item. flex-grow : This property specifies how much the flex item will grow relative to the rest of the flex items.

Does width work with Flex?

Key DifferencesFlex containers (that aren't also flex items) will ignore flex-basis but can use width and height . flex-basis works only on the main axis. For example, if you're in flex-direction: column , the width property would be needed for sizing flex items horizontally.

How can you cause a flex item to grow in size?

flex: 1 0 200px; If you have one element that has a flex-basis of 200px, flex-grow of 1, and flex-shrink of 0, this element will be at minimum 200px wide, but it will be allowed to grow if there is extra space. In this case, you can think of the flex-basis as being a minimum width.


Video Answer


2 Answers

To do that, you don't need to make your services-bar a flex, rather only keep services-bar__title and services-bar__services as flex.

Use justify-content: space-around; in the services-bar__services to distribute the items throughout the container. And for the services-bar, set max-width: 1024px; as well as margin: 0 auto; so that it remains in center and don't go beyond 1024px.

Try this out:

body {
  margin: 0;
}

.total-width {
  width: 100%;
  height: 40px;
  background-color: blue;
}

.width-1024px {
  max-width: 1024px;
  margin: 0 auto;
  height: 40px;
  background-color: yellow;
}

.services-bar {
  height: 20rem;
  box-shadow: 0 -3rem 8rem rgba(#000, 0.5);
  max-width: 1024px;
  margin: 0 auto;
}

.services-bar__title {
  display: flex;
  flex-direction: row;
  justify-content: center;
}

.services-bar__services ul {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  padding: 0;
}
<div class="total-width"></div>
<div class="width-1024px"></div>

<div class="services-bar">
  <div class="services-bar__title">
    test title
  </div>
  <div class="services-bar__services">
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
  </div>
  <div class="services-bar__services">
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
  </div>
  <div class="services-bar__services">
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
  </div>
  <div class="services-bar__services">
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
  </div>
</div>

And here's the jsfiddle: http://jsfiddle.net/yostrh8n/25/

like image 154
UkFLSUI Avatar answered Oct 09 '22 01:10

UkFLSUI


If I understood you correctly, you want to expand the service to 100% up to 1024px. Right?

To do so, u need to set the default width to 100%, and set the max-width to 1024px.

.services-bar {
  display: flex;
  flex-direction: column;
  width: 100%;
  box-shadow: 0 -3rem 8rem rgba(0, 0, 0, 0.5);
  max-width: 1024px;
  
  .services-bar__title {
    display: flex;
    flex-direction: row;
    justify-content: center;
  }
  .services-bar__services {
    display: flex;
    flex-direction: row;
    justify-content: center;
  }
  .services-bar__services_1,
  .services-bar__services_2,
  .services-bar__services_3,
  .services-bar__services_4 {}
<div class="services-bar">
  <div class="services-bar__title">
    test title
  </div>
  <div class="services-bar__services">
    <div class="services-bar__services_1"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
    <div class="services-bar__services_2"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
    <div class="services-bar__services_3"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>
    <div class="services-bar__services_4"></div>
    <ul>
      <li>test</li>
      <li>test</li>
      <li>test</li>
    </ul>

  </div>
</div>
like image 45
Eliya Cohen Avatar answered Oct 08 '22 23:10

Eliya Cohen