keep fixed div inside body on a large screen - html

<div id='wrapr'>
...
</div>
CSS
body{
max-width:1366px;
}
#wrapr{
position:fixed;
right:14px;
top:30px;
}
I just want #divr to bi fixed i.e. not scrollable on page.
But on a large screen (1920 px for example) it is placed outside of my bodytag !
How can I keep it fixed, but inside the body ?

A good rule of thumb for modern web development is to avoid px measurements at all costs. em, ch, vh/vw, and percents will generally provide a much better experience for users across a wide range of devices, aspect ratios, screen resolutions, etc.
body {
max-width: 100vw;
}
#wrapr {
position:fixed;
right: 1em;
top: 2em;
}
Sometime also to watch out for is for children elements which do not respect their parents max width. You may need to write CSS for these elements to ensure that they never grow larger than their parents.
#wrapr .someChildClass {
max-width: 100%;
}
For debugging this sort of thing, I find it extremely helpful to utilize Chrome DevTools. If you select the element you are interested in, it will show the entire box model including margin, content size, padding size, etc. in the style panel.
Finally, if you just want to truncate content, you can turn overflow off to prevent scrolling.
body {
max-width: 100vw;
overflow: hidden;
}
#wrapr {
position:fixed;
right: 1em;
top: 2em;
}

Related

Preventing page reflow due to image loading, while also imposing a max-width on the said images (HTML/CSS only)

I want to prevent page-reflow, caused by image loading on a web page.
Page reflow occurs when images load after the page's text content has already rendered. There's a 'jerk' caused by the said page-reflow. It makes for awful user experience.
My requirements are:
(i) All images be fully responsive
(ii) Have a max-width of 450px (while maintaining aspect-ratio)
(iii) Be center-aligned within their containers
There can be several images on the page. All have different aspect ratios (but scaled to the same width - i.e. 450px). I know their dimensions beforehand.
Currently my code is simply:
.container {
text-align:center;
overflow:hidden;
background:whitesmoke;
border-top:1px solid #F0F0F0;
border-bottom:1px solid #F0F0F0;
}
.container img {
width:100%;
max-width:450px;
vertical-align: top;
}
<div class="container">
<img src="https://s3.ap-southeast-1.amazonaws.com/damadam-2019/public/31a1b420-59c9-405a-a197-e04dd1e2eaf9.jpg" alt="image">
</div>
This fulfils all my requirements - except it can't prevent page reflow. How do I tweak this to get my desired result?
Traditional solutions to prevent such page-reflow go something like this:
HTML
<div class="container">
<img src="https://s3.ap-southeast-1.amazonaws.com/damadam-2019/public/31a1b420-59c9-405a-a197-e04dd1e2eaf9.jpg" alt="image">
</div>
CSS
.container {
display: block;
position: relative;
padding-bottom: calc(100%/(450/562));/* example width=450px height=562px*/
height: 0;
}
.container img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
This works fine. But it doesn't impose a max-width like I need it to. The image fills the entire container - as large as that container is (e.g. the full width of the screen on a laptop).
To tweak it, I tried adding max-width:450px;max-height:562px in .container img. That corrected the image's dimensions. But it gave the container extra padding at the bottom:
That's a shame. What I really wanted was for it to look like below:
Note that the gray colouration above is the background container, which simply disappears on smaller resolutions:
What's the best way for me to achieve my requirements? An illustrative example would be great.
Note: adding max-width: 450px;max-height: 561px; in .container doesn't solve the problem either.

Inconsistent CSS 100vh property with different browsers and operating systems

I'm making a patatap.com website clone for learning purposes. I want the site to take exactly 100% of the available screen height. There is no more content so no scrolling is needed. I'm using the 100vh CSS property for this:
body, html {
max-height: 100vh;
margin: 0;
}
This is the current version of the website:
http://patatap-clone.hexagonwebdev.vxm.pl/ (press any letter or number on keyboard to use it's functionality).
It is OK on Windows 7 on Internet Explorer and Firefox, but on Chrome (latest version - 70.0.3538.102) the site takes more than full screen and I can scroll down a bit.
It is bad on Ubuntu - on Chromium and Firefox around 30% of site is not visible (this laptop has smaller resolution).
Is there a better way to achieve the "non-scrolling" and 100% height goal? I have tried max-height: 100% and !important properties but that did not help.
Content will force just about anything to get bigger if you haven't set overflow properties. Conversely, setting max-height doesn't make the content bigger so the height will be the content height up to the max-height.
Finally, try not to set the height of the body/html tags. Those are "special" tags that don't function the same way other elements do.
Instead, create a container that is the full size of the screen.
/* normal div */
.container {
display:block;
height: 100vh;
overflow:hidden;
background: black;
}
/* or absolutely positioned div */
.container {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow:hidden;
background: black;
}

HTML: How to make an image NOT have influence on the width of whole site?

I have 2 images left and right from center which are placed nicely, but when the screensize is < 1920px, a scrollbar is created because the right image is going "out of the Site". I just want it to be cut to the screensize / go over the side of the screen without widening it.
CSS of the images (simply placed in the body):
#fans_l {
position: absolute;
text-align: left;
margin-left: -955px;
margin-top: -228px;
z-index:3;
}
#fans_r {
position: absolute;
text-align: left;
margin-left: 399px;
margin-top: -228px;
z-index:3;
}
Body css:
body {
height: 100%;
width: 100%;
margin: 0px;
background-image:url(p/back.jpg);
background-repeat: repeat; text-align: center;
}
In this case, there are a few things you can do. The first two that come to mind are as follows:
You can declare in the body css that the overflow-x property be set to either none or hidden, effectively cutting off your excess pixels. Though, at a fixed image size, this may not be desirable on smaller browsers. Keep in mind that many people have monitors smaller than 1920px.
You can use a nifty little tool present in CSS3 called Media Queries. With them, you can change css rules based on a monitor width or height. Using this method, you can ensure that it appear full on all sizes of browser windows.

2-column layout: divs either equal to height of longest, or height of viewport

So I've been trawling SO for an answer to my conundrum, and I know of course the whole chaos with faux-columns and backgrounds and overflow and negative margins but either I'm blind or nobody has solved this one yet.
The situation is very simple:
The body is 100% width.
I want a two-column layout.
If the content in both columns is smaller than the viewport height, I want both columns to be the height of the viewport.
If the content in either column is longer than the viewport height, I want both columns to be the height of the longest.
Is there no way to resolve this without too many CSS hacks? I don't mind terribly about backwards compatibility (for instance, I'm happy to use vw units), as long as the site doesn't fall apart completely in older browsers.
I'd post some example code but all I have so far is half a dozen files, each testing one of the popular solutions. The latest one is using vw:
HTML:
<div id="left">Left</div>
<div id="right">Right</div>
CSS:
body {
margin:0; /* This is used to reset any browser-default margins */
height:100vh; /* This is needed to overcome a Chrome bug. */
width:100vw; /* As above. */
}
div {
height:100vh;
}
#left {
background:snow;
float:left;
width: 20vw;
}
#right {
background:teal;
float:right;
width: 80vw;
}
This solves the first, viewport-height problem, but not the second, longest-column problem (as visible if you fill either column with content).
It's possible with pure CSS. However, it will not work in IE7 because it requires the use of display: table, and this demo uses the css viewport units which don't work in IE7/8.
CodePen demo
Setting a wrapper element to display: table and the two columns to
display: table-cell means we can ensure that the columns remain equal in height
Defining min-height doesn't work on table cells so we need an inner div that we can apply the min-height to. A bit of extra markup, but it solves the issue.
The inner content div has a min-height of the browser viewport. If the height changes by css or content, the table columns will grow.
As a test, I left a commented height style so you can see what it looks like when scrolled.
html, body {
margin:0;
height:100%;
width:100%;
overflow-y: scroll;
}
#wrapper {
display: table;
min-height: 100%;
table-layout: fixed;
}
#wrapper>div {
display: table-cell;
min-height: 100%;
}
#wrapper>div:first-child {
background:snow;
width: 20vw;
}
#wrapper>div:last-child {
background:teal;
width: 80vw;
height: 100%
}
#wrapper .content {
min-height: 100vh;
/*height: 2000px;*/
}

Dynamically change element height when resizing viewport

Is it possible to change the height of an HTML element when the viewport resizes with pure CSS? It's hard to explain the problem, but I'm still going to try:
What I want, is a page with a header, content and a footer, like most webpages. As I'm working with a 1920x1080 monitor, I'm using that as my standard. The problem however is that not everyone is using a 1080p monitor. Some are using the older standard, 1280x1024 or using a tablet where the height can be 2560px (I'm not doing smartphones, as they will have a completely different design due to the small screen width). On my page I have images, covering a fixed width. If this width is greater than the width of the viewport, the images will be displayed underneath each other:
(Right-click on the image and select "show image" to view at full size)
As you can see in this image, when the viewport is smaller, the images will stack and will fall from the background. The 'Follow me on:' section even felt of entirely. What I want to do is, when this happens, to make the content div larger, so all of the content stays on the page. I know this is possible using height: auto, but when you do that, the fixed height of the footer will follow after it, and on a screen with a large height, there might be a white border because the document height is smaller than the viewport height.
Take some time to learn min-width, min-height, max-width, max-height, (css attributes) and device-width, device-height (css default values of the client viewports). I can not guarantee they would refresh while you drag/resize the browser window or viewports in devices, but I think they help your style rules.
It's slightly unclear to me what your end-goal is with this so I did my best interpretation. If it's not what you're looking for, give me a good mental image of what you're trying to do and I'll try to correct it.
Live Demo
CSS:
html, body {
box-sizing: border-box;
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#wrap {
position: fixed;
top: 0;
bottom: 0;
width: 100%;
}
#header, #content, #footer {
position: absolute;
width: 100%;
}
#header {
top: 0;
height: 70px;
background: lightblue;
}
#content {
overflow-y: auto;
top: 70px;
bottom: 70px;
background: limegreen;
}
#footer {
bottom: 0;
height: 70px;
background: purple;
}
HTML:
<div id="wrap">
<div id="header">Header</div>
<div id="content">Content</div>
<div id="footer">Footer</div>
</div>