I'm learning web dev basics with HTML and CSS. I met an issue today : I want to make a horizontal scroll on my page with paragraphs (represented here by Lorem) inside but they appear in line while i want them in block.
Here's my code :
.scroll {
display: flex;
justify-content: flex-start;
flex-direction: row;
overflow-x: scroll;
overflow-y: hidden;
white-space: nowrap;
}
.part {
display: inline-block;
align-content: flex-start;
overflow-x: visible;
overflow-y: visible;
flex-wrap: wrap;
height: 200px;
width: auto;
}
<div class="scroll">
<div class="part">
<p>
Lorem...
</p>
</div>
<div class="part">
<p>
Lorem...
</p>
</div>
<div class="part">
<p>
Lorem...
</p>
</div>
</div>
I have been trying tons of things but unsuccessful... (I think the first part is good)
Thanks in advance
.scroll {
flex-direction: column;
}
add this style..this will change the content flow to column wise
Flexbox provided a property called flex-direction. Which means that what direction the flexbox children are laid out in and by default this is set to row. flex-direction
.parent {
display: flex;
}
<div class="parent">
<div class="child1">
<p>Hello I'm Child 1</p>
</div>
<div class="child2">
<p>Hello I'm Child 2</p>
</div>
<div class="child3">
<p>Hello I'm Child 3</p>
</div>
</div>
As you see that in above example div is a block level element but they appear as inline becuase flex-direction is set to row by default. Now change the direction from row to column that will put flexbox children in a column layout.
.parent {
display: flex;
flex-direction: column;
}
<div class="parent">
<div class="child1">
<p>Hello I'm Child 1</p>
</div>
<div class="child2">
<p>Hello I'm Child 2</p>
</div>
<div class="child3">
<p>Hello I'm Child 3</p>
</div>
</div>
Hope that you understand the concept of flex-direction.
Related
I have the following CSS, which allows me to create a row that wraps based on screen size. If there is enough space all items appear on one row, else it wraps to the next row with proper alignment.
My problem is that I have a description text under each "square" div. If this text is only one line there is no issue, but if this text is 2 or more lines, the square it is beneath gets pushed up and is no longer aligned with it's siblings. How can I prevent this? I've included example code as a snippet, it's easiest to open as a full screen view to see what I'm talking about. I want the tops of the squares to always be aligned
.flex-row {
display: flex;
flex-direction: row;
justify-content: space-evenly;
flex-wrap: wrap;
align-self: stretch;
}
.flex-col {
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
}
.sub-row {
display: flex;
flex-direction: row;
justify-content: space-evenly;
width: 300px;
}
.square {
height: 250px;
width: 250px;
background-color: #900;
}
<body>
<div class="flex-row">
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text Placeholder text Placeholder text Placeholder text Placeholder text
</p>
<p>
00
</p>
</div>
</div>
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text
</p>
<p>
00
</p>
</div>
</div>
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text
</p>
<p>
00
</p>
</div>
</div>
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text
</p>
<p>
00
</p>
</div>
</div>
</div>
</body>
I've tried justifying the columns to flex-start and flex-end but that doesn't work. I've also messed with trying to use the align self property on the p element itself but I am unable to get the squares to be aligned if the text takes 2 or more lines.
Give align-items: flex-start to the flex-row (this overrides the default stretch behaviour).
Your dyanmic text being below the squares makes things easy as when the flex items wrap, the wrapping flex lines will will adjust to the height of the dynamic text in the preceeding flex line.
See demo below:
.flex-row {
display: flex;
flex-direction: row;
justify-content: space-evenly;
flex-wrap: wrap;
/* align-self: stretch;*/
align-items: flex-start; /* ADDED */
}
.flex-col {
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
}
.sub-row {
display: flex;
flex-direction: row;
justify-content: space-evenly;
width: 300px;
}
.square {
height: 250px;
width: 250px;
background-color: #900;
}
<body>
<div class="flex-row">
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text Placeholder text Placeholder text Placeholder text Placeholder text
</p>
<p>
00
</p>
</div>
</div>
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text
</p>
<p>
00
</p>
</div>
</div>
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text
</p>
<p>
00
</p>
</div>
</div>
<div class="flex-col">
<div class="square">
</div>
<div class="sub-row">
<p>
Placeholder text
</p>
<p>
00
</p>
</div>
</div>
</div>
</body>
I am making a site that will have three cards and to create them I'm using flex box. I am using justify-content: space-between which works perfectly when the three columns are all on the same row, as their margins are perfect with the container, and the space in-between them is great.
However, when the columns wrap, the column that is now on the new row is at the same left margin as the first, which is expected with space-between, but in my scenario, space-around's wrap behavior would look much better, as I have and odd number of cards.
Is there a way for me to get space-between's outer margin alignment with space-around's wrapping behavior?
Here is a codepen that gives a quick example of what I have now.
https://codepen.io/anon/pen/xdrpEo
.flex-row {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: spacing-between;
}
.file-links-flex-container {
max-width: 400px;
flex-grow: 1;
background-color: red;
}
.container-content {
justify-content: flex-start;
padding: 0 30px;
max-height: 600px;
}
<div class="flex-row space-around">
<div class="file-links-flex-container col">
<div class="container-header">
<h5>A</h5>
</div>
<div class="container-content">
<p>I AM TEST CONTENT!</P>
</div>
</div>
<div class="file-links-flex-container col">
<div class="container-header">
<h5>B</h5>
</div>
<div class="container-content">
<p>I AM TEST CONTENT!</P>
</div>
</div>
<div class="file-links-flex-container col">
<div class="container-header">
<h5>C</h5>
</div>
<div class="container-content">
<p>I AM TEST CONTENT!</P>
</div>
</div>
</div>
Try adding margin: 0 auto, You get that.
.file-links-flex-container{
margin: 0 auto;
}
I think this one helped you. :)
Even you can solve this one using justify-content:center; but margin will be the good one.
I'm trying to center two div Horizontally, and align one at the Middle of the parent container, and the second one at the Bottom of the parent container. I'm using Foundation 6, with the Flexbox version.
Html
<section id="hero">
<div class="row align-middle align-center">
<h1>Welcome</h1>
</div>
<div class="row align-bottom align-center">
<p>Some text</p>
</div>
</section>
Css
#hero {
height: 100vh;
display: flex;
flex-wrap: wrap;
}
The problem is that every div in the flex container (#hero) appear on the same line.
Thank's for helping !
Pretty sure you have to add the class column to the sub element of row like so:
id="hero">
<div class="row align-middle align-center">
<h1 class="column">Welcome</h1>
</div>
<div class="row align-bottom align-center">
<p class="column">Some text</p>
</div>
</section>
I finally found how to align vertically 3 divs using a Flexbox. Foundation wasn't really in the problem.
I made a Js Fiddle for those who have the same question:
.wrapper {
display: flex;
flex-direction: column;
height:90vh;
}
.center {
width: 100%;
flex-grow: 1;
align-items: center;
display:flex;
}
.a {
width: 100%;
}
<div class="wrapper">
<div class="a">Header</div>
<div class="center">Body</div>
<div class="a">Footer</div>
</div>
JSFiddle: https://jsfiddle.net/ek38ff5r/20/
Here is my code:
.block {
width: 25%;
float: left;
}
<paper-material elevation="1">
<p>List of blocks:</p>
<div class="block">
<p>Block 1</p>
</div>
<div class="block">
<p>Block 2</p>
</div>
<div class="block">
<p>Block 3</p>
</div>
<div class="block">
<p>Block 4</p>
</div>
</paper-material>
Paper-material element contains only "List of blocks" label, all four divs are outside. How can I fix it so that the divs are inside the paper-material element?
Thank you.
Import iron-flex-layout and add #apply --layout-horiztonal; to your CSS, which currently amounts to
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
You have to add div at the end of all .block divs and set style clear: both
inside style:
.block {
width: 25%;
float: left;
}
.clear {
clear: both;
}
and paper-material:
<paper-material elevation="1">
<p>List of blocks:</p>
<div class="block">
<p>Block 1</p>
</div>
<div class="block">
<p>Block 2</p>
</div>
<div class="block">
<p>Block 3</p>
</div>
<div class="block">
<p>Block 4</p>
</div>
<div class="clear"></div>
</paper-material>
but this is still not the best solution. Since your paper-material should have fixed width. At this moment it is better to use display: flex . Be aware that older IE doesn't support flex. So if you really want to support that 1% of people then you can continue using float:left.. but i still think using display: inline-block would be much more better.
In Safari 8.0.8 on Macbook.
I'm using flexbox to distribute elements in a grid-like container.
My .eventContainer is parent to multiple .event boxes which should be distributed across the rows by flexbox. It works fine in Firefox and Chrome but Safari displays the events on their own line, rather than next to each other!
<div class="eventContainer">
<div class="event">
<div class="thumb">
<img src="http://imgur.com/JDxjEknb.jpg">
</div>
<p class="thumbDate">Fri 18 Sept</p>
<p class="thumbArtist">Event 1</p>
<p class="thumbTitle">Subtitle 1</p>
<p class="thumbLocation">Location</p>
</div>
<div class="event">
<div class="thumb">
<img src="http://imgur.com/AwA5ga7b.jpg">
</div>
<p class="thumbDate">Sat 19 Sept</p>
<p class="thumbArtist">Event 2</p>
<p class="thumbTitle">Title 2</p>
<p class="thumbLocation">Subtitle 2</p>
</div>
<div class="event">
<div class="thumb">
<img src="http://imgur.com/9z5NkBxb.jpg">
</div>
<p class="thumbDate">Sat 19 Sept</p>
<p class="thumbArtist">Event 3</p>
<p class="thumbTitle">Title 3</p>
<p class="thumbLocation">Subtitle 3</p>
</div>
<!-- Fix for FlexBox -->
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
<div class="event"></div>
</div>
CSS:
.eventContainer {
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
-webkit-justify-content: space-between;
justify-content: space-between;
}
.event {
display: inline-block;
margin-bottom: 35px;
width: 100%;
max-width: 200px;
margin-right: 15px;
cursor: pointer;
}
What have I missed here?
JSFiddle Example: http://jsfiddle.net/foepzgf8/1/
Can try it in both Chrome and Safari and see what I mean.
It's actually not justify-content in this situation. When you run into issues with flexbox, it's always good to go back and define flex-grow, flex-shrink, and flex-basis on the children of the flexbox container.
If you add this to .event your issue will be resolved:
-webkit-flex: 1 0 200px;
flex: 1 0 200px;
Updated fiddle: http://jsfiddle.net/foepzgf8/5/