One column static width while other colum is dynamic width - html

<div class="table">
<div class="sidebar"></div>
<div class="content"></div>
</div>
* {
margin:0; padding:0;
}
html, body {
width:100%;
height:100%;
}
.table {
display:table;
width:100%;
height:100%;
}
.sidebar {
width:200px;
height:100%;
background:red;
display:table-cell;
white-space:nowrap;
vertical-align: top;
}
.content {
width:100%;
height:100%;
background:orange;
display:table-cell;
vertical-align: top;
}
What am I missing exactly? :), trying to replicate this structure:
http://dev.brigademarketing.com/brigade/old-content/site1/
fiddle:
http://jsfiddle.net/8o50f0cf/

Remove the width:100% from the dynamic column, so it can calculate its width automatically.
Updated Fiddle
A display: table-cell element acts like a <td>, meaning that it takes the remaining space of its table parent if no width is define.

Here's a solution that uses calc() function and floating: http://jsfiddle.net/jyx9orLy/.
HTML:
<div class="container">
<div class="sidebar"></div>
<div class="content"></div>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
html, body, .container {
height: 100%;
}
.container .sidebar {
height: 100%;
background-color: red;
width: 200px;
float: left;
}
.container .content {
float: left;
height: 100%;
width: calc(100% - 200px);
background-color: orange;
}
A second solution is to use a table, which you are doing, and to make it work you do what #LcSalazar mentioned.
A third solution uses flexbox specification and requires a modern browser: http://jsfiddle.net/yp49uqay/.
CSS:
* {
margin: 0;
padding: 0;
}
html, body, .container {
height: 100%;
}
.container {
display: flex;
flex-direction: row;
}
.container > .sidebar {
flex: 0 0 200px;
background-color: red;
}
.container > .content {
flex: 1 1 auto;
background-color: orange;
}
And, a fourth solution that uses floating in a different way: http://jsfiddle.net/079sr0fu/.
HTML:
<div class="container">
<div class="sidebar"></div>
<div class = "contentWrapper">
<div class="content">Content...</div>
</div>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
html, body, .container,
.container > .sidebar,
.container > .contentWrapper,
.container > .contentWrapper > .content {
height: 100%;
}
.container > .sidebar {
width: 200px;
background-color: red;
float: left;
}
.container > .contentWrapper {
overflow: hidden;
}
.container .content {
float: left;
width: 100%;
background-color: orange;
}

Related

Container full width

first of all, sorry for the title not really explicit
Here is my problem:
I've container, inside it a sidebar which is 280px, and I would like to make the home page full width. But if I write something like this
.container {
width: 100%;
.sidebar {
width: 280px;
}
.home {
width: 100%;
}
}
the home page goes underneath, and I would like to position aside the sidebar.
But I have no idea how to do it
Here is a CSS solution using calc() function to minus the .sidebar width from .home
#container {
width: 100%;
height: 300px;
}
#container .sidebar {
width: 280px;
height: 100%;
background: blue;
display: inline-block;
color:#fff;
}
#container .home {
width: calc(100% - 285px);
height: 100%;
background: yellow;
display: inline-block;
}
<div id="container">
<div class="sidebar">SideBar</div>
<div class="home">Home</div>
</div>
This should work:
https://jsfiddle.net/0bsygLsh/
.container {
width: 100%;
overflow:hidden;
.sidebar {
width:280px;
display:inline-block;
float:left;
}
.home {
width:auto;
}
}
It seems like you need to read about the CSS display property. This is perhaps the most important thing to know when dealing with CSS. See here https://www.w3schools.com/cssref/pr_class_display.asp
Here's an example of using display: flex to solve your problem.
Your problem could be solved in several ways though. Consider attempting it yourself using display: inline.
.container {
display: flex;
height: 100vh;
}
.sidebar {
flex-basis: 100px;
background-color: red;
}
.home {
flex: 1;
background-color: blue;
}
<div class="container">
<div class="sidebar">
I'm the sidebar
</div>
<div class="home">
I'm the home page
</div>
</div>
The following will create a sidebar with a fixed width of 280px and a div (.home) with a fluid width:
SCSS
.container {
width: 100%;
overflow:hidden;
.sidebar {
float:right; /* or float:left for left sidebar */
width:280px;
}
.home {
margin-right:280px; /* or margin-left for left sidebar */
}
}
HTML
<div class="container">
<div class="sidebar">
</div>
<div class="home">
</div>
</div>
If you want to make the home div full width and the sidebar div should be on top of it then the css will be following:
.container {
width: 100%;
position: relative;
}
.sidebar {
width: 280px;
position: absolute;
top: 0;
left: 0;
}
.home {
width: 100%;
padding-left: 280px;
}
Or if you want to keep them side by side then the css should be following:
.container {
width: 100%;
}
.container:after {
display: table;
content: "";
clear: both;
}
.sidebar {
float: left;
width: 280px;
}
.home {
float: left;
width: calc(100% - 280px);
}

Absolute-positioned div within a div that takes all remaining space

I'm trying to make a layout that seems simple. Despite looking at lots of examples, I can't crack it.
SideBar| .......MapContainer......
SideBar| ..........Map............
SideBar| .......MapContainer......
Both SideBar and MapContainer should be 100% height.
The tricky bit: Map must have a defined height and width, because the mapbox-gl-js library uses its dimensions to populate it. (Rather than adding content which then sizes it).
MapContainer exists because there will be other absolutely positioned overlay elements within it.
I'm trying to avoid having the sidebar width coded into the definition of MapContainer so I can hide/show the sidebar in JS, and have the MapContainer automatically fill the space.
This gets really, really close:
* {
box-sizing: border-box;
}
.sidebar, .mapcontainer, .container {
height: 200px;
}
.container {
width:100%;
border:1px solid;
display: flex
}
.sidebar {
width:200px;
background:lightblue;
}
.mapcontainer {
width:auto;
background:lightgreen;
position:relative;
flex-grow: 1;
}
.map {
width: 100%;
height: 100%;
position:absolute;
border: 20px dashed grey;
}
<div class="container">
<div class="sidebar"></div>
<div class="mapcontainer">
<div class="map">
</div>
</div>
</div>
But as soon as I change the "height: 200px" to "height: 100%", it collapses to nothing. What do I need to do?
Use viewport units vh instead in the .sidebar, .mapcontainer, .container rule
* {
box-sizing: border-box;
}
html, body {
margin: 0;
}
.sidebar, .mapcontainer, .container {
height: 100vh;
}
.container {
border: 1px solid;
display: flex
}
.sidebar {
width: 200px;
background:lightblue;
}
.mapcontainer {
background:lightgreen;
position:relative;
flex-grow: 1;
}
.map {
width: 100%;
height: 100%;
position:absolute;
border: 20px dashed grey;
}
<div class="container">
<div class="sidebar"></div>
<div class="mapcontainer">
<div class="map">
</div>
</div>
</div>
I just need to add height: 100%; to html and body:
* {
box-sizing: border-box;
}
html, body {
height: 100%;
margin: 0;
}
.sidebar, .mapcontainer, .container {
height: 100%;
}
.container {
width:100%;
border:1px solid;
display: flex
}
.sidebar {
width:200px;
background:lightblue;
}
.mapcontainer {
width:auto;
background:lightgreen;
position:relative;
flex-grow: 1;
}
.map {
width: 100%;
height: 100%;
position:absolute;
border: 20px dashed grey;
}
<div class="container">
<div class="sidebar"></div>
<div class="mapcontainer">
<div class="map">
</div>
</div>
</div>

Horizontally aligning inner `divs`

I am looking to horizontally (and preferably vertically) align three inner divs. Applying margin: 0 auto; to class vbox should do the trick but as in the following minimal code, it isn't affecting the alignment at all. How can i accomplish this aligning?
HTML:
<body>
<div id='site'>
<div class='main'>
<div class='vbox'>
<div id='inner1'>inner1</div>
<div id='inner2'>inner2</div>
<div id='inner3'>inner3</div>
</div>
</div>
</div>
</body>
CSS:
#site {
width: 100%;
height: 100%;
}
#main {}
.vbox {
margin: 0 auto;
}
The result can be seen in this fiddle.
You need to define the width for vbox:
.vbox {
margin: 0 auto;
width: 30%;/*apply as your own*/
}
100% wide element is centered horizontally but you see no alignment for text. For this you should apply text-align: center;
Just give display: table; to .vbox will make it horizontal center.
.vbox {
margin: 0 auto;
display: table;
}
Working Fiddle
Another solution is display: flex;
.main {
display: flex;
}
Fiddle
You can use display: inline-block for your inner divs:
.vbox {
text-align: center;
font-size: 0; /* white spaces fix */
}
.vbox > div {
font-size: 1rem; /* white spaces fix */
display: inline-block;
}
Example
Try something like this
#site {
width: 100%;
height: 100%;
}
#main {
Width:100%;
}
.vbox {
margin: 0 auto;
padding:0;
}
.vbox div{
width:32%;
display:inline-block;
padding:0;
margin:0;
box-sizing:border-box;
}
The important bit is that the default behavior of a div is to take up the full width of its parent. To change this you give it the display mode inline-block.
You could do this :
HTML
<body>
<div id='site'>
<div class='main'>
<div class='vbox'>
<div class='inner'>inner1</div>
<div class='inner'>inner2</div>
<div class='inner'>inner3</div>
</div>
</div>
</div>
</body>
CSS
#main {
width: 100%;
}
.vbox {
width: 500px;
margin: 0 auto;
text-align: center;
}
.inner {
display: inline-block;
margin: 0 4px;
}
You could do this to align all 3 divs vertically by using:
#site {
width: 100%;
height: 100%;
}
#main {}
.vbox {
margin: 0 auto;
}
.vbox div{
width: 30%;
float: left;
}
If you have more divs or less, just update the width accordingly.

Dynamic grid system with different widths css3?

I need to perform a dynamic grid system like this:
Each section is an article that contains an image, a title and a link/button to that article.
The problem is that each section is loaded dynamically and i only have the html of the section so i need to put each section on the correct position dynamically from the CSS. The one i know is that there are 5 sections.
The html code of each section and the container of all the sections is this:
<section class="scroll">
<!-- ARTICLES -->
<!-- ARTICLE -->
<div class="article-content">
<img class="article-image" src="${item.imgPath}" />
<div class="article-texts">
<h1 class="article-title">${item.title}</h1>
<a class="article-button" href="${item.link}.html" role="button">Read Article ></a>
</div>
</div>
<div class="clear"></div>
<!-- END ARTICLE -->
<!-- END ARTICLES -->
</section>
If you have control over the dimensions of your sections, you can use a fixed width container and float the sections inside that. Clear the float on the fourth section.
Example Fiddle: http://jsfiddle.net/abhitalks/mbuf9957/3/
Example Snippet:
* { box-sizing: border-box; padding: 0; margin:0; }
div { width: 380px; overflow: hidden; }
section { border: 1px solid #666; float: left; }
section:nth-child(1) { width: 240px; height: 240px; }
section:nth-child(2) { width: 120px; height: 120px; }
section:nth-child(3) { width: 120px; height: 120px; }
section:nth-child(4) { width: 120px; height: 120px; clear: left; }
section:nth-child(5) { width: 240px; height: 120px; }
<div>
<section>1</section>
<section>2</section>
<section>3</section>
<section>4</section>
<section>5</section>
</div>
Since you have tagged this as CSS3, I think Flexbox would be an option. You could set display:flex on the parent and then have percentage widths for each box's flex-basis and set the flex-grow property to the amount of space, relative to other boxes, you want them to take up in the container and set flex-shrink to 0 since you don't need them to shrink.
CSS/HTML:
.grid-system {
/* Uncomment the next line to see the container */
/* border:1px solid black; */
}
.grid-system .box-width-2 {
border:1px solid black;
-webkit-flex:2 0 65%;
flex: 2 0 65%;
}
.grid-system .box-width-1 {
border:1px solid black;
-webkit-flex:1 0 32%;
flex: 1 0 32%;
}
.grid-system .box-height-2 {
-webkit-flex-grow:2;
flex-grow:2;
}
.grid-system .box-height-1 {
-webkit-flex-grow:1;
flex-grow:1;
}
.grid-system .flex-row {
display:-webkit-flex;
display:flex;
-webkit-flex-flow:row nowrap;
flex-flow:row nowrap;
-webkit-justify-content:flext-start;
justify-content:flex-start;
}
.grid-system .flex-column {
display:-webkit-flex;
display:flex;
-webkit-flex-flow:column nowrap;
flex-flow:column nowrap;
width:32%;
}
.grid-system .flex-row > div {
margin:0.5%
}
.grid-system .box-width-1.box-height-1 {
margin-bottom:0.5%;
-webkit-flex-grow:1;
flex-grow:1;
}
.grid-system .box-width-1.box-height-1.end {
margin-bottom:0px;
}
<div class="grid-system">
<div class="flex-row">
<div class="box-width-2 box-height-2">1</div>
<div class="flex-column">
<div class="box-width-1 box-height-1">2</div>
<div class="box-width-1 box-height-1 end">3</div>
</div>
</div>
<div class="flex-row">
<div class="box-width-1">4</div>
<div class="box-width-2">5</div>
</div>
</div>
jsFiddle
A solution only involving floats can reproduce your layout. Compatibility IE8+ (and even below but nobody cares). Pseudo-class :nth-child() (compat. IE9+) is used here to give an arbitrary width and height for demo, you'll have your own layout in real conditions.
* { box-sizing: border-box; }
div { width: 360px; }
section { border: 1px solid #666; }
.left { float: left; }
.right { float: right; }
.clear { clear: both; }
section:nth-child(1) { width: 240px; height: 240px; }
section:nth-child(2) { width: 120px; height: 100px; }
section:nth-child(3) { width: 120px; height: 80px; }
section:nth-child(4) { width: 200px; height: 120px; }
section:nth-child(5) { width: 160px; height: 100px; }
<div>
<section class="left">1</section>
<section class="right">2</section>
<section class="right">3</section>
<section class="left clear">4</section>
<section class="right">5</section>
</div>

3 div's middle div take up remaining width

Alright guys, I've been busting my balls over this one.
I've got three divs; left, middle, right. All 100% height. The left and right div have a fixed width of 150px. Now I want the middle div to take up the remaining space.
Example: Here
CSS:
#left {
height:100%;
width:150px;
float:left;
background:red;
z-index:999;
}
#middle {
height:100%;
width:100%;
background:yellow;
margin-left:-150px;
margin-right:-150px;
}
#right {
float:right;
height:100%;
width:150px;
background:red;
z-index:998;
}
Use display: table:
#container {
display: table;
width: 100%;
}
#left, #middle, #right {
display: table-cell;
}
#left, #right {
width: 150px;
}
Where the #container is your parent element like in
<div id="container">
<div id="left"></div>
<div id="middle"></div>
<div id="right"></div>
</div>
Here is a Fiddle.
Okay using flex and based this answer
#parent {
display: flex;
}
#left {
width:150px;
background-color:#ff0000;
}
#middle {
flex: 1 1 auto;
background-color:#00ff00;
}
#right {
width: 150px;
background-color:#0000ff;
}
<div id="parent">
<div id="left">left</div>
<div id="middle">middle</div>
<div id="right">right</div>
</div>
This actually works if the left and right divs has variable width also.
Check this similar answer
HTML
<div class = "container">
<div class = "fixed">
I'm 150px wide! Glee is awesome!
</div>
<div class = "fluid">
I'm fluid! Glee is awesome!
</div>
<div class = "fixed">
I'm 150px wide! Glee is awesome!
</div>
</div>
CSS
html, body {
height: 100%;
font-family: 'Arial', 'Helvetica', Sans-Serif;
}
.container {
display: table;
width: 100%;
height: 100%;
}
.container > div {
display: table-cell;
text-align: center;
}
.fixed {
width: 150px;
background: rgb(34, 177, 77);
color: white;
}
.fluid {
background: rgb(0, 162, 232);
}
DEMO
If you don't want to use table cells and don't want to give your outer boxes a fixed width (instead let their width be determined by their content, you can use the overflow-hidden and float method)
The bad thing about this method is you have to adhere to having overflow hidden on your divs and also you have to arrange your divs in a backwards way (see below)
DEMO
HTML
<div class='container'>
<div class='third-box'>Third</div>
<div class='first-second'>
<div class='first-box'>First</div>
<div class='second-box'>Second</div>
</div>
</div>
CSS
.container {
width: 400px;
}
.first-second {
overflow: hidden;
}
.first-box {
float: left;
background-color: coral;
}
.second-box {
overflow: hidden;
background-color: aquamarine;
}
.third-box {
float: right;
background-color: salmon;
}
I used this to set a flexible width for the items to the side. Used it in a listed item to get a progress bar with 2 buttons on the side.
ul {
display: table;
width: 100%;
}
ul > li {
display: table-cell;
}
ul > li:nth-child(2) {
min-width: 100%;
}