I have a div of set width (as a percentage of its parent) which contains a number of child rows. I want the width of these rows to always match each other- so the row with the widest content would determine the width of the others. However, this width may also be larger than the containing div, in which case that div would need to expand to accommodate the child rows.
Here is my HTML:
<div class="outer">
<div class="inner">
<div class="block-wrapper">
<h2>First heading</h2>
<h2>Another heading</h2>
<h2>The third heading which could be quite long</h2>
</div>
</div>
</div>
And current CSS:
.outer {
width:1000px;
background-color:#000;
height:1000px;
}
.inner {
width: 80%;
background-color:#FFF000;
height:50%;
}
.block-wrapper {
width: 40%;
background-color:#fff000;
height:100%;
}
h2 {
font-size:18px;
width: 100%;
float: left;
clear: left;
line-height: 40px;
margin-top: 10px;
padding:1%;
background-color:rgba(0,0,0,0.75);
}
Here is a JS Fiddle.
So my problem is the third row there, "The third heading..." Should be all on one line, with the other h2 rows expanding to the same width. The minimum width for the h2 rows should be the width of the parent div.
I hope this makes sense... any ideas?
Update your .block-wrapper with width:auto and display:inline-block.
.block-wrapper {
width: auto;
min-width:40%;
background-color:#fff000;
height:100%;
display:inline-block;
}
DEMO
inline-block and width:auto on your .block-wrapper should help. Plus you'll need to lose that padding 1% on the h2 to avoid confusing it. There are other ways you can add that in.
http://jsfiddle.net/guxkcqyz/
.outer {
min-width:1000px;
background-color:#000;
height:1000px;
}
.inner {
width : 80%;
background-color:#FFF000;
height:50%;
}
.block-wrapper {
background-color: red;
width : auto;
display : inline-block;
height:100%;
}
h2 {
font-size:18px;
line-height: 40px;
margin-top: 10px;
background-color:rgba(0,0,0,0.75);
}
Related
I'm trying to understand CSS display: table. What happens when the cells/columns within the table don't make 100%? For example within the table in the JSFiddle:
.flag { max-width: 10%;}
.country_manager {max-width: 55%;min-width: 35%;}
.score1 { max-width: 5%;}
.score2 { max-width: 5%;}
At most that cover's 75% of the table width, so how is it decided where the remaining width goes?
In this JSFiddle I have blank cells at either side assuming one or both would take up the remaining space.
Without them, there isn't a lot of difference.
The reason I ask is that I'd like the country section to be slightly adjustable, and if it is smaller, I'd like the table contents to be centered with even spacing to either side.
I think this is no different from how browsers allocate width to actual table columns, and there is a prior question about this How is extra width in a table divided up among columns?. Alas, there is no good answer there either, but a link to further documentation.
Interestingly enough, this behaviour is inconsistent across two very good, standards-committed browsers -- Chrome and Firefox. Firefox appears to actually use the percentages you give, so if you have 75% of the width set in your containing elements, it'll only fill 75% of the table and center the elements in the middle. Chrome, on the other hand, will assume that the width is 100%, and represent the columns as filling the full width.
e.g. if you have two max-widths, both set at 5%, Firefox would show each at 5% width and have 45% empty each on the right and left. Chrome would be 100% full, and each element would be 50% width.
Based on the browser inconsistency alone, I'd avoid ever not letting them add up. Perhaps you could use a :before and :after pseudo-class to create a spacer area with the remaining with, and force it to be centered?
I would suggest using different code structure. You see, tables are useful in displaying chunks of data. But if you want to have a website layout I would split website into header / content / footer. And after that use divs with absolute positioning to represent those block you've shown
i think and test this css code.
<style>
.table {
margin-top:5%;
padding-top:5%;
padding-bottom:5%;
background-color: #ddd;
width: 70%;
height: 50%;
position: absolute;
display:table;
font-size: 2vh;
text-align: center;
overflow:hidden;
}
.row {
display:table-row;
width:100%;
height:40%;
}
.row_small {
display:table-row;
width:100%;
height:5%;
background-color: red;
padding:10px 0;
}
.blank {
display:table-cell;
}
.flag {
display:table-cell;
width:10%;
max-width:10%;
background-color: orange;
}
.country_manager {
display:table-cell;
max-width: 55%;
min-width: 35%;
}
.country {
display:table-cell;
width: 100%;
height:70%;
background-color: yellow;
float: left;
}
.manager {
display:table-cell;
height:30%;
width: 100%;
background-color: pink;
float: left;
}
.score1 {
display:table-cell;
width:5%;
max-width:5%;
background-color: blue;
}
.score2 {
display:table-cell;
width:5%;
max-width:5%;
background-color: green;
}
.title {
display:table-cell;
padding:10px 0;
color:#fff;
}
.ref_contain{
width: 100%;
position: absolute;
height: 15%;
}
.ref {
background-color: #800080;
color: #FFFFFF;
display: table-cell;
float: left;
padding: 10px 0;
position: absolute;
text-align: left;
text-indent: 133px;
width: 100%;
}
</style>
//write your html
<div class="table">
<div class="row_small">
<div class="blank">1</div>
<div class="title">2</div>
<div class="title">3</div>
<div class="title">AET</div>
<div class="title">Pens</div>
<div class="blank">blank1</div>
</div>
<div class="row">
<div class="blank">blank2</div>
<div class="flag">Flag</div>
<div class="country_manager">
<div class="country">Country</div>
<div class="manager">Manager</div>
</div>
<div class="score1">1</div>
<div class="score2">7</div>
<div class="blank">blank3</div>
</div>
<div class="row">
<div class="blank">blank4</div>
<div class="flag">Flag</div>
<div class="country_manager">
<div class="country">Country</div>
<div class="manager">Manager</div>
</div>
<div class="score1">1</div>
<div class="score2">7</div>
<div class="blank">blank5</div>
</div>
<div class="ref_contain">
<div class="ref">Ref</div>
</div>
</div>
Here's what I can't do by any means, using only CSS:
Columns should have equal heights.
And, on a given column, a absolute positioned element should be present relative to that column:
The HTML:
<div class="wrapper">
<div class="inner">
<div class="column">
<img src="http://www.english3.com/wp-content/uploads/2013/05/DFI-Logo-300px-X-200px.png"/>
</div>
<div class="column info">
<p>Some text here yeah :).</p>
<p>Some text here yeah :)</p>
<p>Some text here yeah :)</p>
<a class="link" href="#">I should be absolute.</a>
</div>
</div>
</div>
THE CSS:
.wrapper {
overflow: hidden;
position: relative;
}
/*added */
.inner {
width: 100%;
display: table;
}
.column {
display: table-cell;
width: 50%;
vertical-align: top;
background-color:red;
}
.column img {
max-width:100%;
display: block;
}
.info {
background-color: blue;
padding-bottom: 25px;
}
.link {
display: block;
position: absolute;
left: 50%;
bottom: 3%;
color: yellow;
}
Fiddle to play:
Here's the try with table-cell; and a relative inner container:
http://jsfiddle.net/BuuFv/98/
TRIES AND FRUSTRATIONS:
1) - Display table
For equal heights I can't pull this out, due to FF issues.
Tried with an extra relative parent container
But left image doesn't shrink according to it's container on FF.
Tried giving height: 100%
http://davidwalsh.name/table-cell-position-absolute
But didn't work either, the image doesn't shrink or expands using max-width;
2 - Huge positive padding and negative margin values
Will not work, because the absolute positioned element will not stay in place.
3 - Faux Columns
Seems to be of any help, due to the fact that, we are not playing with solid background colours, and we have an image on the left column instead.
A picture:
Any help, please?
1)
On this case, adding an extra relative container with position relative attribute (.inner), did the trick.
2)
The fact that the image didn't shrink or grow on FF was because it wasn't considering the max-width declaration as it should.
Fix:
.inner {
width: 100%;
display: table;
table-layout:fixed; /* <<-- ADD */
}
the table-layout fixed algorithm made Firefox recognize the max-width in the cell and this let the image shrink.
Credits on this solution should be shared with Paul O'B.
Here is my take on it http://jsfiddle.net/E6UDD/
So basically, uncoment padding and negative margin hack, and add the height of the column to be the same amount.
Then, add relative positioning to the .column.
Then, position of the absolute positioned link should be top: 50% to have it at the very bottom, or 44% to mimic the 3% you originally indended.
DEMO
.wrapper {
position:relative;
overflow: hidden;
white-space:nowrap;
outline:1px solid red;
height:auto;
}
.column {
position:relative;
display:inline-block;
vertical-align:top;
background-color:red;
width:50%;
}
.column img {
max-width:100%;
min-width:30px;
min-height:20px;
}
.info {
position:absolute;
bottom:0;
top:0;
background-color: blue;
}
.link {
clear:left;
display: block;
position: absolute;
bottom: 3%;
color: yellow;
}
http://jsfiddle.net/BuuFv/75/
You can try changing the link from absolute positioned to another table-cell. Not 100% on your use case but it seems to accomplish what you are asking.
.link {
height:100px;
display:table-cell;
vertical-align:bottom;
color: yellow;
}
I've created a JSFiddle to describe what I mean:
http://jsfiddle.net/3yGLT/
I want my .top section to be affected by the height of the .floated-div, as you can see. At present, my .floated-div content drops over the .bottom section, which is not what I want. The height of the .floated-div needs to dictate the height of the .top section, effectively pushing .bottom down to make room for it.
I thought Clear divs were the solution I wanted, but it's not giving me the behaviour I'm after. I think this would only apply if the main content of .top was in a similar div to floated-div and not embedded in this way.
I can add things like clears, but I can't adjust the structure of this code as it's something that's generated through the CMS.
HTML
<section class="top">
<h1>test</h1>
<p>some content</p>
<div class="floated-div">
<h2>aside content</h2>
<p>some aside content</p>
<p>some aside content</p>
<div class="clear"></div>
</div>
<div class="clear"></div>
</section>
<section class="bottom">
</section>
CSS
.top{
width: 60%;
height:auto;
background:#f1f1f1;
}
.floated-div{
width:40%;
position:absolute;
right:0;
top:0;
}
.clear{
clear:both;
}
.bottom{
width: 100%;
height:100px;
background:#d1d1d1;
}
The problem is that your .float-div doesn't actually float. Because of the "position: absolute" rule it would never affect the height of the parent container (that's the meaning of absolute positioning). To make it float you need to remove this rule and add "float:right" to the div. In this case clearance will do its work.
floated-div {
float: right;
width: 40%;
}
Here is working example: http://jsfiddle.net/qvgz4/
i added this to your css and it worked :
.floated-div > p{margin:0;} /**added**/
.floated-div > h2{margin-bottom:0;}/**added**/
.top{
width: 60%;
height:auto;
background:#f1f1f1;
margin-bottom:5%; /*** added to avoid div overlap **/
}
basically <p> is taking extra area in floated-div1, cleared them through margin!!
If I understand the question correctly, I think the best option is to go with a display:table, like this CSS:
.top{ display:table; width:100%;}
.top .side { display:table-cell; padding:.5em;}
.top .left { width:60%; background:#f1f1f1;}
.top .right { width:40%;}
http://jsfiddle.net/3yGLT/8/
If you want the height of the top block to adapt to the height of the floated div, then you cannot absolutely position the floated div. Your only option then is to float it to the right.
But that will place it below the H1 and P that come before it. The only way to avoid that is to take the H1 and P out of the flow of the document. We do that with absolute positioning.
This solution works only if the height of the floated div is always going to greater than the H1 and P. You will also need to fiddle with the left and top positions of the H1 and P to get it just right.
http://jsfiddle.net/3yGLT/15/
.cf:before,
.cf:after {
content: " ";
display: table;
}
.cf:after {
clear: both;
}
.top {
height:auto;
background:#f1f1f1;
}
.top > h1,
.top > p {
position: absolute;
width: 60%;
left: 10;
}
.top > p {
top: 40px;
}
.floated-div {
width:30%;
float: right;
}
.bottom{
width: 100%;
height:100px;
background:#d1d1d1;
}
Without touching html , you can do as below
.top{
width:100%;
background:#f1f1f1;
float:left;
}
.top > h1{
width:60%;
float:left;
position:relative;
}
.top > p{
width:60%;
float:left;
position:absolute;
margin-top:50px; // margin-top depend on your h1 height
}
.top .floated-div{
width:40%;
float:right;
}
.clear{
clear:both;
}
.bottom{
width: 100%;
height:100px;
background:#d1d1d1;
float:left;
}
I have 3 divs in one row
<div id="left"></div>
<div id="middle"></div>
<div id="right"></div>
here's how its layed out
I need the middle div to stay a fix width, but the left and right divs to shrink in as the screen gets smaller, heres an example
how would I write out the css?
this is how I have it so far, and by the way the 3 divs are wrapped in another div#mid
#mid {
max-width: 100%;
min-height: 395px;
max-height: 395px;
position: relative;
background-color: #F00;
display: block;
}
#left {
min-width:35%;
min-height: 395px;
max-height: 395px;
background-color: #00F;
position:relative;
float: left;
}
#middle {
min-width:30%;
min-height: 395px;
max-height: 395px;
background-color: #3F0;
position:relative;
float: left;
}
#right {
min-width:35%;
min-height: 395px;
max-height: 395px;
margin:0;
padding:0;
background-color: #0FF;
position:relative;
float: left;
}
if anyone can help me out id really appreciate it, thanks in advance!
Here I've answered this question, you can do it like this : My Fiddle
<div class="container">
<div class="first"></div>
<div class="static"></div>
<div class="third"></div>
</div>
CSS
.container {
display:-webkit-box;
-webkit-box-orient:horizontal;
-webkit-box-align:stretch;
display:-moz-box;
-moz-box-orient:horizontal;
-moz-box-align:stretch;
display:box;
box-orient:horizontal;
box-align:stretch;
color: #ffffff;
}
div {
height: auto;
}
.first {
background-color: #546547;
}
.static {
background-color: #154d67;
width: 300px;
}
.third {
background-color: #c00000;
}
.first, .third {
-webkit-box-flex:1.0;
-moz-box-flex:1.0;
box-flex:1.0;
}
Its very simple give fixed width to the middle div like width:300px...Hope this will be useful...
Very Simple.
Float the three divs.
Set the display property to 'inline-block'.
Set the width attribute of middle div.
Set max width attribute of the left & right div.
Here is the HTML markup I have tested with:
<body>
<div id="left">LEFT CONTENT ... LEFT CONTENT ... LEFT CONTENT ... LEFT CONTENT</div>
<div id="middle"></div>
<div id="right">
RIGHT CONTENT ... RIGHT CONTENT ... RIGHT CONTENT ... RIGHT CONTENT
</div>
</body>
Here is a sample CSS:
#right,
#left {
background-color:green;
float:left;
display:inline-block;
max-width:20%;
min-height:20px;
}
#middle {
width: 60%;
float:left;
display:inline-block;
background-color:blue;
min-height:20px;
}
And here is the implementation: http://jsfiddle.net/3yEv3/
I have a two column layout:
<html>
<head></head>
<body>
<div id="container">
<div id="header"></div>
<div id="sidewrapper"></div>
<div id="contentwrapper"></div>
</div>
</body>
</html>
I want to have both sidebar and content be 100% in height but the most top container's min-height should be 100%.
I tried to solve it with the following css:
* {
margin: 0;
padding: 0;
}
html {
font-family: Georgia, serif;
color: #000; height:100%; min-height:100px;
}
body {
background: #fff; height:100%; min-height:100px; overflow:hidden;
}
#header {
width: 100%;
position: relative;
float: left;
height: 23px;
}
#container {
position:relative;
width: 100%; height:100%;
margin: auto; background:blue;
}
#contentwrapper {
float:left;
background:red;
position: relative;
width: 700px; padding:0px; margin:0px;
height: auto;
}
#sidewrapper {
float:left;
position: relative;
width: 159px; height:100%;
padding:0px; margin:0px;
}
...but I get a scrollbar because of the header's 23px height. I tried to solve it with overflow:hidden for the body element but I do not know if that is the right solution.
Any ideas?
If my assumption I put forward in my comment to the question is right, then sidebar is 100% high, and on top of that you have a 23px header, so that causs your container to be 100% + 23px high.
In the future you will have in css calc() http://hacks.mozilla.org/2010/06/css3-calc/ . This will solve your problem.
Now, I guess you should calculate the height of the sidebar ( = height of container - 23px), by javascript.
Make #header position absolute. This will cut that block out of the normal flow and you'll be able to make heights of other blocks equal to their parent.
#header {
position: absolute;
width: 100%; height: 23px;
top: 0; left: 0;
}
#sidewrapper,
#contentwrapper {
float: left;
width: 50%;
height: 100%;
}
#sidewrapper .content,
#contentwrapper .content {
margin-top: 23px;
}
The height of an element is compared with its father element. In your case, I recommend you specify the concrete width & height for "containter", because it'll be hard to pretend the size of the screen on many machines.
If you insists use percent, I recommend you use for both element, such as header 25% height and content 75% height.
Lets say I've a html body and a div inside and I want to make the height of the div to the entire browser without scroll bar and side gaps. Then understand the following example below, and implement your own.
Html:
<body>
<div id="yellowDiv">
<div>
</body>
CSS:
body{
margin:0px;
}
yellowDiv{
background-color:yellow;
height:100vh;
}