Home > Blockchain >  justify-content property behavior
justify-content property behavior

Time:12-28

I'm practicing flexbox and I need help to understand why nav ul elements are displayed as flex with justify-content: space-between properties are not stretching and remain stuck to each other.

I expect the ul items to be gradually distributed within header-nav__ul container, which I allowed to be stretched through flex: 1 1 auto. Still, I'm not able to achieve the result.

/*Обнуление*/

* {
  padding: 0;
  margin: 0;
  border: 0;
}

*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

:focus,
:active {
  outline: none;
}

a:focus,
a:active {
  outline: none;
}

nav,
footer,
header,
aside {
  display: block;
}

html,
body {
  height: 100%;
  width: 100%;
  font-size: 100%;
  line-height: 1;
  font-size: 14px;
  -ms-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

input,
button,
textarea {
  font-family: inherit;
}

input::-ms-clear {
  display: none;
}

button {
  cursor: pointer;
}

button::-moz-focus-inner {
  padding: 0;
  border: 0;
}

a,
a:visited {
  text-decoration: none;
}

a:hover {
  text-decoration: none;
}

ul li {
  list-style: none;
}

img {
  vertical-align: top;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-size: inherit;
  font-weight: inherit;
}


/*--------------------*/

body {}

.header__container {
  max-width: 1440px;
  margin: 0px auto;
}

.header__row {
  display: flex;
  background-color: #EDF2EC;
  justify-content: space-around;
}

.icon__label {
  display: flex;
  margin: 60px 0 0 0;
}

.header-nav {
  display: flex;
}

.header-nav__ul {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
  font-family: 'Inika', serif;
  font-size: 16;
  color: #010201;
  list-style: none;
  background-color: #499A18;
  margin: 60px 0 0 0;
}

.label {
  font-family: 'Inika', sans-serif;
  font-size: 16px;
}

.wrapper {
  min-height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.navigation {}

.container {
  max-width: 1440px;
  margin: 0px auto;
}

.content {}

.section {}

.section__row {
  display: flex;
  background-color: #EDF2EC;
}

.section1__img {
  flex: 0 0 448.8px;
  padding: 110px 0px 0px 100px;
}

.section1__item {
  display: flex;
  flex-direction: column;
  /*    padding: 0px 0px 20px 0px; */
}

.section1__title {
  font-size: 45px;
  line-height: 45px;
  font-weight: bold;
  color: #717171;
  font-family: 'Inter', sans-serif;
  margin: 170px 40px 45px 220px;
}

.section1__text {
  font-size: 20px;
  line-height: 35px;
  color: #717171;
  font-family: Inter, sans-serif;
  flex: 1 1 auto;
  margin: 0px 40px 0px 220px;
}

span {
  color: #499A18;
}

.section1__btn {
  border: 2px solid #E06733;
  border-radius: 10px;
  color: #000000;
  font-size: 16px;
  line-height: 20px;
  text-transform: uppercase;
  text-align: center;
  display: block;
  padding: 10px 0px;
  margin: 0px 400px 40px 220px;
  width: 158px;
}
<div >
  <header >
    <div >
      <div >
        <div >
          <div >Icon</div>
          <div >Plants</div>
        </div>
        <nav >
          <ul >
            <li>Home</li>
            <li>About us</li>
            <li>Service</li>
            <li>Price</li>
            <li>Contacts</li>
          </ul>
        </nav>
      </div>
    </div>
  </header>

  <div >
    <div >
      <div >
        <div >
          <div ><img src="img/leafs.png"></div>
          <div >
            <div >
              <H1>We grow <span>plants</span> and give <br> you oxygen</H1>
            </div>
            <div >Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the <br> industry's standard dummy text ever since the 1500s.</div>
            <div >Learn more</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CodePudding user response:

The problem is that the nav has no width.
Imagine you have a container with width:500px and it has 2 children with width:250px, you can space the children around, but they have no space to move, that's why it appears not working but it is.
So try giving the header-nav a flex-grow:1; to make it fill the remaining space of its container, and the header-nav__ul a width:100%; to give it the full width of its container:

.header-nav{
    /* ... the old code */
    flex-grow:1;
}
.header-nav__ul {
    /* ... the old code */
    width:100%;
}

CodePudding user response:

Don't get me wrong. The parent element, use are using display: block. Use flex first. The other thing is the margin:0 auto which is taking the entire space left and right. So, remove it.

Look at this code.

Let me know if it helps.

* {
  padding: 0;
  margin: 0;
  border: 0;
}

*,
*:before,
*:after {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

:focus,
:active {
  outline: none;
}

a:focus,
a:active {
  outline: none;
}

nav,
footer,
header,
aside {
  display: flex;
  justify-content: space-around;
}

html,
body {
  height: 100%;
  width: 100%;
  font-size: 100%;
  line-height: 1;
  font-size: 14px;
  -ms-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}

input,
button,
textarea {
  font-family: inherit;
}

input::-ms-clear {
  display: none;
}

button {
  cursor: pointer;
}

button::-moz-focus-inner {
  padding: 0;
  border: 0;
}

a,
a:visited {
  text-decoration: none;
}

a:hover {
  text-decoration: none;
}

ul li {
  list-style: none;
}

img {
  vertical-align: top;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-size: inherit;
  font-weight: inherit;
}


/*--------------------*/

body {}

.header {
  display: flex;
  flex-direction: row;
}

.header__container {
  display: flex;
  gap: 1rem;
}

.header1-nav {
  display: flex;
  gap: 1rem;
}

.header1-nav__ul {
  display: flex;
  gap: 1rem;
}





.header__container {
  /* max-width: 1440px; */
  /* margin: auto; */
}

.header__row {
  display: flex;
  background-color: #EDF2EC;
  justify-content: space-around;
}

.icon__label {
  display: flex;
  margin: 60px 0 0 0;
}

.header-nav {
  display: flex;
}

.header-nav__ul {
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;

  font-family: 'Inika', serif;
  font-size: 16;
  color: #010201;
  list-style: none;
  background-color: #499A18;
  margin: 60px 0 0 0;
}

.label {
  font-family: 'Inika', sans-serif;
  font-size: 16px;
}

.wrapper {
  min-height: 100%;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.navigation {}

.container {
  max-width: 1440px;
  margin: 0px auto;
}

.content {}

.section {}

.section__row {
  display: flex;
  background-color: #EDF2EC;
}

.section1__img {
  flex: 0 0 448.8px;
  padding: 110px 0px 0px 100px;
}

.section1__item {
  display: flex;
  flex-direction: column;
  /*    padding: 0px 0px 20px 0px; */
}

.section1__title {
  font-size: 45px;
  line-height: 45px;
  font-weight: bold;
  color: #717171;
  font-family: 'Inter', sans-serif;
  margin: 170px 40px 45px 220px;
}

.section1__text {
  font-size: 20px;
  line-height: 35px;
  color: #717171;
  font-family: Inter, sans-serif;
  flex: 1 1 auto;
  margin: 0px 40px 0px 220px;
}

span {
  color: #499A18;
}

.section1__btn {
  border: 2px solid #E06733;
  border-radius: 10px;
  color: #000000;
  font-size: 16px;
  line-height: 20px;
  text-transform: uppercase;
  text-align: center;
  display: block;
  padding: 10px 0px;
  margin: 0px 400px 40px 220px;
  width: 158px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div >
  <header >
    <div >
      <div >Icon</div>
      <div >Plants</div>
    </div>
    <nav >
      <ul >
        <li>Home</li>
        <li>About us</li>
        <li>Service</li>
        <li>Price</li>
        <li>Contacts</li>
      </ul>
    </nav>
  </header>
  

  <div >
    <div >
      <div >
        <div >
          <div ><img src="img/leafs.png"></div>
          <div >
            <div >
              <H1>We grow <span>plants</span> and give <br> you oxygen</H1>
            </div>
            <div >Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the <br> industry's standard dummy text ever since the 1500s.</div>
            <div >Learn more</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

  • Related