Display a div width 100% with margins - html

I want to display an expandable div (width: 100%) with margins...
#page {
background: red;
float: left;
width: 100%;
height: 300px;
}
#margin {
background: green;
float: left;
width: 100%;
height: 300px;
margin: 10px;
}
<div id="page">
<div id="margin">
"some content here"
</div>
</div>

You can use calc() css function (browser support).
#page {
background: red;
float: left;
width: 100%;
height: 300px;
}
#margin {
background: green;
float: left;
width: calc(100% - 10px);
height: 300px;
margin: 10px;
}​
Alternatively, try using padding instead of margin and box-sizing: border-box (browser support):
#page {
background: red;
width: 100%;
height: 300px;
padding: 10px;
}
#margin {
box-sizing: border-box;
background: green;
width: 100%;
height: 300px;
}

Sometimes it's better to do the opposite and give the parent div padding instead:
LIVE DEMO
What I did was change the CSS of #page to:
#page {
padding: 3%;
width: 94%; /* 94% + 3% +3% = 100% */
/* keep the rest of your css */
/* ... */
}
Then delete the margin from #margin
Note: this also adds 3% to the top and bottom (so 6% to the height) which makes it a little taller than 300px - so if you need exactly 300px, you could do something like padding:10px 3%; and change the height:280px; to add up to 300px again.

You can use the following CSS to achieve the desired result:
#page {
background: red;
overflow: auto;
}
#margin {
background: green;
height: 280px;
margin: 10px
}

The correct way to achieve this by standard is:
#margin {
background: green;
height: 280px;
margin: 10px;
width: auto;
display: block;
}

For LESS users only:
Using the nice solution of Vukašin Manojlović doesn't work out of the box because LESS executes + or - operations during the LESS compilation.
One solution is to escape LESS so that it doesn't execute the operation.
Disable LESS-CSS Overwriting calc()
#someMarginVariable = 15px;
margin: #someMarginVariable;
width: calc(~"100% - "#someMarginVariable*2);
width: -moz-calc(~"100% - "#someMarginVariable*2);
width: -webkit-calc(~"100% - "#someMarginVariable*2);
width: -o-calc(~"100% - "#someMarginVariable*2);
or can use a mixin like:
.fullWidthMinusMarginPaddingMixin(#marginSize,#paddingSize) {
#minusValue: (#marginSize+#paddingSize)*2;
padding: #paddingSize;
margin: #marginSize;
width: calc(~"100% - "#minusValue);
width: -moz-calc(~"100% - "#minusValue);
width: -webkit-calc(~"100% - "#minusValue);
width: -o-calc(~"100% - "#minusValue);
}

If possible, try to use padding with box-sizing instead, on #page element
#page {
padding: 10px;
box-sizing: border-box;
background: red;
float: left;
width: 100%;
height: 300px;
}
#margin {
background: green;
float: left;
width: 100%;
height: 100%;
}

Related

Element's side margin overflows parent [duplicate]

I want to display an expandable div (width: 100%) with margins...
#page {
background: red;
float: left;
width: 100%;
height: 300px;
}
#margin {
background: green;
float: left;
width: 100%;
height: 300px;
margin: 10px;
}
<div id="page">
<div id="margin">
"some content here"
</div>
</div>
You can use calc() css function (browser support).
#page {
background: red;
float: left;
width: 100%;
height: 300px;
}
#margin {
background: green;
float: left;
width: calc(100% - 10px);
height: 300px;
margin: 10px;
}​
Alternatively, try using padding instead of margin and box-sizing: border-box (browser support):
#page {
background: red;
width: 100%;
height: 300px;
padding: 10px;
}
#margin {
box-sizing: border-box;
background: green;
width: 100%;
height: 300px;
}
Sometimes it's better to do the opposite and give the parent div padding instead:
LIVE DEMO
What I did was change the CSS of #page to:
#page {
padding: 3%;
width: 94%; /* 94% + 3% +3% = 100% */
/* keep the rest of your css */
/* ... */
}
Then delete the margin from #margin
Note: this also adds 3% to the top and bottom (so 6% to the height) which makes it a little taller than 300px - so if you need exactly 300px, you could do something like padding:10px 3%; and change the height:280px; to add up to 300px again.
You can use the following CSS to achieve the desired result:
#page {
background: red;
overflow: auto;
}
#margin {
background: green;
height: 280px;
margin: 10px
}
The correct way to achieve this by standard is:
#margin {
background: green;
height: 280px;
margin: 10px;
width: auto;
display: block;
}
For LESS users only:
Using the nice solution of Vukašin Manojlović doesn't work out of the box because LESS executes + or - operations during the LESS compilation.
One solution is to escape LESS so that it doesn't execute the operation.
Disable LESS-CSS Overwriting calc()
#someMarginVariable = 15px;
margin: #someMarginVariable;
width: calc(~"100% - "#someMarginVariable*2);
width: -moz-calc(~"100% - "#someMarginVariable*2);
width: -webkit-calc(~"100% - "#someMarginVariable*2);
width: -o-calc(~"100% - "#someMarginVariable*2);
or can use a mixin like:
.fullWidthMinusMarginPaddingMixin(#marginSize,#paddingSize) {
#minusValue: (#marginSize+#paddingSize)*2;
padding: #paddingSize;
margin: #marginSize;
width: calc(~"100% - "#minusValue);
width: -moz-calc(~"100% - "#minusValue);
width: -webkit-calc(~"100% - "#minusValue);
width: -o-calc(~"100% - "#minusValue);
}
If possible, try to use padding with box-sizing instead, on #page element
#page {
padding: 10px;
box-sizing: border-box;
background: red;
float: left;
width: 100%;
height: 300px;
}
#margin {
background: green;
float: left;
width: 100%;
height: 100%;
}

CSS calc function does not center [duplicate]

This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 3 years ago.
I need to use the calc function for width but it doesn't divide distance around.
HTML
<div class="container-card">
<div class="container-holder"></div>
</div>
SCSS
.container-card {
background-color: grey;
height: 500px;
.container-holder {
background-color: gold;
width: calc(100% - 14px);
height: 300px;
}
}
Here is an example:
https://jsfiddle.net/fze3L0w8/
In other words: I need 14px distance from left and right in every width.
You can use margin:auto; for adding space from both side. And you need to set it 100% - 28px
.container-card {
background-color: grey;
height: 500px;
}
.container-holder {
background-color: gold;
width: calc(100% - 28px);
height: 300px;
margin: auto;
}
<div class="container-card">
<div class="container-holder">
</div>
</div>
Just set a margin of 14px, and you will no longer need to set the width property:
.container-card {
background-color: grey;
height: 500px;
.container-holder {
background-color: gold;
margin: 14px;
height: 300px;
}
}
Here's an updated fiddle.
this is the solution:
background-color: gold;
width: calc(100% - 28px);
height: 300px;
margin: auto;
you need margin from left o rright also
you either set it 100% - 28px to reduce width by 14px right and left and set margin: auto; to center the div
.container-card {
background-color: grey;
height: 500px;
.container-holder {
background-color: gold;
width: calc(100% - 28px);
margin: auto;
height: 300px;
}
}
or only set margin:0px 14px; and no need to set width it will take parent width - margin
.container-card {
background-color: grey;
height: 500px;
.container-holder {
background-color: gold;
margin: 0px 14px;
height: 300px;
}
}
More elegant solution is that -
.container-card {
background-color: grey;
height: 500px;
.container-holder {
background-color: gold;
width: calc(100% - 28px);
height: 300px;
margin: 0 auto;
}
}
Calc function will get 28px and to center an element inside another one. use margin: 0 auto;
Just center the container instead of this hard coded brittle approach with something like flex, then you can use whatever margin you want without it breaking.
.container-card {
display: flex;
justify-content: center;
background-color: grey;
height: 500px;
}
.container-holder {
background-color: gold;
margin: 0 14px;
width: 100%;
height: 300px;
}
<div class="container-card">
<div class="container-holder">
</div>
</div>
More elegant solution could be this
`
.container-card {
background-color: grey;
height: 500px;
.container-holder {
background-color: gold;
margin-left: 14px;
margin-right:14px;
height: 300px;
}
}
`

Two elements - Fixed and flexible width (100% - 170px)

At the top level of my website layout are 4 div tags.
The first one is a full width header section, with css:
#header {
margin-top: 0px;
height: 70px;
border: 4px double rgb(255,255,255);
border-radius: 20px;
background: rgb(88,150,183) no-repeat fixed left top;
padding: 0px;
}
At the bottom is a full width footer:
#footer {
clear: both;
margin: 0px;
color:#cdcdcd;
padding: 10px;
text-align: center;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
On the left is my main menu section:
#categories {
float:left;
width:150px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
All of those 3 elements work fine. They're in the right place and that doesn't change whatever screen resolution the user has on their monitor, or whether they view it on not maximum screen size.
My problem is with the main element of the page - where all the interesting stuff is. It's directly to the right of the menu div - or rather, it should be. My css is:
#main {
float:right;
min-height: 440px;
width: 80%;
margin-bottom: 20px;
padding:20px;
border: 4px double rgb(88,150,183);
border-radius: 20px;
}
width 80% works OK for most of my users, but for those with less resolution, the main element shifts below the menu, which is ghastly.
What I would ideally like is for the width set in the css #main to be something like (100% - 170px), thus leaving a nice margin between the menu and the main bit at all times and never pushing it below the menu. However, css standards don't fulfil that desire yet!
Could someone suggest how I amend my css to give me a nice clean page that's clean for all my users? Or do I need to go back to setting out my page using tables?
Using CSS3 flex
* { box-sizing: border-box; margin: 0; }
#parent{
display: flex;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
flex: 1; /* You... fill the remaining space */
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Using CSS3 calc
width: calc(100% - 170px);
Example:
* { box-sizing: border-box; margin: 0; }
#aside {
background: #1CEA6E;
width: 170px;
float: left;
padding: 24px;
}
#main {
background: #C0FFEE;
width: calc(100% - 170px);
float: left;
padding: 24px;
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using float: left; and overflow
* { box-sizing: border-box; margin: 0; }
#aside{
width: 170px; /* You, be fixed to 170 */
float: left; /* and floated to the left */
padding: 24px;
background: #1CEA6E;
}
#main {
background: #C0FFEE;
padding: 24px;
overflow: auto; /* don't collapse spaces */
/* or you could use a .clearfix class (Google for it) */
}
<div id="aside">Aside</div>
<div id="main">Main</div>
Using style display: table;
* { box-sizing: border-box; margin: 0; }
#parent{
display: table;
border-collapse: collapse;
width: 100%;
}
#parent > div {
display: table-cell;
}
#aside{
width: 170px; /* You, be fixed to 170 */
background: #1CEA6E;
padding: 24px;
}
#main{
background: #C0FFEE;
padding: 24px;
}
<div id="parent">
<div id="aside">Aside</div>
<div id="main">Main</div>
</div>
Is this what you are looking for? You don't need any css3
Dont need any css3
.wrapper {
width: 800px;
height: 800px;
background-color: blue;
}
.content {
width: auto;
height: 100%;
background-color: yellow;
}
.menu {
width: 170px;
height: 100%;
float: left;
background-color: red;
}
<div class="wrapper">
<div class="menu">Menu</div>
<div class="content">
Aside
</div>
</div>
You can use 'calc' function supported by all modern browsers and IE9+, or switch to flexbox (supported by IE11+)
See this pen: https://codepen.io/neutrico/pen/MyXmxa
width: calc(100% - 170px);
Keep in mind that all borders matter unless you set 'box-sizing' to 'border-box' (or just remove these borders and apply them on child elements).

HTML - keep inline-block elements from dropping down

I am trying to make this layout....
I have everything pretty much laid out but the different boxes are dropping down when I resize the browser window. I want them to stay where they are and scale.
I am trying to get it to work using display:inline-block.
<body>
<header>header</header>
<article>
<div class="col_left">
<div class="col_left_top margin_18bot">img</div>
<div class="col_left_bot">box1</div>
<div class="col_left_bot margin_18left_18bot">box2</div>
</div>
<div class="col_right margin_18left_18bot">box3</div>
</article>
html {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
*, *:before, *:after {
-webkit-box-sizing: inherit;
-moz-box-sizing: inherit;
box-sizing: inherit;
}
.margin_18left_18bot {
margin: 0px 0px 18px 18px;
}
.margin_18bot {
margin: 0px 0px 18px 0px;
}
body {
max-width: 1080px;
min-width: 850px;
margin: auto;
}
header {
background-color: brown;
height: 153px;
}
article {
padding: 18px;
}
.col_left {
float: left;
width: 66.3%;
max-width: 690px;
}
.col_left_top {
width: 100%;
height: 600px;
max-height: 600px;
max-width: 690px;
background-color: aqua;
}
.col_left_bot {
display: inline-block;
vertical-align: top;
width: 48.697912%;
background-color: aquamarine;
min-height: 300px;
max-width: 336px;
}
.col_right {
display: inline-block;
width: 32.1854%;
background-color: red;
height: 1070px;
max-width: 336px;
}
Here is a fiddle.... *fixed fiddle wasnt showing properly before
The problem you're having is because you are setting their width in percent yet you define those margins in px which will eventually won't have a total of width of 100% when you scale down.
To fix this, you have to do the following:
Change this :
.col_left {
float: left;
width: 66.3%;
max-width: 690px;
}
.col_right {
display: inline-block;
width: 32.1854%;
background-color: red;
height: 1070px;
max-width: 336px;
}
To:
.col_left {
float: left;
width: calc(67% - 9px);
max-width: 690px;
}
.col_right {
display: inline-block;
width: calc(33% - 9px);
background-color: red;
height: 1070px;
max-width: 336px;
}
The reason I use :
calc(33% - 9px); and width: calc(67% - 9px);
is because the margin-left is 18px so we have to divide it by two.
Also when doing an inline-block take note that if you format your structure like below, you will have a space between them because of the newline:
<div class="col_left_bot">box1</div>
<div class="col_left_bot margin_18left_18bot">box2</div>
To fix that, get rid of the newline. It will get ugly, but it will do the trick
<div class="col_left_bot">box1</div><div class="col_left_bot margin_18left_18bot">box2</div>
Update to give further explanation regarding the issue on percentage with a margin set as px
Let's say you have 100px container and 49% width on 2 elements with 2px margin.
Let's compute :
100 * .49 x 2 + 2 = 100 // correct for this scale
But what if the container scales down to 50px?
Computation :
50 * .49 x 2 + 2 = 51 // it doesn't total to 50px
Which is why your other elements go down when the container scales down.
Fiddle

why div within div doesn't position well within

<div id="progressbarr"> still did not wrap well within its ancestor div when I use margin-top :50%, and expecting it will be placed at the middle horizontally.
HTML
box
css
#col3wrap{
height: 50px;
background: #DDD;
}
.profilepic {
width: 50px;
height: 50px;
background: red;
float: left;
margin: 0 10px 0 0;
}
#progressbarr {
float: left;
width: 300px;
height: 20px;
background: #eee;
margin: 50% 0 0 0;
}
#progressbarr > div {
background-color: green;
width: 40%;
height: 20px;
}
http://jsfiddle.net/Xdwhk/
Your margin value is calculated using the parent elements width. Why? Good question. I don't know but here is some discussion on the topic:
Why are margin/padding percentages in CSS always calculated against width?
Here is your demo with the #col3wrap with a width set:
http://jsfiddle.net/Xdwhk/1/
Try adjusting the width to see how the margin-top value changes with it.
So you're going to have to find a different method, other than using margin-top, to vertically centre your progress bar.
Here is one way, using position: relative and calc to set the top value:
CSS
#col3wrap{
height: 50px;
background: #DDD;
position: relative;
}
.profilepic {
width: 50px;
height: 50px;
background: red;
float: left;
margin: 0 10px 0 0;
}
#progressbarr {
float: left;
width: 300px;
height: 20px;
background: #eee;
position: relative;
top: 30%;
top: -webkit-calc(50% - 10px);
top: calc(50% - 10px);
}
#progressbarr > div {
background-color: green;
width: 40%;
height: 20px;
}
Demo
Note calc is not supported by all browsers so we must have a fall back.