I'm trying to put 2 components side by side but I would like one of components to be on the right of the other component without the other component being reorientated.
Example:
Here, after adding a button component to the side of the "Function" heading, the "Function" heading gets pushed to the left. However, I would like the "Function" heading to be in the middle and the "TEST" at the right. As much as possible, I'd like to avoid absolute positions.
<h4 align="right" style={{display: "inline-block"}}>Function</h4><Button sx={{float: "right"}}>Test</Button>```
This is my current code but I'm unsure what I can do. Thanks in advance!
Your question is not very clear to me, but I believe you should read about flex layout CSS property.
All possible solutions of your question
.flex0 {
display: flex;
align-items: center;
justify-content: space-between;
}
.flex1 {
display: flex;
align-items: center;
justify-content: space-around;
}
.flex2 {
display: flex;
align-items: center;
justify-content: space-evenly;
}
<div class="flex0">
<h4 align="right" style={{display: "inline-block"}}>Function</h4><Button sx={{float: "right"}}>Test</Button>
</div>
<div class="flex1">
<h4 align="right" style={{display: "inline-block"}}>Function</h4><Button sx={{float: "right"}}>Test</Button>
</div>
<div class="flex2">
<h4>Function</h4><Button>Test</Button>
</div>
There are many ways to accomplish this. But first you must understand why this misalignment happens when you use float.
Without float on button you can see it perfectly aligns to the heading with inline-block; but as soon as you introduce the float, it detaches the button as pushes it on the right side of the screen on the top corner of the block line. (image attached)
This happened because the heading tags, by default, have margins.
So to align them properly,
1st thing you can do is to provide the float button a similar margin as that of the heading.
<div>
<h4 style='display: inline-block;'>Function</h4>
<Button style='float: right; width: 50px; height: 30px; margin: 1.33em'>Test</Button>
</div>
2nd:
Wrap them in a div and use a grid with align-items: center
<div style='display: grid; grid-template-columns: 1fr 1fr; align-items: center;'>
<h4 style='display: inline-block;'>Function</h4>
<Button style='margin-left: 85%; width: 50px; height: 30px;'>Test</Button>
</div>
3rd
going the usual way, introduce left margin to the button in % (or vice-versa) i.e without float
<div>
<h4 style='display: inline-block;'>Function</h4>
<Button style='margin-left: 75%;'>Test</Button>
</div>
<div>
<h4 style='display: inline-block; margin-right: 85%;'>Function</h4>
<Button style=''>Test</Button>
</div>
Other options:
Utilise flex containers as suggested by other answers.
.flex-container {
display: flex;
}
.flex-child {
flex: 1;
border: 2px solid yellow;
}
.flex-child:first-child {
margin-right: 20px;
}
<div class="flex-container">
<div class="flex-child magenta">
Flex Column 1
</div>
<div class="flex-child green">
Flex Column 2
</div>
</div>
Related
I'm using FlexBox to create a basic Purchase Form, the simplified code is in the below CodePen.
The Code Pen
The Flex Item in question are those with:
class="purchase_body_checkout_form_selectable-product"
When I was testing it in my actual Project (with other items on the page), the results didn't happen until the 7th Flex Item was added, in the Code Pen you can see what I'm talking about after adding the 4th Flex Item.
You'll need to uncomment out the section of HTML I've commented out in the CodePen, there's 3 sections, each with some commentary.
What I'm talking about is that the Flex Container grows once I add the 4th or more Flex Item.
It grows even more after the 5th Flex Item, and continues to for the next few.
This DOES NOT happen when I use Grid, so I assume it has to do with Flex's "self-growth" nature.
I don't understand why this is.
When I tested this in my Development Environment, the Container remained the same size until the 7th Flex Item was added.
There was no difference between Items, I used React, each new Item is an exact replica of the others (I literally duplicated the React Element).
It currently doesn't make sense why the Container will grow after basically repeating what I did for the first Row, without any need to grow.
If the first Row was sized just fine, with 2 Flex Items (again, each Flex Item is coded the exact same from HTML to CSS), then why does the addition of another Row cause the Container to grow?
The second Row should have the same size requirements as the first Row... Right?
That is the confusion I wish to clear up. Any thoughts that could explain this behavior?
My guess is something to do with the Container of the Checkout AND Summary sections of the Purchase Form. Since I have "justify-content: space-between", and something about the 4th Flex Item requires slightly more space, that causes the (Checkout) Container to expand more (since it has that space, via the "space-between" gap being fairly large.
I'm still fairly new to Flex Box, so please try to address any assumptions or ignorances you notices in my explanation above.
Thank you! (:
In summary:
Tried adding Flex Items to Flex Container, expected after the first Row, the size requirements would be the same for the second Row (each Flex Item is identical), but yet, the second Row causes the Flex Container to grow in size.
Here's the CSS (Code Pen has it in a cleaner format):
`
body {
display: flex;
flex-direction: column;
align-items: center;
margin: 0 25%;
gap: 100px;
background-color: gray;
}
.purple-blue { background-color: #5100FF }
.purchase_body_checkout_container {
display: flex;
flex-wrap: wrap;
width: 100%;
justify-content: space-between;
align-items: flex-start;
gap: 25px;
}
.purchase_body_checkout_form,
.purchase_body_checkout_order-summary {
padding: 12px;
border-radius: 12px;
background-color: white;
}
.purchase_body_checkout_form_product-selection {
display: flex;
flex-wrap: wrap;
}
.purchase_body_checkout_form_selectable-product {
position: relative;
color: white;
display: flex;
align-items: center;
justify-content: center;
flex: 1 1 50%;
max-width: 50%;
overflow: hidden;
}
`
Basic HTML Structure is the following (see CodePen for the Comments I mentioned above to add the 4th+ Flex Item to see the Container grow)
This only has a single Flex Item, to keep it concise (CodePen has the rest):
<div class="purchase_body_checkout_container">
<div class="purchase_body_checkout_form">
<h2 class="purchase_body_checkout_form_header">Checkout</h2>
<div class="purchase_body_checkout_form_product-selection">
<div class="purchase_body_checkout_form_selectable-product">
<input type="checkbox" class="purchase_body_checkout_form_selectable-product_checkbox" />
<h3 class="purchase_body_product_name purple-blue">TEST ONE</h3>
</div>
</div>
</div>
<div class="purchase_body_checkout_order-summary">
<h2 class="purchase_body_checkout_order-summary_header">Order Summary</h2>
</div>
</div>
As you can see from this screenshot, the .purchase_body_checkout_form is not already taking up its width potential, which is indicated by the shaded purple. This means with flex this container can grow and will grow into the space when the viewport expands.
The reverse is also true. You will notice if you shrink your codepen down to a mobile device the container has the desired spacing. I suggest putting a max-width on .purchase_body_checkout_form.
body {
display: flex;
flex-direction: column;
align-items: center;
margin: 0 25%;
gap: 100px;
background-color: gray;
}
.purple-blue {
background-color: #5100FF
}
.purchase_body_checkout_container {
display: flex;
flex-wrap: wrap;
width: 100%;
justify-content: space-between;
align-items: flex-start;
gap: 25px;
}
.purchase_body_checkout_form,
.purchase_body_checkout_order-summary {
padding: 12px;
max-width: 280px;
border-radius: 12px;
background-color: white;
}
.purchase_body_checkout_form_product-selection {
display: flex;
flex-wrap: wrap;
}
.purchase_body_checkout_form_selectable-product {
position: relative;
color: white;
display: flex;
align-items: center;
justify-content: center;
flex: 1 1 50%;
max-width: 50%;
overflow: hidden;
}
<div class="purchase_body_checkout_container">
<div class="purchase_body_checkout_form">
<h2 class="purchase_body_checkout_form_header">Checkout</h2>
<div class="purchase_body_checkout_form_product-selection">
<div class="purchase_body_checkout_form_selectable-product">
<input type="checkbox" class="purchase_body_checkout_form_selectable-product_checkbox" />
<h3 class="purchase_body_product_name purple-blue">TEST ONE</h3>
</div>
<div class="purchase_body_checkout_form_selectable-product">
<input type="checkbox" class="purchase_body_checkout_form_selectable-product_checkbox" />
<h3 class="purchase_body_product_name purple-blue">TEST ONE</h3>
</div>
<div class="purchase_body_checkout_form_selectable-product">
<input type="checkbox" class="purchase_body_checkout_form_selectable-product_checkbox" />
<h3 class="purchase_body_product_name purple-blue">TEST ONE</h3>
</div>
<div class="purchase_body_checkout_form_selectable-product">
<input type="checkbox" class="purchase_body_checkout_form_selectable-product_checkbox" />
<h3 class="purchase_body_product_name purple-blue">TEST ONE</h3>
</div>
<div class="purchase_body_checkout_form_selectable-product">
<input type="checkbox" class="purchase_body_checkout_form_selectable-product_checkbox" />
<h3 class="purchase_body_product_name purple-blue">TEST ONE</h3>
</div>
<div class="purchase_body_checkout_form_selectable-product">
<input type="checkbox" class="purchase_body_checkout_form_selectable-product_checkbox" />
<h3 class="purchase_body_product_name purple-blue">TEST ONE</h3>
</div>
</div>
</div>
<div class="purchase_body_checkout_order-summary">
<h2 class="purchase_body_checkout_order-summary_header">Order Summary</h2>
</div>
</div>
Hey I'm new into CSS but I dont know how to make this work. Please help me on how to make this work.
The desired outcome.
My outcome.
The problem is how to make the heading come under the location tags. like in the figma design?
Here is the HTML.
import { GrLocation } from "react-icons/gr"
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="tags-colum">
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
</div>
Here is the CSS.
.container {
display: flex;
align-items: center;
justify-content: center;
}
.main-img {
height: 168px;
width: 125px;
border-radius: 5px;
}
.tags-colum {
display: flex;
align-items: center;
margin: 20px 20px;
}
.container-text {
display: block;
}
.underline-text {
text-decoration: underline;
}
Your problem is .tags-column is display: flex, so you cannot group all 3 of those elements together. Because the default flexbox is row-based style which means it will align all elements on the same row
For the fix,
Create a group of that left image and all content elements (.container)
Separate the location icon and JAPAN to another group with flexbox (.tags-colum)
Put Mount Fuji separately (.container-text)
Note that, .new-group is just an alias name which I'm using for demonstration, and it has no specific styles
import { GrLocation } from "react-icons/gr"
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="new-group">
<div className="tags-colum">
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
</div>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
</div>
The issue is that you have used display flex to the .tags-colum ( which is the outermost parent) to fix this you can use flex-direction: column, yes adding this will make everything stacked up, so what's the solution?
group your elements like this
<div className="container">
<img src="https://source.unsplash.com/WLxQvbMyfas" className="main-img" alt="location-img" />
<div className="tags-colum">
<div className='gp1'>
<GrLocation />
<p>JAPAN</p>
<p className="underline-text">View on Google Maps</p>
</div>
<div className="container-text">
<h1>Mount Fuji</h1>
</div>
</div>
.tags-colum {
display: flex;
align-items: flex-start;
margin: 20px 20px;
flex-direction: column;
}
.gp1 {
display: flex;
align-items: center;
}
here is an example https://codesandbox.io/s/homepage-forked-g18lgm?file=/public/index.html:56-126
or you can completely remove the display flex from the tags-colum
I'm having a bit of trouble to produce the below with flex box. I'd like a centrally aligned "title" with some buttons to the right (2,3,4).
The code below gets me close, but it's not perfectly aligned and loses it when the window resizes.
Any suggestions?
.header {
display: flex;
height: 50px;
align-items: center;
justify-content: center;
}
.title {
width: 250px;
margin-left: auto;
margin-right: 15%;
}
.btn-group {
margin-right: 15%;
}
<div class="header">
<h1 class="title"></h1>
<div class="btn-group">
<button id="btn_1" class="selected">2</button>
<button id="btn_2">3</button>
<button id="btn_3">4</button>
</div>
</div>
Here's a clean and simple process to get you to your layout:
First, note that CSS pseudo-elements (i.e., ::before and ::after), when applied to flex containers, are treated as flex items.
Create a pseudo-element to serve as the first flex item in the container.
Make the pseudo consume all available space (i.e., set it to flex: 1)
Do the same with your button group (.btn-group) on the opposite end (i.e., set it to flex: 1)
Now, with the outer items pressuring from both sides, the title is pinned to the middle of the container.
Make the button group container a flex container.
Set that container to justify-content: center.
Now, the individual buttons are horizontally centered on the right side of the already centered title.
.header {
display: flex;
height: 50px;
align-items: center;
}
.header::before {
content: "";
flex: 1;
}
.btn-group {
flex: 1;
display: flex;
justify-content: center;
}
<div class="header">
<h1 class="title">1</h1>
<div class="btn-group">
<button id="btn_1" class="selected">2</button>
<button id="btn_2">3</button>
<button id="btn_3">4</button>
</div>
</div>
To better understand the concepts and methodology at work here, see this post:
Center and right align flexbox elements
Here are my suggestions when using flexbox layout. You do not need to set the width on the element because the width will resize dynamically. When you set display as flex in the container, the x-axis would change to row by default then use flex property for 'title' class to expand the width to double the width of 'btn-group'. As the result, the second div will push all the way to the right and you can add the width of margin-right as how much you want it to be. Also, I would create another div after header and give it a class name as 'title' instead of giving it on h1. That way you would have two children that allow you to control it. See below how I fixed it:
h1 {
text-align: center;
}
.header {
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
.title {
flex: 1;
}
<div class="header">
<div class="title">
<h1>This is a title</h1>
</div>
<div class="btn-group">
<button id="btn_1" class="selected">2</button>
<button id="btn_2">3</button>
<button id="btn_3">4</button>
</div>
</div>
I am making a website and I'm having a small amount of problems with aligning some buttons to the right side of the page.
I've tried using text-align: right and float: left.
Can anyone try to see what I'm doing wrong?
It's currently live at http://biolinks.redxte.ch/
Anything is appreciated.
Use this
.button-parent {
text-align: right;
}
button {
display: inline-block;
}
or if you want to make it position absolutely, you can use this
.add-me {
position: absolute;
right: 20px;
}
You can use
.add-me{
margin-left : 80%;
}
I am a beginner but I think this will solve your problem.
Just add .col-sm-12 div after the row and it work perfectly
<div class="row social" id="snapchat">
<div class="col-sm-12">
<img src="/i/snapchat.svg" class="social-img">
<span class="social-text">Snapchat</span>
<span class="add-me">
Add Me
</span>
</div>
</div>
If you wrap the stuff you want on the left in an element, you can use flexbox with justify-content: space-between to push the elements on the far edges of the row.
You can also use flexbox on each of those elements with align-items: center to center everything vertically.
I wrapped the content on the left in a div with class .icon, and then all you need to apply is this CSS
.social, .icon, .add-me {
display: flex;
justify-content: space-between;
align-items: center;
}
Here's a demo.
/* included this from your site so the image won't be huge */
.social-img {
position: relative;
display: inline-block;
width: 60px;
height: 60px;
}
/* this is the part you need */
.social, .icon, .add-me {
display: flex;
justify-content: space-between;
align-items: center;
}
<base href="http://biolinks.redxte.ch/">
<div class="row social" id="instagram">
<div class="icon"> <!-- added this div -->
<img src="/i/instagram.png" class="social-img">
<span class="social-text">Instagram</span>
</div>
<span class="add-me">
View Profile
</span>
</div>
You can use the basic One. Which is:
<button class="btn" style="float: right;">See More..</button>
Hopefully this isn't an unsolved task, but I'm trying to vertically justify an unknown (ish) number of divs inside of a container.
Each div should be equal distances from each other, and, additionally, the same distance from the edges. (Assuming the last part can be accomplished using ghost elements before and after)
The divs will each fill the width of the container, and the container is a set height, but the number of elements inside the container is unknown.
I'm assuming it can be done using Flexbox to some degree, but have been unsuccessful in my attempts thus far.
Yep, flexbox is the simplest way to do it.
On the container element:
.container {
display: flex;
flex-direction: column;
justify-content: space-between;
}
On the child elements:
.container div {
flex: 1;
width: 100%
}
For the spacing between the elements, just add padding to the container and bottom margins to the children.
The style would look like this:
.container {
/* Same as above, and */
padding: 20px;
}
.container div {
/* Same as above, and */
margin-bottom: 20px;
}
.container div:last-of-type{
margin-bottom: 0;
/* So that spacing is even at bottom and top of container */
}
(I was typing this when you posted your answer, so I put it up anyway)
Fiddle
I use justify-content:space-evenly.
HTML:
div.container {
display: flex;
}
div.one_item_container {
display: flex;
flex-direction: column;
justify-content: space-evenly;
}
<div class="container">
<div class="one_item_container">
<img height="30" src="hello.jpeg" style="background-color:lightblue;" />
</div>
<div class="one_item_container">
<img height="50" src="hello2.jpeg" style="background-color:lightblue;" />
</div>
<div class="one_item_container">
<img height="40" src="hello2.jpeg" style="background-color:lightblue;" />
</div>
</div>
As usual, no matter how long I search, I find the answer only immediately after I ask the question. :D
For those curious, or for my own future reference: Flexbox's justify DOES work, you just need a few more options:
HTML:
<div id="outer-container">
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
<div class="inner-element"></div>
</div>
CSS:
#outer-container {
height: 250px;
width: 200px;
background: red;
display: flex;
justify-content: space-around;
flex-direction: column;
}
.inner-element {
width: 200px;
height: 10px;
background: blue;
}
https://css-tricks.com/almanac/properties/j/justify-content/
https://jsfiddle.net/WW3bh/