Positioning two div elements relative to the middle one [duplicate] - html

This question already has answers here:
Is there a way to center one of the flex/grid children(more than three) and with different widths?
(2 answers)
Closed 1 year ago.
I need to center an element #center-navigation relative to the page center and then position the two another on the left (#logo) and right (#contacts) sides of the element. Currently in my implementation #center-navigation is not centered on the page. Is it possible to do with flexbox?
#row {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
}
#logo {
height: 20px;
width: 80px;
background-color: red;
margin-right: auto;
}
#contacts {
margin-left: auto;
}
h1 {
text-align: center;
}
<div id="row">
<div id="logo">
</div>
<div id="center-navigation">
Some navigation here
</div>
<div id="contacts">
example#mail.org
</div>
</div>
<h1>
Some long website title goes here
</h1>

You can try with css grid :
#row {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
justify-content: space-between;
align-items: center;
text-align: center;
}
#logo {
height: 20px;
width: 80px;
background-color: red;
}
#contacts {
background-color: blue;
text-align: right;
}
#center-navigation {
background-color: green;
}
h1 {
text-align: center;
background-color: yellow;
}
<div id="row">
<div id="logo">
</div>
<div id="center-navigation">
Some navigation here
</div>
<div id="contacts">
example#mail.org
</div>
</div>
<h1>
Some long website title goes here
</h1>

It is impossible to do with flexbox!
I suggest you to use position:absolute.
#row {
/*display: flex;*/
/*flex-direction: row;*/
/*justify-content: center;*/
/*align-items: center;*/
text-align:center;
position:relative;
}
#logo,#contacts{
position:absolute;
left:0;
top:50%;
transform:translateY(-50%);
}
#logo {
height: 20px;
width: 80px;
background-color: red;
margin-right: auto;
}
#contacts {
margin-left: auto;
left:auto;
right:0;
}
h1 {
text-align: center;
}
<div id="row">
<div id="logo">
</div>
<div id="center-navigation">
Some navigation here
</div>
<div id="contacts">
example#mail.org
</div>
</div>
<h1>
Some long website title goes here
</h1>

The #contacts width should be equal to the #logo width.
#contacts, #logo{
width: 20%
}

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>

How to align element in div independently of the others in css? [duplicate]

This question already has answers here:
Center and bottom-align flex items
(3 answers)
Closed 2 years ago.
So i've got div looking like this. My question is how can I align h1 so it stays always on top of this div and rest of content is aligned in the middle of div like how it is now?
HTML:
<div class="container">
<h1>INFO</h1>
<div class="info"><h2>Age</h2><p>20</p></div>
<div class="info"><h2>Adress</h2><p>Wolna 23, Warszawa</p></div>
<div class="info"><h2>Email</h2><p>lorem#gmail.com</p></div>
<div class="info"><h2>Phone</h2><p>669 133 777</p></div>
</div>
CSS:
.container
{
align-items: center;
justify-content: center;
display:flex;
flex-direction: column;
width: 35.5%;
height: 550px;
padding: 20px;
margin: 20px;
}
.info
{
align-items: center;
justify-content: center;
display:flex;
flex-direction: column;
margin: 0 0 15px 0;
}
Can you please check the below code link? Hope it will work for you. We can solve this issue with the help of flex, without using position: absolute;.
You need to remove justify-content: center; from the .container.
We have wrapped all info items in one div like .content and give margin:auto; to them.
Please refer to this link:
https://jsfiddle.net/yudizsolutions/z71rbu6o/7/
.container {
align-items: center;
display: flex;
flex-direction: column;
width: 35.5%;
height: 550px;
padding: 20px;
margin: 20px;
background: #000;
}
h1 {
color: #fff;
}
.content {
margin: auto;
}
.info {
align-items: center;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 0 15px 0;
color: #fff;
}
.info h2,
.info p {
margin: 0;
}
<div class="container">
<h1>INFO</h1>
<div class="content">
<div class="info">
<h2>Age</h2>
<p>20</p>
</div>
<div class="info">
<h2>Adress</h2>
<p>Wolna 23, Warszawa</p>
</div>
<div class="info">
<h2>Email</h2>
<p>lorem#gmail.com</p>
</div>
<div class="info">
<h2>Phone</h2>
<p>669 133 777</p>
</div>
</div>
</div>
Try adding this:
.container h1{
position: absolute;
top: 0vh;
}
if you want to stick it to the top add this:
h1{
position: absolute;
top: 0;
}
Edit: there are a lot of ways, but in all of them you must to take the header out of the flexbox. You can do it like this:
.container {
box-sizing: border-box;
position: relative;
width: 35.5%;
height: 600px;
background-color: coral;
padding: 20px;
}
.info {
align-items: center;
justify-content: center;
display: flex;
flex-direction: column;
margin: 0 0 15px 0;
}
.wrapper {
position: relative;
align-items: center;
justify-content: center;
display: flex;
flex-direction: column;
height: 450px;
top: 50%;
left: 0;
transform: translateY(-50%);
background-color: cadetblue;
width: 100%;
}
.header {
position: absolute;
top: 0;
left: 50%;
margin-top: 0;
transform: translateX(-50%);
background-color: cadetblue;
}
p {
margin: 0.5rem;
}
<div class="container">
<h1 class="header">INFO</h1>
<div class="wrapper">
<div class="info">
<h2>Age</h2>
<p>20</p>
</div>
<div class="info">
<h2>Adress</h2>
<p>Wolna 23, Warszawa</p>
</div>
<div class="info">
<h2>Email</h2>
<p>lorem#gmail.com</p>
</div>
<div class="info">
<h2>Phone</h2>
<p>669 133 777</p>
</div>
</div>
</div>

How to center these divs correctly? [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How can I horizontally center an element?
(133 answers)
How can I vertically center a div element for all browsers using CSS?
(48 answers)
Closed 2 years ago.
I have tried to center my divs like described on this website but something is wrong and I don't get it. Maybe you know what the problem is, because I can't find a proper solution to center the divs in the exact middle. I have added just one css input description because the others are the same.
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
I want to offer two options - flex and table. Wrap in two parent divs (an additional parent div is needed so that the top label is left-aligned and centered at the same time), and add flex rules to the topmost parent div. Did you need it?
flex:
.center_flex {
display: flex;
justify-content: center;
}
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div class="center_flex">
<div class="center_flex_container">
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
</div>
</div>
For a table, it is enough to wrap it in only one parental div, setting the margin rules:
.center_table {
display: table;
margin-left: auto;
margin-right: auto;
}
.registerBox {
position: relative;
height: 80%;
justify-content: center;
align-items: center;
}
.center {
position: relative;
height: auto;
justify-content: center;
align-items: center;
}
.centergui {
justify-content: center;
align-items: center;
}
//text styles
.h1 {
margin: auto;
justify-self: center;
text-align: center;
}
input[id="usernametext"] {
z-index: 2;
border-radius: $textinputradius;
background-color: $textinputbackground;
font-size: $textinputfontsize;
#include respond-to(medium-devices) {width: 125px;}
#include respond-to(wide-screens) {float: none;}
}
input[id="usernametext"],input[type="password"],input[id="emailtext"],input[id="phonenumbertext"], textarea, select {
outline: #272727;
border: none;
color: #ececec;
}
<div class="center_table">
<div className={"h1"}> <h1>Register</h1> </div>
<div className={"flexCenter"}>
<div className={"registerBox"}>
<div className={"center"}><input type={"text"} id={"usernametext"} placeholder={"Username"}/></div>
<div className={"center"}><input type={"text"} id={"emailtext"} placeholder={"Email-Address"}/></div>
<div className={"center"}><input type={"password"} id={"passwordtext"} placeholder={"Password"}/></div>
<div className={"center"}><input type={"text"} id={"phonenumbertext"} placeholder={"Phonenumber(optional)"}/></div>
</div>
</div>

Centering a search field inside a flex container while keeping a logo to the right? [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 3 years ago.
Trying to center a search field (using flex box) inside a toolbar so that it aligns perfectly with cards contained inside a container below the toolbar.
The yellow search field should be align with the orange cards below it.
The logo is supposed to be self aligned to the left and the menu should be self aligned to the right. I thought justify-self: start | end would do this ...
This is a demo:
h1, h2 {
font-family: Lato;
}
body {
margin: 0px;
}
main {
display: flex;
margin: 0 auto;
}
.Toolbar {
display:flex;
align-items: center;
height: 3.5rem;
width: 100%;
background-color: lightgrey;
}
.Logo {
justify-self: start;
margin-left:10px;
width: 40px;
height:40px;
background-color: orange;
}
.SearchField
{
justify-self:center;
width: 200px;
height:40px;
background-color: yellow;
}
.Menu {
justify-self: end;
width: 40px;
height:40px;
background-color: orange;
}
.Card {
width: 200px;
height:200px;
margin: 1rem;
background-color: orange;
}
.CardContainer {
display: flex;
margin: 0 auto;
flex-direction:column;
}
<div id="app"></div>
<div class="Toolbar">
<div class="Logo">
Logo
</div>
<div class="SearchField">
Search Field
</div>
<div class="Menu">
Menu
</div>
</div>
<main>
<div class="CardContainer">
<div class="Card">
</div>
<div class="Card">
</div>
</div>
</main>
This is as close as you're gonna get it.
By the way, you can only use align-self in flex children, there is no justify-self.
h1, h2 {
font-family: Lato;
}
body {
margin: 0px;
}
main {
display: flex;
align-items: center;
flex-direction: column;
}
.Toolbar {
display:flex;
align-items: center;
justify-content: space-between;
height: 3.5rem;
width: 100%;
background-color: lightgrey;
}
.Logo {
width: 40px;
height:40px;
background-color: orange;
}
.SearchField
{
width: 200px;
height:40px;
background-color: yellow;
}
.Menu {
width: 40px;
height:40px;
background-color: orange;
}
.Card {
width: 200px;
height:200px;
margin: 1rem;
background-color: orange;
}
.CardContainer {
display: flex;
flex-direction: column;
}
<div id="app">
<div class="Toolbar">
<div class="Logo">
Logo
</div>
<div class="SearchField">
Search Field
</div>
<div class="Menu">
Menu
</div>
</div>
<main>
<div class="CardContainer">
<div class="Card">
</div>
<div class="Card">
</div>
</div>
</main>
</div>

Align center and right with flex?

I'm trying to horizontally center align group1 and right align group2 with flex.
<div class="holder">
<div class="group1">
...
</div>
<div class="group2">
...
</div>
</div>
CSS:
.holder{
display: flex;
justify-content:center;
}
.group2{
align-self: flex-end;
}
But everything is just centered, what's the fix?
As flexbox is built on manipulation of margins there is no method of moving one item out of the flow other than using position:absolute.
Any other method leaves the center item affected by other elements.
.holder {
display: flex;
justify-content: center;
border: 1px solid grey;
position: relative;
}
.group1 {
background: plum;
}
.group2 {
position: absolute;
right: 0;
top: 50%;
transform: translateY(-50%);
border: 1px solid red;
}
<div class="holder">
<div class="group1">
Group 1
</div>
<div class="group2">
Group 2
</div>
</div>
Immagine you have a button with a text and a icon and you want the text on the center and the icon on the right. Don't forget to give a width to the button.
With flexbox is really simple to do it.
button {
display: flex;
justify-content: center;
align-items: center;
flex: 1;
width: 400px;
order: 1;
}
span {
flex: 1;
}
<button>
<span>
Click me
</span>
<i>></i>
</button>
We can achieve it using flex, grid and position.
Check out the solution for flex, grid as position has already been submitted above 👆🏻
Flex Solution:
.holder {
display: flex;
}
.group1 {
flex: 1;
text-align: center;
}
<div class="holder">
<div class="group1">...</div>
<div class="group2">.....</div>
</div>
Grid Solution:
.holder {
display: grid;
grid-template-columns: 1fr 0fr;
}
.group1 {
flex: 1;
text-align: center;
border: 1px solid green;
}
.group2 {
border: 1px solid pink;
}
<div class="holder">
<div class="group1">...</div>
<div class="group2">.....</div>
</div>
This is the best what you can do with just Flexbox
.holder{
display: flex;
justify-content: center;
}
.group1 {
flex: 1;
text-align: center;
}
.group2{
margin-left: auto;
}
<div class="holder">
<div class="group1">
Center
</div>
<div class="group2">
Right
</div>
</div>
Better option is to use relative/absolute position with Flexbox
.holder{
display: flex;
justify-content: center;
color: white;
position: relative;
}
.group1 {
flex: 1;
text-align: center;
background: black;
}
.group2{
position: absolute;
top: 0;
right: 0;
}
<div class="holder">
<div class="group1">
Center
</div>
<div class="group2">
Right
</div>
</div>
You can set a height in the .holderand the play with align-self and text-align:
CSS
.holder{
display: flex;
justify-content:center;
align-content: center;
height: 200px;
background: #ccc;
}
.group2{
align-self: center;
text-align: right;
width: 50%;
}
.group1{
align-self: center;
width: 50%;
text-align: center;
}
DEMO HERE