Unable to produce horizonzal scroll with css [duplicate] - html

This question already has answers here:
Horizontal scroll in DIV with many small DIV's inside (no text)
(2 answers)
Closed 9 years ago.
I have HTML structure like this :
<div class="wrapper">
<div class="fixed_column"></div>
<div class="fixed_column"></div>
<div class="fixed_column"></div>
</div>
Here is my CSS :
.wrapper{
width:500px;
float:left;
/*overflow-y:scroll;*/
overflow-x:scroll;
}
.fixed_column{
float: left;
height: 600px;
position: relative;
width: 250px;
}
So I want only two columns to fit inside my wrapper. And so without third column being present it fits inside.
Once I add the third column like in the HTML above, the third column doesn't stay in the same row but it drops to the next line and I end up with vertical scroller instead of horizontal. added overflow-x to my css and I don't get a horizontal scroll-bar but the third column still drops to the next line.
However I tried to increase wrapper to 750px and this time all three columns fit in the same line so I thought nothing is wrong with my css or did I think wrong?
Why would there not be horizontal scroll once my wrapper is 500px and I have three columns inside with width:250px on each.

Add white-space: nowrap; to the container, use inline-block instead of float, and use overflow-x instead of overflow-y.
This works:
http://jsfiddle.net/vXqY2/
.wrapper {
width: 600px;
white-space: nowrap;
overflow:scroll;
}
.fixed_column {
display: inline-block;
height: 100px;
width: 250px;
background-color: red;
}

The floated elements are going to automatically wrap down to the next level if they start going off the right of the parent container. That's how floats work. To keep them on the same line, you have a few options:
Make the parent container wider (as you did), but you'll need an extra element for the scrollbar
Switch from float: left; to display: inline-block; (as #Alex suggested), but you'll need to make concessions for IE7.
Switch from float: left; to display: table-cell;. Don't recommend this, I tried it and it turns out it's kind of painful :-p
See all techniques in a jsFiddle demo

It is because your fixed columns divs are only 250px so they never break the 505px container they are currently in.
Here try this.
example:
<div class="wrapper">
<div class="scroll-container">
<div class="fixed_column">A</div>
<div class="fixed_column">B</div>
<div class="fixed_column">C</div>
</div>
</div>
.wrapper {
width: 505px;
position:relative;
overflow-y: scroll;
overflow-x: scroll;
}
.scroll-container {
width:1000px;
}
.fixed_column {
float: left;
height: 600px;
position: relative;
width: 250px;
background-color: green;
}

Related

Align text on footer at the same line

Please look at this picture it will explain much better
https://gyazo.com/333fc2ef04f558480386b7be67eb1bda
I have a orange footer at the bottom of my webpage and i want the text to be aligned "left", "center" and "right" on the same line within the footer bar.
Right now the text is aligned but the text 3 aligns are under each other at 3 seperatly lines.
This is my HTML:
<div class="row">
<div id="footer">
<div align="left"><h3>Contact</h3></div>
<div align="center"><h3>Computerbasen</h3></div>
<div align="right"><h3>Info</h3></div>
</div>
</div>
This is my CSS:
#footer {
background-color: #FF7633;
width: 100%;
height: 50px;
border-radius: 5px;
padding-top: 10px;
position: absolute;
bottom: 0;
}
I recommend flexbox for this type of layout.
Remove the align attributes and add this to your #footer.
#footer {
display: flex;
justify-content: space-between;
}
You could achieve it in many different ways.
#Vestride gave you one way.
Another approach is to add another selector :
#footer div{
display: block;
float: left;
width: 30%;
}
OR
#footer div{
display: inline-block;
width: 30%;
}
This will select all div inside #footer and align them.
We divided the three div width to be aligned even. So, since width: 100% is even. we need to subtract 10% from it to use it for margin, and the rest will be divided by 3. so each div will be 30% of the footer width. This way it will be on the same line. Remember, any element has 100% of width will be on a separate line. Meaning, if two DIVs in the same line have 100% of width, they'll be under each other, but if the width divided between them (each one of them is 50% width) then they will be at the same line.
You could use the same idea, and be creative in your own way. As there are a various of methods that can be achieved differently in CSS. Just pick your favorite one to do it.

What to do besides FLEXBOX to make put a stretchable item in between two fixed sized items in a single row on website?

I'm trying to create a header. I have a parent div container with 3 child divs. Child div 1 is fixed and will be aligned to the left. Child div 3 is fixed and will be aligned to the right. Child div 2 has a variable size and will fit in between Child 1 and Child 3. Child 2 will have a size the changes based on browser size (it will expand and shrink to a certain point).
I want my header to be compatible with older versions of IE, at least back to I.E. version 8. I'm reading about FLEXBOXes and looks like it's not even fully compatible with I.E. 11 without bugs.
I'm thinking about using a table but was wondering if there are better ways to do this...as I briefly tested the table and it's not doing what I want either. I tried it and even though I specified a width for each element, when I shrink my browser, the child 1 and child 3 change in size - NOT what I want.
Thanks!
With IE8 you can consider tables, but the old way of doing this is with floats.
#wrapper {
overflow: hidden; /* Establish BFC */
}
#child1 {
float: left;
width: 200px;
background: yellow;
}
#child3 {
float: right;
width: 150px;
background: pink;
}
#child2 {
overflow: hidden; /* Establish BFC */
background: cyan;
}
<div id="wrapper">
<div id="child1">Child 1</div>
<div id="child3">Child 3</div>
<div id="child2">Child 2</div>
</div>
To enforce a single row you can add set max-width percentages to #child1 and #child2 that add up 100%.
I'm not too clear on the problem you ran into when attempting to use tables. Could you explain what the problem was?
You can try to use divs and CSS to create the display as a table. This way you can, for example, use feature queries to see if flexbox is available and use it. If not, you can fallback to using the table CSS below. All without changing the HTML.
Let me know if this works for you!
#container{
display: table;
/* use table-layout: fixed if you're
having problems with the content expanding
the fixed cells */
/* table-layout: fixed; */
width: 100%; /* or however big you need it */
}
#row{
display: table-row;
}
#one,
#two,
#three{
display: table-cell;
}
#one{
width: 100px;
background: blue;
}
#two{
width: auto;
background: red;
}
#three{
width: 100px;
background: orange;
}
<div id="container">
<div id="row">
<div id="one">Div 1</div>
<div id="two">Div 2</div>
<div id="three">Div 3</div>
</div>
</div>
Well, flexbox was created specifically to provide a CSS way to do what you describe, so it's a little like asking how to find the area of a square without multiplying...
Prior to flexbox my go-to solution for this sort of problem was to use JavaScript to dynamically resize the appropriate div (the 2nd child in your case). I really disliked this solution even then because I don't think JavaScript is a good place for layout logic to live, but it was the best I could come up with.
I suppose you could use a table, but I think you'll run into quirky limitations on the layout within the "cells" then.
Here is one solution, but it has limitations. You have to know the width of Child 3 and position your parent div to relative.
https://jsfiddle.net/whatisthebigpicture/sj9xv9m6/1/
header {
position: relative;
}
div {
outline: 1px solid red;
}
.div1 {
width: 100px;
float: left;
}
.div2 {
margin-right: 100px;
}
.div3 {
width: 100px;
top: 0;
right: 0;
position: absolute;
}

Line breaking overflowing div

How can I make a div that overflows past it's parent to break the overflowing part into a new line?
What I have at the moment:
https://jsfiddle.net/9f9b4pz0/
<html>
<div class="first">
<div class="second"></div>
</div>
</html>
css:
.first {
width: 500px;
height: 500px;
background-color: red;
}
.second {
width: 700px;
height: 200px;
background-color: blue;
}
I don't know are you meaning to overflow that by using position: absolute?
https://jsfiddle.net/moongod101/5u7q6a1h/1/
By not defining fixed widths and heights, but instead no width (which corresponds to width: auto) and just a max-height instead of height.
The usual situation wouldn't be plain colored squares but text, small imagaes etc. which do break into new lines automatically:
https://jsfiddle.net/vxhmu6xj/1/
(You can't break a block which has fixed width - even if you want that, you'd have to use several blocks next to each other which look like one block but can be stacked if there isn't enough width.)

Hiding scrollbar in non fixed divs [duplicate]

This question already has answers here:
Hide scroll bar, but while still being able to scroll
(42 answers)
Closed 7 years ago.
I've run into trouble while trying to hide scrollbars from certain divs.
I found some solutions on the forum but they never really match my case so I'm still struggling with the problem.
My problem: I'm trying to hide scrollbars in a div that is nested inside another div that has non fixed size. (they are set to 100% of the body).
Here's the HTML:
<div id="events">
<div id="event-list"></div>
<div id="event-details"></div>
</div>
And the CSS:
html, body {
width: 100%;
height: 100%;
margin: 0;
}
#events {
width: 100%;
height: 100%;
}
#event-list {
float: left;
width: 50%;
height: 100%;
background-color: pink;
}
#event-details {
float: right;
width: 50%;
height: 100%;
background-color: cyan;
}
Codepen available here
I would like #event-list and #event-details to have no scrollbar but still be scrollable. If you have any idea (css? js? jquery?), I'll take it!
Thanks in advance,
alex
You can do a nested div with the outer div's width set to 100% with overflow:hidden and the inner div set to a width of 105% (you can fine tune this value) and overflow set to overflow:scroll
JSFiddle here

Center Divs horizontally

I'm trying to figure out how to center more than one DIV horizontally.
My Code looks like this:
HTML:
<div id="circle">
<div id="circle1"></div>
<div id="circle2"></div>
</div>
CSS:
#circle {
text-align: center;
}
#circle1 {
background: #D5DED9;
width: 100px;
height: 100px;
border-radius: 50%;
margin: 0 auto;
}
#circle2 {
background: #D5DED9;
width: 100px;
height: 100px;
border-radius: 50%;
margin: 0 auto;
}
They do center horizontally but there's a break between the circles and I have no clue how to get them in a straight horizontal line.
I googled already, but didn't found anything that works..
You can add display:inline-block; to both #circle1 and #circle2
Also, thee is no need for margin: 0 auto; on both div's since you have text-align:center; in your wrapper.
JSFiddle Demo
You shouldn't use display: inline-block to center elements like divs due to how whitespace in the HTML document will affect the styling.
This jsFiddle outlines the differences. imbondbaby's inline-block divs have a small amount of whitespace between them that can only be removed by eliminating whitespace in your markup. This can be diffcult to diagnose and debug, and has bitten me before.
Instead, center the container of the divs using margin: 0 auto. Then float: left the divs inside their parent to place them next to each other, and apply a clearfix to the container.
Style:
#wrapper {
margin: 0 auto;
}
.clearfix {
display: table;
clear: both;
}
.circle {
float: left;
}
HTML
<div id="wrapper" class="clearfix">
<div class="circle"></div>
<div class="circle"></div>
</div>
If I've understood your question correctly, you want the two circles to be on the same line, centered within the wrapper circle div.
Basically, you could float one circle to the left, and the other to the right to get them on the same line. Then to adjust how close they are together within the wrapper div, you could adjust the width property of the wrapper div with a percentage (which in this case is relative to the div's parent, the body).
Here's an example of a potential solution: http://jsfiddle.net/UFN5S/
By the way, there are other similar questions to this already on SO. I know you've said you googled, but usually with questions like this one there has already been asked and answered on SO.
i.e.:
How to center two divs floating next to one another
or
Aligning two divs side by side center to page using percentage width
Hope that helps!