Here is my css:
.contain
{
min-width: 300px;
background: black;
height: 200px;
display: inline-block;
overflow: auto;
}
.inl1{
/* margin: 5px 5px 5px 5px; */
min-width: 300px;
background: blue;
width: 400px;
height: 200px;
}
<div class=contain>
<div class=inl1></div>
</div>
<div class=contain>
<div class=inl1></div>
</div>
Clearly the two divs display inline, which is what I want.
However, when the browser is resized smaller the divs are displayed one above the other (desired behaviour), but once I make the browser window smaller than min-width, I need to have horizontal scrollbars displayed. This is not happening.
Any help as to why?
Edited: I tried the suggestions here, but they all seem to break the desired behaviour of the divs stacking on top of each other when the browser is sized smaller.
The effect I am after:
display the divs inline (with no scrollbars) in a browser that is wide enough; but in a "narrow" browser (ie mobile) display the divs one on top of another and THEN add horizontal scrolling ONCE the min-width can no longer be displayed for each div.
I think that's a little clearer...
You just need to have a wrapper for the divs and set it with
.wrapper{
min-width: 100%;
white-space: nowrap;
}
Here is the Fiddle: https://jsfiddle.net/1hshzxah/3/
Your outside boxes have the same minimum width as your inside ones, so both will be at least 300px wide, so no scrollbars appear. Because of the defined pixelwidth of your outer elements, your they will not stack next to each other if you do not have 600 pixels to play with or more. If you give your outer boxes a width that can scale (by using % or vw) with the page width, your result magically appears:
.contain {
width: 45%;
background: black;
height: 200px;
display: inline-block;
overflow: auto;
}
.inl1{
/* margin: 5px 5px 5px 5px; */
min-width: 300px;
background: blue;
width: 400px;
height: 200px;
}
#media all and (max-width:600px){
.contain {
width: 100%;
}
}
<div class=contain>
<div class=inl1></div>
</div>
<div class=contain>
<div class=inl1></div>
</div>
(I use 45% because I did not want to bother with floating these nicely next to each other, but you could with some more CSS). You can still add a max-width of 300 pixels to your containers to make sure they don't grow beyond 300px, but still shrink otherwise.
Related
Desired Outcome:
Further Details:
Right now I am setting the Div height based on estimates (ie. 10% for search bar, 60% for middle and 30% for bottom) and set the thumbnail size to fit well on my Samsung Phone. The problem is that on different phones, the width is different and Div3 ends up with large borders. To complicate matters, Div2 can scroll up/down (minor problem) but Div3 can scroll left/right (moderate problem).
All thumbnail images are guaranteed to be 16:9 (I believe) as they are obtained from here https://img.youtube.com/vi/NJ2YyejQjpw/maxresdefault.jpg
I'm having conceptual issues trying to size a div (Div3) based on the height generated when an image is stretched the (more or less) the screen width)
Question:
How can I get the three below divs, while allowing for (1) vertical scrolling in Div2 and (2) horizontal scrolling in Div3
Code:
JSFiddle code. Try on iPhone X, it looks weird
Important sections:
#search_bar {
width: 100%;
height: 10%;
border: 0px;
float: right;
padding:0px;
position: relative;
background-color:red;
}
#search_results {
width: 100%;
height: 58%;
padding:4px;
float: right;
overflow:auto;
background-color:green;
}
#playlist_reel {
width: 100%;
height: 32%;
padding:4px;
clear: both;
overflow-x: auto;
white-space: nowrap;
background-color:blue;
}
I went off the diagrams you included and tried to replicate a simple version of it.
Use flexbox for column layout
Have #search_results take up as much space as possible
Have #search_barand #playlist_reel take up only the space they occupy (totally adjustable)
Use a background-image for #search_results so the element is always covered.
Use a static img in .bottom so that it takes up actual DOM space
As for scrolling, the requirement feels a little broad at the time I am posting this. Maybe this demo will get you close enough to experiment with scrolling on your own.
Note: This demo is more easily viewed in either "full page" mode (here in SO) or in the jsFiddle.
html, body { margin: 0; }
.container {
min-height: 100vh;
display: flex;
flex-direction: column;
width: 380px;
max-width: 100%;
margin: 0 auto;
}
#search_bar [type=search] {
width: 100%;
padding: 1em;
font-size: 1.25rem;
}
#search_results {
flex-grow: 1;
background-image: url('http://placekitten.com/400/500');
background-size: cover;
}
img {
max-width: 100%;
height: auto;
}
<div class="container">
<div id="search_bar">
<input placeholder="Search" type="search">
</div>
<div id="search_results"></div>
<div id="playlist_reel">
<img src="http://placekitten.com/400/50" alt="">
</div>
</div>
jsFiddle
Actually it has to be a little less that 100% because 100% sets it a little off screen to the right. When I load my page on a tablet or laptop screen it zooms into the top left of the screen, if I zoom out I see that the divs only take up about half the page left to right. I've tried playing around with min-width with no luck.
.first {
height: 75px;
width: 99.55%;
background-color: red;
margin-top: 19px;
border: 3px yellow solid;
min-width: 1000px;
margin: 0 auto;
}
h2 {
text-align: center;
width: 100%;
}
<div class="animated fadeInUpBig first o">
<h2>Need to fix positioning on mobile</h2>
</div>
<div class="animated fadeInUpBig first t">
<h2>More work will be done tomorrow evening</h2>
</div>
<div class="animated fadeInUpBig first tr">
<h2>Make it look half good by weekend</h2>
</div>
If you are wanting each red block with yellow border to span a specific width (to be 90%, 300px, etc.), you can add box-sizing: border-box to the element which allows for the width to include both padding and border. However since both the .first (a div) and the h2 are both block levels elements, by default they will take up 100% width of their parent.
I consolidated the margin-top with the margin: 0 auto; which keeps the margin: from overriding the margin-top.
Also the shorthand for border should be width, style, and color, so I have altered it slightly.
Lastly, the body element has a default margin of 8px in most browsers, so in order to get your boxes to touch the outside of the browser window, you'll want to add that last body property or use a common CSS reset (such as http://meyerweb.com/eric/tools/css/reset/)
.first {
height: 75px;
background-color: red;
border: 3px solid yellow;
min-width: 1000px;
margin: 19px auto 0;
}
h2 {
text-align: center;
}
body {
margin: 0;
}
http://codepen.io/phawley/pen/amLRYK
I have a responsive header which is quite complex.
The left block is fixed width and the right block is a percentage (100%) I found this great article here on how to do that except I need it the other way around, this example is right block fixed.
http://radiatingstar.com/make-a-layout-with-fluid-and-fixed-size-columns
I did get it working at one point but can't remember how I did it, there should be no scrollbar the outer container should be 100%. The real issue is that in the right block I have 2 inner divs, 1 div should be horizontally centered on the screen not centered in it's div as the fixed left block has pushed it over already.
http://jsfiddle.net/3519a9p0/1/
<div id="container">
<div id=fixed-width>
</div>
<div id=fluid>
<div class="farRight">right icons</div>
<div class="centeredBlock">centered on screen block</div>
</div>
</div>
And the other challenge is the responsive part in that the right icon block as you can see that that is floated to the right should move on top of the centered block as the screen width shrinks.
It would appear that I need to float the centered block too but then it needs to the centered middle of the screen too.
The the fixed width left block could potentially be a float too but it doesn't really matter as after the screen gets to small I switch to completely different layout, it's just the 2 inner divs that I need centered and responsive.
You're a genius if you can solve this!
Cheers!
Here is a working example. I just got rid of the margin and the float. However, while the answer was simple, you should read on below to understand why this worked.
Working Example
Because the left div has a float: left attribute, you can just set the right div to take up 100% of the remaining space. You do not need the negative margin to work the div into its place.
Also, a floated element is taken out of the normal flow of the document, so now you can use margin: 0 auto and as long as the right div has 100% width, it will center across the entire screen.
Update
There were post-question requests made via comments. To solve the issue, I added media queries and removed the float on the right-side div. Also, I had to add extra markup so that the inner divs on the right-hand side could be absolutely positioned properly.
Here is revised CSS. The major changes are:
No need to float the fluid column, add left margin instead
To center a box on the screen, set relative positioning on the container (not the fluid box) and use absolute positioning on the box
As for responsiveness, you can simply remove float, width, height and positioning from elements so that they appear as rows.
/* body margin/padding is reset to get media queries right */
body {
margin: 0;
padding: 0;
}
#container {
position: relative;
height: 100px;
}
#fixed-width {
float: left;
width: 250px;
height: 100%;
background-color: blue;
}
#fluid {
margin-left: 250px;
height: 100%;
background-color: green;
}
.farRight {
float: right;
width: 100px;
height: 40px;
color: white;
background-color: black;
}
.centeredBlock {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
width: 200px;
height: 40px;
color: white;
background-color: tomato;
}
/* when screen is narrower than 250+200+250 pixels trigger breakpoint 1 */
#media screen and (max-width: 699px) {
.farRight {
float: none;
width: auto;
}
.centeredBlock {
position: static;
width: auto;
}
}
/* when screen is narrower than whatever-you-want pixels trigger breakpoint 2 */
#media screen and (max-width: 499px) {
#container {
height: auto;
}
#fixed-width {
float: none;
width: auto;
height: auto;
}
#fluid {
margin-left: 0;
height: auto;
}
}
<div id="container">
<div id="fixed-width">fixed width</div>
<div id="fluid">
<div class="farRight">right icons</div>
<div class="centeredBlock">centered on screen block</div>
</div>
</div>
I'm trying to do a 4-frame design with css, as in this code:
http://jsfiddle.net/7qBKJ/1/
But I don't want to use position:absolute;, and I'm trying to do it like this:
topframe: block;
left,right and centerframes: inline-block;
And I want to ensure there is, say, 200px of width in both rightframe and leftframe, and the remaining parts should be filled by centerframe. How can I manage this without absolute positioning?
I tried this, but it moves the frames up and down, when the screen width decreases :
http://jsfiddle.net/V4vAc/2/
in this fiddle, centerframe aligns with leftframe, since they are both inline-block, with centerframe rule margin-left:0px; but I have no idea how to set centerframe's right to align with rightframe's left, without specifying a width.
So how can I make #centerframe's width equal to screen width - 400 px ?
Thanks !
What you have to do is to put both sidebars first in the flow of the document. Then you float the first sidebar right and the second one left.
<div id="left"></div>
<div id="right"></div>
<div id="center"></div>
/* main.css */
#left {
width: 200px;
height: 100px;
background-color: blue;
float: left;
}
#right {
width: 200px;
height: 100px;
background-color: red;
float: right;
}
#center {
width: auto;
height: 100px;
background-color: green;
margin: 0 200px;
}
http://jsfiddle.net/samgh/JjsFF/
If you want center to be the full width underneath the two sidebars, you can remove the margins. Hope this helps.
I have a container div which has children anchored to the bottom. The problem is that when the div's overflow scrollbar appears, the bottom margin of the last child gets hidden.
Please see http://jsfiddle.net/TxEAP/3/. At first, there's a correct margin underneath the 1 div. Clicking "append one" so that the scrollbar eventually appears makes the last div not have a bottom margin anymore. Opening DevTools shows that the margin of that last child is there, but it is outside of the container's viewport, even when scrolling completely to the bottom.
How can this be solved? It would suffice to get this working in Google Chrome.
HTML:
<div class="main">
<div class="container">
<div class="item">1</div>
<!-- several of these .item divs -->
</div>
</div>
CSS:
.main {
height: 200px;
overflow-y: scroll;
position: relative;
border: 1px solid black;
}
.container {
width: 100%;
max-height: 100%;
position: absolute;
bottom: 0;
}
.item {
padding: 20px;
margin: 15px;
border: 1px solid black;
}
Here's my final solution using flexbox. It's supported well enough on Chrome despite all -webkit- prefixes. Basically, the idea is to have a dummy element that, in case of no overflow, fills up the space of the container starting from the top (so that the real children are anchored to the bottom); in case of overflow, it is hidden automatically because of height: 0. It does not suffer from the margin issue, and it does not collapse margins.
http://jsfiddle.net/mCYLm/1/
HTML:
<div class="main">
<div class="gap-filler"></div>
<div class="item">foo</div>
<!-- more `div.item`s -->
</div>
CSS:
div.main {
display: -webkit-box;
-webkit-box-orient: vertical;
height: 200px;
overflow-y: scroll;
}
div.main div.gap-filler {
-webkit-box-flex: 1;
height: 0;
}
div.main div.item {
border: 1px solid black;
margin: 20px;
padding: 20px;
}
Edit: This was a solution without flexbox, but it had selection issues.
A solution that eventually worked was the following: http://jsfiddle.net/TxEAP/7/. This appends hidden "content" which makes Chrome not hide the margin of the last .item div.
.container:after {
content: "";
font-size: 0;
display: block;
height: 1px;
}
Edit: The following only works if display: inline-block is possible.
Finally I found a solution. If all .items have display: inline-block except the first one, then the margin does not get hidden.
http://jsfiddle.net/TxEAP/5/
.item:not(:first-child) {
display: inline-block;
/* attempt at getting `width: auto` like `display: block` has */
width: -webkit-calc(100% - 2 * 15px);
box-sizing: border-box;
}
If you just move the overflow-y: scroll; from .main. to .container class then the margin is preserved. The only drawback is for less than 3 items (for the given container height) you get a small scrollbar placeholder, instead of a full height one.
Removing max-height:100% on the container seems to fix it for my test in Chrome 21.
Moving the properties so that the overflow is on the container, preserves the margin/padding for an element added to the end that results in the scrollbar appearing.
.main {
height: 200px;
border: 1px solid black;
}
.container {
width: 100%;
height: 100%;
overflow-y: scroll;
}