I'm currently in planning stage for a site, which needs to scroll horizontally.
The simplest solution I have to tackle this is to go in this direction, JSFiddle.
I'm not sure if this is the best option, as I will have to arrange each div individually i.e. left: 100% left: 200%;.
Is there a way around the divs, with a display: inline-block value auto wrapping to the viewport, so I don't have to arrange each div individually?
Removing the absolute positioning
What you need to do here is remove the float and absolute positioning from your dividers and simply add white-space: nowrap to your body. As your dividers are set to display as inline-block, these get affected by the white-space property.
body {
margin: 0; padding: 0;
white-space: nowrap;
}
.full {
width: 100%;
height: 100%;
display: inline-block;
}
JSFiddle demo.
Removing the spaces between each block
Now that we've removed the floats and the positioning, you'll notice that there is a white space between each divider. If we refer to this CSS Tricks article, we can remove this by simply giving the body a font-size of 0, and giving each divider a font-size of what you're wanting the font size to be within those blocks:
body {
margin: 0; padding: 0;
white-space: nowrap;
font-size:0;
}
.full {
width: 100%;
height: 100%;
display: inline-block;
font-size:16px;
}
Second JSFiddle demo.
http://jsfiddle.net/MsRCS/3/
You can remove the absolute positioning and use float instead.
body {
margin: 0; padding: 0;
width:300%;
}
.full {
width: 33.3%;
height: 100%;
display: inline-block;
float: left;
}
#screen-1 {
background: red;
}
#screen-2 {
background: blue;
}
#screen-3 {
background: yellow;
}
Related
I experienced strange behaviour when trying to position two divs horizontally. I got the same behaviour both for firefox and chrome, so I figured out there might be something deeper about layout that I don't understand.
Here is my HTML:
<div class="parent"><div class="cell left">a</div><div class="cell right">b</div></div>
It is on one line to avoid the whitespaces.
Here is my CSS:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.parent {
width: 100%;
height: 100%;
text-align: center;
margin: 0;
padding: 0;
}
.cell {
height: 100%;
display: inline-block;
margin: 0;
padding: 0;
/* vertical-align: bottom; */ /* toggle this! */
}
.left {
background-color: lightblue;
width: 50%;
}
.right {
background-color: royalblue;
width: 50%;
}
I include here an example jsfiddle.
Here is a picture:
The current code works correctly. I get two divs, each 50% width of the screen.
First quirk: No text in divs
The first quirk happens if I remove the text from both of the divs. That means, if my HTML would be:
<div class="parent"><div class="cell left"></div><div class="cell right"></div></div>
In this case I get a vertical scroll bar.
Picture:
Second quirk: Text only in one div:
This is where the really strange things happen.
If I have text only in one of the divs, like this:
<div class="parent"><div class="cell left">a</div><div class="cell right"></div></div>
The div with the text is pushed down to the bottom, and the other one is unchanged. Pictures:
Solution
I found (by trial and error) that if I add
vertical-align: bottom;
to .cell, it fixes everything.
My problem is that I don't understand why. I will be happy to get any explanation to what is happening here.
Inline elements and boxes vertically align, by default, to the baseline. There are three, not two, inline boxes in your line.
When an inline-block element contains text, its baseline is the base of the last line of text it contains. When it doesn't have any content, its baseline is the the bottom of the box.
The third box on the line is called a strut. Its purpose is to give a minimum height to the line. It is zero width, but is like a text character from the font of the containing block and has a line-height that is defined from the containing block. It is always vertically aligned to the baseline.
So your first scenario is this.
Your second scenario is this. See how the bottom of the strut is below the bottom of the boxes, so the total height of the line is greater than 100% that of the viewport, causing the scrollbar to appear.
Your third scenario is this.
You can try this may be all issue solved for this css given " float:left " in " .cell " And " overflow: hidden" in ".parent":
CSS:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.parent {
width: 100%;
height: 100%;
text-align: center;
margin: 0;
padding: 0;
overflow: hidden;
}
.cell {
height: 100%;
display: inline-block;
margin: 0;
padding: 0;
float: left;
/* vertial-align: bottom; */ /* toggle this */
}
.left {
background-color: lightblue;
width: 50%;
}
.right {
background-color: royalblue;
width: 50%;
}
See Fiddle Demo
vertical-align it acts on the inline-block, rather than its contents.
vertical-align aligns by the border of the line, in which our inline-block is.
when the vertical-align is not specified, the alignment acts by the bottom border of the contents of the inline-block:
<div class="parent"><div class="cell left">a<br>a
</div><div class="cell right">b</div></div>
https://jsfiddle.net/glebkema/qeh9zugg/
UPD. vertical-align: top; and vertical-align: middle; correct the problems by the same way as vertical-align: bottom;.
This may help you.
Add display:table; to parent div and display:table-cell; to child div.
HTML:
<div class="parent"><div class="cell left"> a </div><div class="cell right">b</div></div>
CSS:
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.parent {
width: 100%;
height: 100%;
text-align: center;
margin: 0;
padding: 0;
display:table;
vertial-align: middle;
}
.cell {
height: 100%;
display: table-cell;
margin: 0;
padding: 0;
vertial-align: middle ; /* toggle this */
}
.left {
background-color: lightblue;
width: 50%;
}
.right {
background-color: royalblue;
width: 50%;
}
I took a pricing table HTML/CSS/JS that I found and decided to try and bend it to fit my desires for a given page. Unfortunately I've hit a bit of a wall. The following fiddle is a bare-bones example of the HTML and CSS for the table at the moment:
https://jsfiddle.net/jv89hopf/1/
In order to make the columns evenly space out across the width of the page regardless of the number of columns I used display:table, table-layout:fixed, and display:table-cell. This works perfectly and as I add or remove columns the table adjusts as necessary to fill the space
Now the problem is when one column is taller than the others. I would like all columns to stretch to match the height of the tallest one.
When looking in the Chrome inspector I can see that the table-cell has filled the height entirely:
Now all I need is for the child of this table-cell to fill the height (in the Fiddle provided above, this would be .price-wrapper - and it needs to fill .price-list li)
I have tried both:
height: 100%
position: absolute; top:0; bottom:0; left:0; right:0;
The former does nothing for some reason, and the latter collapses .price-list down to 0 pixels tall (since the only children with height are absolutely positioned and therefore removed from the flow)
If I can get .price-wrapper to be properly 100% of the height of .price-list li then I can use display:table and display:table-row to push the "Buy now" button to the bottom and get the desired appearance:
One solution is give 100% height to .price-list, .price-list > li and .price-wrapper will make child height fit to content.
.price-list {
display: table;
height: 100%; //Here
list-style: outside none none;
margin: 0;
padding: 0;
table-layout: fixed;
width: 100%;
}
.price-list > li {
display: table-cell;
padding: 0 10px;
height:100%; //Here
}
.price-wrapper {
background-color: #fff;
height: 100%; //Here
list-style: outside none none;
margin: 0;
padding: 0;
}
Working Fiddle
some css changes
body {
background-color: #999;
}
.monthly.is-visible {
height: 100%;
margin-bottom: 18px;
position: relative;
}
.is-visible footer {
background-color: #99c;
bottom: 0;
height: 30px;
position: absolute;
width: 100%;
}
.price-list {
display: table;
table-layout: fixed;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
list-style: none;
}
.price-list > li {
display: table-cell;
padding: 0 10px;
height:100%;
}
.price-wrapper {
background-color: #fff;
list-style: none;
height:100%;
margin: 0;
padding: 0;
}
.is-visible footer {
width: 100%;
height: 30px;
background-color: #99c;
}
/* For demonstration purposes */
.is-hidden {
display: none;
}
https://jsfiddle.net/jv89hopf/3/
I have a solution using jQuery. It works like this. When the page loads, it looks for each list and determines the largest among all lists. Then it takes that height and stretches others accordingly. The code looks like;
var height = 0;
$("main").each(function( index ) {
if(height<$(this).height()){
height = $(this).height()
}
});
$("main").height(height);
Here is a demo
I'd like to create a table like div structure, which is placed in a container, can be scrolled horizontally and gets not breaked. I wrote the structure, but when the content gets longer than the container it puts the rest of the content in a new line.
Here's my code so far:
http://jsfiddle.net/rcdzdyv7/2/
where all of these elements represent a "table" row:
<div class="header">...</div>
<div class="body">...</div>
<div class="footer">...</div>
My goal is to make these rows one-lined and look like if it was a table. How could I solve this?
You can't use float:left because when content reach the width there's no way to avoid the floating elements "jumping" to next line (without changing the html structure).
However you can use display:inline-blockbecuase your elements this way can change their behaviour with the property white-space:nowrap.
Basically:
.container {
width: 500px;
overflow-x: scroll;
}
.header {
width: auto;
display:inline-block;
background-color: #D9D9D9;
white-space: nowrap;
clear: both;
}
.body {
display:inline-block;
margin: 5px 0;
}
.body-row {
background-color: #E5EBFF;
display:inline-block;
clear: both;
white-space: nowrap;
}
.footer {
clear: both;
background-color: #D9D9D9;
white-space: nowrap;
}
.row-title {
width: 300px;
display:inline-block;
}
.row-content {
width: 150px;
display:inline-block;
}
.value {
width: 100%;
}
as in this FIDDLE
You could use:
.row-content {
width: 150px;
display: inline-block;
}
instead of:
.row-content {
width: 150px;
float: left;
}
Let me know if it works!
this is because you are using DIV with delimited width no set height.
so when the width needed will be too high for the container width it will automatically do under. Hope this makes sense. A soluation can be to use inline-block, personnally I would recomment to use a classic table but just my opinion
try these css properties to the <div> for which you want a scroll
div {
overflow-x: scroll;
overflow-y: hidden;
}
hope this is what you want !
How do you center an image with text inside a block?
I know you can center a block inside another block by giving the latter a fixed width and margin: auto. However, I don't know the dimensions of text beforehand (actual text content may vary).
The CSS I have got so far:
.outer {
width: 400px;
}
.outer table {
border: 0;
width: 100%;
}
.outer table td {
vertical-align: middle;
text-align: center;
}
.outer table td p {
text-align: left;
}
Please take a look at this DEMO
Here is my css:
.block {
text-align: center;
}
.block:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
.centered {
display: inline-block;
vertical-align: middle;
}
.left {
float: left;
}
Explanation about :before element:
This is an invisible element pseudo element, which is used for better vertical centering: it emulates a 0-sized inline-block element, which, in conjunction with normal inline-block element (.centered) allows us to use vertical-align.
UPDATE:
You can set height to .block to see how it will be centered vertically:
http://jsfiddle.net/jb5EJ/5/
UPDATE 2: Is this closer: http://jsfiddle.net/jb5EJ/13/
Checkout this link. I hope you will get the solution.
http://coding.smashingmagazine.com/2013/08/09/absolute-horizontal-vertical-centering-css/
TLDR: with only this CSS you can position an element in absolute center (both horizontally and vertically):
.Absolute-Center {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
Add vertical-align:middle; to img too....also, i would suggest to add height to outer class
<img src="some_src" style="vertical-align:middle;" /> I have some text too
demo to get u started
In a wrapper div, the floated elements don't seem to respond to left and right margin settings. Example:
html:
<div id ="wrapper">
<div id = "content"></div>
</div>
css:
#wrapper
{
width: 1000px;
display: block;
margin-left: auto;
margin-right: auto;
overflow: hidden;
}
#content
{
width: 400px;
height: 200px;
display: block;
float: left;
margin-left: 30px;
}
The #content ignores its left margin setting. Why?
Margins do not move floated elements, they "push content away".
If you want to move the floated element, you could give it the following CSS rules:
#content {
position: relative;
left: 30px;
}
An alternative is giving the element a transparent border:
#content {
border-left: 30px transparent;
}
If you are just looking to position a div inside of another div, then use absolute positioning:
#wrapper {
position: relative; /* required for absolute positioning of children */
}
#content {
position: absolute;
left: 0;
}
A more recent CSS technique that works perfectly in this scenario is to use the CSS transform property with a translate X or Y. This acts on the floated element only, so does not have the unintended effect of moving other elements around.
An example transform is this:
.floated-element {
// move the floated element 10 pixels to the left
transform: translateX(-10px);
}
#Marcus's answer is good. Another way to fake having margins on a floated element is to put the content inside of another container and use padding:
.outer
{
float: left;
padding: 10px;
}
.inner
{
}