Here is the html & css problem I'm trying to solve:
HTML & CSS:
#fixedLeftMenu {
display: inline-block;
height: 50px;
background-color: yellow;
width: 25px;
position: fixed;
}
#container {
display: inline-block;
background-color: orange;
width: calc(100% - 25px);
margin-left: 25px;
}
#redFixedDiv {
height: 100px;
background-color: red;
width: 25%;
position: fixed;
}
#blueDiv {
float: right;
height: 1000px;
background-color: blue;
width: calc(100% - 25%);
}
<div id="fixedLeftMenu"></div>
<div id="container">
<div id="redFixedDiv">
</div>
<div id="blueDiv"></div>
</div>
The yellow div is a fixed div with a fixed width.
The red div is a fixed div but % width.
The blue is % width;
You can see that the red and blue div's DO NOT match 100% width (the orange div container) as excepted.
The red div is being over the blue one.
If I remove the fixed position of the red, everything will be OK, but I do want it to be fixed. It maybe complex html, but I really trying to solve it. Is it possible? What I'm missing here that causes that html/css behavior?
Here is the fix for your problem.
#fixedLeftMenu {
display:inline-block;height: 50px;background-color:yellow;
width: 25px;
position: fixed;
}
#container {
display:inline-block; background-color:orange;
width: 100%;
margin-left: 25px;
}
#redFixedDiv {
height: 100px; background-color: red;
width: 25%;
position: fixed;
}
#blueDiv {
float:right;height: 1000px;background-color: blue;
width: 75%;
}
Its not the problem of position:fixed. Just avoid calc function. That too like calc(100% - 25px). I'm not sure how browser is calculating, but your code should not depend on it, I feel. Developer/Designer should design all the components width/height/position manually, so everything works out well.
Since an element with a fixed position doesn't look at its parents width when it's given a percentage width, you will need to adjust the width in the calc, so that it has accounted for the 25px margin. What I've done to the code below is first get the pagewidth - 25px, then divide it by 4 to get 25%
#redFixedDiv{
height: 100px; background-color: red;
width: calc((100% - 25px) / 4);
position: fixed;
}
Related
I have a CSS issue. I think this is simple and usual but I know why I can not achieve this.
I am trying to get the div height to take the remaining height of another div.
I have 3 divs
<div id='div1'>
<div id='div2'>
</div>
<div id='div3'>
</div>
</div>
CSS:
#div1{position:fixed;top:0px;right:0px;width:250px;height:100%;}
#div2{width:100%;height:250px;float:left;}
#div3{} // now I want this div to take the remaining height with only CSS and I don't want to use flex.
I think remaining width takes a div by this code.
CSS
{width:auto;overflow:hidden;}
This takes all the remaining width of div but the same code doesn't work with height.
Please suggest me something with this.
height: calc(100% - 250px);
You could use this for div3 in your css. Hope this helps (:
You could do this:
#div1 {
position: fixed;
background: green;
top: 0px;
right: 0px;
width: 250px;
height: 100%;
}
#div2 {
position: absolute;
background: red;
width: 100%;
height: 250px;
}
#div3 {
position: absolute;
top: 250px;
background: blue;
width: 100%;
height: 1000px;
overflow: hidden;
}
DIV 3 gets a very high height value, but is cut off automatically by overflow: hidden
Here's a codepen: http://codepen.io/anon/pen/XXjBJR
You should try the CSS as seen below for #div3 (I have improved answer because now #div3 is not in #div2 but actually #div2 and #div3 is in #div1)
#div3 {
width:100%;
height:100vh;
}
I'm having some strange behavior of the percentages.
I have layout which is 1366 pixels wide and I have one div which should be fluid.
Its 200px wide, which means it should be 14.64% wide.
When the layout is tested in 1366 pixels the div looks fine and there are no problems, but when I expand to 1920 the div is not wide enough.
Here is some samples of the code:
HTML:
<header>
<div class="top-bar">
<div class="fill"></div>
<div class="container">
<nav>
</nav>
</div>
</div>
<div class="bottom-bar">
</div>
</header>
And CSS
.container{
width: 1004px;
margin: 0px auto 0px auto;
}
header{
width: 100%;
height: 95px;
}
header div.top-bar{
background: #ffffff;
width: 100%;
height: 50px;
}
header div.fill{
background: #000000;
width: 14.64%;
height: 50px;
float: left;
}
nav{
height: 50px;
float: left;
}
header.main div.bottom-bar{
background: url("header-bottom.png") repeat-x;
width: 100%;
height: 45px;
}
I've coded liquid designs before, but never had problem like this, maybe my math is wrong or the problem is that everything else is hardcoded in pixels and this is liquid?
I'd guess the problem is because container has a pixel size while fill is in percentage. If all you want to achieve with the fill is to put a background color around container, you can do something like this (and remove the fill class css)
.container{
width: 1004px;
margin: 0px auto 0px auto;
background: #ffffff;
}
header div.top-bar{
width: 100%;
height: 50px;
background: #000000;
}
Now if you want to color only left side, and want to fit your 'fill' div nicely, then both container and fill have be either in percentage or in pixels (won't work properly in different screen sizes). There are different workarounds to make your fill work e.g. the following
header div.fill {
background: #000000;
width: 50%; /*make it wide enough*/
height: 50px;
float: left;
z-index: -1; /*put it behind container*/
position: relative;
}
header div.top-bar{
background: #ffffff;
width: 100%;
height: 50px;
z-index: -2; /*put it behind all*/
position: relative;
}
for managment layout use grid system css famework for example
Bootstrap
http://getbootstrap.com/
or
960 grid system
http://960.gs/
I'm sorry if the question look stupid,
I found many question on the subject, but none who help me.
I have 2 div in my body,div1 which SHOULD be 100% height (not working either) and the second with a content which will change depending on situation, I want the second div to recover 30 % of the page, idc if there is something in or not.
I try height:30%, I can put 30% or 100% , it will take the height of the content.
(ps : I don't want to put a static height:px; or position:absolute;)
my CSS :
body{
font-family: "Gill Sans MT";
}
#div1{
background-color: yellow;
height: 100%;
}
#div2{
top: 5%;
height: 30%;
width: 50%;
margin-left: 20%;
background-image: url(../image/vent.png);
}
Thanks
HTML
<div id="div1">ss</div>
<div id="div2">aa</div>
CSS
html, body {
margin: 0;
padding: 0;
height: 100%;
}
#div1 {
background: red;
width: 50%;
height: 100%;
float: left;
}
#div2 {
background: blue;
width: 50%;
float: left;
min-height: 30%;
}
http://jsfiddle.net/A86Ta/1/
In #div2 min-height is 30%, so if content takes more space then that, height will increase. If you don't want that, just put height instead of min-height.
For example this div is displayed:
<div class="circle"></div
.circle {
width: 300px;
height: 300px;
background: red;
border-radius: 50%;
}
but when width and height are in % it collapses:
.circle {
width: 30%;
height: 30%;
background: red;
border-radius: 50%;
}
Is there a way to get it displayed?
This is because the div has no height. width: 30%; will always make the div 30% width of the parent (<body>, in this case), which is what you want. However, height behaves a little differently.
You need to specify a 100% height for body and html like so:
html,
body {
height: 100%;
}
Working Fiddle
You can read why height: 100%; behaves like that here
You need to declare a hard width/height somewhere, in this example I put a hard width/height on a container div:
http://jsfiddle.net/exrNm/1/
<div class="con">
<div class="circle"></div
</div>
.con{
width:300px;
height:300px
}
.circle {
width: 30%;
height: 30%;
background: red;
border-radius: 50%;
}
You could easily set a hard width somewhere up the parent chain. The % needs a hard value to calculate against.
SO,
I've created a four-column fluid-width layout for a site, and I'm working on placing a fluid square DIV within one of my columns. There are a few techniques I've found to achieve this - namely, setting padding-bottom to the same percentage as the width - but none of these seem to work when the DIV contains content.
Is there a way to maintain a 1:1 (square) ratio on a fluid DIV when that DIV contains content?
Here's my HTML:
<div id="leftmostcolumn">
<div id="logo"></div>
</div>
<div id="leftcolumn"></div>
<div id="rightcolumn"></div>
<div id="rightmostcolumn"></div>
And my CSS:
body {
width: 100%;
height: 100%;
background-color: red;
}
#leftmostcolumn {
position: absolute;
top: 0;
left: 0;
width: 25%;
height: 100%;
background-color: blue;
}
#leftcolumn {
position: absolute;
top: 0;
left: 25%;
width: 25%;
height: 100%;
background-color: green;
}
#rightcolumn {
position: absolute;
top: 0;
left: 50%;
width: 25%;
height: 100%;
background-color: yellow;
}
#rightmostcolumn {
position: absolute;
top: 0;
left: 75%;
width: 25%;
height: 100%;
background-color: gray;
}
#logo {
width:100%;
padding-bottom:100%;
background-color: #aa2d2d;
color: white;
}
And here's a JsFiddle.
The DIV "logo" is the one I'm trying to maintain as a square. Right now, I've used the padding-bottom approach but that doesn't do the trick when there's content in the DIV. Any input is greatly appreciated!
Marca
EDIT:
Getting there...I'm adapting a script I found to find the width of the DIV and then apply that value to the height to keep it a square. However, as it stands now the script doesn't constantly resize the DIV, and it won't allow it to shrink below a certain size. Any thoughts on how to correct either of these issues?
HTML:
<div id="box"></div>
CSS:
#box { width: 75%; height: 50px; background-color: black; }
JQUERY:
$("#box").css("height", function() {
return $(this).width();
});
JsFiddle is here.
This is something I've actually been messing around with for a while, and have come up with a quasi (but not entirely) hacky, CSS-only solution that seems to work on most browsers in the past decade. The trick is to use images, and positioning in a tricky fashion. Consider the following (simplification) of your code.
Markup:
<div class="sqr_box">
your content goes here!
</div>
CSS:
.sqr_box
{
width: 50%; /* or 100px, or 20em, or whatever you want */
border: solid 2px pink;
background-color: grey;
color: white;
}
Now, we can't set the height in terms of percent, so we won't; instead, first we'll go into Photoshop, and make an image that is 2x2 px, transparent, or background-colored. Next we'll add the following to your markup:
<div class="sqr_box">
<img src="images/sizers/2x2.png" class="sizer">
<div class="content">your content goes here!</div>
</div>
and THIS to your CSS:
.sqr_box
{
width: 50%; /* or 100px, or 20em, or whatever you want */
position: relative; /* static positioning is less than ideal for this scenario */
}
.sqr_box > img.sizer
{
display: block; /* images default to an inline-block like thing */
width: 100%;
height: auto; /* CLUTCH!!! this ensures that the image's height changes to maintain proportions with it's width */
visibility: hidden;
}
.sqr_box > .content
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%; /* Our parent element now has a dynamically assigned height, this will work */
border: solid 2px pink;
background-color: grey;
color: white;
}
Best of all, this will work for any sized ratio of box you'd want! Just change the proportions of the image!
Hope this is all still relevant to you, 3 months later.
-Sandy
Put all four columns in one div. set that div to 100% width and set the font size to 100em
Have each of your four columns have a width of 25em instead of 25%
Have your logo width and height set to 25em each