What I want to do is align .prod (products) all next to each other as if they were actually in the .subcontainer.
I can't use position since the 8th prod will not sit next to the 9th prod and so on. I could write some JS to do this however I'm trying to use pure css/sass. I have a feeling it's not possible, therefore stackoverflow amaze me with your work arounds.
Codepen: https://codepen.io/shaggywolfhound/pen/vYpMaPx
HTML:
<div class="container">
<div class="subcontainer">
<ol class="prod-cont">
<li class="prod"></li>
<li class="prod"></li>
<li class="prod"></li>
<li class="prod"></li>
<li class="prod"></li>
<li class="prod"></li>
<li class="prod"></li>
</ol>
<ol class="prod-cont">
<li class="prod"></li>
<li class="prod"></li>
.....
SASS:
.container {
width: 200px;
height: 2000px;
.subcontainer {
border: 1px solid red;
.prod-cont {
display: flex;
flex-wrap: wrap;
.prod {
margin: 2px;
display: inline-block;
border: 1px solid green;
box-sizing: border-box;
width: 45px;
height: 100px;
}
}
}
}
just add display: flex on .subcontainer and padding-left: 0 on .prod-cont.
Your code should look like this:
.container {
width: 200px;
height: 2000px;
.subcontainer {
display: flex;
border: 1px solid red;
.prod-cont {
display: flex;
flex-wrap: wrap;
padding-left: 0;
.prod {
margin: 2px;
display: inline-block;
border: 1px solid green;
box-sizing: border-box;
width: 45px;
height: 100px;
}
}
}
}
Related
I'm trying to create a mobile layout with a logo on the left nav in the middle and a chevron to display and hide the items of a navbar(I'm not interested in doing this functionality right now).
But right now I'm trying to achieve something like this:
+------+---------+---------+
| logo | lorem | chevron |
+------+---------+---------+
| ipsum |
+---------+
| dolorem |
+---------+
I've been trying to align the first item of the ul with the icons.
Any ideas on how can I achieve this while still maintaining the html syntax?
* {
box-sizing: border-box;
}
body {
font-family: "IBM Plex Mono", monospace;
font-size: 18px;
}
.main-nav {
font-size: 24px;
display: flex;
justify-content: space-around;
align-items: center;
border: 1px solid red;
height: 50px;
}
.main-nav__list {
display: flex;
flex: 1;
padding: 0;
list-style-type: none;
border: 1px solid greenyellow;
flex-direction: column;
align-self: flex-start;
position: relative;
}
.main-nav__icon {
height: 24px;
width: auto;
margin: auto;
}
.main-nav__links {
text-decoration: none;
color: white;
}
.main-nav__list-item {
width: 100%;
text-align: center;
}
/*.main-nav__list-item:not(:first-child){
display: none;
}*/
.main-nav__list-item--blue {
background-color: #1c77c3;
}
.main-nav__list-item--green {
background-color: #00aa00;
}
.main-nav__list-item--orange {
background: #dd2e02;
}
.main-nav__logo {
flex: initial;
width: 50px;
background-color: black;
display: flex;
height: 100%;
color: white;
}
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<header class="">
<nav class="main-nav">
<a class="main-nav__logo" href="#"><i class="far fa-user main-nav__icon" alt="logo"></i></a>
<ul class="main-nav__list">
<li class="main-nav__list-item main-nav__list-item--blue"><a class="main-nav__links" href="#">Lorem</a></li>
<li class="main-nav__list-item main-nav__list-item--green"><a class="main-nav__links" href="#">Ipsum</a></li>
<li class="main-nav__list-item main-nav__list-item--orange"><a class="main-nav__links" href="#">dolorem</a></li>
</ul>
<a class="main-nav__logo" href="#"><i class="fas fa-chevron-down main-nav__icon" alt="chevron down"></i></a>
</nav>
</header>
To your .main-nav__list, add:
/* Add margin: 0; and height: 300%; */
margin: 0;
height: 300%;
To your .main-nav__list-item. add:
/* Add flex-grow: 1; and center items */
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
* {
box-sizing: border-box;
}
body {
font-family: "IBM Plex Mono", monospace;
font-size: 18px;
}
.main-nav {
font-size: 24px;
display: flex;
justify-content: space-around;
align-items: center;
border: 1px solid red;
height: 50px;
}
.main-nav__list {
display: flex;
flex: 1;
padding: 0;
list-style-type: none;
border: 1px solid greenyellow;
flex-direction: column;
align-self: flex-start;
position: relative;
/* Add margin: 0; and height: 300%; */
margin: 0;
height: 300%;
}
.main-nav__icon {
height: 24px;
width: auto;
margin: auto;
}
.main-nav__links {
text-decoration: none;
color: white;
}
.main-nav__list-item {
width: 100%;
text-align: center;
/* Add flex-grow: 1; and center items */
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
}
/*.main-nav__list-item:not(:first-child){
display: none;
}*/
.main-nav__list-item--blue {
background-color: #1c77c3;
}
.main-nav__list-item--green {
background-color: #00aa00;
}
.main-nav__list-item--orange {
background: #dd2e02;
}
.main-nav__logo {
flex: initial;
width: 50px;
background-color: black;
display: flex;
height: 100%;
color: white;
}
<script src="https://use.fontawesome.com/releases/v5.7.2/js/all.js"></script>
<header class="">
<nav class="main-nav">
<a class="main-nav__logo" href="#"><i class="far fa-user main-nav__icon" alt="logo"></i></a>
<ul class="main-nav__list">
<li class="main-nav__list-item main-nav__list-item--blue"><a class="main-nav__links" href="#">Lorem</a></li>
<li class="main-nav__list-item main-nav__list-item--green"><a class="main-nav__links" href="#">Ipsum</a></li>
<li class="main-nav__list-item main-nav__list-item--orange"><a class="main-nav__links" href="#">dolorem</a></li>
</ul>
<a class="main-nav__logo" href="#"><i class="fas fa-chevron-down main-nav__icon" alt="chevron down"></i></a>
</nav>
</header>
shouldn't the flex items of the .center-section-container be closer together when this property is use? they doesn't seem to be affected all.
i'm using flex wrap so the items are now in different lines but two far away to each other, I would like them to be closer on the cross axis.
here is what i have done:
body {
border: solid orange;
width: 100vw;
height: 100vh;
color: lightgrey;
}
.main-container {
border: solid black;
width: 100%;
max-width: 840px;
margin: 0;
display: flex;
}
.main-container>* {
border: solid pink;
}
.square {
min-height: 154.53px;
min-width: 171.53px;
border-radius: 10px;
background-color: #555;
}
/**here I have the problem**/
.center-section-container {
display: flex;
flex-wrap: wrap;
align-content: flex-start;
}
.center-section-container>* {
border: solid yellow;
}
.text1 {
color: black;
}
.subtext {
flex-basis: 60%;
}
.button-grey,
.button-white {
border-radius: 5px;
flex-basis: 40%;
height: 50px;
}
.button-grey {
background-color: grey;
color: white;
}
.button-white {
border: solid grey;
background-color: white;
color: inherit;
}
.aside {
width: 200px;
}
<article class=main-container>
<div class="square"></div>
<section class="center-section-container">
<h1 class="text1">Centro de Fisioterapia Guzmán Fernandez </h1>
<h4 class="subtext">Fisioterapia general </h2>
<button class="button-grey" type="button" name="button">Reservar
</button>
<button class="button-white" type="button" name="button">Pedir
</button>
</section>
<aside class="aside">
<h3 class="valoraciones"> 24 valoraciones </h3>
<div class="number-container">
<div class="number">8,9</div>
</div>
<a class="comentarios" href="#"> ver comentarios</a>
<a class="estrella" href="#"> <img src="images/star.svg" alt="votación estrella" width="20px" height="20px" title="simbolo estrella">
</a>
</aside>
</article>
I am not 100% sure if you mean this, but in order to have less vertical distance between the flex items, you need to remove the default margin of the h1 and h4 tags (BTW, you have a typo in the closing h2/h4 tag).
.center-section-container {
display: flex;
flex-wrap: wrap;
align-content: center;
}
.center-section-container>* {
border: solid yellow;
margin: 0;
}
I added margin: 0 to the child elements which moves them closer together. This alone would align the flex items at the top of their container, and I understood you want them in the vertical middle, so I also added / changed align-content: center; for the container. HTH
body {
border: solid orange;
width: 100vw;
height: 100vh;
color: lightgrey;
}
.main-container {
border: solid black;
width: 100%;
max-width: 840px;
margin: 0;
display: flex;
}
.main-container>* {
border: solid pink;
}
.square {
min-height: 154.53px;
min-width: 171.53px;
border-radius: 10px;
background-color: #555;
}
/**here I have the problem**/
.center-section-container {
display: flex;
flex-wrap: wrap;
align-content: center;
}
.center-section-container>* {
border: solid yellow;
margin: 0;
}
.text1 {
color: black;
}
.subtext {
flex-basis: 60%;
}
.button-grey,
.button-white {
border-radius: 5px;
flex-basis: 40%;
height: 50px;
}
.button-grey {
background-color: grey;
color: white;
}
.button-white {
border: solid grey;
background-color: white;
color: inherit;
}
.aside {
width: 200px;
}
<article class=main-container>
<div class="square"></div>
<section class="center-section-container">
<h1 class="text1">Centro de Fisioterapia Guzmán Fernandez </h1>
<h4 class="subtext">Fisioterapia general </h4>
<button class="button-grey" type="button" name="button">Reservar
</button>
<button class="button-white" type="button" name="button">Pedir
</button>
</section>
<aside class="aside">
<h3 class="valoraciones"> 24 valoraciones </h3>
<div class="number-container">
<div class="number">8,9</div>
</div>
<a class="comentarios" href="#"> ver comentarios</a>
<a class="estrella" href="#"> <img src="images/star.svg" alt="votación estrella" width="20px" height="20px" title="simbolo estrella">
</a>
</aside>
</article>
I am trying to center the outer 'div' container using Flexbox. I have an unordered list with 3 li's. The li's width is: width: calc(100%/3). The ul's width is 70%. The problem is that when I try centering the ul (justify-content: center), it doesn't get centered.
I finally figured out the source of the problem. When I remove the line: width: calc(100%/3), it centers properly. My question is: How can I get it to center properly?
I tried margin: auto, but that didn't work.
Here's the JSFiddle, and here's the code snippet:
#flex-container {
width: 70%;
list-style-type: none;
padding: 0;
margin: 0;
display: -webkit-inline-flex;
display: inline-flex;
-webkit-justify-content: center;
justify-content: center;
}
li {
box-sizing: border-box;
background-color: tomato;
width: calc(100%/3);
}
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
When I remove the line: width: calc(100%/3), it centers properly
You should not calculate the width when you are using flex layout, because that is what flex is itself supposed to do.
If you are looking to align the text inside of the lis then text-align is what you need. You should also remove the width from the lis and use the flex property instead.
Snippet:
* { box-sizing: border-box; padding: 0; margin: 0; }
#flex-container {
list-style-type: none;
width: 70%; display: flex;
}
li {
flex: 1 1 auto;
background-color: tomato; border: 1px solid #fff;
text-align: center;
}
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
If you are looking to have variable width lis then justify-content is what you need. You should control the width via the width property and use flex property as required to expand or shrink.
Snippet:
* { box-sizing: border-box; padding: 0; margin: 0; }
#flex-container {
list-style-type: none; width: 70%;
display: flex; justify-content: center;
background-color: #eee;
}
li {
flex: 0 0 auto; width: 15%;
background-color: tomato; border: 1px solid #fff;
}
li:first-child { width: 20%; }
li:last-child { width: 30%; }
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
I modified your code:
http://jsfiddle.net/hrr65ajr/2/
#flex-container {
width: 70%;
list-style-type: none;
padding: 0;
margin: 0;
display: -webkit-inline-flex;
display: inline-flex;
-webkit-justify-content: center;
justify-content: center;
}
li {
box-sizing: border-box;
background-color: tomato;
margin: auto;
width: calc(100%/3);
text-align: center;
}
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
Try this:
HTML
<div class="center">
<ul id="flex-container">
<li class="flex-item">First</li>
<li class="flex-item">Second</li>
<li class="flex-item">Third</li>
</ul>
</div>
CSS
.center {
display:flex;
-webkit-justify-content: center;
justify-content: center;
}
#flex-container {
width: 70%;
list-style-type: none;
padding: 0;
margin: auto 0;
display: -webkit-inline-flex;
display: inline-flex;
-webkit-justify-content: center;
justify-content: center;
}
li {
box-sizing: border-box;
background-color: tomato;
width: calc(100%/3);
}
Is the following result possible with CSS:
So that li.item takes 50% of the width of div.wrapper, not the ul.list (which is extremly long).
I've added a snippet of a basic setup. Any ideas on the matter are appreciated (please keep in mind I'm looking for CSS options). A jsfiddle playground link: http://jsfiddle.net/6o8t9t8L/
.wrapper {
width: 400px;
overflow-y: hidden;
overflow-x: scroll;
border: 1px solid black;
}
.list {
list-style-type: none;
border: 1px solid red;
width: 2000px;
}
.item {
display: inline-block;
height: 200px;
width: 50%;
border: 1px solid green;
}
<div class="wrapper">
<ul class="list">
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
</ul>
</div>
I believe there are some 'workaround' solutions to your issue, so I'll pour in some of my ideas, maybe it will help you out a bit.
Idea 1: Position absolute and a bunch of :nth-child selectors
In order to make the .item have their width relative to .list wrapper, you can absolute position these items, and set .list wrapper to position relative, so that the .item width will be calculated based on .list width.
The major downfall of this idea would be that you have to position these elements next to each, like using the left property, but passing it like a loop:
first item will have left: 0;
second item will have left: 50%;
third item will have left: 100%;
and so on...+50% to the next items
You can either pour in a bunch of :nth-child(n), each with +50% left prop. from each other, OR use some sass stuff to make it faster.
Check out the demo here & sass demo here
*,
*:after,
*:before {
box-sizing: border-box;
}
.wrapper {
width: 400px;
overflow-y: hidden;
overflow-x: scroll;
border: 1px solid black;
/*make the grandparent, .wrapper, relative, so that the grandchilds, .item,
will calculate their width based on this width*/
position: relative;
}
.list {
list-style-type: none;
border: 1px solid red;
width: 2000px;
margin: 50px 0;
padding: 0;
/*since everyone has position absolute, theres no content flow, so a fixed height
has to be supplied*/
height: 200px;
}
.item {
width: 50%;
border: 1px solid green;
position: absolute;
left: 0;
/*you can set a height here, or position them like I did bellow*/
top: 51px;
bottom: 51px;
}
/*now the fun part starts
somehow these .items have to have left: +50% for each of them, like a loop somehow,
so you can either pour in a lot of nth-child(), for how many children you think this
list is going to have, or use sass to write it faster like i did here:
*/
.item:nth-child(1) {
left: 0;
}
.item:nth-child(2) {
left: 50%;
}
.item:nth-child(3) {
left: 100%;
}
.item:nth-child(4) {
left: 150%;
}
.item:nth-child(5) {
left: 200%;
}
.item:nth-child(6) {
left: 250%;
}
.item:nth-child(7) {
left: 300%;
}
.item:nth-child(8) {
left: 350%;
}
.item:nth-child(9) {
left: 400%;
}
.item:nth-child(10) {
left: 450%;
}
.item:nth-child(11) {
left: 500%;
}
<div class="wrapper">
<ul class="list">
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
</ul>
</div>
Idea 2: Display: flex
Using display: flex on .wrapper, will allow you to have the widths of the .item to be relative to their grandparent.
The major downfall of this idea would be that the width of .list element, will be overwritten by the width of .wrapper, no matter if you specify it or not. However, not all is lost, if you need that specific width for some styling, you can specify it, and use some pseudo classes with width: inherit, so they'll stretch to whatever width you specified in the first place.
Check out the demo here
*,
*:after,
*:before {
box-sizing: border-box;
}
.wrapper {
width: 400px;
overflow-y: hidden;
overflow-x: scroll;
border: 1px solid black;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
/*bring on the awesomeness*/
margin: 20px;
}
.list {
list-style-type: none;
/*border: 1px solid red;*/
/*you can keep this defined width, items will calculte their width
based on .wrapper class, wich will overwrite this classes width,
however if you have some use for this width, consider using :after, :before
classes like I did bellow, with .list:before*/
position: relative;
z-index: 1;
width: 2000px;
white-space: nowrap;
margin: 20px 0;
padding: 0;
font-size: 0;
/*display inline block extra spacing ....*/
}
.list:before {
content: '';
position: absolute;
left: 0;
top: 0;
bottom: 0;
width: inherit;
/*it will inherit the width you set above*/
border: 1px solid red;
}
.item {
display: inline-block;
vertical-align: top;
height: 200px;
width: 50%;
border: 1px solid green;
font-size: 16px;
/*bump back the font-size*/
}
<div class="wrapper">
<ul class="list">
<li class="item">a</li>
<li class="item">b</li>
<li class="item">c</li>
<li class="item">d</li>
</ul>
</div>
<div class="wrapper">
<ul class="list">
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
<li class="item"></li>
</ul>
</div>
.wrapper {
width: 400px;
border: 1px solid black;
}
.list {
list-style-type: none;
border: 1px solid red;
width: 400px;
display: block;
white-space:nowrap;
padding: 0;
margin: 0;
}
.item {
display: inline-block;
height: 200px;
width: 47%;
border: 1px solid green;
padding: 1%;
}
http://jsfiddle.net/btsewL9v/
I would suggest something like this..
EDIT:
I don't know how many items you are trying to cram into the list, but take a look at this:
http://lea.verou.me/2011/01/styling-children-based-on-their-number-with-css3/
I have a page with nested flex boxes here: http://jsfiddle.net/fr0/6dqLh30d/
<div class="flex-group">
<ul class="flex-container red">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
</ul>
<ul class="flex-container gold">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
</ul>
<ul class="flex-container blue">
<li class="flex-item">1</li>
<li class="flex-item">2</li>
<li class="flex-item">3</li>
<li class="flex-item">4</li>
<li class="flex-item">5</li>
<li class="flex-item">6</li>
<li class="flex-item">7</li>
<li class="flex-item">8</li>
</ul>
<div>
And the (relevant) CSS:
.flex-group {
display: flex;
flex-direction: row;
height: 500px;
}
.flex-container {
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
display: flex;
flex-direction: column;
flex-wrap: wrap;
flex: 0 0 auto;
}
.flex-item {
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
The outer flexbox (.flex-group) is meant to lay out from left-to-right. The inner flexboxes (.flex-container) are meant to layout from top-to-bottom, and should wrap if there isn't enough space (which there isn't in my jsfiddle). What I want to happen, is that the .flex-containers will grow in the X direction when the wrapping happens. But that's not occurring. Instead, the containers are overflowing.
What I want it to look like (in Chrome): https://dl.dropboxusercontent.com/u/57880242/flex-good.png
Is there a way to get the .flex-containers to size appropriately in the X direction? I don't want to hard-code their widths, since the items that appear in these lists will be dynamic.
I've been playing with this for a few minutes now, and I think I've got what you're looking for.
Here's the CSS
.flex-group {
margin: auto;
display: flex;
flex-direction: row;
justify-content: space-around;
}
.flex-container {
flex: 0 1 auto;
display: flex;
flex-flow: row wrap;
align-items: space-around;
align-content: flex-start;
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
}
.red li {
background: red;
}
.gold li {
background: gold;
}
.blue li {
background: deepskyblue;
}
.flex-item {
flex: 0 1 auto;
padding: 5px;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color: white;
font-weight: bold;
font-size: 2em;
text-align: center;
}
And here's an updated fiddle to see it in action.
Interesting, I also fiddle around with it. I have given the flex-container a flex:1 100%; This evenly space the containers to 100%; The blocks will flow in their own container space and the containers keep equal height and weight no matter how you size the window.
.flex-container {
flex: 0 100%;
display: flex;
flex-flow: row wrap;
align-items: space-around;
align-content: flex-start;
padding: 0;
margin: 0;
list-style: none;
border: 1px solid silver;
}
See fiddle here