I got a project where I put a flex menu on top of a picture slider. I want the items to use the same baseline, but I cannot set it to be on the same line.
a img{
height: 40px;
}
.links{
display: flex;
align-items: baseline;
}
<div class="links">
<a class="img-link"> <img src="anypicture.jpg"></a>
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
</div>
https://jsfiddle.net/7xnesjpy/13/
The container of the picture is slightly above the container of the text-links, what can I do to set them to the same ground/baseline
When you use baseline, it is the img's bottom that will line up with the bottom of the text, as you can see here, where I added a border to the items.
a img{
height: 40px;
border: 1px solid blue;
}
.links{
display: flex;
align-items: baseline;
}
.links a + a {
border: 1px solid blue;
}
<div class="links">
<a class="img-link"> <img src="https://staticaltmetric.s3.amazonaws.com/uploads/2015/10/dark-logo-for-site.png"></a>
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
</div>
For the img to align with the text/link elements bottom, you need flex-end.
a img{
height: 40px;
border: 1px solid red;
}
.links{
display: flex;
align-items: flex-end;
}
.links a {
border: 1px solid blue;
}
<div class="links">
<a class="img-link"> <img src="https://staticaltmetric.s3.amazonaws.com/uploads/2015/10/dark-logo-for-site.png"></a>
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
</div>
But as you can see in the 2nd sample, there is still a gap below the img.
This gap all inline element has, and to get rid of that, change the img's display type to block.
a img{
display: block;
height: 40px;
border: 1px solid red;
}
.links{
display: flex;
align-items: flex-end;
}
.links a + a {
border: 1px solid blue;
}
<div class="links">
<a class="img-link"> <img src="https://staticaltmetric.s3.amazonaws.com/uploads/2015/10/dark-logo-for-site.png"></a>
<a>Link 1</a>
<a>Link 2</a>
<a>Link 3</a>
</div>
Your image has a lot of transparent space around it, but you can fix it by adding
margin to you a img{} like so:
a img{
height: 40px;
margin-bottom:-20px;
}
.links{
display: flex;
align-items: baseline;
}
Your CSS is working fine but the problem is the image. There is unwanted space in your image. Please see in the fiddle now.
Removing all Padding and Margin from the image fixes it.
a img {margin: 0; padding: 0;}
https://jsfiddle.net/7xnesjpy/27/
Related
I want the entire li element to be clickable not just the phone, could it be possible?
Also, are the HTML tags being used correctly for a contact information?
ul {
display: flex;
text-align: center;
justify-content: center;
list-style: none;
padding-left: 0;
}
li {
width: 400px;
border: 1px solid red;
}
li a {
position: relative;
width: inherit;
border: 1px solid green;
}
<address>
<ul>
<li class="">
<a href="12345"><img src="./img/phone.svg" /><span>Phone</span>
<p>+1 (234) 567-89-00</p>
</a>
</li>
<li class="">
<a href="email#email"><img src="./img/email.svg" /><span>Email</span>
<p>email#email</p>
</a>
</li>
<li class="">
<a href="my streeet"><img src="./img/address.svg"/><span>Address</span>
<p>street</p>
</a>
</li>
</ul>
</address>
Thank you!!
You can either set your styles to
a {
display: block;
height: 100%
}
or
li {
display: flex;
}
You should put the end the a tag after the img
<address>
<ul>
<li class="">
<img src="./img/phone.svg" /><span>Phone</span>
<p>+1 (234) 567-89-00</p>
</li>
<li class="">
<img src="./img/email.svg" /><span>Email</span>
<p>email#email</p>
</li>
<li class="">
<img src="./img/address.svg"/><span>Address</span>
<p>street</p>
</li>
</ul>
</address>
Or you can delete the a tag.
The image works without it, but the img won't be clickable
A li is by default not clickable. You can solve this issue by either using javascript or by adding height: 100%; and display: block; to your a.
Just give a tag 100% width and height and it will occupy all the space of li and li will become clickable another way of doing this is that just remove li and and insert a tag directly.
ul {
display: flex;
text-align: center;
justify-content: center;
list-style: none;
padding-left: 0;
}
li {
width: 400px;
border: 1px solid red;
}
li a {
position: relative;
display: block;
width: 100%;
height: 100%;
border: 1px solid green;
}
<address>
<ul>
<li class="">
<a href="12345"><img src="./img/phone.svg" /><span>Phone</span>
<p>+1 (234) 567-89-00</p>
</a>
</li>
<li class="">
<a href="email#email"><img src="./img/email.svg" /><span>Email</span>
<p>email#email</p>
</a>
</li>
<li class="">
<a href="my streeet"><img src="./img/address.svg"/><span>Address</span>
<p>street</p>
</a>
</li>
</ul>
</address>
This was already answered here
Here is the answer for those who don't want to look there
<li onclick="location.href = '';">Make A List Item Clickable</li>
The list element supports a onclick event.
for your case just remove the <a> element and replace it with a text-node or a <p>
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
I'm having an issue with my header, on the .container element when I apply flex the child flex-justify doesn't work, but when I remove it I can't align them center.
* {
margin: 0;
padding: 0;
text-decoration: none;
color: white;
}
header {
background: inherit;
border-bottom: 1px solid var(--border);
padding: 1rem 0rem;
background: pink;
}
/*
Remove the flex to preview
*/
header .container{
display: flex;
align-items: center;
}
.right {
display: flex;
align-items: center;
justify-content: flex-end;
background: green;
}
.left {
display: flex;
align-items: center;
justify-content: flex-start;
background: red;
}
<header>
<div class="container">
<div class="logo left">
<a href="#">
MYLOGO(IMG)
</a>
</div>
<nav class="right">
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
</nav>
</div>
</header>
So basically I want a container with flex to align center and its child one justify end and one right.
Simply do this:
header .container {
display: flex;
align-items: center;
justify-content: space-between;
/* this will put the logo.left at the left and .right at the right */
}
* {
margin: 0;
padding: 0;
text-decoration: none;
color: white;
}
header {
background: inherit;
border-bottom: 1px solid var(--border);
padding: 1rem 0rem;
background: pink;
}
/*
Remove the flex to preview
*/
header .container{
display: flex;
align-items: center;
justify-content: space-between;
}
.right {
display: flex;
align-items: center;
justify-content: flex-end;
background: green;
}
.left {
display: flex;
align-items: center;
justify-content: flex-start;
background: red;
}
<header>
<div class="container">
<div class="logo left">
<a href="#">
MYLOGO(IMG)
</a>
</div>
<nav class="right">
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
<a class="nav-link" href="">
NAV ITEM
</a>
</nav>
</div>
</header>
#Its Fragilis recommended that I add display flex and justify space around, but because I was using bootstrap it showed some margin on the bottom and left. to fix that you need to add this code:
.clearfix:before, .clearfix:after, .container:before, .container:after, .container-fluid:before, .container-fluid:after, .row:before, .row:after{
content: none !important;
}
I´m trying to make a side navigation bar using flexbox. The sidebar looks good, but I want the contained links to be spread out and take over the entire left of the screen. I try to do this using padding, but it ends up leaving a white space at the end. Any ideas?
body {
margin: auto 0;
}
#navbar {
display: flex;
flex-direction: column;
border: solid;
background: grey;
height: 900px;
width: 200px;
text-align: center;
}
.nav-link {
text-decoration: none;
border: solid;
padding: 80px;
}
<nav id="navbar">
<header>"A dissertation on fast food"</header>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
</nav>
Since you're using a flex container, instead of using padding for spacing, take advantage of flex features. The flex-grow property can distribute free space in the container evenly across all items.
body {
margin: auto 0;
}
#navbar {
display: flex;
flex-direction: column;
border: solid;
background: grey;
height: 900px;
width: 200px;
text-align: center;
}
.nav-link {
text-decoration: none;
border: solid;
/* padding: 80px; */
flex-grow: 1; /* equal distribution of free space */
/* for centering the text */
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
<nav id="navbar">
<header>"A dissertation on fast food"</header>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
</nav>
You are having an overflow. You defined the height to be 900px and for each box you have 160px of padding-top/bottom so 800px of padding and if we consider the border plus the content plus the header we will have more than 900px.
Instead of padding you can use flex:1 to stretch the elements then rely on flexbox inside them to center the text:
body {
margin: auto 0;
}
#navbar {
display: flex;
flex-direction: column;
border: solid;
background: grey;
height: 900px;
width: 200px;
text-align: center;
}
.nav-link {
text-decoration: none;
border: solid;
flex:1;
/*to center the text*/
display:flex;
align-items:center;
justify-content:center;
}
<nav id="navbar">
<header>"A dissertation on fast food"</header>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
<a class="nav-link" href="Steak">here</a>
</nav>
I am trying to build a simple navigation where some parts of the navigation items are hidden depending on the available space:
.container {
display: flex;
border: 1px solid red;
width: 100%;
max-width: 600px
}
ul {
margin: 0 30px 0px 0px;
display: flex;
list-style: none;
padding: 0;
flex-grow: 1;
}
li {
margin: 0;
padding: 0px 5px;
border: 1px solid green;
margin-left: 20px;
}
li:first-child{
padding-left: 0px;
margin-left: 0px;
}
li a {
line-height: 18px;
height: 18px;
overflow: hidden;
display: block;
}
.important {
color: red;
}
<div class=container>
<ul>
<li>
<a href=#>
<span class=important>my</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>my</span>
<span class=important>elem</span>
</a>
</li>
<li>
<a href=#>
<span class=important>my</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>my</span>
<span class="important">2nd</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>longerfirst</span>
<span class="important">elem</span>
</a>
</li>
</ul>
</div>
I successfully made some words disappear with using a fixed height and overflow:hidden on the a tag.
Now I'm looking for a solution to keep the red words (.important) visible and hide the others. Is there a way to do this?
If the word on the invisible line is longer than the visible one, the visible has too much whitespace, is there also a solution to this?
With Javascript it would be fairly easy, but I'm looking for a CSS only solution.
JSFiddle: http://jsfiddle.net/86qjexz3/
So I'm making a webpage and for it I am making a header that includes 4 links/tabs, a logo and a dropdown menu when someone hovers over the 4th tab to display a hidden 5th tab. Everything is working except I can't seem to get the 5th tab (the "cater tab") to have the same padding as the other four, even though they all have the same class "tab". Instead, it gives it 0 padding. I was wondering why this is and if there is a way to fix it?
Here's my html:
<div class="header">
<ul>
<li class="tab"><a>Home</a></li>
<li class="tab"><a>About</a></li>
<li id="logo">
<img src="images/logo.png" width="260" height="95"/>
</li>
<li class="tab"><a>Menu</a></li>
<ul class="dropdown_container">
<li class="tab super_tab"><a>Order</a></li>
<div class="dropdown">
<li class="tab sub_tab"><a>Cater</a></li>
</div>
</ul>
</ul>
</div>
Here's my css:
ul {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
.header{
text-align: center;
background-color: red;
}
.tab, #logo{
padding-right: 2.4%;
padding-left: 2.4%;
display: inline-block;
vertical-align: middle;
border: 2px solid black;
}
.dropdown{
display: table;
}
.dropdown_container{
display: inline;
}
.sub_tab{
margin-left: 0;
}
Here's a jsfiddle demonstration: https://jsfiddle.net/90udrejh/
Thanks!
According to MDN
Padding
Applies to all elements except table-row-group, table-header-group,
table-footer-group, table-row, table-column-group and table-column. It
also applies to ::first-letter.
So just change your display:table from .dropdown to another value that you like and you can't have <div> as child of an <ul>.
here is a possible snippet
ul {
margin: 0;
padding: 0;
}
li {
list-style-type: none;
}
.header {
text-align: center;
background-color: red;
}
.tab,
#logo {
padding-right: 2.4%;
padding-left: 2.4%;
display: inline-block;
vertical-align: middle;
border: 2px solid black;
}
.dropdown,
.dropdown_container {
display: inline-block;
}
.sub_tab {
margin-left: 0;
}
<div class="header">
<ul>
<li class="tab"><a>Home</a>
</li>
<li class="tab"><a>About</a>
</li>
<li id="logo">
<img src="images/logo.png" width="260" height="95" />
</li>
<li class="tab"><a>Menu</a>
</li>
<ul class="dropdown_container">
<li class="tab super_tab"><a>Order</a>
</li>
<li class="dropdown">
<span class="tab sub_tab"><a>Cater</a></span>
</li>
</ul>
</ul>
</div>
The problem originates from display: table in the .dropdown style. Remove that and the padding returns to normal. However, this may not be the desired method. Usually, inside of a display: table divs would be assigned to display as display: table-column