right align link items containing image and text along navbar - html

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>

Related

How to make a clickable responsive square box with centered content?

I have a set of columns each containing a square box that fills to the column width and maintains a square height ratio.
The entire box needs to be clickable, with the content inside that also centered within the box. I can't figure out a way of getting the inner <a> element to fill out the space inside the parent div and have its own content centered.
Here is an editable Fiddle
The entire square should be red, entirely clickable, and with a centered download button within.
I've reviewed a bunch of similar questions about making square boxes with CSS but didn't find anything about the inner elements filling out the box like this.
Thanks
.block {
width: 100%;
height: 0;
padding-bottom: 100%;
border: 2px solid #600;
}
.block a {
display: flex;
justify-content: center;
align-items: center;
background: #C00;
text-decoration: none;
}
.block span {
padding: 1em;
border: 2px solid #FFF;
text-align: center;
color: #FFF;
}
/* Demo only */
.row {
display: flex;
justify-content: space-between;
}
.column {
width: 20%;
}
<div class="row">
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
</div>
Setting the parent .block to position: relative allows us to set .block > a to position: absolute, with dimensions that fill its parent; I only added 3 css rules, they're commented so you know which ones:
.block {
position: relative; /* change #1 */
width: 100%;
height: 0;
padding-bottom: 100%;
border: 2px solid #600;
}
.block a {
position: absolute; /* change #2 */
left: 0; right: 0; top: 0; bottom: 0; /* change #3 */
display: flex;
justify-content: center;
align-items: center;
background: #C00;
text-decoration: none;
}
.block span {
padding: 1em;
border: 2px solid #FFF;
text-align: center;
color: #FFF;
}
/* Demo only */
.row {
display: flex;
justify-content: space-between;
}
.column {
width: 20%;
}
<div class="row">
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
<div class="column">
<div class="block">
<a href="#">
<span>Download</span>
</a>
</div>
</div>
</div>

Why doesn't the position work properly in the section

I have a section part on the website where I want four products being displayed in the middle, right and left arrows on both sides of the screen and a title in the middle above the displayed products, I think I have all of the HTML and CSS good but the position isn't working properly, can someone have a look and help me feature it out?img of the sections I am talking about
ps: the background color doesn't feel the space that the items and buttons are in, why does it happens too?
edit: this is a pic of how i wish it would look
HTML:
<section class="one">
<div><span class="title">New products</span></div>
<br>
<button class="left">
<span class="Lmain"></span>
<span class="Lside"></span>
</button>
<div class="items">
<div class="item">
<a href="">
<img class="itemImg" src="../Images/Image1.png" alt="Picture of the product">
</a>
<div><span class="desc">Description about that specific item that is being showen to you above this text
right here</span></div>
</div>
<div class="item">
<a href="">
<img class="itemImg" src="../Images/Image2.png" alt="Picture of the product">
</a>
<div><span class="desc">Description about that specific item that is being showen to you above this text
right here</span></div>
</div>
<div class="item">
<a href="">
<img class="itemImg" src="../Images/Image3.png" alt="Picture of the product">
</a>
<div><span class="desc">Description about that specific item that is being showen to you above this text
right here</span></div>
</div>
<div class="item">
<a href="">
<img class="itemImg" src="../Images/Image4.png" alt="Picture of the product">
</a>
<div><span class="desc">Description about that specific item that is being showen to you above this text
right here</span></div>
</div>
</div>
<button class="right">
<span class="Rmain"></span>
<span class="Rside"></span>
</button>
</section>
CSS:
.title {
text-align: center;
position: absolute;
margin: auto;
border: 1px solid goldenrod;
font-size: 40px;
}
.one {
background-color: hotpink;
position: relative;
}
.two {
background-color: rgb(255, 0, 128);
}
/*items appearance*/
.items {
position: relative;
margin: auto;
}
.item {
border: 1px solid rgb(255, 170, 0);
float: left;
position: absolute;
top: 0px;
margin: 0px 8px;
left: 12%;
width: 350px;
height: auto;
}
.itemImg {
width: 100%;
height: auto;
}
/*end of item appearance*/
.right {
right: 0px;
top: 0px;
position: absolute;
}
.left {
left: 0px;
top: 0px;
position: absolute;
}
Utilize flexbox to make this layout easy:
body {
margin: 0;
background: #1a1a1a;
color: #fff;
font-family: sans-serif;
}
h1#title {
padding: 1rem;
text-align: center;
}
main {
padding: 1rem;
display: flex;
justify-content: space-between;
align-items: center;
}
#products {
display: flex;
justify-content: center;
align-items: flex-start;
margin: 0;
}
.product {
padding: 2rem 1rem;
background: #fff;
color: black;
margin: .5rem;
}
<body>
<h1 id="title">Items</h1>
<main>
<div class="arrow" id="arrow-left">←</div>
<div id="products">
<div class="product">
Product
</div>
<div class="product">
Product
</div>
<div class="product">
Product
</div>
<div class="product">
Product
</div>
</div>
<div class="arrow" id="arrow-right">→</div>
</main>
</body>
Here a list of problem I see:
the .title element is position:absolute, so it doesn't fill any space and other elements will position weirdly. Solution: remove position: absolute
the .title is a <span> element witch is an inline-element who doesn't behave like a block element (<div>, <p>, <h1>, etc.), so the text-align: center doesn't take effect. Solution: remove the span and give the .title class to the parent <div>
the left and right buttons should be wrapped in a <div> with position:relative
Here is a jsfiddle with the fixed code.
I didn't fix the image positioning because i think those will be positioned by js, anyhow it dipends by the images dimensions and is a very weak method. So I strongly suggest using flexbox that is widely supported by browsers.
You can follow the #yerme example or this guide on flexbox.

Underlined text despite `text-decoration: none;` and white lines on side of `<div>` section [duplicate]

This question already has answers here:
How wide is the default `<body>` margin?
(4 answers)
Remove blue underline from link
(22 answers)
Closed 4 years ago.
What are the white lines on the sides? background-color: black; width: 100% doesn't do anything.
Why are there purple lines underneath the p elements? I've set text-decoration: none; for the container, the class for the group of elements, and the p itself.
If you look closely, here's what I mean:
#navbar {
position: fixed;
width: 100%;
top: 0;
background-color: white;
text-align: right;
}
.nav-link {
display: inline-block;
padding: 15px 15px;
text-decoration: none;
color: black;
font-size: 20px;
}
#projects {
background-color: black;
text-align: center;
width: 100%;
padding-bottom: 100px;
}
#projects h3 {
color: white;
font-size: 25px;
padding: 50px;
}
#tile-holder {
display: inline-grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr;
grid-column-gap: 300px;
grid-row-gap: 100px;
text-decoration: none;
}
.project-tile {
width: 100%;
border: 1px solid white;
display: flex;
flex-flow: column;
align-items: center;
}
.project-tile p {
color: white;
text-decoration: none;
}
.project-tile img {
height: 150px;
width: 288px;
}
<navbar id="navbar">
Welcome
Work
Contact
</navbar>
<div id="projects">
<h3>Some of my work:</h3>
<div id="tile-holder">
<a href="https://codepen.io/klin-nj-97/pen/GBzjJE" target="_blank">
<div id="tribute" class="project-tile">
<p>Tribute Page</p>
<img src="https://image.ibb.co/cQGzCp/Screen_Shot_2018_08_28_at_2_33_55_PM.png" alt="tribute-page">
</div>
</a>
<a href="https://codepen.io/klin-nj-97/pen/RBmoRg" target="_blank">
<div id="survey" class="project-tile">
<p>Survey Form</p>
<img src="https://image.ibb.co/n0S6sp/Screen_Shot_2018_08_28_at_9_29_48_PM.png" alt="survey">
</div>
</a>
<a href="https://codepen.io/klin-nj-97/pen/NLPrez" target="_blank">
<div id="landing" class="project-tile">
<p>Landing Page</p>
<img src="https://image.ibb.co/n33hyU/Screen_Shot_2018_08_28_at_9_39_43_PM.png" alt="landing">
</div>
</a>
<a href="https://codepen.io/klin-nj-97/pen/bxEKex" target="_blank">
<div id="tech" class="project-tile">
<p>Technical Documentation Page</p>
<img src="https://image.ibb.co/bN4NyU/Screen_Shot_2018_08_28_at_9_41_29_PM.png" alt="tech-page">
</div>
</a>
</div>
</div>
You need to style the <a> elements to stop the link underline:
a {
text-decoration: none;
}
This will fix your error.

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/

Vertical aligning of pictures with links inside a div

I've been struggling with this simple and basic problem for the last day and can't seem to find a solution for it nowhere.
I'm having a container div on my website, in which there are four social network buttons, and what I'm trying to achieve is vertically align them in to the middle
of the container, so there's equal amount of free space on top of them and underneath them.
Please note, that I've linke normalize.css and reset.css to my html.
Muh code:
HTML
<div class="social-line-container">
<div class="social-line-buttons-group">
<a href="#0"><img src="socialicons/facebook.png" alt="FB"><a/>
<a href="#0"><img src="socialicons/twitter.png" alt="TW"><a/>
<a href="#0"><img src="socialicons/google.png" alt="GO"><a/>
<a href="#0"><img src="socialicons/youtube.png" alt="YT"><a/>
</div>
</div>
CSS
.social-line-container {
width: 66%;
height: inherit;
margin: 0 auto;
}
.social-line-buttons-group a{
display: inline-block;
vertical-align: middle;
height: 100%;
float: right;
padding: 2px;
margin: 0 3px;
}
Any help will be much appreciated.
All you need to add is clear
.social-line-container {
width: 66%;
height: inherit;
margin: 0 auto;
}
.social-line-buttons-group a {
display: inline-block;
/*vertical-align: middle;*/
height: 100%;
float: right;
clear: both; /* add */
padding: 2px;
margin: 0 3px;
}
<div class="social-line-container">
<div class="social-line-buttons-group">
<a href="#0">
<img src="socialicons/facebook.png" alt="FB"><a/>
<a href="#0">
<img src="socialicons/twitter.png" alt="TW"><a/>
<a href="#0">
<img src="socialicons/google.png" alt="GO"><a/>
<a href="#0">
<img src="socialicons/youtube.png" alt="YT"><a/>
</div>
</div>
.social-line-container{
border:2px solid black;
}
.social-line-buttons-group{
display:flex;
align-items: center;
justify-content: center;
height:100%;
}
.social-line-buttons-group a{
padding: 2px;
margin: 0 3px;
}
img{
width:50px;
height:50px;
}
<div class="social-line-container">
<div class="social-line-buttons-group">
<img src="http://lh3.googleusercontent.com/-ElsaNCI_yKg/VcXI_VFictI/AAAAAAAAINA/MeFjL1Ymaac/s640/StylzzZ%252520%252528289%252529.png" alt="FB">
<img src="https://upload.wikimedia.org/wikipedia/it/archive/0/09/20160903181541!Twitter_bird_logo.png" alt="TW">
<img src="http://www.userlogos.org/files/logos/annyella/google3.png" alt="GO">
<img src="http://logok.org/wp-content/uploads/2014/08/YouTube-logo-play-icon-219x286.png" alt="YT">
</div>
</div>