Absolute positioned elements behaving weirdly [duplicate] - html

This question already has answers here:
Why aren't my absolutely/fixed-positioned elements located where I expect?
(3 answers)
Why is '::after' pseudo-selector over the image and not under it? [duplicate]
(3 answers)
Closed last year.
I am following a tutorial on Youtube to make a responsive website, so far so good. But I encountered a weird problem where I am trying to make a bottom gradient border. I followed the video and in it they used positon:absolute, which all worked great. But when I tried the same code it seems that the first element's border appears on the top while the others appear on the bottom. I figured out the solution by adding bottom: 0. But in the video the coder didn't add that, why so? I am guessing different browsers behave differently?
Here is the code I used for the markup:
.flex {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.flex-jc-sb {
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
.flex-jc-c {
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.flex-ai-c {
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.header__links a {
font-size: 0.875rem;
position: relative;
}
.header__links a::before {
content: "";
position: absolute;
left: 0;
right: 0;
display: block;
height: 5px;
background: linear-gradient(to right, #31d35c, #2bb7da);
}
.header__links a:not(:last-child) {
margin-right: 32px;
}
<header class="header">
<nav class="flex flex-jc-sb flex-ai-c">
<a href="/" class="header__logo flex flex-ai-c">
<img src="images/logo.svg" alt="Easybank" />
</a>
<a href="#" class="header__menu hide-for-desktop">
<span></span>
<span></span>
<span></span>
</a>
<div class="header__links hide-for-mobile">
Home
About
Contact
Blog
Careers
</div>
<button type="button" class="hide-for-mobile header__cta">
Request Invite
</button>
</nav>
</header>

pseudo-elements are inline by default, so add display: inline-block; to .header__links a, and use :after if you want the content to come after (below) the text.
.flex {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.flex-jc-sb {
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
}
.flex-jc-c {
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
.flex-ai-c {
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.header__links a {
font-size: 0.875rem;
position: relative;
display: inline-block;
}
.header__links a:after {
content: "";
position: absolute;
left: 0;
right: 0;
display: block;
height: 5px;
background: linear-gradient(to right, #31d35c, #2bb7da);
}
.header__links a:not(:last-child) {
margin-right: 32px;
}
<header class="header">
<nav class="flex flex-jc-sb flex-ai-c">
<a href="/" class="header__logo flex flex-ai-c">
<img src="images/logo.svg" alt="Easybank" />
</a>
<a href="#" class="header__menu hide-for-desktop">
<span></span>
<span></span>
<span></span>
</a>
<div class="header__links hide-for-mobile">
Home
About
Contact
Blog
Careers
</div>
<button type="button" class="hide-for-mobile header__cta">
Request Invite
</button>
</nav>
</header>

Related

How to move links to the right with flex?

What is the best way to move the about and contact links to the right side of the navigation section?
I have tried using align-items: flex-end, however, for some reason it doesn't work.
.container {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
margin-top: 5px;
margin-bottom: 25px;
}
/* Navigation */
.navigation-container {
position: fixed;
top: 0;
}
.navigation-items {
display: flex;
justify-content: center;
}
.footer-items {
display: flex;
justify-content: center;
}
<!-- Container -->
<div class="container">
<!-- Navigation -->
<div class="navigation-container">
<span class="background"></span>
<!-- Results Nav-->
<span class="navigation-items" id="resultsNav">
<h3>About</h3>
<h3> • </h3>
<h3>Contact</h3>
</span>
</div>
</div>
Created a little jsfiddle for you:
https://jsfiddle.net/wrayvon/akzfuvq8/4/
Using this styling:
.container { position: relative; display: flex; margin-top: 5px; margin-bottom: 25px;}
.navigation-container { width: 100%;}
.navigation-items { display: flex; justify-content: flex-end;}
That solves it :)
I found 2 solutions:
Because you are already using position: fixed; you could just add right: 0; to your .navigation-container to make it align to the right.
.navigation-container {
position: fixed;
top: 0;
right: 0;
}
Or you can make your navigation-container element 100% wide. Then you can add some flex styles to it like this, to make the children align to the right.
.navigation-container {
position: fixed;
top: 0;
width: 100%;
display: flex;
justify-content: flex-end;
}
When you are using flex-direction:column then justify-content = vertical alignment and align-items = horizontal alignment.
As you can see in this example of your snippet. Align-items works as expected.
If you where adding align-items to a child of .container then you will not notice the change in alignment as the width of the child e.g .navigation-container needs to be (flex:1) to fill the width of its parent.
.container {
position: relative;
display: flex;
flex-direction: column;
align-items: center;
margin-top: 5px;
margin-bottom: 25px;
align-items: flex-end;
}
/* Navigation */
.navigation-container {
position: fixed;
top: 0;
}
.navigation-items {
display: flex;
justify-content: center;
}
<!-- Container -->
<div class="container">
<!-- Navigation -->
<div class="navigation-container">
<span class="background"></span>
<!-- Results Nav-->
<span class="navigation-items" id="resultsNav">
<h3>About</h3>
<h3> • </h3>
<h3>Contact</h3>
</span>
</div>
</div>
To assign end of the div parent should have 100% width. Just add the below CSS then it should work:
.navigation-container{
width:100%;
}
.navigation-items{
justify-content: flex-end;
}
Just simply add align-items: flex-end; to the flex container.
.container {
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
margin-top: 5px;
margin-bottom: 25px;
align-items: flex-end;
}
/* Navigation */
.navigation-container {
position: fixed;
top: 0;
}
.navigation-items {
display: flex;
justify-content: center;
}
.footer-items {
display: flex;
justify-content: center;
}
<!-- Container -->
<div class="container">
<!-- Navigation -->
<div class="navigation-container">
<span class="background"></span>
<!-- Results Nav-->
<span class="navigation-items" id="resultsNav">
<h3>About</h3>
<h3> • </h3>
<h3>Contact</h3>
</span>
</div>
</div>

How To Use Justify-Content: Space-Between With Position: Fixed?

I am trying to force my footer to the bottom of the page, while still retaining separation between them, but it doesn't seem that Justify-Content, and Position: Fixed work together.
Edit: I have updated my post to include the currently used CSS for this project.
<div class="footer">
<div class="left links">
<a href="https://www.facebook.com/" target="_blank">
<img width="35" height="35" src="FB2.svg" alt="">
</a>
</div>
<div class="right links">
<a href="https://www.twitter.com/" target = "_blank">
<img width="35" height="35" src="Twitter.svg" alt="">
</a>
</div>
</div>
body {
display: flex;
flex-wrap: wrap;
margin: 0;
font-family: "Helvetica";
}
.Nav {
display: flex;
width: 100vw;
background-color: white;
padding: 5px;
}
ul {
display: flex;
color: white;
font-size: 25px;
list-style-type: none;
margin-left: 20px;
gap: 50px;
}
a {
color: black;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.footer {
display: flex;
flex: 1;
flex-wrap: wrap;
align-items: center;
justify-content: center;
justify-content: space-between;
position: fixed;
gap: 20px;
}
Your footer has position: fixed but it does not have any position set, e.g. top bottom left right. Set those values explicitly:
.footer {
position: fixed;
bottom: 0;
left: 0;
right: 0;
}
as a shorthand, you can make use of the inset property.
.footer {
position: fixed;
inset: auto 0 0 0;
}
This is a similar shorthand to margin, where the 4 values map to top, right, bottom, and left, respectively.
Div elements (and every other element using display: block) default to full parent width. If you set position to fixed, this is no longer the case. Your problem probably is fixed by adding the following to .footer:
width: 100%;
Assign width to footer
.footer {
width: 100%;
display: flex;
flex: 1;
flex-wrap: wrap;
align-items: center;
justify-content: center;
justify-content: space-between;
position: fixed;
gap: 20px;
}

Not sure how to align my flex items individually along the main axis

So, I am trying to use flexbox to create a navbar, and basically I want my logo centred and my navbar toggler to the left of the screen.
However, if I give the flex container the justify-content property with the value of center it would just center both my logo as well as my navbar toggler.
I wanted to know whether there is something similar to align-self property but for the main axis?
So I can set my logo to center and my navbar toggler to the left of my screen?
Here is my HTML code:
<header class="main-navbar">
<div class="nav-toggler">
<span></span>
<span></span>
<span></span>
</div>
<div class="brand">
<a class="logo-link" href="#">
<img src="imgs/logo/logo-lightgrey.png" alt="logo-brand-image">
</a>
</div>
</header>
Here is my css code:
.main-navbar {
position:fixed;
display: flex;
justify-content: center;
align-items: center;
height: 3.5rem;
width: 100%;
font-weight: 500;
}
.nav-toggler {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 1rem;
cursor: pointer;
}
.nav-toggler span {
width: 1.3rem;
height: 0.17rem;
background: white;
display: block;
}
Push both child elements with margin-right: auto
.main-navbar {
position:fixed;
display: flex;
justify-content: center;
align-items: center;
height: 3.5rem;
width: 100%;
font-weight: 500;
background-color: #000;
}
.nav-toggler {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 1rem;
cursor: pointer;
}
.nav-toggler span {
width: 1.3rem;
height: 0.17rem;
background: white;
display: block;
}
/**/
.nav-toggler,
.brand {
margin-right: auto;
}
<header class="main-navbar">
<div class="nav-toggler">
<span></span>
<span></span>
<span></span>
</div>
<div class="brand">
<a class="logo-link" href="#">
<img src="https://www.placehold.it/100x50" alt="logo-brand-image">
</a>
</div>
</header>
I'm not sure there is a pure flex solution for this. You may need to try adding position: absolute to .nav-toggler instead. This will remove the toggler from the overall flow and allow the logo to sit in the centre unaffected.
.main-navbar {
position: fixed;
display: flex;
justify-content: center;
align-items: center;
height: 3.5rem;
width: 100%;
font-weight: 500;
background: black;
}
.nav-toggler {
position: absolute;
left: 0;
display: flex;
flex-direction: column;
justify-content: space-between;
height: 1rem;
cursor: pointer;
margin-left: 1rem;
}
.nav-toggler span {
width: 1.3rem;
height: 0.17rem;
background: white;
display: block;
}
.brand img {
display: block; /* not necessary, just added to vertically align image for demo */
}
<header class="main-navbar">
<div class="nav-toggler">
<span></span>
<span></span>
<span></span>
</div>
<div class="brand">
<a class="logo-link" href="#">
<img src="https://placeimg.com/150/30/animals" alt="logo-brand-image">
</a>
</div>
</header>

Css flex layout not aligning aligning items as desired

I am trying to position Author designation under Author name, i tried few thing since theme is using flex i find it hard to make it work.
This them is using flex all over the place and if change one thing it breaks other thing.
How can i place Author Designation under the Author Name with minimal css changes
https://codepen.io/KGuide/pen/OJJBzmp
.article-container .article-thumbnail-wrapper {
height: 480px;
height: auto;
}
.article-thumbnail-info {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
position: absolute;
width: 100%;
left: 0;
bottom: 20px;
padding: 0 15px;
-webkit-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
-webkit-align-items: flex-start;
-ms-flex-align: start;
align-items: flex-start;
-webkit-justify-content: flex-start;
-ms-flex-pack: start;
justify-content: flex-start;
}
.article-author {
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
-webkit-align-items: center;
-ms-flex-align: center;
align-items: center;
text-decoration: none !important;
}
.article-author figure {
margin: 0;
width: 50px;
height: 50px;
margin-right: 18px;
}
.article-author figure img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}
Just wrapped two spans to div and aligned it to column with flex property:
https://codepen.io/Nevados/pen/mddzpYw
If the width of the image is static you can consider some margin trick. The 68px I am using is the width+margin for the image.
I removed some CSS to keep only the relevant one
.article-author {
display: flex;
flex-wrap: wrap; /* added */
/*align-items:center; removed */
text-decoration: none !important;
}
.article-author figure {
margin: 0;
width: 50px;
height: 50px;
margin-right: 18px;
}
.article-author figure img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}
/* Added */
.blog-detail-author {
flex-basis: calc(100% - 68px);
margin-top: 5px;
}
.blog-detail-designation {
margin-left: 68px;
margin-top: -25px; /* This one is a bit hacky, you may need to change it based on the font or other CSS*/
}
<div class="article-thumbnail-wrapper blog-thumbnail-wrapper text-center">
<div class="article-author">
<figure class="article-author-avatar"><img alt="" src="http://themeflex.com/strucflex/en/structures/assets/img/avatar_2.jpg"></figure>
<span class="blog-detail-author">Author Name</span>
<span class="blog-detail-designation">Author Designation</span>
</div>
</div>
Try With this :
HTML
<div class="article-thumbnail-wrapper blog-thumbnail-wrapper text-center">
<div class="article-author">
<figure class="article-author-avatar"><img alt="" src="http://themeflex.com/strucflex/en/structures/assets/img/avatar_2.jpg"></figure>
<span>
<span class="blog-detail-author">Author Name</span>
<span class="blog-detail-designation">Author Designation</span>
</span>
</div>
</div>
CSS:
figure + span {
display:flex; flex-direction:column;
}

Center items correctly when using justify-content: space-between

I created a header bar and want to place 3 items there. The first should be aligned on the left side, the second in the middle of the header and the third on the right side.
I went for this
body{
background: #eeeeee;
}
#header {
background: #ffffff;
height: 53px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
.headerEle {
}
<div id="header">
<img class="headerEle" src="https://cdn.discordapp.com/attachments/316916526760591362/390814093340311565/unknown.png">
<a class="headerEle" href="https://www.google.de/">Google</a>
<button class="headerEle">Logout</button>
</div>
but when using justify-content: space-between; the items don't get centered correctly. The image takes a bigger place than the small button on the right.
So the link in the middle is not centered correctly, it overlaps the right side. I want the link being in the horizontal center of the page.
You can do something like this:
body {
background: #eee;
}
#header {
background: #fff;
height: 53px;
display: flex;
/*flex-direction: row; not necessary, by default*/
align-items: center;
justify-content: space-between;
}
#header > span {flex: 1} /* each 33.33% of the parent's width */
img {display: block; max-width: 100%; max-height: 100vh} /* responsiveness; "display: block" removes bottom margin/whitespace */
.link {
display: flex;
justify-content: center; /* horizontally centered */
}
.btn {
display: flex;
justify-content: flex-end; /* placed far right */
}
<div id="header">
<span>
<img class="headerEle" src="https://cdn.discordapp.com/attachments/316916526760591362/390814093340311565/unknown.png">
</span>
<span class="link">
<a class="headerEle" href="https://www.google.de/">Google</a>
</span>
<span class="btn">
<button class="headerEle">Logout</button>
</span>
</div>
You can use absolute positioning for the <a> element to exactly center the link on the header:
body{
background: #eeeeee;
}
#header {
background: #ffffff;
height: 53px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
position:relative;
}
#header a {
position:absolute;
left:50%;
top:50%;
transform:translate(-50%, -50%);
}
<div id="header">
<img class="headerEle" src="https://cdn.discordapp.com/attachments/316916526760591362/390814093340311565/unknown.png">
<a class="headerEle" href="https://www.google.de/">Google</a>
<button class="headerEle">Logout</button>
</div>
Is this what you are looking to do?
body{
background: #eeeeee;
}
#header {
background: #ffffff;
height: 53px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}
#header > div {
-webkit-flex: 1; /* Safari 6.1+ */
-ms-flex: 1; /* IE 10 */
flex: 1;
text-align: center;
}
<div id="header">
<div>
<img class="headerEle" src="https://cdn.discordapp.com/attachments/316916526760591362/390814093340311565/unknown.png">
</div>
<div>
<a class="headerEle" href="https://www.google.de/">Google</a>
</div>
<div>
<button class="headerEle">Logout</button>
</div>
</div>