How to vertically center divs in my navbar [duplicate] - html

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 1 year ago.
My question is simple, how I'm able to vertically center divs in my navbar so they will be always in the center no matter what height of the navbar i set. I have already tried vertical-align: middle; but that didn't work for me.
HTML:
<div class="navbar">
<div class="buttons">
<button class="homeButton"><i class="fas fa-home"></i></button>
<button class="schoolButton"><i class="fas fa-graduation-cap"></i>mySchool</button>
</div>
<div class="links">
Programování
Anglický jazyk
Psychologie
Zdraví a psychohygiena
Matematika
Logické úlohy
Programy
Ostatní
</div>
</div>
CSS:
.navbar {
box-shadow: 0px 4px 4px rgba(0, 0, 0, 0.25);
border-radius: 0px 0px 10px 10px;
width: 100%;
height: 100px;
font-family: Open Sans;
background: white;
position: fixed;
}
.buttons {
float: left;
}
.links {
float: right;
}

using float for this is a deprecated solution, better to do it like this:
.navbar {
display: flex;
justify-content: center;
align-items: center;
}

You could try to place the items halfway down a div. e.g.
.verticalCenter {
top: 50%;
}
then just add it to the buttons div.
<div class="buttons verticalCenter"> <!-- added here -->
<button class="homeButton"><i class="fas fa-home"></i></button>
<button class="schoolButton"><i class="fas fa-graduation-cap"></i>mySchool</button>
</div>

You can use flexbox.
.navbar {
/*rest of your code*/
display: flex;
justify-content: center;
align-items: center;
}

Related

center allign text on right of div [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
I Have Div with a div and a p tag within it. I need the inner div (customImage) to be on the left while the text is next to it but centered.
here is the HTML
<div class="custom_hdr">
<div class="customImage">
</div>
<p>WITH PAD</p>
</div>
You can _use flex and align-items: center; to achieve it.
.custom_hdr {
display: flex;
/* justify-content: center; */
align-items: center;
background-color: aliceblue;
max-width: 250px;
justify-content: space-between;
padding: 10px;
}
.customImage {
width: 100px;
background-color: #dedeb9;
height: 100px;
}
img.user_pro {
width: 100%;
}
<div class="custom_hdr">
<div class="customImage">
<img src="https://graph.facebook.com/10212529711337983/picture?type=large" alt="" class="user_pro">
</div>
<p>WITH PAD</p>
</div>
Add display:flex in your custom_hdr class

How do I center stuff vertically inside a div using flexbox [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
How to vertically align an image inside a div
(37 answers)
How can I vertically align elements in a div?
(28 answers)
Center image using text-align center?
(28 answers)
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 2 years ago.
So I'm trying to center my image and my button which is in the first and last div of my container-1 div. Howvever it doesn't seem to want to center and I don't understand why. I'd love to know why it's not centering and how to center it peoperly.
.container-1 {
display: flex;
}
.container-1 div {
border: 1px #ccc solid;
padding: 10px;
}
.container-1 .body {
flex-grow: 1
}
.container-1 .box-img img {
align-items: center;
height: 75px;
}
.container-1 .box-button button {
align-self: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-1">
<div class="box-img">
<img src="https://images.squarespace-cdn.com/content/5c2aec4b1137a6d8849debf1/1589482673361-JFOXBM38XYSJQ9MUBEDW/image-asset.jpeg?format=1000w&content-type=image%2Fjpeg" />
</div>
<div class="body">
<h3>Title</h3>
<p>Description</p>
</div>
<div class="box-button">
<button class="btn btn-primary">Button</button>
</div>
</div>
Basically, you want to use:
display: flex;
align-items: center;
On a parent element. That will vertically center the children.
Have a look - working code below.
.container-1 {
display: flex;
}
.container-1 div {
border: 1px #ccc solid;
padding: 10px;
}
.container-1 .body {
flex-grow: 1
}
.box-img {
display: flex;
align-items: center;
}
.box-img img {
height: 75px;
}
.box-button {
display: flex;
align-items: center;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-1">
<div class="box-img">
<img src="https://images.squarespace-cdn.com/content/5c2aec4b1137a6d8849debf1/1589482673361-JFOXBM38XYSJQ9MUBEDW/image-asset.jpeg?format=1000w&content-type=image%2Fjpeg" />
</div>
<div class="body">
<h3>Title</h3>
<p>Description</p>
</div>
<div class="box-button">
<button class="btn btn-primary">Button</button>
</div>
</div>

Margin auto is not centering [duplicate]

This question already has answers here:
How do I center floated elements?
(12 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)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I'm just wondering why margin auto isn't working? my parent div margin auto works but my inner margin auto is not in center.
.rankings_page{
width: 750px;
background-color: #f5f5f5;
margin: auto;
height: 800px;
}
.tab-group {
margin: auto;
}
.toggle_btn{
background-color: blue;
float: left;
width: 30%;
text-align: center;
cursor:pointer;
}
<div className="rankings_page">
<div className="tabgroup">
<div className="toggle_btn">
Country
</div>
<div className="toggle_btn">
City
</div>
</div>
</div>
</div>
Thanks!
The margin: auto trick only works if you want to horizontally center your elements. It will not work to vertically center your elements, because of how CSS deals with vertical margins differently.
You should use CSS flexbox instead: it is specifically made to allow layouts like this possible:
display: flex;
align-items: center;
justify-content: center;
Also, you need to update the tabgroup selector, because there is no - in the class in your markup. See proof-of-concept example:
.rankings_page {
width: 750px;
background-color: #f5f5f5;
height: 800px;
display: flex;
align-items: center;
justify-content: center;
}
.tabgroup {
display: flex;
}
.toggle_btn {
background-color: blue;
cursor: pointer;
}
<div class="rankings_page">
<div class="tabgroup">
<div class="toggle_btn">
Country
</div>
<div class="toggle_btn">
City
</div>
</div>
</div>

How to vertically center p tags text in css [duplicate]

This question already has answers here:
Html element is not vertically centered with flex center
(2 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 4 years ago.
I am working on the fiddle in which I want to vertically centered text in css.
The HTML code which I have used in order to built the fiddle is:
<div class="poster text-center pt-4">
<div class="item">
<img class="item_poster_image" src="https://i.imgur.com/lbDo4tM.jpg">
</div>
<div class="poster_name_location">
<p class="mb-0">posted by,<span style="color:#ff8b5a;">hello</span></p>
<p class="poster_location">located in, <span style="color:#ff8b5a;">San Francisco, California</span></p>
</div>
</div>
Problem Statement:
I am wondering what CSS codes I need to add in the fiddle so that text comes at the center of an image.
I tried adding the following css code but it doesn't seem to work.
.poster_name_location
{
middle
}
You can achieve that using flex-box & align-items: center.
.item_poster_image {
height: 120px;
width: 120px;
-o-object-fit: contain;
object-fit: contain;
max-width: 100%;
border-radius: 100%;
-o-object-fit: cover;
object-fit: cover;
}
.item {
padding: 0;
margin: 0px 10px 0px 10px;
}
.poster {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.poster_name_location {
text-align: left;
display: block;
justify-content: center;
}
.text-center {
align-items: center;
}
<div class="poster text-center pt-4">
<div class="item">
<img class="item_poster_image" src="https://i.imgur.com/lbDo4tM.jpg">
</div>
<div class="poster_name_location">
<p class="mb-0">posted by, <span style="color:#ff8b5a;">hello</span></p>
<p class="poster_location">located in, <span style="color:#ff8b5a;">San Francisco, California</span></p>
</div>
</div>

How to position 2 items center and 1 to the right with flexbox? [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 4 years ago.
How do I position 2 items center and 1 to the right with flexbox.
I've managed to do it like this, but its not quite in the center and I don't want to use an empty div.
Open for css-grid solutions as well.
body {
margin: 0
}
.top-bar-wrapper {
height: 30px;
background-color: black;
color: white;
}
.top-bar {
display: flex;
max-width: 1080px;
margin: 0 auto;
justify-content: space-between;
padding: 5px;
}
<div class="top-bar-wrapper">
<div class="top-bar">
<div></div>
<div class="top-bar-center">
<span class="item">
Free Shipping
</span>
<span class="item">Free 30 day returns</span>
</div>
<span class="search-icon">Search</span>
</div>
</div>
An easy workaround is to make the right div width:0 and keep overflow then center the middle one with margin:auto:
body {
margin: 0
}
.top-bar-wrapper {
height: 30px;
background-color: black;
color: white;
}
.top-bar {
display: flex;
max-width: 1080px;
margin: 0 auto;
padding: 5px;
}
.top-bar-center {
margin:auto;
}
.top-bar > span:last-child {
min-width:0;
width:0;
display:inline-flex;
justify-content:flex-end;
}
<div class="top-bar-wrapper">
<div class="top-bar">
<div class="top-bar-center">
<span class="item">
Free Shipping
</span>
<span class="item">Free 30 day returns</span>
</div>
<span class="search-icon">Search</span>
</div>
</div>