Display a new div in-line directly after content of parent div? - html

I have a banner that's width is set to 100%.
I then have another div set at 30px nested within the main div placed at the end of the first div. This close-ban div has the background image of a close icon and the javascript is set so when a user hits the div the entire banner hides.
I want to position this close-bar div immediately after the text inside of the div labeled banner.
This is what it should look like
[this is a banner it has text inside of it [X]]
<div id="banner">this is a banner it has text inside of it<div id="close-ban"></div></div>

You can set float left for banner and float right for close-ban. Like this:
#banner {
width: 100%;
float: left;
}
#close-ban {
float: right;
}
<div id="banner">this is a banner it has text inside of it<div id="close-ban">close</div></div>
or create a container with 100% width and put inside it banner and close-ban divs, like this:
#container {
width: 100%;
}
#banner {
float: left;
}
#close-ban {
float: right;
}
<div id="container">
<div id="banner">this is a banner it has text inside of it <div id="close-ban">close</div></div>
</div>

You could consider making the close-ban a span element rather than a div element. That way it will be placed inline with your other HTML:
http://www.w3schools.com/css/css_inline-block.asp
(The span tag is formatted the same as an inline-block. You could alternatively add display:inline-block in the CSS for your close-ban)
Keep in mind that if your text takes up the width of your top ban, it will push the close-ban to the next line with the method above. To avoid this, you could alternatively look into positioning the close-ban relative to the screen or the banner: http://www.w3schools.com/cssref/pr_class_position.asp

Related

Set div height to fit container with overflow

I have a two column div in a modal with overflow.
The left div is taller than the right div and causes overflow, and the right div has a slightly darker background color.
If I set right div height to 100%, or set the top and bottom to 0, then it only fills to the hight of the modal, so if you scroll down to see contend in the left div, then the background color of the overflow in the right goes to default.
How can I get that grey background to fill the entire height of the modal? I can't set a specific height for the column divs.
edit: JS FIDDLE
Use CSS table-layout:
.two-column {
display: table-cell;
width: 50%;
}
.right-column {
background-color: gray;
}
Example jsFiddle: http://jsfiddle.net/5Lvp3n5h/1/
.modal {
display: table;
}
.two-column {
display: table-cell;
width: 50%;
}
.left-column {
height: 5000px; /* This is actually dynamic in my case */
}
.right-column {
background-color: gray;
}
<div class="modal">
<div class="two-column left-column">Stuff</div>
<div class="two-column right-column">I wish this div would keep a gray background color even after scrolling.</div>
</div>
You can use CSS3 for this with display flex for the parent, to fit the height of the children to parent's height.
Example: https://css-tricks.com/boxes-fill-height-dont-squish/
Reliable documentation: https://developer.mozilla.org/en-US/docs/Web/CSS/flex
You can calculate and assign the height using javascript.
If you want to do it using css only:
you can use
display:table-cell;
see below link for reference:
fiddle
and if your left panel will always going to remain the larger one then you can also use the following trick:
fiddle
You can use Javascript or Jquery to set height of right div equal to left div when the page already loaded.
If you don't want to use Javascript or Jquery, I have a trick for your page like this:
Wrap two div inside a wrap div then set background-color darker to it.
The left div set background-color same as the modal.
The right div is not set background-color => It looks like darker background same as wrap div.
Try using padding on the right div or you can use tags to fill the space. Although it isn't the perfect coding practice but always does the trick

Make equal height columns and vertically aligned caption in Bootstrap 2.0

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:

Adjusting Width for an Absolute DIV block

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.

How to use one background Image for a main div which have two div, one on left and one on right

I want to make an HTML, CSS page where the layout is as:
<div id="content">
<div id="left">
.....
</div>
<div id="right">
.....
</div>
</div>
The content div has a background image which should be repeated in y-direction. Also the left and right div should be side by side over the same background image.I am able to accomplish it but by keeping the height of the content fixed but I don't want to make the content's height fixed. Please help me folks.
Thanks in Advance :)
without seeing your code... my guess is you're floating the left and right DIVs... but you're not floating the content DIV...
your CSS should look similar to this to make it work:
#content {
float:left;
background-image:url('whatever.png');
background-repeat:repeat-y;
}
#left {
float:left;
}
#right {
float:left;
}
I am able to accomplish it but by
keeping the height of the content
fixed but I don't want to make the
content's height fixed.
If you are able to repeat the background image in the Y direction then it shouldn't matter how heigh the #content div is, as your background will just fill the remaining space - correct?
If your content div is not expanding to the height of the child div's then clearly #content must be outside of the normal flow of the page, in which case you should float it and not set a height for the container div.
It's quite hard to understand what you're trying to do, but I think what you want to do is add overflow: auto to your content div, so that it becomes the same height as the left and right divs:
#content {
overflow: auto;
background: [bg code]
}
#left, #right {
float: left;
}

Background vertically aligned with div

I am trying to create a full width background image that aligns vertically with a centered div. Should I create a wrapper around all the content, or is there another way to do this (without absolute positioning)? Thanks.
alt text http://img229.imageshack.us/img229/7391/99479284.png
EDIT: Sorry if I didn't explain this very well. I've changed the image.
Background images do not display outside of their containing elements. That doesn't mean that all or part of a background image cannot be positioned outside its element, just that the parts that are outside the element boundary won't be displayed when the page is rendered.
So in short, yes. You'll have to use a wrapper div.
Yes, you want to use a wrapper:
CSS:
#wrapper { width: 100%; background: url(yourimage.png) left center repeat-x; }
#content { width: 960px; margin: 0 auto; }
HTML:
<div id="wrapper">
<div id="content">
My content
</div>
</div>