Horizontally scrolling div within display:table div? - html

I am able to make a horizontally scrolling div using the following:
CSS
.scroll {
width:100%;
height:100px;
overflow-x:auto;
white-space:nowrap;
}
.box {
width:200px;
height:100%;
display:inline-block;
vertical-align:top;
}
HTML
<div class="scroll">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
However, once this is nested inside a div with display:table, the .scroll div no longer scrolls and instead stretches the .scroll div to show all of the boxes.
Pretty sure there's an easy fix for this, any ideas?
For reference: http://jsbin.com/makigome/29/edit?html,css,output

Try this CSS, hope it helps
.table {
display: block;
width:100%;
}
.table-cell {
display:block;
width:100%;
}
.scroll {
height:100px;
width:100%;
overflow-x:auto;
border:1px solid red;
white-space:nowrap;
margin:10px 0px;
}
.box {
width:200px;
height:100%;
background:orange;
display:inline-block;
vertical-align:top;
}

Is not possible using table and table cell, you can do it using float like this:
.table {
float:left;
width:100%;
}
.table-cell {
float:left;
width:100%;
}
.scroll {
clear:both;
height:100px;
width:100%;
overflow-x:auto;
border:1px solid red;
white-space:nowrap;
margin:10px 0px;
}
.box {
width:200px;
height:100%;
background:orange;
display:inline-block;
vertical-align:top;
}

For anyone interested, I fixed this by setting position:absolute to the .scroll div and wrapping it within another div with position:relative like so:
http://jsbin.com/makigome/39/edit?html,css,output
<div class="table">
<div class="table-cell">
<div class="scroll-wrapper">
<div class="scroll">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
</div>
</div>
</div>
.table {
display:table;
width:100%;
}
.table-cell {
display:table-cell;
width:100%;
}
.scroll-wrapper {
height:100px;
position:relative;
}
.scroll {
height:100%;
width:100%;
overflow-x:auto;
white-space:nowrap;
position:absolute;
}
.box {
width:200px;
height:100%;
display:inline-block;
vertical-align:top;
}

Related

float div to left of parent div not working

With my current code, how do I make the purple div stick to the left side of the green div? I tried float:left which didn't work. I got close when adding position:absolute; and left:50%; to my purple div class but when the screen resized the div fell off screen. Is there a quick way to float the purple div to the left of the green one so it's not in the center?
html, body{
margin:0;
height:100%;
width:100%;
}
#container{
background-color:pink;
height:91%;
width:100%;
display:flex;
}
#left{
width:50%;
background-color:lightblue;
display:flex;
position:relative;
}
#right{
width:50%;
background-color:lightgreen;
display:flex;
}
#logo {
left:0px;
width: 100%;
margin:auto;
max-width:calc(80vh - 25px);
background-color:purple;
}
#logo:before {
content:"";
display:flex;
padding-top: 100%;
}
<div id="container">
<div id="left"></div>
<div id="right">
<div id="logo"></div>
</div>
</div>
Try like this.
html, body{
margin:0;
height:100%;
width:100%;
}
#container{
background-color:pink;
height:91%;
width:100%;
display:flex;
}
#left{
width:50%;
background-color:lightblue;
display:flex;
position:relative;
}
#right{
width:50%;
background-color:lightgreen;
display:flex;
}
#logo {
left:0px;
width: 100%;
margin:auto;
margin-left: 0;
max-width:calc(80vh - 25px);
background-color:purple;
}
#logo:before {
content:"";
display:flex;
padding-top: 100%;
}
<div id="container">
<div id="left"></div>
<div id="right">
<div id="logo"></div>
</div>
</div>

flexbox div goes off screen on small screen

Code first:
html {
display:flex;
width:100%;
height:100%;
}
body {
display:flex;
flex:1;
}
.container {
display:flex;
flex:1;
overflow-y:auto;
flex-direction:column;
justify-content:center;
align-items:center;
}
.block1 {
justify-content:center;
background-color:green;
display:flex;
width:300px;
min-height:150px;
}
.block2 {
background-color:blue;
display:flex;
min-height:300px;
width:500px;
}
<div class="container">
<div class="block1">
<img src="https://tse2.mm.bing.net/th?id=OIP.M252f960f4a4f32c22914d8d87623f066o0&pid=15.1">
</div>
<div class="block2"></div>
</div>
I have two blocks in a container. I want them centered on the screen.
The issue is when the screen height is small, I have a scrollbar that appear but the first block have a part that go offscreen (is invisible)
To reproduce, decrease the height of the jsfiddle preview window. You will understand what I mean by going off screen.
The expected behavior is to let the scroll bar appear and keep the div visible.
I tried by setting flex-shrink to 0 on every element but it isn't working...
You can make use of Flexbox's auto margins.
Remove justify-content: center from .container.
Add margin-top: auto to .block1.
Add margin-bottom: auto to .block2.
html {
display:flex;
width:100%;
height:100%;
}
body {
display:flex;
flex:1;
}
.container {
display:flex;
flex:1;
overflow-y:auto;
flex-direction:column;
align-items:center;
}
.block1 {
justify-content:center;
background-color:green;
display:flex;
width:300px;
min-height:150px;
margin-top: auto;
}
.block2 {
background-color:blue;
display:flex;
min-height:300px;
width:500px;
margin-bottom: auto;
}
<div class="container">
<div class="block1">
<img src="https://tse2.mm.bing.net/th?id=OIP.M252f960f4a4f32c22914d8d87623f066o0&pid=15.1">
</div>
<div class="block2"></div>
</div>
You can add position: absolute; top: 0 to the container:
html {
display:flex;
width:100%;
height:100%;
}
body {
display:flex;
flex:1;
}
.container {
display:flex;
flex:1;
overflow-y:auto;
flex-direction:column;
justify-content:center;
align-items:center;
position: absolute;
top: 0;
}
.block1 {
justify-content:center;
background-color:green;
display:flex;
width:300px;
min-height:150px;
}
.block2 {
background-color:blue;
display:flex;
min-height:300px;
width:500px;
}
<div class="container">
<div class="block1">
<img src="https://tse2.mm.bing.net/th?id=OIP.M252f960f4a4f32c22914d8d87623f066o0&pid=15.1">
</div>
<div class="block2"></div>
</div>

Vertically align contents of div

I'd like to vertically align the contents of my div.
So basically 'Hello test' should be in the center of the div, vertically.
Demonstration
.parent {
background:blue;
float:left;
width:100%
}
.parent div {
float:left;
width:50%;
}
h1 {
font-size:50px;
margin:0;
padding:0;
}
<div class="parent">
<div>
<h1>hello!</h1>
<p>test</p>
</div>
<div>
<img src="http://placehold.it/250x250" />
</div>
</div>
You can use table layout for this:
.parent {
background:blue;
width:100%;
display: table;
}
.parent div {
display:table-cell;
vertical-align:middle;
width:50%;
}
h1 {
font-size:50px;
margin:0;
padding:0;
}
<div class="parent">
<div>
<h1>hello!</h1>
<p>test</p>
</div>
<div>
<img src="http://placehold.it/250x250" />
</div>
</div>
Modern flexbox solution (IE10+ and all modern browsers supported):
.parent {
display: flex;
align-items: center;
}
DEMO
You can learn more about flexbox with this excelent article :)
Use this css
body{
margin:0 auto;
}
.parent {
background:blue;
float:left;
width:100%
}
.parent div
{
text-align:center;
width:50%;
margin:0 auto;
}
h1 {
font-size:50px;
margin:0;
padding:0;
}

Three column layout using CSS

I am trying to do a 3-column layout and was wondering why the blue (right) column wraps around. This works fine in IE but fails to work in Chrome (30.0.1599.101m)
http://jsfiddle.net/V85JF/
HTML
<body>
<div class="top">
<div class="left">
</div>
<div class="center">
</div>
<div class="right">
</div>
</div>
</body>
CSS
body
{
height:100%;
margin:0;
background:gray;
}
.top
{
width:225px;
height:200px;
background:black;
}
.left
{
width:75px;
height:200px;
background:Red;
float:left;
display:inline-block;
}
.center
{
width:75px;
height:200px;
background:green;
float:none;
display:inline-block;
}
.right
{
width:75px;
height:200px;
background:Blue;
float:right;
display:inline-block;
}
EDIT
I need the center element to have fluid height. Top should take whatever height center takes.
Use float:left for .center and .right as well.
For fluid height, keep min-height:200px of .center.
Try this:
.top{overflow:hidden;}
.left,.center,.right{float:left;}
.center{min-height:220px;}
Fiddle here.
jsFiddle demo
Html
<body>
<div class="top">
<div class="left">
</div><div class="center">
</div><div class="right">
</div>
</div>
</body>
CSS
body
{
height:100%;
margin:0;
padding:0;
background:gray;
}
.top
{
width:225px;
height:auto;
background:black;
}
.left
{
width:75px;
height:200px;
background:Red;
display:inline-block;
}
.center
{
width:75px;
height:570px;
background:green;
display:inline-block;
clear:both;
}
.right
{
width:75px;
height:200px;
background:Blue;
display:inline-block;
}
Try this
This Layout is Fluid
Fiddle DEMO
CSS
body
{
height:100%;
margin:0;
background:gray;
}
.top
{
width:100%;
height:200px;
background:black;
}
.left
{
width:20%;
height:200px;
background:Red;
float:left;
display:inline-block;
}
.center
{
width:60%;
height:200px;
background:green;
float:left;
display:inline-block;
}
.right
{
width:20%
height:200px;
background:Blue;
float:right;
display:inline-block;
}

How align horizontal DIV inside parent DIV?

I want to align horizontal DIVS inside parent div #main and hide horizontal scroll
I tried to make such: http://jsfiddle.net/Ty9kg/30/
<div id="main">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
#main {
border:1px solid black;
width:250px;
height:150px;
overflow-y:hidden;
overflow-x:scroll;
}
#main div {
width:165px;
height:120px;
display:inline-block;
float:left;
background:#ccc;
border:1px solid #ccc
}
Do you want the following?
#main {
border:1px solid black;
width:250px;
height:150px;
overflow-y:hidden;
overflow-x:scroll;
white-space: nowrap;
}
#main div {
width:165px;
height:120px;
display:inline-block;
background:#ccc;
border:1px solid #ccc;
white-space: normal;
}
If so, float was redundant in your code.
I think you want this:
http://jsfiddle.net/umQ32/
html:
<div id="mainContainer">
<div id="main">
<div class="child"></div>
<div class="child"></div>
<div class="child"></div>
</div>
</div>
css:
#mainContainer{
width:300px;
height:120px;
overflow-x:scroll;
}
#main{
width:1200px;
height:100px;
background-color:blue;
}
.child{
width:200px;
height:100px;
margin:0 5px 0 0;
float:left;
background-color:red;
}
If i understand correctly, you want the grey boxes to be next to each other in a row? Currently you've given your main div a width of 250px and the inside divs a width of 165px. For obvious reasons they won't fit.
try this:
#main {
border:1px solid black;
overflow-y:visible;
width:990px;
}
#main div {
width:165px;
height:120px;
display:inline-block;
background:#ccc;
margin-right:-4px;
padding:0;
}
http://jsfiddle.net/Ty9kg/32/