Expanding on this question on how to make equal height columns in Twitter Bootstrap (original fiddle).
Now the column heights match, but I need to make the captions vertically align at the bottom.
Here is a new fiddle.
As you can see the H2 captions do not line up.
Here's how I want it to look:
Starting a new with: <div class="row-fluid col-wrap"> does not work because the captions separate from the text at media breaks.
The Problem
The problem is, the answer from the original question pretty much sets the height of each of those divs to infinite. After the content, the div just stretches on and on.
The downside of which, is we don't know where the bottom of this element is.
The padding is sized to content:
But the element stretches forever
A Solution
One element that does know where the element ends is the wrapper. That was it's job. So what we'd like to do is position our header tag relative to the .col-wrap container.
To position an element relative to it's grandparent, we'll set the grandchild position to absolute. This will position relative to the first parent element it finds with position equal to relative. So we can set the header to the bottom of the col-wrap container with the following code:
HTML
<div class="row-fluid col-wrap">
<div class="span4 col well">
<p>left column</p>
<h2>Title or Caption</h2>
</div>
</div>
CSS
.col-wrap {
overflow: hidden;
position: relative;
}
.col-wrap > .col > h2{
position: absolute;
bottom: 0;
}
One thing we have to adjust for is now that the header is positioned absolutely, it's taken out of the document flow and no longer occupies any space. A hackish way to get around this is to double up your header tags and use one as a placeholder.
HTML
<h2 class='absolute'>Title or Caption</h2>
<h2 class='relative'>Title or Caption</h2>
Quick CSS Overview:
visibility: hidden; /* hides an element but still takes up the same space*/
display: none; /* hides an element and collapses*/
Using that:
On a big screen, we'll use the relative header as a placeholder by setting the visibility to hidden and positioning the absolute header at the bottom of it's grandparent.
On a small screen, we'll just collapse the absolute header, since we don't need absolute positioning anymore so the first will still be inline and we don't need both.
In CSS, it will look like this:
#media (min-width: 768px) {
.col > h2.absolute{
position: absolute;
bottom: 0;
}
.col > h2.relative{
visibility: hidden;
}
}
#media (max-width: 767px) {
.col > h2.absolute{
display: none;
}
}
Finally, since the h2 element is positioned absolutely, the column no longer acts like a container to prevent the text from spilling over. We can fix this by applying some sizing constraints on our visible absolutely positioned elements. These should be done to mimic the placeholder constraints so they line up.
We can apply a span4 class to the absolute header as well as the following css
<h2 class='absolute span4'>Title or Caption</h2>
.col > h2.absolute{
margin-left:0;
padding-right:40px;
}
Demo
jsFiddle
It will look like this:
Related
I have one element (a div) floating to the right of content, and below the content (which can be varying in height) I have another div that I want to stack above the floated right div, but stay below the content.
Here's an example: https://jsfiddle.net/8nap0qm6/
While this is close, I need the content within the ".over" div to not wrap when it hits that right-hand div, but instead fill up the whole ".over" div while still overlapping the right-hand div.
Putting a "clear: both/left" on the ".over" div pushes the div below the right-hand div instead of overlapping it.
I know I could absolute position the over div:
.over {
position: absolute;
top: 200px; // or xx%
left: 0px;
z-index: 5;
}
but I need it to be vertically controlled by the content so I can't put a set "top" on it.
Is there a way to achieve this? (Make white text in blue box go full width of blue box.) I'm open to using completely different code if necessary.
You just need to set position: absolute;
.over {
position: absolute;
z-index: 5;
}
JSFiddle
As the given answers don't seem to satisfy exactly what's expected, I decided to change some things to make the output closer to what you expect. Check my fiddle.
Major changes:
1) Added a #parent div to wrap the whole content
2) Absolutely positioned the .right div, relative to #parent
3) Added width to .right and all #parent's p elements so that summing both results in 100%
Just add clear: both; to your .over class:
.over{
clear: both;
/* your properties */
}
I will try to explain my situation the best that I can.
I have a site where it displays a list of DIV blocks with info inside. 3 DIV blocks are created using width:33%
Inside that div block I want another hidden div block that is exactly the same as the previous div block.
So I have something like this....
<div class="columnParent">
<div class="columnChild">
"Various stuff here that overlaps parent DIV"
</div>
"Various text that initially appears, but disappears after clicking a button"
</div>
Here is the CSS...
.columnParent { width:31%; float: left; margin-right:15px; margin-bottom:4%; }
.columnChild { width:31%; float: left; margin-right:15px; margin-bottom:4%; visibility:hidden; position: absolute;
background: #FFF;}
Now what happens when the child DIV is visible, it ends up being larger than the parent. This is because the width:31% is taking 31% of the entire HTML page, while the parent div is taking 31% of its parent div(not listed here).
Is there a way to get the child DIV to take the same width as its parent?
I want the Child div to be an exact replica of its parent div. I will be changing the text inside, but the actual div should be the same size and be in the exact same position.
Purpose: I have a button that displays the child div to make the parent disappear and display a new child div that has different information. Inside the child div there will be a back button to make that div disappear again (using JS).
Any help is greatly appreciated!!! Thanks!
put the child element width: 100%; , then it will take the exact width of parent element. After that, you can include jQuery to fadeIn/fadeOut the child element and manipulate it as you wish in javascript codes.
You can try by giving width 100% and position relative to columnChild, like below.
.columnChild {
width:100%;
float: left;
margin-right: 15px;
margin-bottom: 4%;
visibility: hidden;
position: relative;
background: #FFF;
}
Note: Position relative will help to adjust the position relative to the parent position and width 100% will help to make the same width as the parent div.
OK, I'm really having problems understanding the behavior of the float property.
The page is 750 px wide. In order to keep it positioned in the center of the screen, I used this code:
<div align="center">
<div align="left" style="width:750px; border-style:double;">
stuff
</div>
</div>
The elements within the main 750 px wide container are other containers that have the following styles:
div.infoCont //these containers usualy have two more containers within- for an image and text
{
width: 740px;
position: relative;
left: 2px;
border-style: double; //for debugging
float: left;
}
div.textContNI //text only
{
width: 730px;
float: left;
clear: right;
border-style: double; //for debugging
}
The small containers behave normally (they should be, because they are positioned and as big as the way I want to). But the height of the main container is A LOT less that the total height of the smaller containers... 0 px. It ignores the height of ALL the smaller containers.
This can be "fixed" by adding text right before the closing tag of the main container. I can also manipulate the height with <br> tags and no text: the height becomes as big as the total height of the borders. Another way to "fix" the problem is to add float:left; in the style, but that positions the container on the left side of the window and I can't have that.
I'm missing something and I don't know what it is.
Why does the main container ignore the total height of the containers within it and how can I take care of this bug?
Containing floated elements
This is the correct behavior of floated elements. It's not a bug.
By default, a floated element doesn't occupy space within its parent. The same happens with elements given absolute position. The parent has two children, but they're both floated, so nothing occupies space in the parent. As a result the parent has a height of zero, unless it contains some other non-floated content.
There are three common ways to make a parent contain its floated children, so that it's at least as tall as its children.
1. Fixed height
Give the parent a fixed height that's at least as tall as the height of the floated child. Technically, the floated element still does not occupy space within the parent, but the parent is tall enough to make it appear as if it does.
.parent { height: 30px; }
.child { height: 30px; float: left; }
2. clear div
Add a trailing div with clear:both inside the parent. This forces the parent to contain the floated children. The parent automatically increases in height as needed. By default, an empty div has a height of zero in all browsers, so no additional styling is required for the trailing div.
.clear { clear: both; }
...
<div class="parent">
<!-- Floated children go here -->
...
<!-- Trailing clear div goes here -->
<div class="clear"></div>
</div>
3. clearfix
Add a clearfix rule to the CSS stylesheet, and add the class clearfix to the parent. The end result is the same as adding a clear div, but it doesn't require adding additional HTML elements. Like adding a clear div, this forces the parent to contain the floated children. The parent automatically increases in height as needed.
/* The traditional version of clearfix, a good one to start with. */
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
.clearfix {display: inline-block;}
* html .clearfix {height: 1%;}
.clearfix {display: block;}
...
<div class="parent clearfix">
<!-- Floated children go here -->
...
</div>
So what I'm trying to accomplish is to have a div centered on the page (margin: auto auto;) with a navigation div just to the left of it.
The idea being the navigation div can be switched on or off (so may or may not be there). If its there or not should not interferer with the centering of the main div.
Below is an example
I've tried a few things
Wrapping both divs with a main div. Setting the main div to margin: auto auto and then setting both child divs to float: left. The problem is that when the nav div dissapears the entire thing shifts left.
Keeping the middle div margin: auto auto; floating the nav div left and then using margin-left but this changes when the page gets bigger or smaller.
Any pointers would be appreciated in the best way to do this. I was hoping to avoid tables.
JSFiddle link
Try this:
In your html:
<body>
<div class="encasing">
<div class="leftmenu"></div>
</div>
</body>
In your css:
html, body
{
width: 100%;
height: 100%;
}
div.encasing
{
top: 50px;
margin-left: auto;
margin-right: auto;
width: 70%;
height: 500px;
background-color: green;
position: relative;
}
div.leftmenu
{
right: 100%;
width: 10%;
height: 300px;
background-color: red;
position: absolute;
}
The important parts are:
To put your block containing the menu inside your center block
Make the center block have margin-left: auto; margin-right: auto;
Make the center block have a relative positioning
Have the menu have a absolute positioning
Make the menu have right: 100%
The idea here is to make the left menu use the position of the center block and then adjust itself. Right: 100% will put the right edge of the menu on the left edge of the menu.
In the end, a really good trick in css is that absolute positioned elements adjust themselves relative the the nearest relative or absolute positioned parent. :)
A few solutions I can think of:
Use absolute positioning for the navigation div. You probably want to give the body element a min-width to avoid the navigation div overlapping the main div when the window is too small.
Three-column layout, e.g. two divs with fixed widths floated to the left and right, and the content div between them. Inside the left-floated div, display your navigation div (or not). Alternatively, try display: inline-block on the three columns. The difference is in how small windows are handled (try it out). Again, you can counter undesired effects by setting a min-width on the body.
Completely fixed layout. Decide on an ideal screen resolution, and hard-code everything to that. This way, you can absolute-position everything where you want it, but the downside is that it won't look good on anything that deviates too much from the intended resolution. Especially mobile devices will see devastating results; you counter these with #media queries to adjust your layout to other screen resolutions.
You should also try to find a site that does what you want to do, and see how they did it (inspect HTML, CSS, and maybe Javascript).
I have this issue: two floating divs inside an absolute-positioned parent div.
CSS:
.wrapper {
position: absolute;
left: 0;
top: 0;
overflow: hidden; /* doesn't do anything! */
}
.left {
float: left;
}
.right {
float: right;
}
.clear {
clear: both;
}
HTML:
<div class="wrapper">
<div class="left">some text here</div>
<div class="right">some text here too</div>
<div class="clear"></div>
</div>
In Firefox, if the wrapper doesn't have position:absolute, everything works, as intended. As soon as I make it absolute, the wrapper shrinks and the content in floating divs overlap the rest of the document. Also, if I set the wrapper's width to 100% it overlaps the vertical scrollbar.
What am I missing?
Since you're using the overflow: hidden clearfix, the div.clear is completely unnecessary. But since you're also positioning your wrapper absolutely the overflow: hidden clearfix is also unnecessary as position: absolute will also clearfix (at least in Chrome and FF).
http://jsfiddle.net/j6jkk/
Floating an element takes it out of the document flow. That means it will not have an impact on its parent's dimensions, and absolutely positioned elements are 0 x 0 by default. Clearing the floated element's next sibling is only going to expand the width of the parent container if you actually have content in that element, have its width set in its style, or one of its other siblings still in the document flow has width greater than the parent's initial width.
See this article from the Mozilla Developer Network, although this is true of CSS in general, not just Firefox:
float - MDN
Regarding the width: 100% issue, if the offset parent of the wrapper is the document, this may be how Firefox responds to the positioning style. You might be able to alleviate that by placing the absolutely positioned element in an empty div with position set to relative.
Try adding this:
.wrapper{
display: inline-block;
}