Setting div width to 100% minus certain amount of px - html

Have been searching the net for the past hours to find a solution to this, but couldn't find it, so here I am.
I need the width of the div to be 100% minus the width of the left div.
So that the div to the left of it stays the same width (390px) but the other div adjusts its size depending on the resolution . I have found the solution where it has a fixed width div on both sides, but just cant figure this out.

Simple solution:
<div id="content">
<div class="padder">
</div><!-- .padder -->
</div>
<div id="sidebar">
<div class="padder">
</div><!-- .padder -->
</div>
CSS:
div#content {
float: right;
width: 100%;
}
div#content .padder {
margin-left: 330px;
padding: 0 30px 0 0;
}
div#sidebar {
float: left;
width: 300px;
margin-top: -30px;
padding-left: 30px;
margin-right: -330px;
}
This will allow you to have a fixed sidebar width and a full width content area. I have used it many times and it works like a charm.

This type of calculation isn't currently supported in CSS (certainly not in Chromium 12/Ubuntu 11.04), but there is a calc() function defined in CSS 3, which would allow for this kind of behaviour, using simple mathematical functions:
section {
float: left;
margin: 1em; border: solid 1px;
width: calc(100%/3 - 2*1em - 2*1px);
}
p {
margin: calc(1rem - 2px) calc(1rem - 1px);
border: solid transparent; border-width: 2px 1px;
}
p:hover { border-color: yellow; }
(Above example taken directly from the W3.org.)
My own (in-exhaustive) tests show:
+----------------+-------------------+
| Browser | Ubuntu 11.04 |
+----------------+-------------------+
| Chromium 12 | Fails |
| Firefox 5 | Fails |
| Opera 11.10 | Fails |
+----------------+-------------------+
The above results were obtained using the named browsers and a css calc() demo, the code of which is below:
HTML:
<div id="box">This is the box.</div>
CSS:
#box {
width: calc(100% - 20%);
background-color: #f90;
font-weight: bold;
color: #fff;
line-height: 2em;
text-align: center;
margin: auto;
}
(If anyone would like to run the above test in the browsers on their platform, and supply the results, or edit them in to this answer, that would be much appreciated.)
As pointed out, by clairesuzy, in comments:
[Take] a look at caniuse it does work in Firefox if you use width: -moz-calc() but that's about it ;)
And, indeed, in Firefox 5 (Ubuntu 11.04) it does work (the other vendor prefixes don't appear to do anything for Opera or Webkit, though; sadly): Revised vendor-prefixed demo.
Reference:
CSS3 Values and Units.

<div id="left">...</div>
<div id="content">...<br> more content</div>
#left {float: left; width: 390px; background: #f76922;}
#content {overflow: hidden; background: #eee;}
float the left div and make a new block formatting context out of the right div (overflow: hidden is one way to do that), that way it will take the remaining space
Example Fiddle

There really isn't a way of doing this with straight up CSS right now in browsers other FireFox (see the MDN docs). You could use javascript to do the same, but I'd suggest rethinking your layout.
EDIT: actually IE 9 can handle it as well, see MSDN docs. Yay for IE?

Maybe this is not directly related to the question but I had a similar problem to arrange items in a list. The fixed-width element is to the right of each item. I've managed to solve this with the following approach. The idea is to balance a positive padding-left with a negative margin-left, while the width is set to 100%:
.item {
max-height: 40px;
}
.item-title {
display: inline-block;
height: 40px;
width: 100%;
margin-left: -70px;
padding-left: 65px;
text-align: left;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* This one should be fixed-width. */
.item-params {
width: 60px;
height: 40px;
float: right;
}

Related

CSS Styling a Kanban - resize & re-position DIVs to fit content

I have been developing a Kanban board for a generic system we maintain in work. I have the core functionality all working but am getting a little stuck on the styling. The following screenshot shows what I currently have:
There is a main DIV that contains all the 'columns/containers' and this is styled:
.kanban-board {
height: calc(100% - 60px);
width: calc(100% + 20px);
display: normal;
padding-bottom: 0px;
overflow-y: scroll;
}
This div contains a series of 'containers' - each grey column which is another DIV styled:
.kanban-container {
float: left;
clear: right;
margin: 20px 10px 0px 10px;
border-radius: 5px;
background-color: #eaebed;
width: 300px;
padding-bottom: 0px;
padding-left: 6px;
min-height: calc(50% - 20px);
display: table;
}
and to note - there could be any number of these 'containers' so they currently wrap to the next line as per the above screen shot. Each 'container' DIV contains an UnOrdered sortable List (UL) and this is styled:
.kanban-sortable {
list-style-type: none;
margin: 0;
padding: 2px;
min-height: 30px;
border-radius: 5px;
height: 100%;
width: 285px;
display: table-row;
}
If you look back to the screenshot, the last 'container' of the first row extends beyond the (min) height of the other containers... Seems okay until the longest is NOT the last in the row:
Now it just looks rubbish... And I have not a clue on how to get this sorted. I have checked some other Kanban examples and my other option is to just flow off page to the right with an X-Scroll bar - but my current spec requires me to fix this issue.
I hope I have supplied enough info for a quick fix.
BTW - im happy to address any other rookie mistakes I may have made from my example.
UPDATE
I have had tried the advice given to use display: inline-block;(in the .kanban-container) instead of:
float: left;
display: table;
And its difficult to see if it has fixed my issue as the auto-resize of the 'containers' height is no longer working
Since you are using floats, you'll need a container for each row that clears the previous one. Another way would be to remove the float, and set the .kanban-container to display: inline-block. If you need table properties in that, you'll have to add an inner container, but frankly I'm a little confused by all the styles used here and the !importants that go with them.
<div class="kanban-row">
<div class="kanban-container">
<div class="kanban-container">
...
</div>
<div class="kanban-row">
...
css:
.kanban-row {
clear: left;
overflow: hidden; // may not be necessary
}
.kanban-container {
float: left;
}
or (without rows):
.kanban-container {
display: inline-block;
}

HTML label center aligned getting cropped on both sides

I'm seeing a bug that is intermittently reproducible only on iPhone Safari (not on any other mobile or desktop browser not even macbook safari). The code looks like:
<div style="overflow: auto; height: calc(100% - 55px); background-color: cyan;">
<ul ...><li..></li> <!-- some ul containing li omitted for brevity-->
<label class="certainClassname" style="font-size: small; font-style: italic; width: 100%; text-align: center; background-color: yellow">
{{"msg" | translate}}
</label>
</div>
In other style sheet I have:
.certainClassname {
cursor: pointer;
word-break: break-all;
}
msg = "PKtest!" for my testing though it usually has another value.
The problem is that this label is getting cropped at the edges when the bug is reproduced as below:
This bug is only seen on Safari iPhone and none of the other browsers, and seems to only happen the first time that page is loaded, and not later. It could be related to zooming because even the tiniest pinch-to-zoom on iPhone fixes it and the label text (and yellow) expands fully.
Could someone please help me diagnosing this in CSS above? I've tried to fix using each in css of label but none have worked:
display: inline;
display: inline-block;
display: block;
padding: 2px 10px 2px 10px;
margin-left: auto;
margin-right: auto;
z-index: 999;
clear: both;
(all these above tested but none fixes this issue seen intermittently and only on iPhone Safari).
The reason the label is getting cropped might be because of style="overflow: auto; height: calc(100% - 55px)" for div.
div is the parent of label. Therefore label is inheriting its style from its parent. You may consider removing height: calc(100% - 55px) to see if it works
It's difficult to get the idea without reviewing working code. But I tried to run your code, regenerate the bug and here is the possible solution. Let me know, is it something you are looking for?
.certainClassname {
cursor: pointer;
word-break: break-all;
font-size: small;
font-style: italic;
width: 100%;
text-align: center;
background-color: yellow;
display: inline-block;
}
ul {
padding: 0;
}
<div style="overflow: auto; height: calc(100% - 55px); background-color: cyan;">
<ul ...><li..></li> <!-- some ul containing li omitted for brevity-->
<label class="certainClassname">
{{"msg" | translate}}
</label>
</div>
I think you have to set you label around a div with width: 100% a background-color: yellow

How percentage works in css

I'm a newbie in HTML and CSS. Today I want to make a checkout form like that:
I want height of checkout-option-header equal 50% checkout-option's, but It always bigger than I want. Like that:
As I understand the percentage will determine size of children by size of parents.
But why I can't use it right?
CSS file:
.checkout-container {
margin-left: 10%;
margin-right: 10%;
background: black;
overflow: hidden;
}
.checkout-container .checkout-option {
margin: 5%;
background: red;
min-height: 100px;
}
.review-container {
margin: 5%;
background: yellow;
min-height: 100px;
}
.information-container {
margin: 5%;
background: blue;
min-height: 100px;
}
.checkout-container .checkout-option .checkout-option-header {
background: yellow;
min-height: 50%;
}
.checkout-option-container {
background: green;
min-height: 50px;
}
HTML file:
<div class="checkout-container">
<div class="checkout-option">
<div class="checkout-option-header"></div>
<div class="checkout-option-container"></div>
</div>
<div class="review-container"></div>
<div class="information-container"></div>
</div>
I'm not sure if I actually understand what you are asking but your code seems to be working.
http://codepen.io/TheStonedTurtle/pen/NbNNxw
I copied your code added the below CSS.
.checkout-option {
border: 5px solid white;
}
If you're whole question is trying to understand how percentages work they are dependent on the parent element. I.E. if you have an element with 500px height and you give a child 50% height it will have 250px as that is 50% of 500. See these for more info:
https://developer.mozilla.org/en-US/docs/Web/CSS/percentage
You could also use flexbox to accomplish this with less CSS but it is a relatively new feature so it is not fully supported in I.E. yet.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Flexbox browser support: http://caniuse.com/#feat=flexbox
I copied and pasted your code but I didn't see such problem. The two boxes inside the .checkout-option are exactly 50% in height. Instead of min-height, use height because your .checkout-option is blocking the checkout-option-header.

Div height not adapting to parent

Still developing my html5/css3 mobile site, I have trouble adjusting the height of a div to its parent.
http://jsfiddle.net/1eg2cwLs/
The fiddle doesn't exactly look like this because I'm using webfonts (saved offline though as I'm not going to have internet connection on the target system). But the problem remains the same.
You might be seeing what the problem is right from the spot, if not: I would like the green and red bar (.itemclass) always have the same size as the content of its parent (.item).
Depending on font, its size (still playing around with it) and the overall height of each item, I have to precisely adjust the negative margin. Otherwise it looks like in the screenshot. The negative margin I just mentioned is in the CSS-class .itemclass: (marked with an arrow also in the fiddle)...
.itemclass {
height: 100px;
width: 50px;
background-color: #27ae60;
vertical-align: middle;
text-align: center;
color: white;
font-size: 2em;
margin-top: -27px; /* <=== */
display: inline-block;
}
This cannot be the solution. I tried a lot of stuff and I only got it "working" the way I mentioned.
Any better idea how to make it look clean without a hack?
As well, tips for other improvements regarding my html/css are well appreciated.
Sorry for appending the entire code into the fiddle. I don't know whether it was representative if I was going to remove stuff.
Best regards
I'd probably go this route:
.item {
position: relative;
...
}
.itemclass {
position: absolute;
top: 0;
bottom: 0;
...
}
.itemcontent {
margin-left: 50px;
...
}
Demo
Really big font demo
Consider a reasonable min-width for the body to prevent .tagline from overlapping, etc.
You can set .item's margin-top to 0, and instead adjust the margin-top of .vcenter:before. This way you're just adjusting the text and not the div.
Or you could drop the static height and width of .itemclass altogether. Now the .itemclass will scale.
http://jsfiddle.net/1eg2cwLs/5/
.item {
width: 100%;
height: auto;
background-color: #eeeeee;
border-bottom: 1px solid #cccccc;
overflow: hidden;
}
.itemclass {
height: 100%;
width: auto;
background-color: #27ae60;
vertical-align: middle;
text-align: center;
color: white;
font-size: 2em;
margin-top: 0;
display: inline-block;
}
As a fallback, you can set .item to not show overflow, and then adjust the line-height of :
.item {overflow:hidden}
overflow: hidden; is your best friend in this case! It hides any overflow content from view outside of .item
Add it into .item {} declaration.
http://jsfiddle.net/1eg2cwLs/1/

Filling left side of a float: right element in a full width design

I am working on a new design in HTML5/CSS3 and it has been a while since I last worked with html more than for writing content, not layout.
The thing I am trying to achieve is a "full-width" or rather "80% width" design so that it fits the browser size down to 800px. (min-width).
A part of the content will contain a navigation block placed to the right (fixed size, say 300px), I have achieved this by using float although I have learned to hate float because of the "clearfix" issue, bur for the lack of better.
To the left of it I wanted the content to take up space to the right floating element.
However if that content grows beyond the width then it pushes the right floated element down.
So something like:
-----------------------------------------------------------
| Header |
| <header> |
|-------------------------------------------|
<-10%->| <- Fill -> |<- 300px ->|<-10%->
| | |
| <section> | <aside> |
-----------------------------------------------------------
Section (id=articles) is set to:
margin: 0 0 0 30px;
display: inline-block;
float: left;
Aside (id=sidebar) is set to:
margin: 0 30px 10px 30px;
width: 290px;
border:1px solid #6B6B6B;
border-radius: 6px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
box-shadow: 0px 0px 2px 1px #6B6B6B inset;
display: inline-block;
float: right;
Ways of fixing it:
display: table and table-cell, works but it adds extra tags I would like to avoid, also not sure that works everywhere?
flexbox? well so I thought but having issues with it, but it is not widely supported anyways.
Let all have fixed width.
Let all have dynamic width. (ohh boy)
Tables, but we know that discussion all to well.
Now ill properly work on with the first one on that list as that is the only thing that currently let me achieve the goal, but I would like to see if there wasn't a more clean way.
How about something like this? Create a container for the left and right pieces. Provide a right margin to make room for the right container. You could float it, but I absolutely positioned it because you hate floats =).
http://jsfiddle.net/Z62f2/
div { border: 1px solid gray; width: }
#container { width: 80%; min-width: 600px; margin: 0 auto; }
#header { height: 100px; }
#content { position: relative; }
#left { margin-right: 300px; height: 1000px; }
#right { width: 300px; height: 500px; position: absolute; top: 0px; right: 0px; }
<div id="container">
<div id="header">Header</div>
<div id="content">
<div id="left">Left</div>
<div id="right">Right</div>
</div>
</div>
I'd like to take a stab at this,
Here's how I would mark it up within the HTML you already have. Basically I inserted another div to contain the section and the aside.
<div id="contentContain">
<div id="section"></div>
<div id="aside"></div>
</div>
And the CSS..
#contentContain {width: 80%; }
#section {float:left; width: 60%;}
#aside {float:left; width: 40%; }
This code should give you some more perspective, and It works, you can have "more" content in aside and it won't break the layout.
Best of luck!