Flexbox row center one item and not the other [duplicate] - html

This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Horizontally center an element and put another element to the right of it
(1 answer)
Center one and right/left align other flexbox element
(11 answers)
Closed 2 years ago.
I am trying to center one item with flexbox, the one in blue. And the item in red should not be centered but simply be aligned to the right of the blue item.
How to do this?
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.box.right-item {
background: red;
width: 200px;
}
.box.left {
background: blue;
width: 200px;
height: 20px;
}
<div class="container">
<div class="box left">
xx
</div>
<div class="box right-item">
yy
</div>
</div>

Just use margin-left: calc(50% - 100px); for .box.left This centers the blue box.
.container {
display: flex;
flex-direction: row;
align-items: center;
}
.box.right-item {
background: red;
width: 200px;
}
.box.left {
background: blue;
width: 200px;
height: 20px;
margin-left: calc(50% - 100px);
/* ↑ half of the width */
}
<div class="container">
<div class="box left">
xx
</div>
<div class="box right-item">
yy
</div>
</div>

This is a little bit tricky to do but you could do a few workarounds like make the second box's position absolute so that it's really just the blue box being centered - or what I think would be a simpler elegant solution - just give the blue box a left margin the size of the red box -
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.box.right-item {
background: red;
width: 200px;
}
.box.left {
background: blue;
width: 200px;
height: 20px;
margin-left:200px;
}
<div class="container">
<div class="box left">
xx
</div>
<div class="box right-item">
yy
</div>
</div>

If you want to center the blue item while keeping justify-content: center, you'll need to shift the center point by positioning both items 100px to the right of their original position, which translates to adding position: relative; left: 100px; to both item. Or, you could try using margin-left.
There's no direct way to center 1 item and at the same time push the other to the end. You could do it with grid, but for flex, above will work.
.container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.box.right-item {
background: red;
width: 200px;
position: relative;
left: 100px;
}
.box.left {
background: blue;
width: 200px;
height: 20px;
position: relative;
left: 100px;
}
<div class="container">
<div class="box left">
xx
</div>
<div class="box right-item">
yy
</div>
</div>

Although your question is not quite clear, here is what I make from your question -
If you are trying to center things inside your blue-box on left, you will have to add display properties for that container too. Here I am using flexbox as you might be familiar with it.
the modified class will be like this -
.box.left {
background: blue;
width: 200px;
display: flex;
justify-content: center;
}
You don't need to add flex-direction as it is by default row.
You can run the following snippet below to check out
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
.box.right-item {
background: red;
width: 200px;
}
.box.left {
background: blue;
width: 200px;
display: flex;
justify-content: center;
}
<div class="container">
<div class="box left">
xx
</div>
<div class="box right-item">
yy
</div>
</div>

Related

How do you center an element w.r.t an already centered element in flex?

[![Red: Centered on canvas: Black: Centered between center and top of canvas][1]][1]
Using flex, it is easy to position elements in the center of a div. But how can this be done relative to already centered elements, using flex?
Shown below in the code is the best I've come up so far. I am able to center the red square in the middle but cannot get the blue one above it to be vertically center-aligned between the red square and the top border.
.flex {
border-style: solid;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: white;
height: 100vh
}
.square {
height: 10px;
width: 10px;
}
#square1 {
background-color: blue;
}
#square2 {
background-color: red;
}
.flexdivider {
display: flex;
flex-direction: column
justify-content: center;
align-items: center;
flex: 1;
}
p {
color: white;
}
<body>
<div class="flex">
<div class="flexdivider">
<div class="square" id="square1"></div>
</div>
<div class="flexdivider">
<div class="square" id="square2"></div>
</div>
<div class="flexdivider">
</div>
</div>
</div>
</body>
[1]: https://i.stack.imgur.com/4lUop.png
I have created 2 separated approach.
h2{
color: #333;
}
.box,.fakebox{
width: 30px;
height: 30px;
background-color: red;
}
/* Example #1 */
.container-box-1{
background-color: #eee;
width: 300px;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
position: relative;
}
.fakebox{
opacity: 0;
}
.box2{
position: absolute;
top: 25%;
left: 50%;
transform: translate(-50%,-50%)
}
<h2>Example #1 (Using Display Flex and position absolute)</h2>
<div class="container-box-1">
<div class="box"></div>
<div class="box box2"></div>
</div>

Flexbox: columns with different alignment [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I would like to build a "grid"/"table" using flexbox which has 2 rows and 2 columns. In the two items of the left column I want the text to be aligned to the left while in the two items of the right column I want the text to be centered. As easy as it seems to be, I cannot figure out how to achieve this goal using flexbox. I would be very delighted if someone could provide a small example on how to do this.
* {
margin: 0;
padding: 0;
outline: 0;
box-sizing: border-box;
}
body {
height: 100vh;
display: grid;
place-items: center;
}
.flex {
display: flex;
}
.container {
position: relative;
width: 350px;
height: 350px;
border: 1px solid red;
}
.column {
position: relative;
width: 100%;
height: 100%;
padding: 2rem;
display: flex;
flex-direction: column;
background-color: pink;
}
.column:nth-child(2) {
background-color: coral;
}
.column:nth-child(2) .box {
text-align: center;
/* if you also want to align text vertically center */
/* display: grid;
place-items: center; */
}
.column .box {
width: 100%;
height: 100%;
background-color: lightgreen;
}
.column .box:not(:first-of-type) {
margin-top: 2rem;
}
<div class="container flex">
<div class="column">
<div class="box">
aligned left
</div>
<div class="box">
aligned left
</div>
</div>
<div class="column">
<div class="box">
aligned center
</div>
<div class="box">
aligned center
</div>
</div>
</div>
If you also want to align text vertically center add the below mentioned code inside .box.
/* grid method */
display: grid;
place-items: center;
/* flex method */
display: flex;
align-items: center;
justify-content: center;
CSS:
.container {
display: flex;
width: 200px;
height: 200px;
border: 1px solid red;
flex-wrap: wrap;
box-sizing:border-box
}
.container div {
width: 50%;
height: 100px;
border: 1px solid green;
box-sizing:border-box
}
.container .box1,
.container .box3{
text-align: left
}
.container .box2,
.container .box4{
text-align: center
}
HTML:
<div class="container">
<div class="box1">sample text</div>
<div class="box2">sample text</div>
<div class="box3">sample text</div>
<div class="box4">sample text</div>
</div>

How to align 2 by 2 divs around a centre? E.g. internal corners should be in parent centre

looking for some help with html and css (more with css) I want to make that work as a template to be use with a parent div in my app, but having problems to center 4 divs in a centre of sub parent. I need they align in central point by they inner corners.
E.g. corner of 1st right bottom, 2nd - left bottom, 3rd - right top, 4th - left top should be centered.
1-2-3-4 div size may vary
I have this https://codepen.io/Kvark/pen/zYzEgWo (updated) to explain:
But don't know how to make cantering stuff I need,
now its look like as that:
But I want it to be look like as that:
any advice or help on that?
You can position each of the squares within the right hand box by making the right hand box have a position (I've given it relative) and the colored boxes positioned in relation to that.
As the boxes have differing dimensions, which may not be overtly known at run time, this snippet uses a translation of each to get them in the right positions as a translate with a % uses the relevant dimension of the item itself (not its parent) if you use % units.
html,
body,
{
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
div .item {
display: flex;
justify-content: center;
align-items: center;
border: 2px dashed #f69c55;
}
#container {
height: 50%;
}
.column {
float: left;
padding: 10px;
height: 300px;
}
.left {
width: 50%;
}
.right {
width: 45%;
}
.column.right.square {
position: relative;
}
#chart_1 {
height: 100px;
width: 100px;
}
#chart_2 {
height: 60px;
width: 60px;
}
#chart_3 {
height: 150px;
width: 120px;
}
#chart_4 {
height: 50px;
width: 180px;
}
.square {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.square::after {
content: '';
width: 100%;
}
.square .item:nth-child(n + 3) {
order: 1;
}
<div class="row">
<div class="column left" style="background-color:#aaa;">
<h2>tbd</h2>
<div id="container"></div>
</div>
<div class="column right square" style="background-color:#bbb;">
<div class="item" id="chart_1" style="background-color:red;position: absolute; left: 50%; top: 50%;transform: translate(-100%, -100%);">1</div>
<div class="item" id="chart_2" style="background-color:blue;position: absolute; left: 50%; top: 50%;transform: translate(0, -100%);">2</div>
<div class="item" id="chart_3" style="background-color:green;position: absolute; left: 50%; top: 50%;transform: translate(-100%, 0%);">3</div>
<div class="item" id="chart_4" style="background-color:yellow;position: absolute; left: 50%; top: 50%;transform: translate(0, 0);">4</div>
</div>
</div>
If I understand your question correctly you want the boxes to be tightly placed together, and then you want the "box of boxes" to be placed in the middle of their parent?
If so, here is one way to solve it: using an additional wrapping div to group the boxes together, they can then be placed together.
.item {
height: 50px;
width: 50px;
display: flex;
justify-content: center;
align-items: center;
border: 2px dashed #f69c55;
}
/* This puts the boxes in two columns. */
.boxwrapper {
display: grid;
grid-template-columns: 1fr 1fr;
}
.wrapper {
background-color: slategray;
height: 80vh;
/* This puts the boxes in the middle, and restricts the width and height
of the boxwrapper, so that the boxes end up close together. */
display: flex;
justify-content: center;
align-items: center;
}
<div class="wrapper">
<div class="boxwrapper">
<div class="item" style="background-color:tomato;">1</div>
<div class="item" style="background-color:royalblue;">2</div>
<div class="item" style="background-color:forestgreen;">3</div>
<div class="item" style="background-color:gold;">4</div>
</div>
</div>
You need Grid to make a template here. Try this
.container {
display: grid;
grid-template-columns: auto auto;
grid-row-gap: 20px;
justify-content: center;
}
.item {
width: 50px;
height: 50px;
display: grid;
justify-content: center;
align-content: center;
background-color: red;
border: 1px solid black;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
Working example:
.container {
display: grid;
grid-template-columns: auto auto;
justify-content: center;
}
.item {
width: 50px;
height: 50px;
display: grid;
justify-content: center;
align-content: center;
background-color: red;
border: 1px solid black;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>

Can't center both vertically and horizontally [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 1 year ago.
I just can't center the div(Horizontal-Container)both vertically and horizontally and I can't figure out why it's not working...
I've try all the methods by w3school, but either it's not horizontally or vertically center, it can't be both achieved...
Below is my code:
body {
background-color: #62306D;
}
.Horizontal-Container {
width: 100%;
height: auto;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
text-align: center;
}
.Yellow {
background-color: #F7EC7D;
width: 90px;
height: 180px;
}
<div class="Horizontal-Container">
<div class="Yellow"></div>
<div class="Yellow"></div>
<div class="Yellow"></div>
</div>
Your issue arises from .Horizontal-Container not being full height so it is technically vertically centered, just it hasn't moved. To fix this, you need to set the height of body and html to 100% which then allows the container to have the height you desire. It may seem off centre now, but that is down to padding and margin on the elements which you can easily remove.
html, body {
background-color: #62306D;
height: 100%;
margin: 0;
padding: 0;
}
.Horizontal-Container {
width: 100%;
height: 100%;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
text-align: center;
}
.Yellow {
background-color: #F7EC7D;
width: 90px;
height: 180px;
}
<div class="Horizontal-Container">
<div class="Yellow"></div>
<div class="Yellow"></div>
<div class="Yellow"></div>
</div>
It's not working because your .Horizontal-Container does not have a specific height. If you set the height to auto it will consume as much space as its children need. Thus you have to add a height either to your container or simply to your body in order to center your elements over the whole page.
body {
background-color: #62306D;
}
.Horizontal-Container {
width: 100%;
height: 100vh; /* <-- set a specific height */
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
text-align: center;
}
.Yellow {
background-color: #F7EC7D;
width: 90px;
height: 180px;
}
<div class="Horizontal-Container">
<div class="Yellow"></div>
<div class="Yellow"></div>
<div class="Yellow"></div>
</div>

Align 3 elements vertically in the same line, one on the left, one in the middle and one on the right [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 4 years ago.
i'm having a bad time trying to figure out how to align 3 divs vertically, inside another div. I want one div to be on the left, one on the right, and one aligned on the center. I'm using position relative, top 50% and transformY(-50%), to align everything vertically. When i tried this, they still stay one above another
i made this example on jsfiddle: https://jsfiddle.net/6jb4c5gz/8/
<section style="background-color: yellow">
<div class="aaa" style="background-color:red;"></div>
<div class="bbb" style="background-color:blue;"></div>
<div class="ccc" style="background-color:green;"></div>
</section>
css:
body {
padding: 0;
margin: 0;
}
header{
margin: 0 auto;
text-align: center;
}
div {
height: 30px;
width: 30px;
}
1 {
position: relative;
top: 50%;
transform: translateX(-50%);
}
can someone help me with this?
Edit: i think i wasn't really clear. I want the 3 divs to be in the same line, one on the left, one on the center, and one on the right, but aligned vertically
Should be as simple as:
header{
width: 100%;
display: flex;
justify-content: space-between;
}
header > div {
height: 30px;
width: 30px;
}
You can use Bootstrap 4 to do that :
.aaa, .bbb, .ccc {
height: 30px;
width: 30px;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<section class="d-flex flex-column" style="background-color: yellow">
<div class="align-self-start aaa" style="background-color:red;"></div>
<div class="align-self-center bbb" style="background-color:blue;"></div>
<div class="align-self-end ccc" style="background-color:green;"></div>
</section>
Use flex to get that result:
body {
padding: 0;
margin: 0;
}
section {
width: 100%;
height: 120px;
display: flex;
flex-direction: column;
}
div {
height: 30px;
width: 30px;
}
.aaa {
align-self: flex-start;
}
.bbb {
align-self: center;
}
.ccc {
align-self: flex-end;
}
<section style="background-color: yellow">
<div class="aaa" style="background-color:red;"></div>
<div class="bbb" style="background-color:blue;"></div>
<div class="ccc" style="background-color:green;"></div>
</section>
Simple solution
header{
width: 100%;
height: 120px;
display: flex;
justify-content: space-between;
align-items: center;
}
https://jsfiddle.net/6jb4c5gz/28/
try using CSS:
text-align: center;
OR include "Bootstrap" and add class:
pull-right, text-center