Flexbox - Vertically align both blocks and text - html

I want h2 with a button on the side like seen in the picture below:
I tried flexbox, but by centering the button, it doesn't fill full height anymore.
header {
display: flex;
background-color: blue;
color: white;
margin-bottom: 2em;
}
.header1 {
align-items: center;
}
.button {
margin-left: auto;
background-color: yellow;
}
<header class="header1">
<h2>Text centered but Button not full Height</h2>
<a class="button" href="https://google.com">Read More</a>
</header>
<header class="header2">
<h2>Button full height but Text not centered</h2>
<a class="button" href="https://google.com">Read More</a>
</header>
Is there a way to achieve as pictured? I want to avoid adding Padding or fixed height to the button because then I need to adapt it for multiple media queries.

Make your button a flexbox too and vertically align using align-items: center - see demo below:
header {
display: flex;
background-color: blue;
color: white;
margin-bottom: 2em;
}
.header1 {
align-items: center;
}
.button {
margin-left: auto;
background-color: yellow;
display: flex;
align-items: center;
}
<header class="header2">
<h2>Button full height but Text not centered</h2>
<a class="button" href="https://google.com">Read More</a>
</header>

header {
display: flex;
background-color: blue;
color: white;
margin-bottom: 2em;
}
.header1 { align-items: center; }
.button {
margin-left: auto;
background-color: yellow;
display:block;
vertical-align:middle;
line-height:30px;
position:relative;
padding:0 10px;
}
a span {
display:block;
height:50%;
margin-top:-15px;
}
<header class="header1">
<h2>Text centered but Button not full Height</h2>
<a class="button" href="https://google.com">Read More</a>
</header>
<header class="header2">
<h2>Button full height but Text not centered</h2>
<a class="button" href="https://google.com"><span></span>Read More</a>
</header>

Related

right align link items containing image and text along navbar

I would like to create a right aligned navbar. Each link item should contain an image and a text. When having text only this code works fine
#navbar {
height: 60px;
display: grid;
justify-content: end;
padding: 20px;
background: red;
align-items: center;
}
a {
text-decoration: none;
margin: 0 10px;
color: white;
background: black;
}
<div id="navbar">
<div>
Home
About
Projects
Contact
Imprint
</div>
</div>
Now I added images and texts to the link item and built a wrapper around these elements.
#navbar {
height: 60px;
display: grid;
justify-items: end;
padding: 20px;
align-items: center;
background: red;
}
#navbarLinkContainer {
margin: 0 60px;
}
.navbarItemContainer {
text-align: center;
text-decoration: none;
margin: 0 10px;
}
.navbarItemImg {
width: 64px;
height: 64px;
}
<div id="navbar">
<div id="navbarLinkContainer">
<a class="navbarItemContainer" href="/">
<div>
<img class="navbarItemImg" src="https://discordapp.com/assets/19654c38399b0e75c351d6fc960fe0ca.svg">
<div class="navbarItemTitle">Home</div>
</div>
</a>
<a class="navbarItemContainer" href="/about">
<div>
<img class="navbarItemImg" src="https://discordapp.com/assets/19654c38399b0e75c351d6fc960fe0ca.svg">
<div class="navbarItemTitle">About</div>
</div>
</a>
<a class="navbarItemContainer" href="/projects">
<div>
<img class="navbarItemImg" src="https://discordapp.com/assets/19654c38399b0e75c351d6fc960fe0ca.svg">
<div class="navbarItemTitle">Projects</div>
</div>
</a>
</div>
</div>
Unfortunately the CSS breaks now. How can I fix it so that each link item is aligned to the right, centered vertically and is placed along the navbar?
I achieved it using flexbox
https://jsfiddle.net/q48tyuac/
but maybe there is a better solution using the grid attribute. Because when you lower the screen size these elements are fixed and I would like them to break to the next row if there is no space left.
use flex-wrap:wrap;
#navbar {
/* height: 60px; */
display: flex;
justify-content: flex-end;
padding: 20px;
flex-wrap:wrap;
align-items: center;
background: red;
/* margin: 0 60px; */
}
https://jsfiddle.net/6p03s5cm/
if you dont want to use flexbox with grid you can target #navbarLinkContainer and do it with columns
#navbar {
display: grid;
justify-items: end;
padding: 20px;
align-items: center;
background: red;
}
#navbarLinkContainer {
display:grid;
grid-template-columns:repeat(3 , 1fr) ;
}
.navbarItemContainer {
text-align: center;
text-decoration: none;
margin: 0 10px;
}
.navbarItemImg {
width: 64px;
height: 64px;
}
<div id="navbar">
<div id="navbarLinkContainer">
<a class="navbarItemContainer" href="/">
<div>
<img class="navbarItemImg" src="https://discordapp.com/assets/19654c38399b0e75c351d6fc960fe0ca.svg">
<div class="navbarItemTitle">Home</div>
</div>
</a>
<a class="navbarItemContainer" href="/about">
<div>
<img class="navbarItemImg" src="https://discordapp.com/assets/19654c38399b0e75c351d6fc960fe0ca.svg">
<div class="navbarItemTitle">About</div>
</div>
</a>
<a class="navbarItemContainer" href="/projects">
<div>
<img class="navbarItemImg" src="https://discordapp.com/assets/19654c38399b0e75c351d6fc960fe0ca.svg">
<div class="navbarItemTitle">Projects</div>
</div>
</a>
</div>
</div>

Css grid layout issues

I need help with my grid layout for a free code camp project.
Basically, I'd like to show 3 of my portfolio pages in a row. I setup a grid layout for this and can't seem to get the middle page to lineup with the others. Also, as I am brand new, feel free to give feedback on what I have so far in general.
here is the link to the codepen just in case https://codepen.io/eddiepearson/pen/xMaaYX
* {
#import url('https://fonts.googleapis.com/css?family=Roboto+Mono:300,300i,400');
}
html, body {
margin: 0;
padding: 0;
}
nav ul {
text-align: right;
position: fixed;
margin-top: 0;
width: 100%;
background-color: #002171;
}
nav ul li {
display: inline-block;
margin: 55px;
margin-bottom: 15px;
margin-top: 30px;
}
nav ul li a {
color: #fff;
text-decoration: none;
}
#media only screen and (max-width: 500px) {
nav ul {
text-align: left;
height: 75px;
}
nav ul li {
margin: 20px;
height: 5px;
}
}
.intro {
top: 0;
background: #002171;
min-height: 55vh;
padding-top: 45vh;
}
.intro p {
text-align: center;
color: #fff;
}
.intro h1 {
text-align: center;
color: #fff;
}
.work {
margin-top: 50px;
}
.work-header {
text-align: center;
}
#projects {
display: grid;
grid-template-columns: 300px 300px 300px;
grid-row-columns: 300px 300px;
justify-content: space-evenly;
}
#third-p {
}
.project-pic {
width: 100%;
}
.project-title {
text-align: center;
}
<nav>
<ul style="list-style-type: none" id="navbar">
<li>About</li>
<li>Work</li>
<li>Contact</li>
</ul>
</nav>
<section>
<div id="welcome-section" class="intro">
<h1>Hey i'am eddie</h1>
<p>a web dev with a focus on UX</p>
</div>
</section>
<section>
<h2 class="work-header">These are some of my projects.</h2>
<div id="projects" class="work">
<div id="first-p">
<a href="https://codepen.io/eddiepearson/pen/vbxQEp" target="_blank" class="project project-box">
<img class="project-pic" src="https://previews.dropbox.com/p/thumb/AAU0aRvU53Ban4nVNY5N70nno6nDVvhkDsD0qSzP0NYsVh20CPfm-jFQB4GrArV09A9eVa8YUpJqpQJDdBaHnyJ24GAfLey4u1qdJZ5gp2JY4WF-DkfnXfIawSA8n7jronkkUR_mT9xH5sFDTm0jagwpWpM93tn_zZs8c62-3c9fAQKvFmvjqyOjFenQsBgK5XUG62avpwvwjGtSf0IWMiXUrXUWhJIl2wFc3L4UK4z-Hw/p.png?size_mode=5" alt="project-pic">
<div class="project-title">
Tribute Page
</div>
</div>
</a>
<div id="second-p">
<a href="https://codepen.io/eddiepearson/pen/vbxQEp" target="_blank" class="project project-box">
<img class="project-pic" src="https://previews.dropbox.com/p/thumb/AAU0aRvU53Ban4nVNY5N70nno6nDVvhkDsD0qSzP0NYsVh20CPfm-jFQB4GrArV09A9eVa8YUpJqpQJDdBaHnyJ24GAfLey4u1qdJZ5gp2JY4WF-DkfnXfIawSA8n7jronkkUR_mT9xH5sFDTm0jagwpWpM93tn_zZs8c62-3c9fAQKvFmvjqyOjFenQsBgK5XUG62avpwvwjGtSf0IWMiXUrXUWhJIl2wFc3L4UK4z-Hw/p.png?size_mode=5" alt="project-pic">
<div class="project-title">
Tribute Page
</div>
</div>
</a>
<div id="third-p">
<a href="https://codepen.io/eddiepearson/pen/vbxQEp" target="_blank" class="project project-box">
<img class="project-pic" src="https://previews.dropbox.com/p/thumb/AAU0aRvU53Ban4nVNY5N70nno6nDVvhkDsD0qSzP0NYsVh20CPfm-jFQB4GrArV09A9eVa8YUpJqpQJDdBaHnyJ24GAfLey4u1qdJZ5gp2JY4WF-DkfnXfIawSA8n7jronkkUR_mT9xH5sFDTm0jagwpWpM93tn_zZs8c62-3c9fAQKvFmvjqyOjFenQsBgK5XUG62avpwvwjGtSf0IWMiXUrXUWhJIl2wFc3L4UK4z-Hw/p.png?size_mode=5" alt="project-pic">
<div class="project-title">
Tribute Page
</div>
</div>
</a>
</div>
</section>
First, you have a small issue (typo I assume) here in the CSS
grid-template-columns: 300px 300px 300px;
grid-row-columns: 300px 300px;
Shouldn't that last part be grid-template-rows?
Also you could use this to manually control each element in the grid:
#first-p {
grid-row:1;
grid-column:3;
}

How to align items to left (header)

I'm trying to align some items to the left, but I don't know what I'm doing wrong.
I'm making the youtube header, and at this time, I've done this
I got to align the first two items to the right with a simple padding, but when it comes to the left side of the header, nothing works. I've used align-items but they do not move.
this is my html code:
header {
width: 100%;
padding: 0px;
/*FLEX*/
display: flex;
flex-direction: row;
border-bottom: 1px solid #000;
}
header .youtube nav a span {
color: red;
padding: 5px;
line-height: 50px;
}
header .menu nav a span {
padding: 30px;
line-height: 50px;
}
header .bloque nav a span {
line-height: 50px;
align-items: flex-end;
}
header .bloque nav img {
line-height: 50px;
align-items: flex-end;
width: 50;
}
<header>
<div class="menu">
<nav>
<span class="icon-menu"></span>
</nav>
</div>
<div class="youtube">
<nav>
<span class="icon-youtube"></span>YouTube
</nav>
</div>
<form>
<fieldset>
<input type="search" placeholder="Search">
<button type="submit">
<span class="icon-magnifying-glass"></span>
</button>
</fieldset>
</form>
<div class="bloque">
<nav>
<span class="icon-camera"></span>
<span class="icon-grid"></span>
<span class="icon-forward"></span>
<span class="icon-globe"></span>
<img src="guía.png" width="100">
</nav>
<div/>
</header>
when I use justify-content:Space-round, the items are placed the way I want to, but not properly spaced between them. But for some strange reason when I use align-item:flex-end they are arranged like in the pic related. how do I solve this? any advice?
To align items within the header tag you need to add justify-content: flex-end; to the header to read more about it Check this site
header {
width: 100%;
padding: 0px;
/*FLEX*/
display: flex;
flex-direction: row;
justify-content: flex-end;
/* justify-content: flex-end;*/ /*uncomment to align left*/
align-items: center; /* to vertical align items */
border-bottom: 1px solid #000;
}
header .youtube nav a span {
color: red;
padding: 5px;
line-height: 50px;
}
/* header .menu nav a span {
padding: 30px;
line-height: 50px;
} */
header .bloque nav a span {
line-height: 50px;
align-items: flex-end;
}
header .bloque nav img {
line-height: 50px;
align-items: flex-end;
width: 50;
}
<header>
<div class="menu">
<nav>
<span class="icon-menu"></span>
</nav>
</div>
<div class="youtube">
<nav>
<span class="icon-youtube"></span>YouTube
</nav>
</div>
<form>
<fieldset>
<input type="search" placeholder="Search">
<button type="submit">
<span class="icon-magnifying-glass"></span>
</button>
</fieldset>
</form>
<div class="bloque">
<nav>
<span class="icon-camera"></span>
<span class="icon-grid"></span>
<span class="icon-forward"></span>
<span class="icon-globe"></span>
<img src="guía.png" width="100">
</nav>
<div/>
</header>

Navbar with Title in center and buttons to right

So I'm attempting to create a navbar with a title in the center and a button that appears to the right. As you can see, when I attempt to do it, the button appears on the next line and out of the div:
Fiddle
.title-bar {
background-color: #48A6B8;
font-style: italic;
margin-bottom: 3%;
color: #fff;
}
.title {
text-align: center;
font-size: 36px;
line-height: 180%;
}
.buts {
float: right;
}
<div class="title-bar">
<div class="title">Title</div>
<div class="button">
<button class="buts">Whats Up</button>
</div>
</div>
display:inline-block seems to remove the centering of the text so I can't use that...
Thanks in advance!
Use flexbox
flex: 1 0 auto; will make title flexible, it will acquire all available free space in flex-container.
.title-bar {
background-color: #48A6B8;
font-style: italic;
margin-bottom: 3%;
color: #fff;
display: flex;
}
.title {
text-align: center;
font-size: 36px;
line-height: 180%;
flex:1 0 auto;
}
<div class="title-bar">
<div class="title">Title</div>
<div class="button">
<button class="buts">Whats Up</button>
</div>
</div>
Edit: after #burak's comment that it's not in exactly center.
As you can see in the below image, some of the space is acquired by the Whats Up button, that's why title is not in the exact center but Text is in the exact center of it's parent.
We can make it in exact center as well, but for that we'll need to shift Whats Up button on another layer, by using position:absolute
.title-bar {
background-color: #48A6B8;
font-style: italic;
margin-bottom: 3%;
color: #fff;
display: flex;
position:relative;
}
.title {
text-align: center;
font-size: 36px;
line-height: 180%;
flex:1 0 auto;
background:rgba(0,0,0,.5)
}
.button{
position:absolute;
right:0;
top:0;
}
<div class="title-bar">
<div class="title">Title</div>
<div class="button">
<button class="buts">Whats Up</button>
</div>
</div>
I used a flexbox for the title bar and let the required margin at the left be calculated automatically.
.title-bar {
display: flex;
justify-content: center;
width: 100%;
}
.title, .button {
margin-left: auto;
}
<div class="title-bar">
<div class="title">Title</div>
<div class="button">
<button class="buts">Whats Up</button>
</div>
</div>
Use display:inline; so that title and button both will appear on same line.
.title-bar {
background-color: #48A6B8;
font-style: italic;
margin-bottom: 3%;
color: #fff;
display: flex;
position:relative;
}
.title {
text-align: center;
font-size: 36px;
line-height: 180%;
flex:1 0 auto;
}
.buttonRight{
position:absolute;
right:0;
top:0;
padding:5px;
}
<div class="title-bar">
<div class="title">Title</div>
<div class="buttonRight">
<button class="btn btn-primary">Whats Up</button>
</div>
</div>

HTML/CSS: horizontally center text after reflow

I'm sure this has been answered before, but I can't seem to find it anywhere.
I have a toolbar with links:
Once I resize the browser, text reflows:
The question is how do I center that text after the reflow? text-align doesn't help here, neither does margin: 0 auto.
The HTML looks like this:
<div class="desktop">
<div>
<a class="root-nav-links" href="/details">Event Details</a>
</div>
<div>
<a class="root-nav-links" href="/details">...</a>
</div>
...
</div>
CSS:
.desktop {
display: flex;
align-items: center;
}
.root-nav-links {
margin: 0 15px;
padding: 2px 6px;
}
Use display: block & text-align: center property on .root-nav-links, like:
.root-nav-links {
display: block;
text-align: center;
margin:0 15px;
padding:2px 6px;
}
Have a look at the snippet below (try compressing the browser width):
.desktop {
display: flex;
align-items: center;
background: #6440C0;
padding: 10px 0;
}
.root-nav-links {
text-decoration: none;
display: block;
text-align: center;
margin: 0 15px;
padding: 2px 6px;
color: white;
}
.root-nav-links:hover {
color: white;
text-decoration: underline;
}
body {
margin: 0;
}
<div class="desktop">
<div>
<a class="root-nav-links" href="/eventdetails">Event Details</a>
</div>
<div>
<a class="root-nav-links" href="/schedule">Schedule</a>
</div>
<div>
<a class="root-nav-links" href="/floorplan">Floor Plan</a>
</div>
<div>
<a class="root-nav-links" href="/details">Directory</a>
</div>
</div>
Hope this helps!
div -> text-align: center
links -> block
.desktop {
display: flex;
align-items: center;
align-content: center;
}
.desktop div {
text-align: center;
}
.root-nav-links {
display: block;
margin: 0 15px;
padding: 2px 6px;
}
<div class="desktop">
<div>
<a class="root-nav-links" href="/details">Event Details</a>
</div>
<div>
<a class="root-nav-links" href="/details">...</a>
</div>
</div>
.desktop {
display: flex;
align-items: center;
}
.root-nav-links {
margin: 0 15px;
padding: 2px 6px;
display: inline-block;
text-align: center;
}
<div class="desktop">
<div>
<a class="root-nav-links" href="/details">Event Details</a>
</div>
<div>
<a class="root-nav-links" href="/details">Long Long Word</a>
</div>
</div>
It looks like treating each element as an inline block and adding align-center to them should center the text when the words are forced onto multiple lines. Based on the comments I'm now less certain this is what you were asking about.
It should be like this:
Html
<div class="desktop">
<div class="extra-div">
<a class="root-nav-links" href="/details">Event Details</a>
</div>
</div>
Css:
.extra-div {
display:flex;
align-items: center;
}
.root-nav-links {
margin: 0 15px;
padding: 2px 6px;
}
jsfiddle: https://jsfiddle.net/622L0mqz/