Achieving Layers and Gaps with CSS: A Dive into DIVs

I am relatively new to CSS and I am currently working on creating a timeline similar to what Facebook used to have, where each post would be displayed alternately on the left and right sides. However, I am struggling with the layout of my timeline as the posts are stacked on top of each other without any spacing between them.

My goal is to have at least 35px spacing between each post so that they are visually separated. For example, the second post (on the right side) should be positioned 35px lower than the first post (on the left side), and so on for subsequent posts.

If anyone has suggestions on how I can achieve this desired layout, I would greatly appreciate it!

<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>

Answer №1

To enhance the organization of your CSS, it is recommended to utilize a separate stylesheet. By doing so, you only need to establish 2 sets of rules which helps in avoiding code duplication. Once you have implemented this setup, proceed to apply the following code:

.leftblock, .rightblock{
    margin-bottom: 35px;
}

With the above code snippet, a margin of 35px will be added to the bottom of every div that possesses either the leftblock or rightblock class.

UPDATE: I made an error in my initial response. Kindly follow the advice provided by @Skullclutter and use classes instead of ids.

Answer №2

To address your query directly, you can utilize margin-bottom: 35px; to insert spacing beneath each box.

To optimize this code for future use and adhere to standard practices, consider the following steps:

  1. Ensure that all id attributes are unique across the page. Instead of using repeated ids like #content, opt for classes that can be applied multiple times.
  2. Transition from inline styles to an external style sheet for better organization and compliance with industry standards.
  3. Where possible, consolidate styling onto a single element to minimize unnecessary elements in your code.

I have provided a revised version of your snippet below:

#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;
}

You can view the updated version here: http://jsfiddle.net/daCrosby/g2w6dzo3/1/

Answer №3

Just a quick note: it's important to remember that you can only use each ID name once on a page. In this case, I would recommend using class=leftblock and class=rightblock instead.

So, Eric's revised code would look like this:

.leftblock, .rightblock{
    margin-bottom: 35px;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue of delayed loading of CSS in Vue.js application

My vue PWA is experiencing slow loading of CSS when using Vue Bootstrap and Buefy. I attempted to use V cloak, but it only hides components milliseconds after the raw HTML is briefly displayed without any CSS applied. I am looking for a method to display ...

A triangular-shaped div positioned at the bottom with a captivating background image

Is there a way to create a div with a unique twist - a triangle shape at the bottom? I've been attempting to add a background image within the triangle using a pseudo element (:after), but so far, it hasn't been successful. #homebg:after{ co ...

Leverage the true browser size of iOS Safari for CSS styling, regardless of the eventual toolbar display

When applying height: 100vh to iOS Safari, it utilizes the entire iPhone screen size rather than the visible part excluding the toolbar. I have researched extensively but have not found a suitable workaround for this issue. Is there a CSS-only method to ...

Implementing AJAX notifications in a contact form integrated with PHP, Google reCAPTCHA, and SweetAlert

I've implemented an AJAX script to handle the submission of a basic contact form with Google reCAPTCHA at the end of my HTML. Here's the relevant HTML code: <div class="col-md-6"> <form role="form" id="form" action="form.php" method ...

Scrolling Effect with Fixed Background Image in FullPage JS

I am trying to use FullPage JS to create a fixed video background with scrolling content. The issue I am facing is that when I set the video to section 1, it shifts to a white background when scrolling. Any assistance would be highly appreciated! #fullp ...

Django is experiencing difficulties loading CSS on certain template files

Working on my page design using twitter-bootstrap, I've created a base.html file that serves as the global base for all templates to extend. However, I'm facing an issue where upon extending the base.html in all templates, the CSS doesn't d ...

Applying Tailwind styles to dynamically inserted child nodes within a parent div

Currently, I am in the process of transitioning my website stacktips.com from using Bootstrap to Tailwind CSS. While initially, it seemed like a simple task and I was able to replace all the static HTML with tailwind classes successfully. However, now I ha ...

Trouble with achieving equal height columns in nested flexbox layout on Google Chrome

I am looking to evenly space several sidebar blocks throughout a lengthy post. To achieve this, I have utilized Flexbox within the sidebar for handling the even distribution of the blocks and it has been working flawlessly. Check out Code Pen: http://cod ...

CSS styling: Adjusting button placement to appear to the right of an HTML table

I've encountered a challenge with an HTML table that has dynamically generated columns through Ajax calls and jQuery, resulting in a variable table width. My aim is to include buttons at the top and bottom of the table that are aligned to the right b ...

Media queries are behaving incorrectly, causing them to trigger when regular CSS should be applied, and vice versa

When I switch to laptop/desktop view, the regular CSS styles are being overridden in the inspect element, and the query styles are taking precedence. Can anyone explain why this is happening? For instance, let's take a look at the code for the nav se ...

Adding and Removing Attributes from Elements in AngularJS: A Step-by-Step Guide

<input name="name" type="text" ng-model="numbers" mandatory> Is there a way to dynamically remove and add the "mandatory" class in Angular JS? Please note that "mandatory" is a custom class that I have implemented. Thank you. ...

Steps to obtain an Element binding from a Svelte component

Is it possible to retrieve the bounding client rect of an anchor element? I typically mark this using the syntax bind:this={someVariable}. However, when I add this code to a Svelte component, I receive a different object that Svelte uses to represent a c ...

Tips for adjusting the position of the second child in my navigation menu using CSS

Struggling with customizing my navigation menu in CSS, I'm seeking assistance. My goal is to have the second child element of the navigation menu on a new line instead of below the first child (refer to the image for clarification). An example of the ...

Initialization of webix combo options values in HTML code

Currently, I am working with the Webix UI framework and I am trying to achieve something similar to: <div view="combo" options="fruit"></div> This is what I have in my JavaScript file: $scope.fruit =[{id:1, value: "Apple"}, {id:2, value: "Ba ...

Launching Angular Application on GitHub Pages

I am encountering an issue with my GitHub Pages website that is connected to a custom domain. My domain is hosting a web application created with Angular, but I am having trouble loading the .js and .css files when I visit the site. In the DevTools/Networ ...

Prevent the jQuery Circle Progress animation from starting at 0 when data is updated

I am utilizing the jquery-circle-progress plugin. Everything is functioning as expected. However, I have added a button to update the percentage value. <button id="add">Add</button> In the example below, when the button is clicked, ...

Is there a way to show escaped html code on the blog editing page?

After developing a Content Management System (CMS), I encountered an issue with code formatting in my blog posts. <pre> <code> &lt;input type=&quot;color&quot; name=&quot;favcolor&quot; /&gt; </code> < ...

The page.php file in the Scratch WordPress theme is failing to display the page content as expected

I am currently building a Wordpress theme from scratch, but I am facing an issue with my page.php file not displaying the content for the Lorem page. //page.php get_header(); ?> <div id="primary" class="content-area"> <main id="m ...

My JavaScript/jQuery code isn't functioning properly - is there a restriction on the number of api calls that can be made on

Below is the code I have implemented from jquery ui tabs: <script> $(function(){ // Tabs $('#tabs1').tabs(); $('#tabs2').tabs(); $('#tabs3').tabs(); //hover states on the sta ...

What is the best way to ensure that my image gallery adjusts its width to be responsive to the div

I am facing an issue with my image gallery named slider1_container. The style properties are defined in the HTML code as follows: <div id="slider1_container" style="position: relative; top: 0px; left: 0px; width:700px; height: 600px; background: #1 ...