I have 3 divs in Flex div i want the position of the last be in the right of the parent…
- User
I have 3 divs in Flex div i want the position of the last be in the right of the parent div while the two other divs stays in left with small gap between them css
- Assistant
You can achieve this by using the CSS property
justify-content: space-between;on the parent flex container. This will place the first two child divs at the beginning of the container, and the third div at the end with space in-between. Here's an example:HTML:
<div class="parent"> <div class="child">Div 1</div> <div class="child">Div 2</div> <div class="child">Div 3</div> </div>CSS:
.parent { display: flex; justify-content: space-between; } .child { width: 100px; height: 100px; background-color: yellow; } .child:nth-child(1) { margin-right: 10px; }In this example, the parent div has
display: flex;set to create a flex container.justify-content: space-between;is used to space the child elements apart. The first two child divs have a small margin-right value applied to separate them from each other.You can adjust the margin and padding values to suit your design needs.
- User
without giving fixed width
- Assistant
You can achieve this without giving a fixed width to the child elements using CSS flexbox. You can set the
flex-grow: 1;property on the first two child elements to make them occupy the remaining space in the parent element. For the third child element, setmargin-left: auto;to push it to the right side of the parent element. Here's an example:HTML:
<div class="parent"> <div class="child">Div 1</div> <div class="child">Div 2</div> <div class="child">Div 3</div> </div>CSS:
.parent { display: flex; justify-content: space-between; align-items: center; } .child { background-color: yellow; } .child:not(:last-child) { flex-grow: 1; } .child:last-child { margin-left: auto; } .child:not(:last-child) + .child:not(:last-child) { margin-right: 10px; }In this example, the parent div has
display: flex;set to create a flex container, and to push the first two child elements to the left side, and the third child element to the right side of the parent element. is used to vertically align the child elements.