How to center horizontally and vertically headings (flexbox)? - html

I don't know how to put <h1> heading exactly on center of the page using a flexbox.
Here is a link: https://jsbin.com/movevezesi/edit?html,css,output
Desired effect: https://tutorialzine.com/media/2016/06/landing-page-hero.jpg
HTML:
<div class="wrap">
<header class="header">
<a class="logo" href="#">logo</a>
<ul class="nav">
<li>Features</li>
<li>Pricing</li>
<li>Sign In</li>
<li>Free Trial</li>
</ul>
</header>
<div class="hero">
<h1>how to center horizontally and vertically this text ???</h1>
<h2>any</h2>
<h3>ideas???</h3>
</div>
</div>
css:

You'll want to nest your flex boxes and change their direction to column. I put together an example using flexbox if you want to continue using that instead of a position 50% hack:
.wrap {
display: flex;
/*
flex-direction: column keeps the nav and
hero in vertical alignment
*/
flex-direction: column;
border: 1px solid red;
height: 500px;
}
.hero {
display: flex;
/*
again, using flex-direction:column to keep
the nested headers in vertical alignment
*/
flex-direction: column;
/*
flex-grow tells .hero to grow along the main
flex axis of its parent. in this case we set
wrap to flex-direction:column, so .hero stretches
vertically
*/
flex-grow: 1;
/*
justify-content sets the children's layout
along the parent's main axis. in this case
the headers will group up in the vertical middle
of .hero
*/
justify-content: center;
/*
align-items sets the children's layout perpendicular
to the parent's main axis. so in this case they'll bunch up
along the horizontal center
*/
align-items: center;
border: 1px solid red;
}
.hero > * {
border: 1px solid black;
/*
without this text-align, the headers would
be centered horizontally, but the text inside those
headers would still be left-aligned
*/
text-align: center;
}
<div class="wrap">
<header class="header">
<a class="logo" href="#">logo</a>
<ul class="nav">
<li>Features</li>
<li>Pricing</li>
<li>Sign In</li>
<li>Free Trial</li>
</ul>
</header>
<div class="hero">
<h1>how to center horizontally and vertically this text ???</h1>
<h2>any</h2>
<h3>ideas???</h3>
</div>
</div>

You could use the next class. It will center horizontally and vertically the element.
.centerElement{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="hero centerElement">
<h1>how to center horizontally and vertically this text ???</h1>
<h2>any</h2>
<h3>ideas???</h3>
</div>
Hoping it helps.

Related

Center one div and have 2 other divs stay by it? [duplicate]

This question already has answers here:
Align 3 unequal blocks left, center and right
(4 answers)
Closed last month.
I have 3 divs that contain text. The text has different lengths, meaning that if I put these three divs into another div, if I justify-content: center this div, the middle one out of the three divs won't be in the center of the page.
What I want to achieve is to have the middle div in the center of the webpage and the first div to be to left of it and the third one to the right, with no space between (except padding).
Edit: I don't know if I maybe was unclear about what I am asking: I want to center .container-footer-3 but .footer-company ("Center") should be in the center of the page, not in the center of the div.
-> In other words, how do I center the parent div, containing all these 3 divs but not the center of the div to the center page, instead "Center" div to the center of the page?
.container-footer-3 .nav {
display: inline-table;
padding: 0 20px 0 20px;
}
.footer-nav {
display: flex;
justify-content: center;
}
.footer-company{
position: absolute;
left: 50%;
margin-left: -50px;
width: 100px;
height: 100px}
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left</h20>
<ul>
</ul>
</div>
<div class="footer-company nav">
<h20>Center</h20>
<ul>
</ul>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight</h20>
<ul>
</ul>
</div>
</div>
</div>
If you want to use flex, then you can make all three divs share the same width by adding flex-basis: 100% to the children within the flex container. I simplified your HTML to better show how it can be done. 100% just states that they are all equally treated. It's not like 100% of width.
You will then need to add an extra div inside .footer-explore if you want to align it to the right.
Do note, I added the outline to make the alignments more clear.
.footer-nav {
display: flex;
justify-content: center;
align-items: top;
}
.nav {
padding: 0 20px 0 20px;
flex-basis: 100%;
}
* {
outline: 1px solid grey;
}
<div class="footer-nav">
<div class="footer-explore nav">
<h20>Left</h20>
</div>
<div class="footer-company nav">
<h20>Center</h20>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight RightRightRightRight</h20>
</div>
</div>
You can also use a grid, and set the middle column to adapt to it's content, and the two divs on the side to fill up the rest of the space (using 1fr). Again, you need to add an extra div inside .footer-explore if you want to align it to the right. I added an outline here too for clarity.
.footer-nav {
display: grid;
grid-template-columns: 1fr min-content 1fr;
}
.nav {
padding: 0 20px 0 20px;
}
* {
outline: 1px solid grey;
}
<div class="footer-nav">
<div class="footer-explore nav">
<h20>Left</h20>
</div>
<div class="footer-company nav">
<h20>Center</h20>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight RightRightRightRight</h20>
</div>
</div>
There's already a lot of answers, but I figured I'd give my 2 cents seeing as none of the answers actually show an example where the leftmost and rightmost columns aren't expanded to full width.
In a grid-layout, you can define the 3 columns with:
grid-template-columns: minmax(max-content, 1fr) auto minmax(max-content, 1fr);
The first and the last column (left and right) will have their column stretch to fit its content width, while the layout stays a 3-column layout.
The middle column will just have a value of auto.
You can then specify each grid-item's positioning with justify-self, which works in both flex- and grid-layouts - I used flex in my example.
This solution isn't fully responsive, but you can specify e.g. a max-width-value on the grid-items which will make it responsive for most screens, not small screens such as mobile devices though.
.container-footer-3 {
display: grid;
grid-template-columns: minmax(max-content, 1fr) auto minmax(max-content, 1fr);
justify-items: center;
grid-gap: 40px;
}
.nav {
padding: 0 20px 0 20px;
border: 1px solid black;
display: flex;
flex-direction: column;
max-width: 150px;
}
.footer-explore {
justify-self: right;
}
.footer-company {
justify-self: center;
}
.footer-socials {
justify-self: left;
}
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left</h20>
<ul>
</ul>
</div>
<div class="footer-company nav">
<h20>Center</h20>
<ul>
</ul>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight</h20>
<ul>
</ul>
</div>
</div>
</div>
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left Left Left Left Left</h20>
<ul>
</ul>
</div>
<div class="footer-company nav">
<h20>Center Center Center</h20>
<ul>
</ul>
</div>
<div class="footer-socials nav">
<h20>Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right Right </h20>
<ul>
</ul>
</div>
</div>
</div>
#grid {
display: grid;
grid-template-rows: 1fr;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;
width: 100vw;
height: 100vh;
}
#div1 {
grid-area: 1 / 1 / 2 / 2;
background-color: rgba(29, 240, 35, 0.5);
}
#div2 {
grid-area: 1 / 2 / 2 / 3;
background-color: rgba(213, 209, 50, 0.5);
}
#div3 {
grid-area: 1 / 3 / 2 / 4;
background-color: rgba(158, 43, 127, 0.5);
}
<div id="grid">
<div id="div1">div1</div>
<div id="div2">div2</div>
<div id="div3">div3</div>
</div>
I don't know if that correspond exactly to what you are looking, but css grid could be your answer
A flex box solution is as follows:
The left and right div are allowed to grow using flex-grow:1; The center is given a flex-grow of 0 so it shrinks. If we set the left and right divs with a flex-basis of 100% then they'll expand to equal size. I've put a red line where the center of the screen is so you can see the center div is actually centered.
.container-footer-3 {
display: flex;
gap: 0.5rem;
}
.container-footer-3 > div {
flex-grow:1;
outline:1px solid blue;
flex-basis:100%;
}
.container-footer-3 > div:first-child {
text-align:right;
}
.container-footer-3 > div:nth-child(2) {
flex-grow:0;
flex-basis:0;
}
.centerline {
border:1px solid red;
height:50px;
width:0px;
position:absolute;
left:50%;
top:0;
}
<div class="footer-nav">
<div class="container-footer-3">
<div class="footer-explore nav">
<h20>Left</h20>
</div>
<div class="footer-company nav">
<h20>Center</h20>
</div>
<div class="footer-socials nav">
<h20>RightRightRightRight</h20>
</div>
</div>
</div>
<div class='centerline'></div>

aligning the bottom bar elements

Hi I am trying to align the bottombar elements so that they are in 2 columns on the side of 102. I was wondering if there is a way to fix it as they are all floating on the right at the moment. I am a beginner html css programmer and I am not very experienced yet. Ill appreciate any help you can give me!
CSS
/*bottom navbar*/
.bottomnav{
width: 100%;
background-color: rgb(248, 138, 180);
display: flex;
flex-direction: row;
}
.navbarlogo2{
display: block;
margin-left: auto;
margin-right: auto;
width: 10%;
text-decoration: none;
}
/*bottombar*/
.nav {
display: flex;
flex-direction: row;
}
.left, .right {
flex: 1;
}
HTML
<div class="bottomnav">
<ul class="bottomlogo">
<li class="navbarimg2"><img class="navbarlogo2" src="img/LOGO.png"></li>
</ul>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
</ul>
</div>
</div>
</div>
END RESULT
WANTED RESULT
I made things like that. CSS Grid is one of the new HTML5 standard you should take a look. In your case, use a grid is better choice against flex because you're looking for a table-like structure.
I choosed to split your needs in 2 parts:
Center your logo
Make a 2 columns grid for your links
Centering your logo
We need to center an element and prevent it to interfere with our incoming links grid. So we'll set our container with a position: relative and place the img tag in position: absolute. Note the image's top right bottom left properties are now relative to the first parent positioned as relative.
And so we only need to make some simple maths. Note the calc() function, we don't want to center the top left corner of your logo but the center. So we need to remove the half of the defined logo's width.
navbarlogo2 {
left: calc(50% - 60px);
}
Make a 2 columns grid for your links
In order make a grid, you have to display your container as grid and set its grid-template-columns to 1fr 1fr. You can translate fr with the word fraction. So here, we're asking for a row split in 2 fractions.
Because we want a place for our logo, we're adding a gap (grid-cap) in out container to make some space between our 2 columns.
Learn more about the fr unit here.
body {
margin:0
}
.bottomnav {
width: 100%;
background-color: rgb(248, 138, 180);
position: relative;
}
.navbarlogo2 {
display: block;
margin-left: auto;
margin-right: auto;
width: 120px;
text-decoration: none;
position: absolute;
filter: brightness(10);
top: 15px;
left: calc(50% - 60px) /*center top left corner then remove half logo width (120px)*/
}
/*bottombar*/
.nav {
display: grid;
grid-gap: 120px;
grid-template-columns: 1fr 1fr;
}
.nav ul {
padding-left: 0;
}
.nav ul li {
list-style: none;
text-align: center;
padding-left: 0;
}
.left,
.right {
flex: 1;
}
<div class="bottomnav">
<div class="bottomlogo">
<img class="navbarlogo2" src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg">
</div>
<div class='nav'>
<div class='left'>
<ul>
<li>About Us</li>
<li>Affiliates</li>
</ul>
</div>
<div class='right'>
<ul>
<li>TOS</li>
<li>Fourth </li>
</ul>
</div>
</div>
</div>

Centering the links in a horizontal navigation bar

First time messing around with a grid layout. Here is what I have so far:
http://fordsseafoodrockhall.com/marina/index.html#
My question is, how to I center the links in my Horizontal nav bar? As can be seen Im left with a little extra space on the right side then I am on the left.
HTML:
<div class="row">
<div class="twelve columns">
<ul class="nav-bar">
<li class="active">Home</li>
<li>Marina</li>
<li>Services</li>
<li>Amenities</li>
<li>Activities</li>
<li>Photos</li>
<li>Yacht Club</li>
<li>Directions</li>
<li>Contact Us</li>
</ul>
</div>
CSS:
.nav-bar {
height: 40px;
background: #435d78;
margin-top: 20px;
padding: 0;
color: #fff;
text-align:center; }
Add this to your existing code:
.nav-bar {
display: flex;
}
.nav-bar > li.active {
flex-grow: 1;
}
The flex-grow property distributes free space among sibling elements with the property applied.
Set display: flex on the parent in order to enable the use of flex properties.

Flexbox item within css grid cell appears to wrap

I'm new to using css grid, and I am trying to position two items within a single grid cell using flexbox to position the items. There is a logo on the left and a nav bar on the right, but the nav bar is not centered within cell "a", it appears to go below the lower boundary of cell "a" (I tried to upload a jpeg image, but Stack Overflow is having problems right now accepting image uploads, see Image upload fails with "imgur is rejecting the request").
Here is the html code:
<div class="a">
<div class="a_left">
Logo for Project
</div>
<div class="a_right">
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
</div>
Here is the css code:
.a{
display: grid;
font-family: sans-serif;
color: green;
font-size: 16pt;
}
.a_left{
display: flex;
text-align: left;
vertical-align: left;
flex-wrap: nowrap;
}
.a_right{
display: flex;
height: 100%;
flex-wrap: nowrap;
justify-content: right;
vertical-align: right;
}
.topnav {
align-content: right;
justify-content: center;
overflow: hidden;
background-color: #333;
height: 100%
}
The grid container is "a", and the nav bar is in the a_right flexbox. I have tried a lot of the likely height, width and centering properties without success, but I don't know if the property should be applied to a or to a_right.
Thanks for any help centering this nav bar.
If you want to have the logo on the left and menu on the right, you can simply do:
.a {
display: flex;
justify-content: space-between;
}
<div class="a">
<div class="a_left">
Logo for Project
</div>
<div class="a_right">
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
</div>
Flexbox is a container for centering its children - right now it looks like you're making the children into flexboxes, which won't do you a whole lot.
If you need to maintain the "grid" display property, add a container for both your logo and nav bar. This is the container that you will want with display:flex, and it will be the container that you apply your flex-related alignment properties to.
Also, to vertically align content in a flexbox, use align-items. Horizontal alignment requires justify-content.
So you set your element .a to be a grid container:
.a {
display: grid;
}
… and you want to know why the children (.a_left and .a-right) are stacking vertically instead of on the same row.
The reason is that you haven't defined explicit tracks with grid-template-columns or grid-template-areas. Because you haven't defined explicit columns, grid-auto-columns comes into play to create implicit columns.
The default value of grid-auto-columns is auto, which essentially allows each grid item to occupy an entire row. That's what you're seeing; it's like block elements stacking.
Try this instead:
.a {
display: grid;
grid-template-columns: auto 1fr; /* define explicit columns */
color: green;
font-size: 16pt;
}
.a_left {
display: flex;
}
.a_right {
display: flex;
justify-content: center;
}
.topnav {
background-color: yellow;
}
<div class="a">
<div class="a_left">Logo for Project</div>
<div class="a_right">
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
</div>
You may also want to read this post about centering elements on a row having other elements: Center and right align flexbox elements

I want to create the following menu style by css

You can see,
| Nav --- Logo --- Info |
This is the style I want on mobile.
Nav is left align.
Logo is center align.
Info is right align.
<div id="navbar">
<div class="item">Nav</div>
<div class="item">Logo</div>
<div class="item">Info</div>
</div>
What styles do I have to implement for each item to get what I want?
Please, Thanks.
By far the easiest way to achieve this is using flexbox. To limit it to mobile devices, you'd need to wrap it in a #media query, which, for Bootstrap, is below 768px
#navbar {
/*
* place non-mobile styles here
* For example, to hide the navbar outside mobile...
*/
display: none;
}
#media (max-width: 767px) {
#navbar {
display:flex;
justify-content: space-between;
align-items: center; /* optional, to center vertically */
}
}
<div id="navbar">
<div class="item">Nav</div>
<div class="item">Logo</div>
<div class="item">Info</div>
</div>
Use flexbox make the parent flex and space around the content.
Fiddle
.navbar{
max-width: 600px;
border: 1px solid black;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
}
<header class="navbar">
<div class="menu">
<ul>
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
</ul>
</div>
<div class="logo">
<img src="https://placehold.it/88x88" alt="logo">
</div>
<div class="info">
<p> Info about the website </p>
</div>
</header>