How to force content to overflow horizontally in CSS? - css

Is there any way to make the content to flow to left/right instead of down while the container doesn't have enough space for it in CSS?
.container1 {
width: 70%;
float: left;
}
.container2 {
width: 30%;
float: left;
}
.content {
width: 50%;
float: left;
}
.overflowContent {
width: 20%;
float: left;
}
/* You can add background colors to see where every part is */
<div class="container1">
<div class="content">First half</div>
<div class="content">Second half</div>
<div class="overflowContent">Overflow</div>
</div>
<div class="container2"></div>
I want a way that makes the overflow part go on the right side of the container instead of below it.(i want the third part to be displayed on/over the container 2)
My idea was to add something to a link with [ display : hidden ] that only shows up [ display : block ] on the right side of the link on the other parts of the website while we hover on the link.

Add a div inside the container that will hold the overflowing content, and apply overflow-x: auto to the container.
The flexbox code is just a friendly suggestion, much easier to work with than floats for layout.
* { box-sizing: border-box; }
.container {
width: 70%;
border: 5px solid red;
overflow-x: auto; /* this causes anything inside that is wider to overflow horizontally */
}
.inner {
display: flex;
}
.content {
flex: 0 0 50%;
padding: 20px;
background: papayawhip;
}
.overflowContent {
flex: 0 0 20%;
padding: 20px;
background: dodgerblue;
}
<div class="container">
<div class="inner">
<div class="content">First half</div>
<div class="content">Second half</div>
<div class="overflowContent">Overflow</div>
</div>
</div>

You can do this considering inline-block and white-space:nowrap. Don't forget to reset the whitespace between inline element (I used the font-size trick here)
.container1 {
width: 70%;
display:inline-block;
outline:1px solid red;
}
.container2 {
width: 30%;
display:inline-block;
outline:1px solid green;
}
.content {
width: 50%;
display:inline-block;
font-size:initial;
}
.overflowContent {
display:inline-block;
font-size:initial;
}
body {
font-size:0;
white-space:nowrap;
}
<div class="container1">
<div class="content">First half</div>
<div class="content">Second half</div>
<div class="overflowContent">Overflow</div>
</div>
<div class="container2"></div>

Related

css3 : how to put height 100% on a static/relative (no absolute positionned) div?

I have a container and 2 divs inside:
1 header (whose height should be free if I add some lines) and an userList.
I want the userList to have the height of the container : any idea how ?
(no JS solution, better if no position: asbolute used)
#container {
width: 300px;
height:400px;
background-color: #FF0000;
}
#header{
background-color: #FFF500;
}
#userList {
background-color: #00FF00;
width:290px;
height: 100%;
overflow-y:auto;
}
<div id="container">
<div id="header">line1<br>line2<br>line3</div>
<div id="userList">
line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>
line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>
</div>
</div>
Right now, your .userList have the same height as his container, but with the yellow box it goes down. The best solution with your requirements is as this:
Your requirements:
no JS solution, better if no position: asbolute used)
#container {
width: 300px;
height:400px;
background-color: #FF0000;
}
#header{
width: 300px;
background-color: #FFF500;
}
#userList {
background-color: #00FF00;
width:290px;
height: 100%;
overflow-y:auto;
}
<div id="header">line1<br>line2<br>line3</div>
<div id="container">
<div id="userList">
line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>
line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>
</div>
</div>
The only I need is take out the #header division and give it the same with as #container. By this mode, #container and #userList have got the same height.
One good way of doing this is with display: flex and flex-direction properties.
This way, you can have a header with flexible height, and a userlist that is always contained within the container. This way, you also don't have to change your markup and move the header outside your container.
Demo
Full code:
#container {
width: 300px;
height:400px;
background-color: #FF0000;
display: flex;
flex-direction: column;
}
#header{
background-color: #FFF500;
}
#userList {
background-color: #00FF00;
width:290px;
height: 100%;
overflow-y:auto;
}
<div id="container">
<div id="header">line1<br>line2<br>line3</div>
<div id="userList">
line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>
line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>
</div>
</div>

Centering the middle of three divs and positioning the other two relative to the middle one

Sorry if the title is confusing. Basically, I'm working on a tumblr theme where I need three adjacent divs wrapped in a fixed-width container. None of their contents are fixed, so they all have variable widths. The middle div should always be centered to the container, while the divs to the left and right will always be "touching" the middle div, and, thus, move around as the middle div's width changes (the left and right s may be images, so text-align doesn't always work). Plus, I may also need to hide the left, right, or both the left and right divs.
Here's a conceptual image:
I can obtain this using flexboxes easily (JFiddle), but flex only has 86% global support.
This is the closest I could get without using flexboxes, but I can't get that middle div (with the text) centered to the title div, while preserving the relative positions of the two images on either side: JFiddle
* {
height: 100%;
position: relative;
}
body {
height: 200px;
}
/* just to get rid of scrollbar */
p {
margin: 0;
}
.title {
background: #aaa;
height: 22px;
width: 450px;
/* for example */
margin: 0 auto;
}
.container {
background: #abc;
float: left;
}
.lr {
transform: translate(0, -100%);
}
.left {
background: green;
float: left;
}
.left img {
transform: translate(-100%);
}
.center {
background: red;
display: inline-block;
z-index: 2;
}
.right {
background: blue;
float: right;
}
.right img {
transform: translate(100%);
}
.left img, .right img {
height: 100%;
}
<div class="title">
<div class="container">
<div class="center">CENTERCENTERCENTERCEN</div>
<div class="lr">
<div class="left">
<img src="http://i.imgur.com/7bvErJN.jpg" />
</div>
<div class="right">
<img src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
</div>
</div>
Other people have mentioned trying to display the title as a table, but that would require centering the middle cell to the whole row, and having the cells to the left and right take up the rest of the space, and I'm not sure if you can do that when their widths aren't fixed.
Anyone know of any other solutions?
If you can change your HTML then apply this:
First move the left and right elements inside center:
<div class="center">
CENTERCENTERCENTERCEN
<div class="left">
testtest<img src="http://i.imgur.com/7bvErJN.jpg" />
</div>
<div class="right">
<img src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
Then on the CSS :
/*Keep the center container on the middle*/
.title {
text-align:center;
}
.center {
position:relative;
display:inline-block;
}
/*Position elements based on the relative center parent*/
.left {
position:absolute;
top:0;left:0;
transform:translateX(-100%)
}
.right {
position:absolute;
top:0;right:0;
transform:translateX(100%)
}
Check this DemoFiddle
Using position: absolute should help in this.
I changed your HTML to following:
<div class="title">
<div class="container">
<img class="left" src="http://i.imgur.com/7bvErJN.jpg" />
<div class="center">CENTERCENTERCENTERCEN</div>
<img class="right" src="http://i.imgur.com/q8Mq0YZ.jpg" />
</div>
</div>
CSS
.title {
background: #aaa;
height: 22px;
width: 450px;
/* for example */
margin: 0 auto;
text-align: center;
}
.container {
background: #abc;
display: inline-block;
margin: 0 auto;
position: relative;
text-align: left;
}
.center {
background: red;
}
.left, .right {
position: absolute;
top: 0px;
}
.left {
right: 100%;
}
.right {
left: 100%;
}
Working Fiddle
Updated to show OP Update
No need for flex here, why not just use percentages? Float all the containers and put the percentages as relative to the sizes you want. (50% for the middle, 25% for the outside containers).
You can use the outside containers as wrappers so you can still use a border on the inner containers without messing up the sizing. Then just float the inner containers within the outside containers (if that makes sense). The example below just floats the inner p tags to the outer containers.
This makes it always hug the inner container, while keeping relative sizes and also keeping the middle centered.
Example below:
Fiddle
HTML
<div class="container">
<div class="flexa">
<div class="left">
<p>leftleft</p>
</div>
<div class="center"><p>CENTERCENTdsfdfdERCENTsdfdfsfERCEN</p></div>
<div class="right">
<p>ri</p>
</div>
</div>
<div class="bottom">BOTTOMOMOM</div>
</div>
CSS
* {
margin: 0;
padding: 0;
}
div {
background: #aaaaaa;
overflow: hidden;
}
p{
border: 1px solid black;
}
.container {
width: 500px;
/* for example */
margin: 0 auto;
}
.right p{ /* This is what makes it work. This could be a div with class of inner or something like that. */
float:left;
}
.left p{
float:right;
}
.flexa div{
float:left;
}
.left {
width:25%;
}
.center {
width: 50%;
}
.right {
width:25%;
}
.bottom {
display: block;
margin: 0 auto;
text-align: center;
}

Div containing other divs does not have proper height

I have a 'frame' containing two divs which are respectively aligned on the left and on the right. Unfortunately, the main div does not have the proper height to englobe the inner divs.
Here is the HTML:
<div id="frm">
<div id="a">aaa<br>aaa</div>
<div id="b">bbb</div>
</div>
and the CSS:
#frm {
background: red;
width: 100%;
height: auto;
}
#a {
background: blue;
float: left;
}
#b {
background: green;
float: right;
}
Here is the JsFiddle: http://jsfiddle.net/mPH4H/
I should see a red frame, but there is none.
The floated elements are removed from the flow of the document, so the parent container thinks that it has nothing inside of it. You can add overflow:auto to your CSS rules for #frm to bring the background back and "contain" the floated children:
#frm {
background: red;
width: 100%;
height: auto;
overflow:auto;
}
jsFiddle example
overflow:hidden; will give height to #frm
Try:
#frm {
background: red;
width: 100%;
height: auto;
overflow:hidden;
}
DEMO here.
OR
Clear floats:
HTML:
<div id="frm">
<div id="a">aaa<br>aaa</div>
<div id="b">bbb</div>
<div class="clr"></div>
</div>
CSS:
.clr{clear:both;}
DEMO here.
i think this is worked as fine:
#frm {
background: red;
width: 100%;
height: auto;
}
#a {
background: blue;
width: 50%;
float: left;
}
#b {
background: green;
width: 50%;
float: right;
}

Get two aside sections to float left with fixed width CSS

I'm trying for two sections to float aside of the main column on the right side. The sections should be of a fixed width and the main should be fluid. This is the closest I have come. Problem is that the main does not change its size. If it where one aside section I had used the holy grail, but that doesn't work either.
[edit]To clarify; the HTML cannot be changed (much). Left and right need to stay after main which is best for screen readers and seo. The asides are actually the left and right column if content is wide enough. So only specific widths get this layout I am trying to achieve.[/edit]
https://jsfiddle.net/TR2SD/1/
<div id="container">
<div id="main">
main contents<br>with some content
</div>
<aside id="left">
left contents
</aside>
<aside id="right">
right contents
</aside>
</div>
and the CSS:
#container {
border: 1px solid red;
width: 100%;
}
#main {
border: 1px dotted #f0f;
margin: 0 -240px 0 0;
position: relative;
width: 100%;
float: left;
}
#left {
background-color: #0ff;
}
#right {
background-color: #ff0;
}
#left,#right {
float: left;
width:220px;
position: relative;
z-index:2;
}
For one sidebar, you can position in by accounting for its width using padding on the container and a margin on the main section:
.container {
padding-right: 200px; /* Matches sidebar width */
overflow: hidden;
}
.main {
width: 100%;
float: left;
}
.sidebar {
width: 200px;
margin-right: -200px; /* Matches sidebar width */
float: right;
}
For a left and right sidebar with a scaling center you can use a similar technique:
.container {
padding-right: 200px;
padding-left: 200px;
overflow: hidden;
}
.main {
width: 100%;
float: left;
}
.left-sidebar {
float: left;
width: 200px;
margin-left: -200px;
}
.right-sidebar {
float: left;
width: 200px;
margin-right: -200px;
}
.left-sidebar, .right-sidebar {
display: none;
}
Here's the final result on JSBin. You'll need to resize the page to see the different views.
Note that an auxiliary sidebar for small screens was used in the example above as it is inordinately difficult to use CSS to render elements out of DOM order.
I finally answered it by utilizing an inside element of the #main. What I need to check is compatibility of this fix. And what haapens to the backgrounds assigned to #main.
https://jsfiddle.net/TR2SD/5/
I added an "inside" element
<div id="container">
<div id="main">
<div class="inside">
main contents<br>with some content
</div>
</div>
<aside id="left">
left contents
</aside>
<aside id="right">
right contents
</aside>
</div>
And the css floats everything with some corrections.
#container {
border: 1px solid red;
width: 100%;
}
#main {
background-color: #f0f;
margin-left: -240px;
width: 100%;
float: left;
}
#main .inside {
margin-left: 240px;
}
#left {
background-color: #0ff;
}
#right {
background-color: #ff0;
}
#left,#right {
float: left;
width:240px;
}
In order to locate your divs as you are requiring you should:
Position divs left and right floating right
For the div you named left to be placed to the left of right , you must declare right before left and lastly main which does not require to be positioned relative
The container should declare a min width to prevent the break of the lay out.
So HTML should be a sort of...
<div id="container">
<aside id="right">
right contents
</aside>
<aside id="left">
left contents
</aside>
<div id="main">
main contents<br>with some content
</div>
</div>
And CSS should be
#container {
border: 1px solid red;
width: 100%;
min-width:600px;
}
#main {
border: 1px dotted #f0f;
margin: 0 -240px 0 0;
}
#left {
background-color: #0ff;
}
#right {
background-color: #ff0;
}
#left,#right {
float: right;
width:220px;
}
From here onwards, adjust as your will
A fiddle here

Two columns inside container

What I want to do is have a <div> with a container class and a fixed width, holding a <div> with the block class to prevent other content encroaching on any uneven blank space, then two columns (<div>'s) side-by-side inside the block, and to be 50% of the width of the block.
When I create this, I get what appears to be a margin after the first block, which I do not want. I want the block to pack up tight, no margins.
I have an example here of what I have so far, and here if the code:
<html>
<head>
<title>Columns</title>
<style>
div {
margin: 0;
padding: 0;
}
.container {
background: #DDD;
width: 1200px;
margin: 0 auto;
padding: 2% 0;
}
.block {
background: #555;
width: 100%;
display: block;
}
.col {
width: 49%;
display: inline-block;
background: #333;
}
</style>
</head>
<body>
<div class="container">
<div class="block">
<div class="col left">
<h1>Left</h1>
</div>
<div class="col right">
<h1>Right</h1>
</div>
</div>
</div>
</body>
</html>
Your problem is being causes by inline-block, using this makes a space appear inbetween.
Try using float:left to get around this:
See on jsFiddle
.col {
width: 50%;
float: left;
box-sizing: border-box;
background: #333;
}
Note that I added, box-sizing:border-box; this means when you use padding it will be included in the width, not on top of it. Effectively enabling the use of it without an extra inner div.
Remember to include a clear fix afterwards also to "clear" the floats.
CSS
.clear {
clear:both;
}
HTML
<div class="block">
<div class="col left">
<h1>Left</h1>
</div>
<div class="col right">
<h1>Right</h1>
</div>
<div class="clear"></div>
</div>
Try replacing these classes:
.block {
background: none repeat scroll 0 0 #555555;
display: block;
overflow: auto;
width: 100%;
}
.col {
width: 49%;
float: left;
background: #333;
}
.container {
background: #DDD;
width: 900px;
margin: 0 auto;
padding: 30px 30px 30px 30px;
}
.block {
background: #555;
width: 100%;
display: block;
}
.block:after {
content: "";
display: table;
clear: both;
}
.col {
width: 50%;
float: left;
background: #333;
}

Resources