Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a div appear at the same position regardless of screen size

I am developing a website for a client based upon Google material design (i.e. heavy use of cards).

What I am wanting is to have the first card of the page be constantly peeking just above the bottom of the screen at the half way point of the floating action button like in the image below.

peeking card

My problem is that this does not work well when the site is being viewed on different devices with screen resolutions. I have tried using percentage and em's to pad this card from the top but these have failed and the card changes position. My remaining option would be to use media queries but this would potentially prove to be convoluted.

The following is the CSS for the card as it currently exists:

#content-card
{
    position: relative;
    display: table;
    background-color: white;  
    top: 0;
    left: 0;
    right: 0;    
    width: 100%; 
    overflow: hidden;
    box-shadow: 0px 0px 15px 0px rgba(0,0,0,0.3);
    z-index: 2;
    height: auto;
 }

Any advice and insight into how I could achieve the desired effect is welcome.

Thanks :)

Basic jfiddle implementation as requested :- https://jsfiddle.net/7fudv084/1/

like image 566
Jack Avatar asked Nov 11 '16 21:11

Jack


People also ask

Does the <Div> stick to the top of the screen?

As you can see, the <div> sticks to the top of the screen, but it goes back to its original position when scrolling back to the top of the page. The element with position: sticky; is positioned regarding the user's scroll position.

How do I make a Div take up the entire screen?

position:absolute. You can also use position absolute as well as setting all the viewport sides (top, right, bottom, left) to 0px will make the div takes the full screen. You can also set height and width to 100% instead of setting 0 to top, right, bottom and left.

How to make a Div take the full width horizontally?

I do not have to add one for horizontal as div is a block-level element that will take the full width horizontally by default. You can also use position absolute as well as set all the viewport sides (top, right, bottom, left) to 0px will make the div take the full screen.

How to make a Div take full screen viewport?

You can also use position absolute as well as setting all the viewport sides (top, right, bottom, left) to 0px will make the div takes the full screen..box { background: red; position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; } You can also set height and width to 100% instead of setting 0 to top, right, bottom and left.


1 Answers

Use vw (viewport width) instead of percentage. 1vw = 1/100th of the viewport width - regardless of device PPI, etc. So, if you want the div to remain proportionally fixed regardless of device size and scren resolution - set your horizontal axis position with VW.

Example:

.somediv{ margin-right:20vw}
like image 97
Korgrue Avatar answered Oct 03 '22 05:10

Korgrue