How to vertically align items with different sizes? - html

I'm facing a issue of vertically aligning items when a div contains nested child elements, please check: https://codepen.io/akashpen0501/pen/rNaGgXv
Note, the container has a fixed height of 200px & I want them children to be centered vertically
Current result:
I want to align all divs vertically center, as
Required:
.container {
display: flex;
flex-flow: row;
align-items: center;
border: 1px solid black;
height:200px;
}
.container div {
margin: 10px;
padding: 10px;
border: 1px solid black;
}
.container span {
display: block;
}
<div class="container">
<div>left</div>
<div>center<span class="nested child">nested content</span></div>
<div>right</div>
</div>
Please advice.

try align-items: flex-start on the container
.container{
display: flex;
flex-flow: row;
align-items: self-start;
border: 1px solid black;
}
.container div{
margin: 10px;
padding: 10px;
border: 1px solid black;
}
.container span{
display: block;
}
<div class="container">
<div>left</div>
<div>center<span class="nested child">nested content</span></div>
<div>right</div>
</div>

You can approximate this using baseline alignment and pseudo element. Change baseline with center in the below example to see that left/right will stay at the same place.
.container {
display: flex;
flex-flow: row;
align-items: baseline;
border: 1px solid black;
height:200px;
background:linear-gradient(red,red) center/100% 1px no-repeat
}
.container:before {
content:"";
height:calc(50% + 0.25em)
}
.container div {
margin: 10px;
padding: 10px;
border: 1px solid black;
}
.container span {
display: block;
}
<div class="container">
<div>left</div>
<div>center<span class="nested child">nested content</span></div>
<div>right</div>
</div>
<div class="container">
<div>left</div>
<div>center</div>
<div>right</div>
</div>

.container {
display: flex;
align-items: flex-start; // <----- here
border: 1px solid black;
}
.container div {
margin: 10px;
padding: 10px;
border: 1px solid black;
}
.container span {
display: block;
}
<div class="container">
<div>left</div>
<div>center<span class="nested child">nested content</span></div>
<div>right</div>
</div>

Wrap your child components inside a <div> and give then class .children with following properties, it will keep your content vertically aligned and horizontally at the same height.
.children{
display: flex;
align-items: flex-start;
}
.container {
display: flex;
flex-flow: row;
align-items: center;
border: 1px solid black;
height:200px;
}
.container div {
margin: 10px;
padding: 10px;
border: 1px solid black;
}
.container span {
display: block;
}
.children{
display: flex;
align-items: flex-start;
}
<div class="container">
<div class=children>
<div>left</div>
<div>center<span class="nested child">nested content</span></div>
<div>right</div>
</div>
</div>

Related

Stretching and centering nested 2d flexbox element

.outer {
border: 2px solid red;
display: flex;
}
.inner {
border: 2px solid cyan;
display: flex;
flex-flow: column wrap;
align-self: center;
}
.innest {
border: 2px solid orange;
flex-grow: 1;
}
<div class="outer">
<div class="inner">
<div class="innest">a</div>
<div class="innest">b</div>
<div class="innest">c</div>
</div>
<div class="inner">
<div class="innest">d</div>
</div>
</div>
I've been trying to both stretch and center the 'd' element of the above code example for a while now, but haven't been able to come up with a solution. I have been able to either stretch it vertically, or center it vertically, but not both at the same time. How can I do that?
EDIT: I would like the element with the cyan border to extend the height of its parent (red), while its inner element (orange border) also extends the height of its parent (cyan). All while the 'd' content is being vertically centered.
I guess this is what you're looking for
.outer {
border: 2px solid red;
display: flex;
}
.inner {
border: 2px solid cyan;
display: flex;
flex-flow: column wrap;
align-self: center;
}
.second_inner {
display: flex;
flex-basis: auto;
}
.second_inner .innest {
display: flex;
align-items: center;
}
.innest {
border: 2px solid orange;
}
<div class="outer">
<div class="inner">
<div class="innest">a</div>
<div class="innest">b</div>
<div class="innest">c</div>
</div>
<div class="second_inner">
<div class="innest">d</div>
</div>
</div>

How do I set 100% height of outer element for inner

I have a flexbox 'table' where I'm basically trying to put an interesting thing on the top. The problem I've encountered is being unable to write it in HTML
My current result
The result I'm trying to get
I have tried to do it without inner divs and spans, by doing margin:auto but unfortunately it relocates borders from the left and right to the middle :( So the code for the current result is:
.flex-container {
width: auto;
height: 100vh;
display: flex;
flex-direction: column;
}
.flex-container .middle {
flex: 1;
display: flex;
}
.top {
padding-top: 30px;
border: 2px solid #05788D;
display:flex;
}
.leftSide {
padding-top: 30px;
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
}
.rightSide {
padding-top: 30px;
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
border-left-style: none;
}
.firstOption
{
border: 2px solid #05788D;
border-top-style: none;
border-bottom-style:none;
}
.anotherOption
{
border: 2px solid #05788D;
border-top-style: none;
border-bottom-style:none;
border-left-style:none;
}
<div class="flex-container">
<div class="top">
<div style="width:50%;">
<span class="firstOption">One option</span>
</div>
<div style="width:50%;">
<span class="anotherOption">Another option</span>
</div>
</div>
<div class="middle">
<div class="leftSide">
left
</div>
<div class="rightSide">
right
</div>
</div>
</div>
Simply use text-align to control text-alignment of your span and use padding inside your span and don't forget to make them inline-block:
.flex-container {
width: auto;
height: 100vh;
display: flex;
flex-direction: column;
}
.flex-container .middle {
flex: 1;
display: flex;
}
.top {
border: 2px solid #05788D;
display: flex;
}
.top div {
flex:1;
}
.leftSide {
padding-top: 30px;
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
}
.rightSide{
display: flex;
flex-wrap: wrap;
flex-basis: 50%;
overflow: auto;
border: 2px solid #05788D;
border-left-style: none;
}
.firstOption {
text-align:right;
}
.firstOption span,.anotherOption span{
border: 2px solid #05788D;
padding-top: 30px;
display:inline-block;
}
<div class="flex-container">
<div class="top">
<div class="firstOption"><span>One option</span></div>
<div class="anotherOption"><span>Another option</span></div>
</div>
<div class="middle">
<div class="leftSide">
left
</div>
<div class="rightSide">
right
</div>
</div>
</div>
You've added padding-top: 30px to class="top". Instead, the inner child (which are 50%) should have padding-top:30px;
While of course this can be done in a better way, above is the quickest solution to your problem.
You could use justify-content: flex-end on the left option to make it position at the end of the div. I applied the suggested changes to your code in this fiddle.
I can really recommend this guide!

Creating divs that have a width of the top div in a foreach

so I have a goal but I have no idea if it's possible or not:
Here's an image of what i got: https://screencast.com/t/88ImEmMif
And code:
<style type="text/css">
#contrainer {
width: 100%;
}
#main {
width: 600px;
margin:0 auto;
}
.wrapper {
display: inline-block;
}
.parent {
flex-direction: column;
}
.child {
border: 1px solid #0EA2E8;
margin: 2px;
padding: 5px;
display: inline-block;
}
</style>
<div id="container">
<div id="main">
<div class="anchors">
<div class="wrapper">
<div class="parent">
<?php
$links = array("https://google.com","foo", "bar", "hello", "world","google.com","google.com/adwords/","foo", "bar", "hello", "world","https://google.com/gmail","https://google.com/adwords/");
foreach($links as $link) {
echo '<div class="child">'.$link.'</div>';
}
?>
</div>
</div>
</div>
</div>
</div>
And here is what I'm looking to do: https://screencast.com/t/ze5fCya3wpbJ
So each of the boxes will have the same width in col's depending on the length in the box.
You could give a try to flex setting a fixed height:
example using lorem ipsum text. (your pHp is not the HTML output)
div {/* your .parent */
display: flex;
height: 4em;/* makes about 2 lines before wrapping to next */
flex-direction: column;
flex-wrap: wrap;
width: 500px;
margin: auto;
}
b {/* your .child */
border: solid 1px turquoise;
margin: 2px;
padding:2px;
}
<div>
<b>lorem</b>
<b>Pellentesque</b>
<b>habitant</b>
<b>morbi</b>
<b>tristique</b>
<b>senectus</b>
<b>et</b>
<b>netus</b>
<b>et</b>
<b>malesuada</b>
<b>fames</b>
<b>ac</b>
<b>turpis</b>
<b>egestas.</b>
</div>
An alternative would be using FlexBox on the outer div, applying the styles flex-wrap: wrap; and justify-content: space-between; to make them evenly spaced and distributed inside the outer div. Thanks to FlexBox each of the inner contents will have their own width.
.box {
width: 600px;
margin:0 auto;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
p {
margin: 0 0 5px;
padding: 3px 5px;
border: 1px solid black;
}
<div class="box">
<p>Bulbasaur</p>
<p>Ivysaur</p>
<p>Venusaur</p>
<p>Charmander</p>
<p>Charmeleon</p>
<p>Ash</p>
<p>Charizard</p>
<p>Squirtle</p>
<p>Wartortle</p>
<p>Blastoise</p>
<p>Ekans</p>
<p>Pikachu</p>
<p>Paras</p>
<p>Abra</p>
</div>

Inline-flex centering and gap issue

I want to center two divs with display: inline-flex; inside a block container, but somehow align-items: center; and justify-content: center; doesn't work. Only text-align: center; works, but it shouldn't be like that (because I've read that with display: inline-flex; it should be align-items and justify-content) I guess? If my solution is correct, then can you tell me what's the difference?
Also, I want to get rid of that little gap between these two centered divs, but I've tried some solutions from the internet and none of them works. Why?
I'd be glad if you guys could help me out with both of my questions.
Here's the code example:
.parent {
border: 1px solid blue;
background-color: yellow;
padding: 10px;
}
.container {
border: 1px dotted green;
padding: 10px;
text-align: center;
}
.child, .child2 {
display: inline-flex;
border: 1px solid red;
background-color: honeydew;
padding: 50px;
}
<div class="parent">
<div class="container">
<div class="child">
<h1> Test1.</h1>
</div>
<div class="child2">
<h1> Test2.</h1>
</div>
</div>
</div>
It will work if you use display: flex on container element. align-items and justify-content position flex items inside flex-container so you need to set display: flex on parent element.
.parent {
border: 1px solid blue;
background-color: yellow;
padding: 10px;
}
.container {
border: 1px dotted green;
padding: 10px;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.child,
.child2 {
display: inline-flex;
border: 1px solid red;
background-color: honeydew;
padding: 50px;
}
<div class="parent">
<div class="container">
<div class="child">
<h1> Test1.</h1>
</div>
<div class="child2">
<h1> Test2.</h1>
</div>
</div>
</div>

Center child divs inside parent div

I have a parent div that must stay at 100% with 3 child divs inside. I need to center the 3 child divs, but don't know how.
.parent {
width: 100%;
border: 1px solid blue;
}
.child {
float: left;
border: 1px solid red;
margin: 2px;
}
<div class="parent">
<div class="child">child1</div>
<div class="child">child2 - center us child divs! :)</div>
<div class="child">child3</div>
<div style="clear: both"></div>
</div>
Here is a JSFIddle:
http://jsfiddle.net/qdHH3/2/
Try using display: inline-block and text-align: center
.parent {
width: 100%;
border: 1px solid blue;
text-align: center;
}
.child {
display: inline-block;
border: 1px solid red;
margin: 2px;
}
jsFiddle: http://jsfiddle.net/qdHH3/3/
Flex solution: set justify-content: center; to the parent element:
.parent {
display: flex;
justify-content: center;
/* align-items: center; /* To align vertically, if needed */
}
.parent, .child { border: 1px solid #000; }
<div class="parent">
<div class="child">child1</div>
<div class="child">child2 - center us child divs! :)</div>
<div class="child">child3</div>
</div>
.child {
$box-size: 100%;
width: $box-size;
height: $box-size;
display: flex;
justify-content: center;
align-items: center;
}