This seems like a simple question, but I couldn't find anything here/with search engines.
Let's first define what I mean by "same": I mean that if I put a ruler (the plastic one) next to the screen, the box would always, in different zoom levels, be the same width in centimeters (whatever that would be).
Let's also say that I'm talking about desktop browsers, since I don't want to complicate things by taking mobile into account. Also the browser should be wide enough to let there be some extra space for zooming.
Here is the box:
https://jsfiddle.net/a4be6ov5/
<div></div>
div {
border: solid 1px #000;
width: 800px;
height: 200px;
margin: auto;
}
Now, if you zoom in/out, the box width will change. The browser's Developer Tools will always show that the width is 200px, because the way this zoom thing works. But what I would actually want, is that I would want it to match the initial width compared to that ruler of ours.
I can partly do that with viewport units, but I couldn't figure out to do it automatically by calculating something. I could only do it by manually defining all the steps at which to change the box width. This turns out to be cumbersome and there are too many steps to do it at. For example I could do this:
#media screen and (min-width: 1500px) {
div {
width: 53vw
}
}
... not very easy.
How to do this automatically for all the screen widths and zoom levels?
What about having a slider on your page which alters the font size of your body element (or selected elements)?
document.querySelector('input').addEventListener("input", evt => {
document.querySelector('.body').style.fontSize = evt.target.value + 'px'
})
.a, .b {
background-color: cyan;
width: 250px;
height: 250px;
padding: 1em;
overflow: auto;
display: inline-block;
box-sizing: border-box;
}
.a {
background-color: cyan;
}
.b {
background-color: lime;
}
<p>
Smaller <input type="range" min="10" max="22" value="16"> Larger
</p>
<div class="body">
<div class="a">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vel aliquet mauris. Donec ipsum orci, ornare et tellus at, pretium aliquet nibh. Praesent quis tincidunt tortor. Integer ac varius nisi. Integer tempus varius justo. Quisque eget elementum sapien. Mauris id blandit arcu. Mauris dui erat, ultrices vitae ligula vitae, auctor cursus lacus.
</div>
<div class="b">
Cras venenatis, nunc in tempus dictum, justo augue imperdiet nisl, id rutrum eros quam sed arcu. Maecenas fringilla diam in erat venenatis, sed sagittis elit tincidunt. Vivamus vel varius ex, id scelerisque ante. Donec ultricies, urna at aliquet gravida, urna erat porta nibh, vel semper magna urna eu dolor. Nullam condimentum ex ligula, a fringilla tortor eleifend in. Vestibulum congue eget lectus vel congue. Praesent eget malesuada est. Nulla nec semper nunc. Mauris id nulla molestie, varius turpis ut, pulvinar tortor.
</div>
</div>
Related
Hi I have responsive menus sliding from right and left, only left, or it could be hidden at all. So the main-content is fluid based on this.
I make it fluid by setting proper margin-left and width properties, which looks like this:
.main-container {
width: 100%;
}
.main-content {
width: 100%;
margin-left: 0px;
}
.main-container:not(.menu-hidden) .main-content {
width: calc(100% - 500px);
margin-left: 250px;
}
#media (max-width: 1200px) {
.main-container:not(.menu-hidden) .main-content {
width: calc(100% - 300px);
margin-left: 300px;
}
}
#media not print {
.main-content {
transition: width 1s, margin 1s;
}
}
#media print {
.main-content {
width: 100% !important;
margin-left: 0 !important;
transition: none;
}
}
<div class="main-container">
<div class="main-content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque acipsum elit. Donec malesuada purus quis mauris accumsan tempus. Sed ipsum tellus, ultrices posuere odio eget, aliquet convallis urna. Aliquam pulvinar consequat lectus, a gravida quam mattis sit amet. Curabitur molestie fringilla velit sed cursus. Donec a risus diam. In interdum porttitor sem.
Nullam tincidunt lacus nec leo accumsan aliquet. Integer id justo et urna varius euismod ac non orci. Curabitur sed dignissim nibh, at consectetur augue. Maecenas id tortor id enim maximus laoreet. Etiam id aliquet odio. Integer vel nibh non sapien rhoncus consequat sit amet in nisi. Etiam condimentum vel tortor eu dignissim. Morbi sodales luctus libero non dictum. In fermentum elit et congue efficitur. Vestibulum nunc turpis, posuere vel odio ut, lobortis rutrum sem.
</div>
</div>
https://jsfiddle.net/z7hnstfx/2/
I also added the media print so there will be no margins while printing. But with this declarations it doesn't work in chrome if the screen width is bigger then 1200px...
What is weirdest thing about it, if you remove the transition line, or the declarations in max-width media, it will work. I really run out of ideas what the hell is wrong with it. Anyone can help?
I'm working on an old website for someone and can't understand something with it's css:
I have a div the contains the page (article) content, which includes of course some images. in the top of the div there's another div, with extra information about the article. this second div is floated to the left.
<div class="entry-content">
<div class="lefttable"> //floated to the left
//some information here
</div>
//content here, including images
</div>
somehow the imgs inside the content are full sized even on the top of the page, and where they supposed to be beside the lefttable div, they jump beneath it.
here a print-screen: https://snag.gy/qFChjB.jpg
and the page itself: http://www.bayadaim.org.il/95b
Thanks,
Itamar
The parent of your image has
an inline style rule of width: 970px
wp-caption and aligncenter classes which mean:
width: 650px !important from style.css:1271
display: block; from style.css:1257.
All the above rules forbid your element from displaying inline, side by side with the floating content that precedes it.
You need to give the parent element of your image a width that compensates the width of the floating content responsively (you can do that using max-width and calc, provided that the page container has position:relative, which it does) and also you need to set it's display to either inline, inline-block or inline-flex. I recommend inline-block.
That's the theory.
In practice, for your very specific case, you also need to compensate for some padding/margin of the left-floating elements. Here's the CSS:
#post-34917 #attachment_34937 {
display: inline-block;
max-width: calc(100% - 220px);
position: relative;
margin-left: -20px;
}
#content .aligncenter>img {
width: 100%;
height: auto;
}
#media (max-width: 1023px) {
#post-34917 #attachment_34937 {
max-width: calc(100% - 170px);
}
}
You can give a try to max-width and calc():
.lefttable {
width:200px;
float:left;
}
.right {
overflow:hidden: /* to deal with floats in and out */;
}
.right img {
max-width:calc(100% - 200px); /* where 200px is the room used by lefttable ( mind borders, padding and margins) */
}
<div class="entry-content">
<div class="lefttable"> <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</div>
<div class="right">
<img src="http://dummyimage.com/1200x200" />
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</div>
</div>
Using some jQuery I found the way. It is not responsive, but neither this theme I'm editing - so it's good enough for me. And of course anyone who wants to develop it, is very welcome to do so.
CSS Part
Set width for all the images wrapper (wp-caption) to the width of the main content div where it is narrower, next to the floating div.
#content .wp-caption {
width: 66%;
}
jQuery Part
//make all images in posts 100% width except images next to the left table
jQuery(document).ready(function(){
var h = jQuery(".lefttable").height(); //get left table height
jQuery('.entry-content .wp-caption').each(function(){ //loop through all .wp-captions in the content
jQuery(this).removeAttr('style') //remove any disturbing inline styles. optional.
var p = jQuery(this).position(); //get each .wp-caption position
var top = p.top; //top position
if(top > h){ //if .wp-caption is below .lefttable
jQuery(this).css("width", "initial") //change .wp-caption width to original (or anything you like)
}
});
});
That's it. I hope someone could benefit from that.
Itamar
I need some help with hiding my horizontal scrollbar and still able to scroll. I have used webkit but does not work in IE and firefox. I have seen a lot of help with vertical scrollbar, but does not work with horizontal. Any help?
Update:
I have created a JSFiddle to show my problem. I want to hide the horizontal scrollbar and still able to scroll without using
::-webkit-scrollbar {
display: none;
}
http://jsfiddle.net/o1xoh9w8/1/
Here is how you do it, I have tested this in Chrome, IE, Firefox, Opera, Safari(Windows) and Edge
<h1>You can scroll with mouse wheel</h1>
<div id="box">
<div id="content">
<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
</div>
</div>
h1{font-weight:bold;font-size:2em;} /* ignore only for header */
/* *********************** */
div#box{
height:200px;
width:300px;
overflow:hidden;
border:1px solid black;
padding: 10px;
}
div#content{
height:200px;
width:326px;
/*
* Uncomment to see scrollbar
width:300px;
*/
overflow:auto;
}
Here is a JSFiddle: http://jsfiddle.net/JoshMesser/VUSuZ/
Credits go to creator of the JsFiddle
EDIT:
For vertical it is just a matter of changing the height. What you are doing is you are just pushing the scroll bar outside of what user can see, so to them its not there, while in reality it is there hidden behind elements. Here is a JS Fiddle based on my last one. You will see I just forced p to be in single line to get horizontal scrolling and then increased the height to hide the scroll-able bar.
http://jsfiddle.net/VUSuZ/575/
I used a fixed height approach.
Note: this approach can help only in specific cases.
#container1{
height: 50px;
/* Just for presentation. Can be removed */
border: 1px solid red;
/* Hides content outside this container */
overflow: hidden;
}
#container2{
/* Height is significantly greater than the height of container#1 to hide
any possible scroll */
height: 100px;
overflow: auto;
white-space: nowrap;
}
<div id="container1">
<div id="container2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam hendrerit, ante laoreet mattis blandit, arcu nisi blandit felis, et molestie justo lacus et sem. Nunc tempor tellus sit amet eleifend tristique. Integer eget condimentum lectus, nec viverra risus. Nullam leo lectus, placerat vitae porta eget, auctor et nisi. Suspendisse feugiat in lacus accumsan tincidunt. Fusce pulvinar accumsan sem sit amet finibus. Curabitur volutpat mi vitae eros mattis congue. In ut sem eu tellus egestas lobortis vitae eu felis. Maecenas sodales, nisl eu bibendum vulputate, neque leo finibus odio, sit amet bibendum libero dolor sed diam. In molestie magna vitae dui vulputate, eu consequat dui ullamcorper. In hac habitasse platea dictumst. Vestibulum pulvinar, mi quis mollis pulvinar, metus justo aliquet arcu, vel venenatis ipsum dolor at sapien. Sed ac odio bibendum, feugiat nibh at, viverra mi. Morbi sem nisi, ultricies non nulla pretium, gravida malesuada neque.
</div>
</div>
(My case is horizontal scrollable buttons container for mobile screens - the buttons are stuck to the above block (+ margin) and have fixed height)
I think you do not want to use
::-webkit-scrollbar {
display: none;
}
Because it will hide all the scroll bars.
A better way to hide the scroll bar but still enable scrolling in a particular container will be to follow the following example:
HTML
<div class="container">
<table>
<tbody>
<tr>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
<td>Example</td>
</tr>
</tbody>
</table>
</div>
CSS
.container {
overflow-x: auto;
white-space: nowrap;
}
.container::-webkit-scrollbar {
display: none;
}
Assuming I have the following markup:
<div id='container'>
<div id='content'>
</div>
</div>
And css:
#container {
width: 100%; /* container fills window */
height: 100%;
max-width: 1000px;
}
#content {
width: 100%;
padding-top: 66%; /* (1.5:1 aspect ratio */
object-fit: contain;
}
This has the behaviour I want (even without the object-fit) whenever the
browser aspect ratio is smaller than 1.5:1. I would like the #container
element to always stay completely in view, while also maintaining the aspect ratio.
Is this at all possible in pure css (I do not mind adding extra elements)?
I do not want to use vw and vh because the width of the container is bounded by max-width.
It seems you want something like this:
body {
margin: 0;
}
#container {
position: relative; /* Containing block for absolutely positioned descendants */
float: left; /* Shrink-to-fit width */
background: red;
}
#container > canvas {
display: block; /* Avoids vertical-align problems */
max-width: 100%; /* Like object-fit:contain (part 1) */
max-height: 100vh; /* Like object-fit:contain (part 2) */
}
#content {
position: absolute; /* Take it out of flow */
top: 0; right: 0; bottom: 0; left: 0; /* Same size as containing block */
overflow: auto; /* In case its contents are too big */
}
<div id='container'>
<canvas width="1000" height="666"></canvas>
<div id='content'> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non nulla augue. Vivamus hendrerit arcu id fermentum vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed non efficitur eros. Mauris pulvinar tortor eros, vitae mollis est suscipit non. Sed accumsan mi vel odio sollicitudin sagittis. Curabitur euismod justo et lorem suscipit tempus.Fusce enim metus, maximus sed lacinia ut, ultrices eu arcu. Vivamus interdum ex ac justo pretium pulvinar. Integer ornare vulputate ligula nec imperdiet. Sed suscipit nisi metus. Aliquam massa ante, dapibus laoreet mauris et, dignissim malesuada urna. Vivamus eleifend pellentesque nisl vitae laoreet. Phasellus a fringilla mauris. Nunc condimentum dui est, eget lobortis ipsum feugiat dictum. Vivamus ultricies, nisi ac gravida luctus, leo augue pulvinar massa, sit amet dictum eros magna at justo. Vivamus eu felis a ipsum auctor imperdiet. Donec eget bibendum tortor. Pellentesque mollis, orci ac molestie mollis, mi eros commodo magna, ac rutrum tellus ipsum in tortor. Nulla vel dui egestas, iaculis felis id, iaculis sem.Vivamus vel varius magna. Vestibulum vulputate massa quis urna finibus rhoncus. Etiam varius in dui fermentum venenatis. In fermentum enim sed laoreet porta. Proin sit amet auctor sapien, eu dapibus nunc. Praesent malesuada leo nec libero interdum efficitur. Nulla ipsum est, tristique ut malesuada id, blandit at odio. Interdum et malesuada fames ac ante ipsum primis in faucibus. Nullam ac ipsum tristique, feugiat justo eu, pellentesque odio.</div>
</div>
It uses canvas with its width attribute set to the maximum desired width, and its height attribute given by the aspect ratio. Then it is styled with max-height: 100vh and max-width: 100% to achieve something like object-fit: contain.
Since #container has height: auto and float: left, its size will be the same as the canvas.
Then you can add some content inside an absolutely positioned element with the same size as #container.
I have a css problem like this:
I have 3 divs, imagine they're all stacked in a column (like table with 3 rows) and the top one and the bottom one has height: 100px; and position: absolute; for example and they're like header and footer, they always stick to the top and the bottom of my form.
The height of the whole form can vary depending on other elements in the form, so I need my middle div to be just between the other 2 divs, and if it doesn't fit with it's content, to scroll.
(For example header and footer are 100px height, the form is just 500px height and the text in middle div is very very long) <- at the moment the middle div expands and streches the whole form.
I've tried many things to solve this with positioning, marging, padding of the elements, but still no success... Can anyone help me? Thanks!
You can do it this way: jsFiddle
CSS
.header, .footer {
background-color: green;
height: 100px;
width: 100%;
}
.form {
height: 300px;
position: relative;
}
.content {
position: absolute;
top: 100px;
bottom: 100px;
overflow-y: scroll;
}
.footer {
position: absolute;
bottom: 0px;
}
What's wrong with just a little bit of JS as simple as this?
var form_height=300, footer_height=100, header_height=100;
document.getElementById('content').style.height=form_height - header_height - footer_height +'px';
Just adding that your code works:
http://jsfiddle.net/n8sZ7/23/
HTML
<div class="form">
<div class="header"></div>
<div id="content" class="content">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc
</div>
<div class="footer"></div>
</div>
CSS
.header, .footer {
background-color: green;
height: 100px;
}
.form {
height: 300px;
}
.content {
overflow-y: scroll; <!-- I want this to fit between header and footer (in this example to get 100px of height) without using any javascript -->
}