Place a div next to a child div - html

i have a simple question.
My code:
'''
<div class="container">
<div class="child_1">
</div>
<div class="child_2">
</div>
</div>
<div class="another_div">
</div>
'''
How to place 'another_div' next to 'child_2' ?

You have to wrap all your content in a new <div> with a class main-container (say).
Now, set the display of the main container to flex;
display: flex;
As another <div> is supposed to be added to the left of the elements of the <div> with class container, we set the flex-direction to row-reverse.
flex-direction: row-reverse;
The entire main container gets aligned to the right. As it's already in reverse, we set justify-content to flex-end.
justify-content: flex-end;
The another_div is added at the bottom and not on the top, which implies the items are vertically aligned to the bottom. For this, we align the items to the flex-end.
align-items: flex-end;
.main-container {
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
align-items: flex-end;
}
Link to codepen: https://codepen.io/geekyquentin/pen/wvyJOBX

.child_1,
.child_2,
.another_div {
width: 100px;
height: 100px;
}
.child_1,
.child_2 {
border: 1px solid red;
}
.another_div {
border: 1px solid black;
order: 1;
}
body {
display: flex;
flex-direction: row;
align-items: flex-end;
}
<div class="container">
<div class="child_1"> </div>
<div class="child_2"> </div>
</div>
<div class="another_div"></div>

Related

Aligning the last div to the bottom of a flexbox

Scenario :
I'm creating a pricing comparison table and am having difficulties aligning the last div, card-vat-fee, to the bottom of the container.
I need to do this because the tiers have longer running lists than
one another, causing the last div isn't aligned with the bottom of
the container.
How can I get the last div to align to the bottom of the flexbox?
Tried Case :
Of course, if I set a min-height: 320px; on the card-vat-fee class it will align the div to the bottom, however this isn't a responsive solution and I feel like there is a better approach that uses flex properties. Moreover, setting the card-vat-fee div to flex-grow, flex: 1 1 auto, produces an unideal solution.
Code :
<div class='pricing__tier'>
<div class='uni-card-header'>
</div>
<div class='uni-card-body'>
<div class='uni-row-on'>
</div>
<div class='uni-row-off'>
</div>
<div class='uni-row-on card-vat-fee'>
<div class='vat-fee-text'>
Credit card fees and VAT apply. See below for details.
</div>
</div>
</div>
</div>
<style>
.pricing__tier {
padding: 0;
text-align: center;
display: flex;
flex-direction: column;
width: 0%;
flex: 1;
}
.uni-card-body {
display: flex;
flex-direction: column;
}
</style>
Pricing Tier
Please Suggest.
Thanks in advance
Use margin-top:auto on the last div.
.pricing__tier {
padding: 0;
text-align: center;
display: flex;
flex-direction: column;
width: 25%;
flex: 1;
height: 200px; /* for demo purposes */
border: 1px solid grey;
}
.uni-card-body {
display: flex;
flex-direction: column;
flex: 1;
}
.card-vat-fee {
margin-top: auto; /* push to bottom */
background: green;
}
<div class='pricing__tier'>
<div class='uni-card-header'>
</div>
<div class='uni-card-body'>
<div class='uni-row-on'>
</div>
<div class='uni-row-off'>
</div>
<div class='uni-row-on card-vat-fee'>
<div class='vat-fee-text'>
Credit card fees and VAT apply. See below for details.
</div>
</div>
</div>
</div>
plaesa try this one :
.uni-card-body {
display: flex;
flex-direction: column;
width: 'for example' 700px;
}
.uni-row-on.card-vat-fee{
align-self: flex-end;
}
Ihope this will help you!
.uni-card-body {
display: flex;
flex-flow: row wrap;
justify-content: center;
background: yellow;
height: 90vh;
}
.uni-row-on.card-vat-fee {
align-self: flex-end;
background: green;
}
<div class='pricing__tier'>
<div class='uni-card-header'>
</div>
<div class='uni-card-body'>
<div class='uni-row-on'>
</div>
<div class='uni-row-off'>
</div>
<div class='uni-row-on card-vat-fee'>
<div class='vat-fee-text'>
Credit card fees and VAT apply. See below for details.
</div>
</div>
</div>
</div>
I've illustrated the thing in the snippet, it'll help.
Note: Content justification, background and height are for demonstration and not necessary.
1- set the parent div relative position without top & left & right &
bottom property
2- set the last div position absolute with bottom:0;right:0;left:0;height:36px;
<style>
.pricing__tier {
position:relative;
padding: 0;
text-align: center;
display: flex;
flex-direction: column;
width: 0%;
flex: 1;
}
.pricing__tier>.vat-fee-text {
position:absolute;
bottom:0;
right:0;
left:0;
height:36px;
}
</style>

How to make flex children have equal widths and at the same time not larger than its contents?

I have the following code:
.parent {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.firstChild {
flex: 1;
text-align: left;
}
.lastChild {
flex: 1;
text-align: right;
}
<div class="parent">
A very loooooooooooooooooong link
<span>Work</span>
<span class="lastChild">Other</span>
</div>
Complete fiddle here
The Problem is: The first flex child (long link) element is larger than its content (because of flex-grow). So when the user click next to its text, it also activates the link.
We used flex-grow here in order to make 1. and 3. childs equally wide, so the 2. element can stand -almost- in the middle (since the flex childrens' width vary a lot). How can i achieve this without using flex-grow? Or in other words, how can i guarantee that, the link's width is equal to its text's width?
simply use this.
<div class="parent">
<div class="firstChild">
A very looooooooooooooooooooong link
</div>
<div>
<span>Work</span>
</div>
<div class="lastChild">
<span>Other</span>
</div>
</div>
Wrap it with an additional span.firstChild:
.parent {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
}
.firstChild {
flex:1;
text-align:left;
}
.lastChild {
flex:1;
text-align:right;
}
/* for overview */
a {
border: 1px solid red;
}
<div class="parent">
<span class="firstChild">
A very loooooooooooooooooong link
</span>
<span>Work</span>
<span class="lastChild">Other</span>
</div>
Try this one:
.parent {
display: grid;
align-items: center;
justify-items: start;
grid-auto-flow: column;
justify-content: space-between;
grid-template-columns: repeat(3, 1fr);
justify-items: center;
}
<div class="parent">
A very looooooooooooooooooooong link
<div>
<span>Work</span>
</div>
<div class="lastChild">
<span>Other</span>
</div>
</div>

Flexbox align-items stretch issue

I have a 2 column container. On the left side, there is an image. On the right side, I have 2 <p> elements. I want to position one <p> element at the top, the other at the bottom of the container.
I tried to use flexblox, with align-items to stretch, but that doesn't work. Although flex-start and flex-end does work.
Is this even possible with flexbox?
.container {
display: flex;
align-items: stretch;
}
img {
display: block;
width: 100%;
}
.element-box {
height: 100%;
}
<div class="container">
<div>
<img src="https://placeimg.com/640/480/nature">
</div>
<div class="element-box">
<p>Should be on Top</p>
<p>Should be on Bottom</p>
</div>
</div>
You can make your element-box a column flexbox and give justify-content: space-between. Also remove height: 100% from it.
See demo below:
.container {
display: flex;
align-items: stretch;
}
img {
display: block;
width: 100%;
}
.element-box {
display: flex;
flex-direction: column;
justify-content: space-between;
}
<div class="container">
<div>
<img src="https://placeimg.com/640/480/nature">
</div>
<div class="element-box">
<p>Should be on Top</p>
<p>Should be on Bottom</p>
</div>
</div>
You need flex for second container too where you are having you <p> elements, please look at css below it'll help you:
.element-box {
display: flex;
flex-direction: column;
justify-content: space-between;
flex-grow: 1;
}

Right Align an element INSIDE FLEXBOX DIV?

If we have this structure of HTML
<div id="main">
<div id="sub-1"><div>
<div id="sub-3"><div>
<div id="sub-3"><div>
</div>
And the SASS styles for that structure as:
#main
display: flex
flex-direction: column
justify-content: center
align-items: center
align-content: center
width: 1200px
#sub-1, #sub-2, #sub-3
width: 100px
height: 100px
With that structure we will have a column of 3 squares perfectly center-aligned. But what if we want to move #sub-1 to the left and #sub-3 to the right? Is there a way to do that?
Thank you very much!
You can use the property align-self like this:
#main {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
}
#sub-1, #sub-2, #sub-3 {
background:red;
width: 50px;
height: 50px
}
#sub-1 {
align-self:flex-start;
}
#sub-3 {
align-self:flex-end;
}
<div id="main">
<div id="sub-1"></div>
<div id="sub-2"></div>
<div id="sub-3"></div>
</div>

Display a div content as row while parent is displayed as column

I'm now working on a problem for a few hours (not that experienced :D) and right now I'm pretty close, but theres a last thing not working:
I have multiple divs with different orientations in each other. An inner parent div has flex-direction: column; and the child has flex-direction: row; but its not shown inline.
JSFiddle Link: Live Demo
HTML
<div class="flexcon_game">
<div class="flexcon_game_left">
Left
</div>
<div class="flexcon_game_center">
<div class="flexcon_game_center_top">
center_top
</div>
<div class="flexcon_game_center_mid">
<p> (-- </p>
<p> -:- </p>
<p> --) </p>
</div>
<div class="flexcon_game_center_bottom">
center_bottom
</div>
</div>
<div class="flexcon_game_right">
right
</div>
CSS
.flexcon_game{
width: 80%;
margin: auto;
display: flex;
flex-wrap: nowrap;
justify-content: space-around;
align-items: center;
}
.flexcon_game_left{
background-color: red;
}
.flexcon_game_right{
background-color: green;
}
.flexcon_game_center{
flex-direction: column;
justify-content: space-around;
background-color: orange;
}
.flexcon_game_center_mid{
flex-flow: row nowrap;
}
Paragraphs are block level elements. Setting the flex direction to row doesn't change that. You have to explicitly make them inline in your CSS:
.flexcon_game{
width: 80%;
margin: auto;
display: flex;
flex-wrap: nowrap;
justify-content: space-around;
align-items: center;
}
.flexcon_game_left{
background-color: red;
}
.flexcon_game_right{
background-color: green;
}
.flexcon_game_center{
flex-direction: column;
justify-content: space-around;
background-color: orange;
}
.flexcon_game_center_mid{
flex-flow: row nowrap;
}
.flexcon_game_center_mid p{
display: inline;
}
<div class="flexcon_game">
<div class="flexcon_game_left">
Left
</div>
<div class="flexcon_game_center">
<div class="flexcon_game_center_top">
center_top
</div>
<div class="flexcon_game_center_mid">
<p> (-- </p>
<p> -:- </p>
<p> --) </p>
</div>
<div class="flexcon_game_center_bottom">
center_bottom
</div>
</div>
<div class="flexcon_game_right">
right
</div>