Responsive divs needed with auto width - css

I try to have a row with 3 divs.
The middle div should be responsive with a mex-width, remaining space left and write should be filled in with a color.
I know I could solve this using an wrapper with auto-margin left and right but I need to have the center div transparent and not showing the wrapper background color.
I tried to solve this using a display table but did not manage to do this.
<div id="table">
<div id="row">
<div id="left"></div>
<div id="middle">Row 1</div>
<div id="right"></div>
</div>
<div style="clear:both;"></div>
<div id="row">
<div id="left"></div>
<div id="middle">Row 2</div>
<div id="right"></div>
</div>
<div style="clear:both;"></div>
</div>
with css
#table {
width:100%;
display:table;
}
#row {
width:100%;
display:table-row;
margin-bottom:10px;
}
#left {
width:auto;
display:table-cell;
background-color: #FF0000;
}
#middle {
display:table-cell;
width:auto;
max-width: 100px;
height:50px;
background-color: #eeeeee;
}
#right {
width:auto;
display:table-cell;
background-color: #FF0000;
}
http://jsfiddle.net/thepofo/kdJHh/2/
Ideas are welcome.

The hacky way
http://jsfiddle.net/coma/rFg2L/2/
.foo {
overflow: hidden;
}
.foo > div {
position: relative;
margin: 0 auto;
max-width: 100px;
}
.foo > div:before,
.foo > div:after {
content: "";
position: absolute;
top: 0;
bottom: 0;
width: 999999px;
background-color: red;
}
.foo > div:before {
right: 100%;
}
.foo > div:after {
left: 100%;
}
The flexbox way (http://caniuse.com/#search=flex)
http://jsfiddle.net/coma/YYm32/
.foo {
display: -webkit-box;
-webkit-box-orient: horizontal;
display: -moz-box;
-moz-box-orient: horizontal;
display: box;
box-orient: horizontal;
}
.foo > div {
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
}
.foo > div:nth-child(2) {
max-width: 100px;
}
.foo > div:first-child,
.foo > div:last-child {
background-color: red;
}
Bonus track
You don't need to add extra markup to clear your floats: http://jsfiddle.net/coma/Lqc2H/

This may or may not be enough to work for you, but how about setting the div to 33% width of the wrapper.
<div class="wrap">
<div class="left"> </div>
<div class="middle">Lorem ipsum dolor...</div>
<div class="right"> </div>
</div>
.wrap>div {
float:left;
width: 33%;
}
.middle {
max-width: 200px;
outline:1px solid;
}
fiddle here
Otherwise, you might want to consider media queries for specific widths.

Related

CSS - 2 Columns, One Fixed width, One Responsive

There are many examples on this topic here, but what I am after is something that I cannot seem to find the answer to.
I want to create a two column page:
Left column for Navigation (Fixed Width), Right column for content (Responsive). The variation I am after is that I want the Nav to appear on the left for desktop and beneath the content on mobile.
I have the code 'working' kind of, but it's the responsive width of the right column that is the issue.
My code is below, Any guidance would be really valued.
.CF:after { content:"."; display:block; height:0; clear:both; visibility:hidden; }
.CF { display:inline-block; }
.CF { display:block; }
.content {max-width: 1300px; margin: 0 auto; padding: 0 25px; display:block; }
.information {display:block; background: lime;}
.menu {display:block; background: lightblue;}
#media all and (min-width: 992px) {
.content {padding: 0 50px; }
.information {display:block; float: right; width: auto; }
.menu {width: 250px; float:right; }
}
<div class="content CF">
<article class="information">
Article Information
</article>
<nav class="menu">
Menu
</nav>
</div>
I would use flexbox to solve this.
Below the menu is at the left side when the width of the screen is more than 991 pixels. Otherwise it is below the article.
I have assigned colors for visibility only.
.content {
display: flex;
background: blue;
}
.menu {
width: 100px;
background: red;
}
.information {
background: green;
flex-grow: 1;
}
#media all and (max-width: 991px) {
.content {
flex-wrap: wrap;
}
.menu {
order: 2;
}
.information {
order: 1;
min-width: 100%;
}
}
<div class="content">
<nav class="menu">
Menu
</nav>
<article class="information">
Article Information
</article>
</div>
I would suggest looking at CSS Grid or Flexbox for layout. Support for Grid is good.
Example using Grid...
codepen
.content {
max-width: 1300px;
margin: 0 auto;
padding: 0 25px;
display: block;
grid-template-columns: 250px 1fr;
}
.information {
background: lime;
order: 2;
}
.menu {
background: lightblue;
}
#media all and (min-width: 992px) {
.content {
display: grid;
padding: 0 50px;
}
}
<div class="content CF">
<article class="information">
Article Information
</article>
<nav class="menu">
Menu
</nav>
</div>
You've already had two great answers but just to add a few extra options, here are 5 different ways of achieving the same thing:
Example 1: Floats, Widths and Margins
The most compatible method, works on pretty much all browsers. Its also got the smallest CSS footprint.
#example1 .fixedColumn {
width: 180px;
float: left;
}
#example1 .flexibleColumn {
margin-left: 200px;
}
/* gratuituous styling */
.fixedColumn { background-color: #e84c1b; padding: 10px;} .flexibleColumn { background-color: #039be4; padding: 10px;} body { margin: 0; }
<div id="example1">
<div class="fixedColumn">
Fixed Column
</div>
<div class="flexibleColumn">
Flexible Column
</div>
</div>
Example 2: CSS calc()
Compatible with IE9+. Its a solid alternative if you don't need backwards compatibility.
#example2.calc {
overflow: hidden;
}
#example2.calc .fixedColumn {
width: 180px;
float: left;
}
#example2.calc .flexibleColumn {
width: calc(100% - 220px); /*200px for the fixed column and 20 for the left and right padding */
float: left;
}
/* gratuituous styling */
.fixedColumn { background-color: #e84c1b; padding: 10px;} .flexibleColumn { background-color: #039be4; padding: 10px;} body { margin: 0; }
<div id="example2" class="calc">
<div class="fixedColumn">
Fixed Column
</div>
<div class="flexibleColumn">
Flexible Column
</div>
</div>
Example 3: Display as Table
Another solid contender for backwards compatibility works pretty much across the board, but still feels like a bodge making things behave like a table.
#example3.table {
display: table;
width: 100%;
}
#example3.table .fixedColumn {
width: 180px;
display: table-cell;
}
#example3.table .flexibleColumn {
display: table-cell;
}
/* gratuituous styling */
.fixedColumn { background-color: #e84c1b; padding: 10px;} .flexibleColumn { background-color: #039be4; padding: 10px;} body { margin: 0; }
<div id="example3" class="table">
<div class="fixedColumn">
Fixed Column
</div>
<div class="flexibleColumn">
Flexible Column
</div>
</div>
Example 4: Flexbox
Great for modern browsers that support it; simple and intuitive.
#example4.flex {
display: flex;
}
#example4.flex .fixedColumn {
width: 180px;
}
#example4.flex .flexibleColumn {
flex: 1;
}
/* gratuituous styling */
.fixedColumn { background-color: #e84c1b; padding: 10px;} .flexibleColumn { background-color: #039be4; padding: 10px;} body { margin: 0; }
<div id="example4" class="flex">
<div class="fixedColumn">
Fixed Column
</div>
<div class="flexibleColumn">
Flexible Column
</div>
</div>
Example 5: Grid
Out of all the techniques here Grid was supported by browsers last. But its a great option if you have the option to use it.
#example5.grid {
display: grid;
grid-template-columns: 200px 1fr;
}
#example5.grid .fixedColumn {
grid-column: 1;
}
#example5.grid .flexibleColumn {
grid-column: 2;
}
/* gratuituous styling */
.fixedColumn { background-color: #e84c1b; padding: 10px;} .flexibleColumn { background-color: #039be4; padding: 10px;} body { margin: 0; }
<div id="example5" class="grid">
<div class="fixedColumn">
Fixed Column
</div>
<div class="flexibleColumn">
Flexible Column
</div>
</div>

How to split a column in a responsive view using Bootstrap?

I'm using Bootstrap v4 alpha4
Currently I have:
.row
.col-xs-12.col-md-8
div A
.col-xs-12.col-md-4
div B
div C
For the xs layout, I'd like the div order to be:
Div B
Div A
Div C
I have no idea how to do this or how to even ask about it. I'm not a front-end dev so I don't know what things are called.
We can change the HTML to whatever we want. It does not have to stay like it is now.
Bootstrap does have column ordering classes, but in this case you can simply use the responsive float classes..
<div class="row">
<div class="col-md-4 pull-md-right">
b
</div>
<div class="col-md-8">
a
</div>
<div class="col-md-4">
c
</div>
</div>
http://www.codeply.com/go/XL5zJELyLD
So using the classes from bootstrap and some general style you can achieve that like I did in this pen.
http://codepen.io/TunderScripts/pen/PGadpr
The Html:
<div class="row">
<div class="col-xs-12 col-md-4 pull-right col1"></div>
<div class="col-xs-12 col-md-8 pull-left col2"></div>
<div class="col-xs-12 col-md-4 pull-right col3"></div>
</div>
the css:
.col1{
background: red;
height: 200px;
}
.col2{
background: blue;
height: 600px;
}
.col3{
background: green;
height: 200px;
}
You can change the default behavior by using their classes for floats(pull-left, pull-right).
Instead of flexbox, I used combination of float and position css properties to get the expected result. Assuming large width as 150px and small width as 100px.
Working Fiddle
.container {
width: 250px;
position: relative;
}
.blue {
width: 150px;
height: 300px;
background: blue;
position: absolute;
}
.pink {
width: 100px;
height: 100px;
background: pink;
float: right;
}
.green {
width: 100px;
height: 100px;
background: green;
clear: right;
float: right;
}
#media (max-width: 450px) {
.blue {
position: relative;
}
.green,
.pink {
float: none;
width: 150px;
}
}
<div class="container">
<div class="pink"></div>
<div class="blue"></div>
<div class="green"></div>
</div>
As promised, a simple draft
HTML
<div class="row">
<div class="col1">DIV A</div>
<div class="col2">DIV B</div>
<div class="col3">DIV C</div>
</div>
CSS
.row {
display: flex;
flex-wrap: wrap;
flex-direction: row;
justify-content: space-between;
width: 400px;
margin: 0 auto;
}
.col1 {
width: 200px;
height: 400px;
background-color: #86a0ff;
}
.col2 {
width: 150px;
height: 150px;
background-color: #ff6cde;
}
.col3 {
margin-top: -200px;
margin-left: auto;
width: 150px;
height: 150px;
background-color: #35af6d;
}
#media (max-width: 768px) {
.row {
justify-content: center;
flex-direction: column;
}
.col1 {
order: 2;
width: 200px;
margin-top: 50px;
}
.col2 {
order: 1;
width: 200px;
}
.col3 {
order: 3;
width: 200px;
margin-top: 50px;
margin-left: 0;
}
}
As for explanation, here is a great guide to flexbox. The main idea in my example is that by using order property you can manipulate the order in which blocks are displaying. The main plus of using flexbox is that you won't need to load any library(such as Bootstrap) to achieve the desired result, such as responsiveness. And it also has a good browser support, unless you need to support older versions of browsers. I hope my answer will be helpful for you!

Sidebar on the left of the content but after in the dom tree

I need to put a sidebar on the left of the content.
I had this html:
<div class="sidebar"></div>
<div class="content"></div>
and I solved using:
.sidebar{
width: 280px;
float: left
}
.sidebar + .content{
margin-left: 300px
}
For this example: https://jsfiddle.net/VixedS/fcx2aLLa/
But now my that .content comes before the .sidebar,
<div class="content"></div>
<div class="sidebar"></div>
how can I obtain the same result just using css?
I don't want to loose the remaining space of the body for the width of .content with or without .sidebar.
So please remember that before saying to float the .content to right. Also, I don't know which page has a .sidebar.
This solution is very powerfull. Works for every browser, any device. Also a great way for responsive design and for a third column.
Update:
.container {
overflow:auto;
}
.sidebar {
width: 280px;
float: left;
background: #EEE;
margin-left: -100%;
}
.content {
float: left;
width: 100%;
}
.center {
margin-left: 280px;
}
.container > div:only-child > div.center {
margin-left: 0;
}
<div class="container">
<div class="content">
<div class="center">
Spaghetti
</div>
</div>
<div class="sidebar">
Pizza
<br /> Hobby
</div>
</div>
Do following way. Use float:right to sidebar and display:table to it's parent.
body {
display: table;
}
.content {
float: right;
}
.sidebar{
width: 280px;
float: left;
background:#EEE; // just for this example
}
.sidebar + .content{
margin-left: 300px
}
<div class="content">Spaghetti</div>
<div class="sidebar">Pizza <br /> Hobby</div>
Flexbox...means you don't have to use floats...and you can re-order the elements as you require.
Support is IE10 and up.
body {
display: flex;
}
.sidebar {
width: 280px;
background: #aaa; // just for this example
order: 0;
}
.content {
flex: 1;
order: 1;
background: plum;
}
<div class="content">
Spaghetti</div>
<div class="sidebar">Pizza
<br />Hobby</div>

How to add text after an h1 but keep the h1 centered

I have an H1, and a link that needs to go after it (actually an image link, but I'm using text in the example below to simplify the code)
What I would like is for the link to appear after the H1 on the same line, but for the H1 to remain centered in it's containing div.
Right now, the link displaces the header from center...
Here's what I have
<div id="container">
<h1>Hello World</h1> <a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
#container {
width: 400px;
text-align: center;
background-color: pink;
}
#container h1 {
display: inline-block;
}
And a fiddle of the same thing: http://jsfiddle.net/7xzq60x4/
In pictures:
now:
****HEADER TEXTlink****
what I want:
******HEADER TEXTlink**
Thanks in advance
Is this what you want?
#container {
width: 400px;
text-align: center;
background-color: pink;
position: relative;
}
#container h1 {
display: inline-block;
}
#container span {
position: absolute;
right: -75px;
top: 65%;
}
<div id="container">
<h1>Hello World</h1><span><a id="gear" href="/aaa">Long text so you can see the displacement</a></span>
</div>
Fiddle: http://jsfiddle.net/7xzq60x4/2/
Something which you might like...
#container {
width: 400px;
text-align: center;
background-color: pink;
position: relative;
}
#container h1 {
display: inline-block;
}
#container span {
position: absolute;
right: 25px;
top: 35%;
}
<div id="container">
<h1>Hello World</h1><span><a id="gear" href="/aaa">Small Text</a></span>
</div>
you may use the flex property + a pseudo element : http://jsfiddle.net/7xzq60x4/8/
#container {
width: 400px;
text-align: center;
background-color: pink;
display:flex;
}
#container a {
text-align:left;
}
#container:before {
content:'';
}
#container h1 {
white-space:nowrap;/* if this is what you 'd like */
}
#container h1 , #container a, #container:before{
flex:1;
margin:auto;
}
<div id="container">
<h1>Hello World</h1> <a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
or the table layout http://jsfiddle.net/7xzq60x4/9/
#container {
width: 400px;
text-align: center;
background-color: pink;
display:table;
table-layout:fixed;
}
#container a {
text-align:left;
}
#container:before {
content:'';
}
#container h1, #container a, #container:before {
display:table-cell;
vertical-align:middle;
}
<div id="container">
<h1>Hello World</h1> <a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
If you're okay with using flexbox, this can be achieved quite easily. This will need a few prefixes for compliance with all browsers, and may not go back to support IE9.
#container {
/* Use flexbox. */
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
/* Tells flexbox to place all items on a single line. */
-ms-flex-direction: row;
-webkit-flex-direction: row;
flex-direction: row;
/* Lastly tells flexbox to ensure all items are aligned on the center. */
align-items: center;
width: 400px;
text-align: center;
background-color: pink;
}
<div id="container">
<h1>Hello World</h1>
<a id="gear" href="/aaa">Long text so you can see the displacement</a>
</div>
For more information, try this quick guide.

css two rows 1 column box layout

I've been working on a two row and 1 column layout with flexbox, I'm using flexbox because I don't think css2.1 can fill the remainding space for box-B. In my example of my jsFiddle, I can't get box-C to shift up on the right hand side and also I can't get box-B to flex vertically and fill the contents can someone please help me with this layout
jsFiddle here
#container {
background-color:red;
width:100%; height:100%
}
#three-box-layout {
display:flex;
display:-ms-flex;
display:-webkit-flexbox;
display:-moz-flex;
height:100%;
-ms-flex-direction:column;
-webkit-flex-direction:column
}
.shuffle-box {
}
#box-a {
background-color:#f601ff; -ms-flex-order:1; -webkit-flex-order:1;
margin-right:30%;
}
#box-b {
-ms-flex:3;
-webkit-flex:3;
-moz-flex:3;
flex:3;
background-color:#37fe02;
margin-right:30%;
}
#three-box-layout #box-c {
-ms-flex:3;
-webkit-flex:3;
-moz-flex:3;
flex:3;
background-color:#02effe;
margin-left:70%; float:right;
}
<div id="container">
<div id="three-box-layout">
<div id="box-a" class="shuffle-box">
<div style="height:425px; background-color:pink">A</div>
</div>
<div id="box-b">B</div>
<div id="box-c">C</div>
</div>
</div>
You can do this with CSS tables (Flexbox isn't necessary)
Resize the browser to see the media queries in action!
FIDDLE1 (little content) / FIDDLE2 (lots of content)
Markup
<div class="container">
<div class="row1">
<div>A</div>
<div></div> /* give this div table cell 50% width on wide screens */
</div>
<div class="row2">
<div>B</div>
<div>C</div>
</div>
</div>
CSS
--
.container {
width: 800px;
height: 500px;
display:table;
text-align: center;
position: relative;
}
.row1 {
display:table-row;
max-height: 425px;
background: pink;
}
.row1 div {
display:table-cell;
width:50%;
}
.row2 {
display:table-row;
height: 100%;
}
.row2 div {
width: 100%;
height: 100%;
float:left;
background: green;
}
.row2 div + div {
background: aqua;
width: 50%;
height: 100%;
position: absolute;
top:0;
right:0;
}
#media (max-width: 1024px) {
.row1 {
width: 100%;
}
.row1 div + div {
display: none;
}
.row2 div {
width: 50%;
}
.row2 div + div {
position: static;
}
}

Resources