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>
Related
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>
Is it possible to locate the hamburger icon in the fixed menu bar?
I tried to put the element inside the class="sidenav" in the body tag but the I couldn't find the icon.
span {
font-size: 30px;
cursor: pointer;
position: absolute;
}
/* Style the navigation bar */
.navbar {
width: 100%;
margin-left: 0px;
background-color: #555;
overflow: auto;
}
/* Navbar links */
.navbar a {
float: left;
text-align: center;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
}
<body>
<div id="main">
<span onclick="openNav()">☰</span>
<div class="navbar">
Home
Search
Contact
Login
<input type="text" placeholder="Search.." name="search">
<button type="submit"><i class="fas fa-search"></i></button>
</div>
</div>
</body>
Expectation
Reality
Try this, I noticed a few things that can be improved on you code, for one, you can use Flexbox to align all items on a single row quite easily; removing the absolute position from the nav icon so everything behaves normally, this also lets you remove the float property. Then the you can wrap your links in another div (or a ul tag) and push them to right with margin-left: auto
span {
font-size: 30px;
cursor: pointer;
}
/* Style the navigation bar */
.navbar {
width: 100%;
background-color: #555;
display: flex;
align-items: center;
}
/* Navbar links */
.navlinks{
margin-left: auto;
}
.navbar a {
text-align: center;
padding: 12px;
color: white;
text-decoration: none;
font-size: 17px;
}
<body>
<div id="main">
<div class="navbar">
<span onclick="openNav()">☰</span>
<div class="navlinks">
Home
Search
Contact
Login
<input type="text" placeholder="Search.." name="search">
<button type="submit"><i class="fas fa-search"></i></button>
</div>
</div>
</div>
You can fix your issue with 2 lines on CSS:
#main {
display: flex;
justify-content: space-between;
}
http://jsfiddle.net/r24uyeq5/
You can set 'margin-left' a value equal to the width of the icon to navBar
.navbar {
width: 100%;
margin-left: 100px;//Icon width
background-color: #555;
overflow: auto;
}
I have an image ('repairPic') I would like to align to the right which is no problem, and two elements ('repairTitle & repairBtn') I would like to stack on top of each other on the left side. I would like to use flexbox because I think it's a great way to set up a site.
I'm pretty new to html and css and this is the first real project I'm working on so any help would be much appreciated!
Here's the HTML.
<!DOCTYPE html>
<html>
<head>
<title>Quandtico | Home</title>
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/styles.css">
</head>
<body>
<header id="top" class="main-header">
<div class="top-nav">
<ul class="info">
<li>quandticoguitars#gmail.com</li>
<li>810-304-2166</li>
<li id="f1"><img class="fbook" src="imgs/facebook-f-brands.svg"/></li>
</ul>
</div>
<div class="main-nav">
<ul class="nav">
<li><h2>Quandtico</h2></li>
<li id='home'><a href='#'>Home</a></li>
<li><a href='#'>Repair Rates</a></li>
<li><a href='#'>About</a></li>
<li><a href='#'>Custom Guitars</a></li>
</ul>
</div>
</header>
<div class="intro">
<h1 id="quandtico">QUANDTICO GUITARS & REPAIRS</h1>
<p>Looking for a new guitar? Need a repair done that you want done right? We’ve got it all here at Quandtico. Whether you’re looking for a custom built guitar, the highest quality repair, or just a tune up, we’ve got you covered. We’re located in the thumb of Michigan , and we make sure our customer receives only the best quality products and service. Contact us today to see what we’ve got in store for you!</p>
</div>
<div class="main">
<div class="repair">
<ul id="repairList">
<li class="repairTitle">
<h4>Repair</h4>
</li>
<li class="repairBtn">
<button>Repair Rates</button>
</li>
<li>
<img id="repairPic" src="imgs/bald-eagle-flying-water.jpg"/>
</li>
</ul>
</div>
<div class="build">
<h4>Build</h4>
<button class="build-btn">Custom Guitar</button>
<img id="buildPic" src="../"/>
</div>
<div class="best">
<img src="../"/>
<h4>Only The Best</h4>
</div>
</div>
<div class="contact">
<form action="index.html" method="post">
<h1>Contact</h1>
<fieldset>
<input placeholder="Name" type="text" id="name" name="user_name">
<input placeholder="Email" type="email" id="mail" name="user_email">
<input placeholder="Subject" type="text" id="subject" name="subject">
<textarea placeholder="Message" cols="50">
</textarea>
<button class="send">Send</button>
</fieldset>
</form>
</div>
<footer class="foot">
<p>quandticoguitars#gmail.com</p>
<img class="fbook" src="imgs/facebook-f-brands.svg"/>
<p>© by Quandtico.</p>
</footer>
</body>
</html>
And here's the CSS. I'm looking specifically at the Repair and Build Sections. Right now I'm only working on the "repair" div. Once I get that figured out I can move on to the build section.
/* Base Styles ------*/
* {
box-sizing: border-box;
}
body {
font-family: 'Anton', sans-serif;
background-color: #fbeee6;
margin: 0;
display: flex;
flex-direction: column;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
a {
text-decoration: none;
color: #fbeee6;
}
h2 {
letter-spacing: .2em;
font-size: 2em;
padding-left: .8em;
margin-right: auto;
}
.fbook {
width: 1em;
height: 1em;
}
/* Header Section ---- */
.main-header {
background-color: #284c5e;
color: #fbeee6;
}
.top-nav {
margin-top: 1em;
border-bottom: 1px solid rgba(251, 238, 230, .25);
}
.top-nav .info {
display: flex;
flex-direction: row;
justify-content: flex-start;
padding-left: 15%;
padding-right: 15%;
}
.main-nav {
margin-top: -1em;
}
.main-nav .nav {
display: flex;
flex-direction: row;
padding-left: 15%;
padding-right: 15%;
}
.nav li {
display: inline-flex;
align-items: center;
}
#home {
margin-left: auto;
}
.info a {
text-align: center;
display: block;
padding: .85em 1.3em;
}
.nav a {
padding-left: 1em;
}
#f1 {
margin-left: auto;
}
/* Intro Section -----*/
.intro {
width: 100%;
height: 38em;
color: #fbeee6;
text-align: center;
vertical-align: middle;
background: url('../imgs/E-kit.jpg') no-repeat center;
background-size: cover;
}
#quandtico {
letter-spacing: .1em;
font-size: 5em;
text-align: center;
margin: 2em 20% 0 20%;
}
.intro p {
margin-left: 25%;
margin-right: 25%;
line-height: 1.5em;
}
/* Repair and Build Sections -----*/
#repairList {
display: flex;
flex-wrap: wrap;
height: 569px;
width: 100%;
justify-content: space-between;
}
#repairPic {
display: block;
margin-left: auto;
}
.repairTitle {
display: inline-flex;
margin: auto;
}
.repairBtn {
margin: auto;
}
One solution is to merge the first two li tags into one, wrapping each in a div.
<ul id="repairList">
<li>
<div class="repairTitle">
<h4>Repair</h4>
</div>
<div class="repairBtn">
<button>Repair Rates</button>
</div>
</li>
<li>
<img id="repairPic" src=""/>
</li>
</ul>
Setting the h4 and button to display: block will also work if you'd prefer to avoid the extra divs.
These solutions avoid the common issues that arise from float positioning.
You could modifiy ccs of id repairList.
#repairList {
display: flex;
flex-wrap: nowrap;
height: 569px;
/* width: 100%; */
justify-content: space-between;
display: block;
float: right;
clear: right;
.repair {
display: flex; /*added*/
text-align: center;/*added*/
}
#repairList {
/* display: flex */remove
flex-wrap: wrap;
height: 569px;
width: 100%;
justify-content: space-between;
}
I've got three div's going here. One's a container then the other 2 are in place for some buttons! But it just doesn't look right. I'm not quite sure how to fix it
.button {
display: inline-block;
padding: 20px 30px;
font-size: 12px;
cursor: pointed;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #AB0002;
border: none;
border-radius: 15px;
}
.divButton {
height: 100%;
width: 100%;
background-color: #e4e4e4;
display: inline-block;
padding-bottom: 5px;
}
HTML:
<div class="divButton">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<!--2 buttons "centered"-->
<div style="float: left; padding-left: 325px;">
<p style="padding-left: 72px;">Centered text above button</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<!--add spacing to move away from 2 buttons-->
<div style="float: left; padding-left: 125px;">
<p style="padding-left: 40px;">TEXT</p>
<button class="button" style="float: right;">TEXT</button>
</div>
</div>
jsfiddle: https://jsfiddle.net/3ar1L0zy/1/
And what i'm trying to achieve in paint form!
I would move away from using floats - css has moved on sufficiently so you shouldn't need to use them anymore.
Use flex instead:
.button {
display: inline-block;
padding: 20px 30px;
font-size: 12px;
cursor: pointed;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #AB0002;
border: none;
border-radius: 15px;
}
.divButton {
width: 100%; /* you don't really need this - divs are block elements which are 100% by default */
background-color: #e4e4e4;
padding: 0 20px 5px 20px;
box-sizing: border-box; /* as you have set the width, you need this to stop the div being 100% + 40px wide */
display:flex; /* this will align items in a row by default */
flex-wrap:wrap; /* this allows the content to wrap to multiple rows */
justify-content:space-between; /* this will push any content to either side of the row */
}
.divButton > p {
width:100%; /* make this take up full row */
}
.divButton > div {
text-align:center; /* use this to centre text - not padding */
}
<div class="divButton">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<!--2 buttons "centered"-->
<div>
<p>Centered text above button</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<!--add spacing to move away from 2 buttons-->
<div>
<p>TEXT</p>
<button class="button">TEXT</button>
</div>
</div>
One other tip I would give you is try not to use inline styles - they become very hard to maintain and make it harder to debug too (and cause a lot larger files as you have to repeat code for styles instead of just using a class that can be used multiple times but programmed once)
Instead of use float: left on both div, you can use float right on the right one and remove the padding-left you set:
<div class="divButton">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<!--2 buttons "centered"-->
<div style="float: left;">
<p style="padding-left: 72px;">TEXT</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<!--add spacing to move away from 2 buttons-->
<div style="float: right;">
<p style="padding-left: 40px;">TEXT</p>
<button class="button" style="float: right;">TEXT</button>
</div>
</div>
https://jsfiddle.net/3ar1L0zy/13/
you can achieve this too by using flexbox too. (better solution in my opinon)
HTML code:
<div class="divButton">
<h3 style="text-align: center; padding-top: 15px; color: black;">TEXT!</h3>
<!--2 buttons "centered"-->
<div class="divText">
<p style="padding-left: 72px;">TEXT</p>
<p style="padding-left: 40px;">TEXT</p>
</div>
<!--add spacing to move away from 2 buttons-->
<div>
<div class="divButtons">
<div>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
<button class="button">TEXT</button>
</div>
</div>
</div>
CSS code:
.button {
display: inline-block;
padding: 20px 30px;
font-size: 12px;
cursor: pointed;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #AB0002;
border: none;
border-radius: 15px;
}
.button:hover {
background-color: #880002;
}
.divButton {
height: 100%;
width: 100%;
background-color: #e4e4e4;
display: inline-block;
padding-bottom: 5px;
}
.divText {
display: flex;
justify-content: space-around;
}
.divButtons {
display: flex;
justify-content: space-around;
}
<div class="divButton" style="text-align: center;">
<p style="text-align: center; padding-top: 15px; color: black;">TEXT!</p>
<div style="display: inline-block;">
<!--add spacing to move away from 2 buttons-->
<div style="float: right; display: inline-block; padding-left: 125px;">
<p style="text-align: center;">TEXT</p>
<button class="button" style="float: right;">TEXT</button>
</div>
<!--2 buttons "centered"-->
<div style="display: inline-block;">
<p style="text-align: center;">TEXT</p>
<button class="button">TEXT</button>
<button class="button">TEXT</button>
</div>
</div>
</div>
https://jsfiddle.net/3ar1L0zy/85/
I would recommend flexbox
.parent{
background: tomato;
width: 400px;
padding: 30px;
display: flex;
flex-direction: column;
}
.header{
background: yellow;
text-align: center
}
.body{
background: green;
width: 100%;
display: flex;
}
.body-item{
background: pink;
width: 50%;
text-align: center;
padding: 10px;
}
.blue{
background: blue; /* to make it easier to see */
}
.buttons{
display: flex;
flex-wrap: wrap; /* wrap items onto multiple lines if needed, from top to bottom*/
justify-content: space-evenly; /* items are distributed so that the spacing between any two items (and the space to the edges) is equal */
}
<div class="parent">
<h3 class="header">HEADER TEXT</h3>
<section class="body">
<div class="body-item">
<div class="text">Text</div>
<div class="buttons">
<button>Button</button>
<button>Button</button>
</div>
</div>
<div class="body-item blue">
<div class="text">Text</div>
<div class="buttons">
<button>Button</button>
</div>
</div>
</section>
</div>
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>