Removing whitespace at bottom of a page when using a sticky image - html

I have the following layout where I am using position: sticky to place an image near the bottom right corner of the page (below a flex layout):
.footer-logo {position: sticky; bottom: 50px; z-index: 100; margin: 50px; padding: 25px; width: 100px; height: 100px; background-color: green; float: right;}
.flex-container {height: 400px; display: flex; flex-wrap: wrap; background-color: #f2f2f2; padding-top:20px; justify-content: center;}
<div class="flex-container"></div>
<img class="footer-logo" src="https://placehold.it/100x100"></img>
The img sticks in the correct position, but below the flex-container there is whitespace which spans the full width of the page and is the same height as the img (including padding & margin).
How do I not display this whitespace whilst still retaining the correct positioning of the img?

You can remove it using negative margin equal to the height. That space is the space of the image since position:sticky will keep the element part of the flow:
.footer-logo {
position: sticky;
bottom: 50px;
z-index: 100;
padding: 25px;
width: 100px;
height: 100px;
background-color: green;
float: right;
}
.flex-container {
height: 400px;
display: flex;
flex-wrap: wrap;
background-color: #f2f2f2;
padding-top: 20px;
justify-content: center;
margin-bottom: -150px;
}
<div class="flex-container"></div>
<img class="footer-logo" src="https://placehold.it/100x100">

You can use this code
body {
margin: 0;
}
.footer-logo {
position: sticky;
z-index: 100;
padding: 25px;
width: 100px;
height: 100px;
background-color: green;
float: right;
}
.flex-container {
height: 400px;
display: flex;
flex-wrap: wrap;
background-color: #f2f2f2;
padding-top: 20px;
margin-bottom: -150px;
justify-content: center;
}
<div class="flex-container"></div>
<img class="footer-logo" src="https://placehold.it/100x100" alt="100x100" />

I realised that I just needed to use position: fixed with bottom and right also set:
body {
margin: 0;
}
.footer-logo {
position: fixed;
bottom: 50px;
right: 50px;
z-index: 100;
padding: 25px;
width: 100px;
height: 100px;
background-color: green;
}
.flex-container {
height: 400px;
display: flex;
flex-wrap: wrap;
background-color: #f2f2f2;
padding-top: 20px;
margin-bottom: -150px;
justify-content: center;
}
<div class="flex-container"></div>
<img class="footer-logo" src="https://placehold.it/100x100" alt="100x100" />

Related

aliginment and position issues when using display grid

I have 2 parent containers in my code below. The first one is just for reference for what I want my second container to look like. The difference between the two is the first one I used absolute positioning and flex display but the second one is grid display. What I'm stuck on is understanding how to center class .item1 and position class .item2 all the way to the right just how it's like on the first parent container i.e class .topAdCon. My specific questions are 1) how to center .item1
2) how to set .item2's position all the way to the right (right: 0%)
3) on the first parent container I just set top: 0% to align it all the way to the top because it has absolute positioning how can I set the positioning of the second parent container where ever I want currently I'm using margin-top for top positioning is that the way to go or what is the right way?
4) Lastly how do I set the height for the second container because height isn't responding as it does on the first container?
Note: I commented out things I tried in order to achieve these things but they aren't working.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.topAdCon {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0%;
width: 100%;
height: 18%;
background-color: pink;
}
.topAdCon .adCon {
width: 40%;
height: 100%;
}
.topAdCon .adCon img {
width: 100%;
height: 100%;
}
.topAdCon .sideInfo {
display: flex;
text-align: center;
width: 17%;
height: 100%;
position: absolute;
right: 0%;
border: 1.5px solid #000000;
}
.topAdCon .sideInfo p {
font-size: 0.9vw;
margin: auto;
}
.wrapper {
display: grid;
align-items: center;
justify-content: center;
/*position: relative;
top: 20%;*/
margin-top: 20%;
grid-template-columns: 40% 17%;
width: 100%;
height: 18%;
/*height not responding*/
background-color: gold;
}
.item1 {
/*align-self: center;*/
width: 100%;
height: 100%;
}
.item1 img {
width: 100%;
height: 100%;
}
.item2 {
display: flex;
text-align: center;
/*align-self: flex-end*/
width: 100%;
height: 100%;
border: 1.5px solid #000000;
}
.item2 p {
font-size: 1.5vw;
margin: auto;
}
<div class="topAdCon">
<div class="adCon">
<img src="https://images.pexels.com/photos/356830/pexels-photo-356830.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" />
</div>
<div class="sideInfo">
<p>this is test statement 1</p>
</div>
</div>
<div class="wrapper">
<div class="item1">
<img src="https://images.pexels.com/photos/356830/pexels-photo-356830.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" />
</div>
<div class="item2">
<p>this is test statement 2</p>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.topAdCon {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0%;
width: 100%;
height: 18%;
background-color: pink;
}
.topAdCon .adCon {
width: 40%;
height: 100%;
}
.topAdCon .adCon img {
width: 100%;
height: 100%;
}
.topAdCon .sideInfo {
display: flex;
text-align: center;
width: 17%;
height: 100%;
position: absolute;
right: 0%;
border: 1.5px solid #000000;
}
.topAdCon .sideInfo p {
font-size: 0.9vw;
margin: auto;
}
.wrapper {
display: grid;
margin-top: 20%;
grid-template-columns: 30% 40% 12% 18%;
grid-template-areas: 'item item1 item2 item3';
width: 100%;
height: 18vh;
background-color: gold;
}
.item1 {
width: 100%;
height: inherit;
}
.item1 img {
width: 100%;
height: 100%;
}
.item3 {
display: flex;
text-align: center;
justify-content: end;
width: 100%;
height: inherit;
border: 1.5px solid #000000;
}
.item3 p {
font-size: 1.5vw;
margin: auto;
}
<div class="topAdCon">
<div class="adCon">
<img src="https://images.pexels.com/photos/356830/pexels-photo-356830.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" />
</div>
<div class="sideInfo">
<p>this is test statement 1</p>
</div>
</div>
<div class="wrapper">
<div class="item"></div>
<div class="item1">
<img src="https://images.pexels.com/photos/356830/pexels-photo-356830.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2" />
</div>
<div class="item2"></div>
<div class="item3">
<p>this is test statement 2</p>
</div>
</div>

Centering certain div elements while keeping another element at the bottom

I have three elements in a div which itself is in another div. I want the first two elements of the inner div to be centered in relation to the outermost div and stacked on top of each other, while the third element should be at the bottom in relation to the outermost div as well. This is a WordPress project, which I am new to, so I don't want to change any of the div or class structure, just style the existing classes. I would prefer Flexbox-only solutions.
Here's the html:
<div class="outerDiv">
<div class="innerDiv">
<p class="e1"> Centered element 1 </p>
<p class="e2"> Centered element 2 </p>
<p> Bottom element </p>
</div>
</div>
Here's the relevant CSS:
.outerDiv{
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
padding: 90px 0 0 0;
box-sizing: border-box;
}
.innerDiv {
align-self: center;
margin: 30px 30px 30px 30px;
}
.e1 {
margin: 10px 0;
}
.e2{
margin: 10px 0;
}
.innerDiv {
/*...*/
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}
.e1 {
margin: 10px 0;
margin-top: auto;
}
.e2{
margin: 10px 0;
margin-bottom: auto;
}
Edit: Added width/height properties to .innerDiv
Welcome to Stackoverflow. I've updated this to meet your new requirements:
.outerDiv {
width: 100%;
height: 600px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
text-align: center;
}
.innerDiv {
width: 50%;
height: 100%;
}
.e1 {
width: 100%;
height: 25%;
}
.e2 {
width: 100%;
height: 25%;
}
.e3 {
width: 50%;
height: 25%;
position: absolute;
bottom: 0;
}

images and buttons are not horizontally aligned

I have met some problems while doing a image-viewer project. The problem is that my buttons and the image are not following justify-content property, which they don't distributed equally inside my div block, how could it be solved? Also the image is not centered as the title does despite I set the align item property. I dow know how to fix that. I've searched over the website for solutions but none of them seems working.
Could anyone help me, please? Thanks in advance.
Here are the html and css code:
<div class="image-viewer__container">
<div class="image-viewer__title">Image Viewer</div>
<div class="image-viewer__main">
<div class="image-viewer__button"><img src="./images/back.png" id="previous" /></div>
<div class="image-viewer__display" style="background-image: url(./images/loading.gif);background-repeat: no-repeat; background-position: center;">
<img src="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" id="display">
<div class="image-viewer__display-source-wrapper">
<span><a href="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" target="_blank">
https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF</a>
</span>
</div>
</div>
<div class="image-viewer__button"><img src="./images/next.png" id="next" /></div>
</div>
</div>
.image-viewer__container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
.image-viewer__title {
font-size: 5rem;
font-weight: 600;
color: #615dec;
margin: 0;
margin-top: 2rem;
}
.image-viewer__main {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
margin: auto;
}
.image-viewer__button {
display: inline;
background: none;
border: none;
border-radius: 50%;
}
.image-viewer__button img {
width: 80px;
height: 80px;
border: 1px solid transparent;
border-radius: 50%;
cursor: pointer;
}
.image-viewer__display {
position: relative;
padding: 15px;
margin: 3rem;
max-width: 80rem;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
font-size: 0.6rem;
}
.image-viewer__display-source-wrapper {
position: absolute;
font-size: 12px;
left: 50%;
margin-right: 50%;
transform: translate(-50%, -50%);
min-width: 100em;
text-align: center;
bottom: 0;
}
#display {
object-fit: contain;
width: 50rem;
height: 30rem;
margin-bottom: 1rem;
}
#source {
display: inline;
color: black;
}
This is because you've set a fixed width to your image. By setting the main image to 100% the image will fit and fill up the remaining space so the 3 elements are always distributed equally.
main image size = full width - both your arrows
current
#display {
object-fit: contain;
width: 50rem; /*fixed width*/
height: 30rem; /*fixed width*/
margin-bottom: 1rem;
}
amended
#display {
margin-bottom: 1rem;
width: 100%; /*was added*/
height: auto; /*was added*/
}
jsFiddle
Add css float:"right" in css button.

Align an <hr> element inside a flex Container

Does anyone know how to align an <hr> element inside a flex-container. When I do flex-start all of the other elements align, apart from the <hr>. I need a solution that doesn't use position: absolute on the <hr> element because this affects the document flow and causes other issues.
codepen: https://codepen.io/emilychews/pen/QaPQaW
CSS
body {margin: 0; padding: 0;}
.box {
width: 300px;
height: 300px;
background: red;
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
}
hr {
position: relative;
display: block;
background: white;
height: 3px;
width: 75px;
margin-left: 0 auto;
}
HTML
<div class="box">
<h1> Hello </h1>
<hr>
<p> Thanks </p>
</div>
The hr element has a default margin set, and in Chrome it is set to:
-webkit-margin-before: 0.5em;
-webkit-margin-after: 0.5em;
-webkit-margin-start: auto;
-webkit-margin-end: auto;
And as auto margin's in Flexbox override the justify-content/align-* properties, you need to remove it, which e.g. margin: 0; will, and make the in this case align-items: flex-start; be properly applied.
Stack snippet
body {margin: 0; padding: 0;}
.box {
color: white;
width: 300px;
height: 300px;
background: red;
display: flex;
flex-direction: column;
justify-content: center;
align-items: flex-start;
}
hr {
position: relative;
background: white;
height: 3px;
width: 75px;
align-self: flex-start;
margin: 0; /* added */
}
<div class="box">
<h1>Hello</h1>
<hr>
<p>Thanks</p>
</div>
Change your hr to
background: white;
height: 3px;
width: 75px;
margin-left: 0;

`position:absolute` with `bottom:0` is not working with parent having display:flex?

I want to fix div with class sidebar to the bottom of the container, but position: absolute with bottom:0 is not working with a container having display: flex.
how to solve that problem?
code: https://jsfiddle.net/zgmg48z1/1/
/*******************page layout**************************/
.container{
width: 100%;
background-color: #d5d5d5;
display: flex;
align-items: flex-start;
}
.sidebarcontainer{
width: 250PX;
/*height: 6000px;*/
display: inline-block;
box-sizing: border-box;
padding: 5px;
padding-right: 2px;
}
.innersidebarcontainer{
position: relative;
width: 100%;
height: 100%;
}
.sidebar{
width: 243px;
background-color: white;
height: 500px;
/*top: 1px;*/
/*bottom: 0;*/
/*position: absolute;*/
}
.mainpage{
width: calc(100% - 250px);
padding: 5px;
padding-left: 2px;
height: 600px;
display: inline-block;
box-sizing: border-box;
}
.page{
width: 100%;
height: 100%;
background-color: white;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: baseline;
align-content: flex-start;
}
.footer{
height: 500px;
width: 100%;
margin: 0 auto;
background-color: #031003;
}
/***************end of pagelayout******************/
.card{
width: 250px;
/*height: 400px;*/
align-self: flex-start;
margin: 10px;
display: inline-block;
}
.image{
width: 200px;
margin: 0 auto;
height: 250px;
}
.image img{
width: 100%;
height: 100%;
}
<div class="container">
<div class="sidebarcontainer">
<div class="innersidebarcontainer">
<div class="sidebar">
</div>
</div>
</div><!--
--><div class="mainpage">
<div class="page">
<h1>something in the page</h1>
</div>
</div>
</div>
<div class="footer"></div>
Try this:
.sidebarcontainer{
/*OTHER STUFF*/
align-self: flex-end;
}
Explanation here: https://css-tricks.com/snippets/css/a-guide-to-flexbox/#article-header-id-14
"This allows the default alignment (or the one specified by align-items) to be overridden for individual flex items."
If you want the sidebar to be at the bottom of the container, you can use align-self: flex-end on the sidebar container.
In this case you don't have to use an absolute position on your sidebar. But i'm not sure that's what your trying to do, maybe you need to have a 100% height (of the container) sidebar container and to align bottom the content of the sidebar. If you need that you can set an height on your sidebar container and a relative position. Then use position: absolute and bottom: 0 on the sidebar child.
/*******************page layout**************************/
.container{
width: 100%;
background-color: #d5d5d5;
display: flex;
align-items: flex-start;
}
.sidebarcontainer{
width: 250PX;
/*height: 6000px;*/
display: inline-block;
box-sizing: border-box;
/* Add align-self: flex-end to the sidebar-container */
align-self: flex-end;
padding: 5px;
padding-right: 2px;
}
.innersidebarcontainer{
position: relative;
width: 100%;
height: 100%;
}
.sidebar{
width: 243px;
background-color: white;
height: 500px;
/*top: 1px;*/
/*bottom: 0;*/
/*position: absolute;*/
}
.mainpage{
width: calc(100% - 250px);
padding: 5px;
padding-left: 2px;
height: 600px;
display: inline-block;
box-sizing: border-box;
}
.page{
width: 100%;
height: 100%;
background-color: white;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: baseline;
align-content: flex-start;
}
.footer{
height: 500px;
width: 100%;
margin: 0 auto;
background-color: #031003;
}
/***************end of pagelayout******************/
.card{
width: 250px;
/*height: 400px;*/
align-self: flex-start;
margin: 10px;
display: inline-block;
}
.image{
width: 200px;
margin: 0 auto;
height: 250px;
}
.image img{
width: 100%;
height: 100%;
}
<div class="container">
<div class="sidebarcontainer">
<div class="innersidebarcontainer">
<div class="sidebar">
</div>
</div>
</div><!--
--><div class="mainpage">
<div class="page">
<h1>something in the page</h1>
</div>
</div>
</div>
<div class="footer"></div>
This exact same problem happened to me recently. In my case it wasn't the fault of flex or even the positioning at all in the end, turns out I had another class that was cascading down and also effecting the element's absolute position making it look like my new values weren't working.