With space when I add a picture [duplicate] - css

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

make <sections> stay in place when shrinking window

I have two boxes inside my <header>-tag, which has a total width of 100%. When i align them up they fit perfect, but when i'm shrinking the window the right box jumps down beneath the left one. Here's the code:
header {
width: 100%;
height: 90px;
}
#head {
float: left;
background-image: url('img/header.jpg');
height: 120px;
margin-right: 10px;
width: 65%;
}
#userinfo {
float: left;
height: 100px;
width: 33.2%;
background-color: #202020;
padding: 10px;
position: relative;
}
<header>
<section id="head">
</section>
<section id="userinfo">
test
</section>
</header>
Any quick fixes? Imagining this happens with the rest of my design as I'm moving forward. Thanks in advance.
The problem here is you are using some values for padding and margin on your elements and those by default are added to the actual size of the element, since both elements are 98.2% of the container +20 of the values at some point they don't fit into the 100% and will break.
To solve it you can use box-sizing property which will make the padding and border values be inside the total declared size, and since that doesn't work with margin you will need an extra container to use padding and create the separation:
header {
width: 100%;
height: 90px;
}
#head {
float: left;
box-sizing:border-box;
padding-right: 10px;
width: 65%;
}
#head > div {
background-image: url('http://placehold.it/500');
height: 120px;
}
#userinfo {
box-sizing:border-box;
float: left;
height: 100px;
width: 35%;
background-color: #202020;
padding: 10px;
position: relative;
}
<header>
<section id="head">
<div></div>
</section>
<section id="userinfo">
test
</section>
</header>
Thanks DaniP, that solved it. But what about the content and sidebars part then?:
#container {
margin: 30px auto;
width: 70%;
min-height: 400px;
}
#leftmenu {
float: left;
box-sizing: border-box;
width: 20%;
min-height: 600px;
background-color: #202020;
}
#content {
float: left;
box-sizing: border-box;
width: 60%;
background-color: #202020;
min-height: 600px;
}
#content > div {
background-color: #202020;
min-height: 600px;
}
#rightmenu {
float: left;
box-sizing: border-box;
width: 20%;
min-height: 600px;
background-color: #202020;
}

CSS Float with blocks

I have a problem, how i can fix blocks?
Block Sidebar
.sidebar {
display: block;
width: 250px;
float: left;
height: 100%;
background-color: #2b2b2b;
margin-right: 15px;
}
Block Content
.content-panel {
margin-right: 15px;
margin-top: 15px;
}
Problem:
image
How i can fix content block? I have to make sure that the unit does not take into account the width of the sidebar, while its width is 100%, and sidebar 250px
use
.content-panel {
width: calc(100% - 280px);
margin-left: 265px;
margin-right: 15px;
margin-top: 15px;
}
That width subtracts the width of the sidebar and all the margins from 100%, the margin-left moves it to the right.
You can also use just margin-left to get content block width 100% - 250px:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.sidebar {
background: lightblue;
float: left;
height: 100vh;
width: 250px;
}
.content {
margin-left: 250px;
padding: 16px;
height: 100vh;
background: gold;
}
<div class="sidebar"></div>
<div class="content">Content</div>

Alternative to calc()

I have a container with auto-width and some margin left and right:
.inner {
margin-left: 20px;
margin-right: 20px;
width: auto;
}
And in this container different elements. Some elements should be 100% of the page (without the margin) and for that I use calc():
.fullwidthelement {
left: -20px;
width: calc(100% + 40px);
width: -webkit-calc(100% + 40px);
}
But it looks like Safari (5.1.7 on Windows 8) can't handle that. In the Web Inspector I see e yellow exclamation mark and it does not take the rule for the width:
So, is there a work around for this or can I get Safari to work fine with calc somehow?
First it is important to note that you should always specify the properties with vendor prefixes before the unprefixed properties :
.fullwidthelement {
left: -20px;
width: -webkit-calc(100% + 40px);
width: calc(100% + 40px);
}
Second, if you check canIuse (click on "show all") you will see that safari 5.1 doesn't support the calc() function.
Would negative margins work for you?
#container
{
border: 1px solid;
width: 400px;
}
.inner
{
background: red;
height: 200px;
margin-left: 20px;
margin-right: 20px;
width: auto;
}
.fullwidthelement
{
background: green;
height: 100px;
margin-left: -20px;
margin-right: -20px;
}
<div id="container">
<div class="inner">
<p>Blabla</p>
<div class="fullwidthelement"></div>
</div>
</div>
use padding for that
.outer-div {
width: 200px;
height: 200px;
background: #000;
margin: 0 auto;
position: relative;
}
.inner-div {
width: 100%;
height: 150px;
padding-right: 100px; /*extra width you want to add*/
background: #f00;
position: absolute;
top: 20px;
}
<div class="outer-div">
<div class="inner-div"></div>
</div>
Jsfiddle http://jsfiddle.net/0xdscqLo/2/

Creating a fixed size grid with a variable amount of cells

I'm trying to create a grid up to 64x64 that is contained in a 960x960 pixel container. Unfortunately, I'm not sure exactly how to accomplish that. I tried doing it percentage based, but then my cells completely disappeared. Can anyone give me any insight?
.square
{
display: table-cell;
vertical-align: middle;
text-align: center;
height: 1.67%; // I have no idea what I'm doing here
max-height: 6.25%;
width: 1.67%;
max-width: 6.25%;
background-color: white;
}
#container
{
width: 960px;
height: 960px;
display: inline-block;
}
There is a few ways you could do this depending on exactly what you're looking for, but here is one solution: Fiddle
#container {
width: 100%;
height: 100%;
max-height: 480px;
max-width: 480px;
background: red;
}
.square {
height: 100%;
width: 100%;
max-width: 64px;
max-height: 64px;
display: inline-block;
/* or use float: left; */
background: yellow;
}
And here is a completely responsive version: Fiddle2
#container {
width: 100%;
height: 100%;
max-height: 480px;
max-width: 480px;
background: red;
}
.square {
padding-bottom: 19%;
width: 19%;
display: inline-block;
/* or use float: left; */
background: yellow;
}
The problem is that in the original code the size of the .square is wrong. If each row or column measures 960px and you want to have a 64 x 64 grid, each cell should measure 1 / 64 of the total, that is 960px / 64 = 15px. In % it would be 100% / 64 = 1.5625%.
This way you will have a 64x64 grid whatever the size of the #container is, although multiples of 64 will be the best option because all the cells will have the same size.
const cells = [];
for (let i = 0; i < 4096; ++i) {
cells.push('<div class="square"></div>');
}
document.getElementById('container').innerHTML = cells.join('');
body {
margin: 0;
background: #000;
}
#container {
position:relative;
width: 100vmin;
height: 100vmin;
background: #FFF;
margin: 0 auto;
cursor: none;
}
.square {
position:relative;
box-sizing: border-box;
width: 1.5625%;
height: 1.5625%;
float: left;
}
.square:hover {
background: #F00;
z-index: 1;
box-shadow: 0 0 32px 8px #F00;
}
<div id="container"></div>
Alternatively, you could consider using CSS Grid.

Div wont go into wrapper?

I'm having a bit of trouble with a div, my website has one wrapper sized height: 100%; this wrapper contains various divs like a header, slider and a content div. The only problem is the content div gets pushed out of the wrapper div for some mysterious reason.
html {
overflow-y: scroll;
height: 100%;
position: relative;
}
a {
outline: none;
}
img {
width: 100%;
border: none;
-moz-border-radius: 20px;
border-radius: 20px;
}
body {
width: 100%;
height: 100%;
background-color: yellow;
margin: 0px 0px 0px 0px;
}
.wrapper {
width: 87%;
height: 100%;
background-color: red;
margin: 0 auto;
}
.header {
width: 100%;
height: 150px;
background-color: green;
float: left;
overflow: hidden;
}
.logo {
width: 7%;
height: 114px;
margin: 18px 0% 18px 3%;
float: left;
background-image: url("..//img/logo.png");
background-size: 100%;
background-repeat: no-repeat;
}
.slogan {
width: 30%;
height: 100px;
background: orange;
margin: 25px 13% 25px 13%;
float: left;
}
.nav {
width: 31%;
height: 150px;
background-color: purple;
float: left;
margin: 0% 3% 0% 0%;
}
.search {
width: 100%;
height: 50%;
background: blue;
float: left;
}
.menu {
width: 100%;
height: 50%;
float: left;
background: grey;
}
.slider-container {
width: 100%;
height: 100%;
background-color: white;
}
.main-content {
width: 100%;
height: 100px;
background-color: pink;
float: left;
}
.column {
width: 31%;
height: auto;
background-color: orange;
float: left
}
/* SLIDER */
.caption {
width: 500px;
background-image: url("..//img/caption-bg.png");
background-size: 100%;
position: absolute;
z-index: 99;
overflow: hidden;
margin-top: 7%;
margin-left: 5%;
-moz-border-radius: 20px;
border-radius: 20px;
}
.caption-text {
max-width: 460px;
overflow: hidden;
margin: 20px;
}
.wrapper .slider-container #slideshow {
float: left;
position: relative;
width: 100%;
}
.wrapper .slider-container #slideshow > div {
position: absolute;
}
You can see a live demo at http://k2stuc.nl/test/
I don't understand your question 100%. But I saw an issue, the navigation behind the slideshow is because your slides, .wrapper .slider-container #slideshow > div has set to position:absolute.
Try setting .slider-container height to a fixed height. Otherwise slides will be above the content.
you are floating things that should not be floated
i.e .header, .main-content,#slideshow - none of these need floats - body should not have height:100%;
give .slide-container a fixed height in pixels not a percentage.. doing the above will fix your problem
Setting height: 100% on the <body> sets its height to 100% of the viewport.
Now, the viewport is as high as the browser window's inner-height; and that changes when you resize the browser itself.
Any direct child of the <body>, set to height: 100% then inherits the viewport's height.
That's part of how a lot of the 'parallax' websites do their thing.
I think the problem comes from the fact the slider div has height:100%

Resources