DIV Stacking & Spacing Using CSS - html

As you will see (very quickly), I am somewhat new to CSS. Ultimately I am trying to develop a "timeline", sort of like what facebook had years ago, where each post would be displayed in a timeline. Each post would ultimately alternate between the left side and the right side. For example, Post 1 - left side, post 2 - right side, post 3 - left side, etc...
I am having trouble with the layout of my timeline. I've found a way to make the posts alternate sides, however each "post" is essentially stacked on top of each other, whereas I'd like to put at least 35px spacing between each post. What this means is the second post (on the right side), would be 35px lower than the bottom of the first post (on the left side) and the third post would be 35px lower than the bottom of the second post (on the right side)...so on and so forth.
Any thoughts on how I might accomplish this?
<div id="timeline" style="background-color: #fff; float: left; padding: 10px; clear: left; width: 900px; margin-top: 25px;">
<div id="middle" style="width: 900px; height: 133px; background: transparent url('/timeline.png') repeat-y scroll 0px 0px;">
<div id="leftblock" >
<img src="/dot1.png">
<div style="float: left; margin-right: 23px;">
<div style="border: 1px solid #ccc; width: 400px; float: left;padding: 5px;">adfasdfsdaf</div>
<img style="float: left;" src="/right_arrow.png">
</div>
</div>
<div id="rightblock">
<img style="float: left;" src="/dot1.png">
<div style="float: left; margin-left: 23px;">
<img style="float: left;" src="h/left_arrow.png">
<div style="border: 1px solid #ccc; width: 400px; float: left; padding: 5px;">adfasdfsdaf</div>
</div>
</div>
<div id="leftblock" >
<img src="/dot1.png">
<div style="float: left; margin-right: 23px;">
<div style="border: 1px solid #ccc; width: 400px; float: left;padding: 5px;">adfasdfsdaf</div>
<img style="float: left;" src="right_arrow.png">
</div>
</div>
<div id="rightblock">
<img style="float: left;" src="dot1.png">
<div style="float: left; margin-left: 23px;">
<img style="float: left;" src="/left_arrow.png">
<div style="border: 1px solid #ccc; width: 400px; float: left; padding: 5px;">adfasdfsdaf</div>
</div>
</div>
</div>
</div>

You need to use a separate stylesheet for your css. This way you only need to define 2 sets of rules and you avoid code duplication. Anyways, once you have that set up, simply apply this code.
#leftblock, #rightblock{
margin-bottom: 35px;
}
This gives a 35px margin to the bottom of every div that has an id of leftblock or rightblock.
EDIT: My mistake, as per #Skullclutter's answer, please use classes instead of ids.

As a direct answer to your question, you can use margin-bottom: 35px; to add spacing below each box.
There's a few things you can do to make this code work well for future use, and to follow standard practices.
First off, id attributes must be unique throughout the page - an id of #content can only appear one time, so they're not ideal for repeating elements. Use class instead, which can be used as often as needed.
Next, use an external style sheet instead of inline styles. They're much easier to manage and the industry standard.
It's also generally a good idea to use as few elements as possible for your requirements (combining styling onto one element when possible).
I've marked up your code to show how a fixed up snippet may look:
#timeline{
background-color:#fff;
float: left;
padding: 10px;
clear: left;
width: 900px;
margin-top: 25px;
}
#middle{
background: transparent url('/timeline.png') repeat-y scroll 0px 0px;
}
.block{
float: left;
width: 400px;
display: block;
margin-right: 23px;
margin-bottom: 35px;
}
.block .content{
border: 1px solid #ccc;
padding: 5px;
}
.icon{
float: left;
}
.block:nth-of-type(odd) .icon{
float: right;
}
<div id="timeline">
<div id="middle">
<div class="block" >
<img class="icon" src="/dot1.png" />
<img class="icon" src="/right_arrow.png" />
<div class="content">adfasdfsdaf</div>
</div>
<div class="block">
<img class="icon" src="/dot1.png" />
<img class="icon" src="h/left_arrow.png" />
<div class="content">adfasdfsdafadfasdfsdaf<br />adfasdfsdaf<br /></div>
</div>
<div class="block" >
<img class="icon" src="/dot1.png" />
<img class="icon" src="/right_arrow.png" />
<div class="content">adfasdfsdaf<br />adfasdfsdaf<br /></div>
</div>
<div class="block">
<img class="icon" src="/dot1.png" />
<img class="icon" src="h/left_arrow.png" />
<div class="content">adfasdfsdaf</div>
</div>
</div>
</div>
http://jsfiddle.net/daCrosby/g2w6dzo3/1/

One thing to add: you can only have one of each ID name on the page. For what you're doing, I'd suggest using class=leftblock and class=rightblock instead.
Then Eric's code becomes:
.leftblock, .rightblock{
margin-bottom: 35px;
}

Related

Center items vertically without setting height

I'm trying to vertically center certain items within a table cell. I've tried most solutions on stackoverflow and several other sites without any luck.
In this cell, the image is stuck at the top of the table cell, while the text is properly centered vertically:
<tr>
<td class='sidebar-middle'> <!--sets a left and right border-->
<a target="_blank" href="data/Standards.pdf">
<div style='width: 100%;text-align: center;overflow: hidden;'>
<div style='float: left;width: 34%; text-align: center;height: 100%;'>
<img src='images/logo.jpg' alt='Standards' style='width: 80px;vertical-align: middle;'/>
</div>
<p style='float: right; vertical-align: middle;width: 64%;'>Local Facility Standards to be Followed</p>
</div>
</a>
</td>
</tr>
However, using the same method, this DOES seem to work:
<tr>
<td class='sidebar-bottom'> <!--sets a left, right, and bottom border-->
<a target="_blank" href="Policies.html">
<div style='width: 100%;text-align: center;overflow: hidden;'>
<div style='float: left;width: 35%; text-align: center;height: 100%;'>
<img src='images/patch.png' alt='Policies' style='height: 80px;vertical-align: middle;'/>
</div>
<p style='float: right; vertical-align: middle;width: 64%;'>Policies</p>
</div>
</a>
</td>
In the first (frustrating) example, the image is 112 pixels in height, scaled down to 30. In the second (working) example, the image is 122 pixels in height, scaled down to 80. I suspect that image height has something to do with it, but can't get any further in resolving the problem.
While assigning classes to the elements I didn't see a change. When I replaced the <tr> and <td> with <div> and <section> it didn't change. It just works like the way you wanted it to. There's no style info provided for classes, .sidebar-middle and .sidebar-bottom so that might be your problem (or the rest of the code you neglected to post). Note: I didn't need to modify the div.C or the <section>s I added, so table components may have not been needed and the floats were sufficient.
When using inline styling heavily, your HTML gets cluttered and there's no easy way of fixing it should you have many lines of that coding disaster. As Paulie_D and hidanielle already stated, your vertical-align does not function on floated elements, and HTML table -layouts are so 90s. In the 21st century we use table-* CSS properties.
SNIPPET
.A {
width: 100%;
text-align: center;
overflow: hidden;
}
.B {
float: left;
width: 34%;
text-align: center;
height: 100%;
}
.img {
width: 80px;
height: 80px;
}
.note {
float: right;
width: 64%;
}
<div class='C'>
<section class='sidebar-middle'>
<a target="_blank" href="http://www.orimi.com/pdf-test.pdf">
<div class='A'>
<div class='B'>
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png' alt='Lenna' class='img' />
</div>
<p class='note'>Local Facility Standards to be Followed</p>
</div>
</a>
</section>
</div>
<div class='C'>
<section class='sidebar-bottom'>
<a target="_blank" href="http://www.example.com">
<div class='A'>
<div class='B'>
<img src='https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png' alt='Lenna' class='img'>
</div>
<p class='note'>Policies</p>
</div>
</a>
</section>
</div>
Instead of floats, use CSS Tables (since you started with an actual table for layout).
* {
margin: 0;
padding: 0;
}
.inner {
display: table;
table-layout: fixed;
width: 100%;
text-align: center;
overflow: hidden;
}
.left {
display: table-cell;
width: 34%;
text-align: center;
background: pink;
}
img {
width: 80px;
}
.right {
display: table-cell;
width: 64%;
vertical-align: middle;
background: lightblue;
}
<a target="_blank" href="data/Standards.pdf">
<div class="inner">
<div class="left">
<img src='http://www.fillmurray.com/80/80' alt='Standards' />
</div>
<p class="right">Local Facility Standards to be Followed</p>
</div>
</a>

How to get two divs inline beside eachother?

Need help getting two divs to align beside each other, one contains 2 pictures, the other a video.
HTML
<img src="example3.png" alt="CopeLogo" height="200" width="200" class="exlogo" >
</div>
</div>
CSS
#pics{
text-align: center;
background-color: #194b6f;
height: 200px;
width: 500px;
float: left;
border-radius: 5px;
margin-left: 50px;
border: 2px dashed black;}
#vid{
text-align: center;
background-color: #194b6f;
height: 490px;
width: 660px;
float: right;
border-radius: 5px;
border: 2px dashed black;
margin-right: 50px;}
just use float:left; for both, also you can use display:inline-block; for both as well if you don't want to use float.
I get your point but please do add your working codes to be more clear in your question.
You can actually do this:
HTML
<div class="header">
<div class="pic">
<img src="http://placehold.it/150x150" alt="">
<img src="http://placehold.it/150x150" alt="">
</div>
<div class="vid">
<img src="http://placehold.it/400x150" alt="">
</div>
</div>
CSS:
.header {
display: inline-block;
width: 100%;
border: 1px solid #ccc;
}
.vid {
float: right;
}
.pic {
float: left;
}
Working fiddle
Think of it this way: use DIVs to group each entity that you want to float:left.
You want to align the two images, so put each into a div, and float those divs left.
You want to align the box containing the two images with the box containing the video. Float each of those left.
When you use float:left (or right), the floated DIV is removed from the HTML "flow" and therefore has zero height. Therefore, the DIV containing a floated DIV (or other element) has zero height. Not good. To fix that, apply the clearfix hack to the parent div.
Here is a better explanation (although it uses a different method to clear the floats -- both methods work fine): Customising Insightly HTML contact form (aligned, spaced fields)
/* CSS: */
body{min-width:650px;min-height:650px;}
#pics{float:left;border-radius:5px;XXXmargin-left:50px;border:2px dashed black;}
#picLeft {float:left;height:200px;width:200px;}
#picRight{float:left;height:200px;width:200px;}
#vid{float:left;height:200px;width:200px;border-radius: 5px;border:2px dashed black;}
.clearfix:after{content:"";clear:both;display:block;}
<!-- HTML: -->
<div id="container" class="clearfix">
<div id="pics" class="clearfix">
<div id="picLeft">
<img src="http://placekitten.com/200/200" alt="MyLogo" height="200" width="200" class="mainlogo">
</div>
<div id="picRight">
<img src="http://placekitten.com/195/195" alt="CopeLogo" height="200" width="200" class="exlogo" >
</div>
</div>
<div id="vid">
<iframe src="https://www.youtube.com/watch?v=8E8xMcXmI9E" width="195"></iframe>
</div>
</div>
Additional References:
CSS-Tricks: Clearfix hack
Another example
Ahmed Alaa's answer also has a good tip: display:inline-block is also useful - +1
float both elements left and adjust there widths accordingly.

Laying out Images in HTML

I'm super bad/newb when it comes to HTML/CSS so this is probably super easy. How do you layout four images like this? -
I'm using four div tags right now to do it (or shall I say attempt?).
div.imageBlockA {
float: top;
float: left;
}
div.imageBlockB {
float: top;
float: right;
}
div.imageBlockC {
float: bottom;
float: left;
}
div.imageBlockD {
float: bottom;
float: right;
}
Thanks!
Without seeing your layout, try the following:
.image {
width: 50%;
float: left;
box-sizing: border-box;
padding: 10px;
}
.image img {
max-width: 100%;
border: 2px solid red;
display: block;
margin: 0 auto;
}
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
You'll need to add some more styling to make it look the way you want, but there's one way of achieving a basic layout that you're describing.
As rightfully pointed out in comment below, try also adding box-sizing: border-box so you can safely add padding / border to the outer div element without worrying about the extra width / height it will add (possibly breaking the layout by making the next image go to the next line as the width will then be over 50%).
EDIT - forgot to mention and as #Paulie_D pointed out in comments on your question, there isn't a top or bottom value for the float property.
table {
width: 100%;
}
td {
text-align: center;
}
.image {
background-color: lightblue;
width: 80%;
height: 40px;
}
<table>
<tr>
<td>
<p class="image">IMAGE</p>
</td>
<td>
<p class="image">IMAGE</p>
</td>
</tr>
<tr>
<td>
<p class="image">IMAGE</p>
</td>
<td>
<p class="image">IMAGE</p>
</td>
</tr>
</table>
Regardless of what some people may say, I find that using the table tag along with separate table rows and fields reduces the amount of work that you have to do when doing layouts. You may not agree with this method, but it's often useful - especially when starting out with HTML and CSS.
Another way would be
body {
text-align: center;
}
.image {
display: inline-block;
width: 49%;
}
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
<div class="image">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcQgnZ-1mZ2Q2jRN2OZ2HIMESBjOfC295h0cZ_Bzgk9c30HRUR59eg">
</div>
this uses text-align: center; and in some cases is this more flexible.

how to show product info after image in bigger zoom levels

In shopping cart details page product image is defined using code below. Its proportions should preserved.
For bigger zoom levels image hides start of text after it.
How to render this page properly in all zoom levels?
For unknow reason div #productinfo starts at left side but it shoult start after image.
I tried to add display:inline-block to every div but this does not have any effect.
jquery, jquery ui, fancybox and pikachoose are used.
html:
<div style="float: left; width: 30%; margin-right: 1%">
<a href="#" class="details-picture">
<img src="/Thumb?product=1308318&size=198" alt="">
</a>
</div>
<div id="productinfo">
<div>
Price <span id="Price">
1.73
</span>
</div>
css:
.details-picture {
background: none repeat scroll 0 0 #FFFFFF;
border: thin ridge #BBBBBB;
display: block;
float: left;
height: 200px;
line-height: 200px;
margin: 0 20px 15px 0;
overflow: hidden;
position: relative;
text-align: center;
width: 198px;
}
.details-picture img {
border-width: 0;
height: auto;
max-height: 198px;
max-width: 198px;
vertical-align: middle;
width: auto;
}
This seems to be due to those inline attributes on the top-level div. The combination of float: left and width: 30% causes the issue you were experiencing.
<div class="productimage">
<a href="#" class="details-picture">
<img src="/Thumb?product=1308318&size=198" alt="" />
</a>
</div>
<div class="productinfo">
<div>
Price <span id="Price">1.73</span>
</div>
</div>
jsfiddle

Vertically align text within div element

I know this question has been asked to death but nothing through searching has worked for me.
You know the deal, I have a div element that I need to vertically align text in but nothing has worked (position:absolute;top:50%;margin-top:-x;display:table-cell;vertical-align:middle;etc., etc.)
Here is what I am working with (sorry for the inline CSS). Anyway, the I would use line-height but the text can be one or two lines. It should vertical align with the image which is always max-height of 30px (30x50).
<div style="margin:0 0 10px 0;padding:10px;border:2px solid #606060;background-color:#2b2b2b;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px;">
<div style="float:left;width:55px;height:40px;">
<img style="max-width:50px;border:1px solid #ffb92c;" src="image.jpg" alt="" />
</div>
<div style="float:right;width:155px;font-size:0.7em;height:40px;">
This is the text to vertically align
</div>
</div>
One other thing you can do. If it's only one line of text in the div you can use line-height
example
div {
line-height:40px;
}
The idea is from here and should work for all browsers.
<div style="margin: 0 0 10px 0; padding: 10px; border: 2px solid #606060; background-color: #2b2b2b;
-webkit-border-radius: 8px; -moz-border-radius: 8px; border-radius: 8px;">
<div style="float: left; width: 55px; height: 40px;">
<a href="link">
<img style="max-width: 50px; border: 1px solid #ffb92c;" src="image.jpg"
alt="" /></a>
</div>
<div style="float: right; width: 155px; font-size: 0.7em; height: 40px; display: table; #position: relative; overflow: hidden;">
<div style="#position: absolute; #top: 50%; display: table-cell; vertical-align: middle;">
<div style="#position: relative; #top: -50%;">
This is the text to vertically align
</div>
</div>
</div>
<div style="clear: both"></div>
</div>
You need to do like this:
http://jsfiddle.net/rathoreahsan/5u9HY/
Use fixed height instead of padding in main div. and use line height for left & right Divs
Here is a clean version of the solution
<div style="background: yellow">
<div style="width: 155px; font-size: 0.7em; height: 40px; display: table; overflow: hidden;">
<div style="display: table-cell; vertical-align: middle;">
<div style="">
This is the text to vertically align
</div>
</div>
</div>
<div style="clear: both"></div>
http://jsfiddle.net/5y4Nb/
Seems like a common float issue which can be fixed with a clearfix or, like i did in the following code snippet, with a fixed height of the wrapper.
I also sat an line-height of the floating divs and made it a little wider.
Take a look at this:
<div style="margin:0 0 10px 0;padding:10px;border:2px solid #606060;background-color:#2b2b2b;-webkit-border-radius:8px;-moz-border-radius:8px;border-radius:8px;height:40px"> <div style="float:left;width:55px;height:40px;"> <img style="max-width:50px;border:1px solid #ffb92c;" src="image.jpg" alt="" /> </div> <div style="float:right;width:185px;font-size:0.7em;height:40px;line-height:40px"> This is the text to vertically align </div> </div>
http://jsfiddle.net/YqxPZ/3/
Is you need to show only a few lines of a very long text, here is the Fiddle. Adjust height according as needed.
.container-text {
height:40px;
width:180px;
overflow-y:hidden;
display:table-cell;
vertical-align:middle;
text-align: center;
}