Utilizing CSS to Properly Position HTML Elements

Check out my code on jsFiddle

I'm facing some challenges in positioning HTML elements using CSS. While I grasp the fundamental concepts of block and inline elements, implementing them in real coding scenarios can be confusing. I've shared my HTML code below, which I'm mostly satisfied with. Now, I've moved on to styling the code with CSS. My goal is to create a Navigation bar at the top of the page, for which I've used the CSS code (provided below).

Initially, I set my list items as inline-block. Then, I floated my heading to the left and the list to the right, resulting in a satisfactory layout. However, I encountered an issue where my 'slider' class element moves up between the two elements.

I'd appreciate a solution to this problem, but more importantly, I want to understand the underlying theory and functionality behind it. My speculation is that by floating these two elements, they have been taken out of the normal flow of the document, allowing the next element to fill the vacant space. I seek not only a fix but also a deeper comprehension of this phenomenon so I can handle similar challenges independently in the future.


<style>
.nav li {
display: inline-block;
}

.nav ul {
float: right;
}

.nav h1 {
float: left;
}
</style>

<div class="nav">
<h1>Resume</h1>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Skills</li>
<li>Experience</li>
<li>Contact</li>
</ul>
</div>
</div>


<div class="slider">
<h2>Andrew Hardiman</h2>
<p>Webdesigner & Developer</p>
<p>Read More</p>
<p>HTML</P>
<p>CSS</P>
<h3>I design and develop amazing websites that are sleek, easy-to-navigate and exciting to use.</h3>
<p>Work with us to plan your digital marketing mix and achieve better results online.</p>
</div>
</div>


<div class="Portfolio">
<div class="container">
<h3>Portfolio</h3>
<img src="https://download.unsplash.com/photo-1423655156442-ccc11daa4e99">
<h3>Client</h3>
<h4>JPMorgan</h4>
<h3>Role in Project</h3>
<h4>Project Lead</h4>
<h3>Project Included</h3>
<h4>Mobile and Web Interface</h4>
</div>
</div>


<div class="skills">
<div class="container">
<h3>Skills</h3>
<p>I am a bit of a hybrid Web Developer and like to utilize my skills both front and back end as well
as keep in touch with my creative side designing websites</p>
<p>html</P>
<p>CSS</p>
<p>JavaScript</p>
<p>jQuery</P>
</div>
</div>


<div class="Experience">
<div class="container">
<h3>Experience</h3>
<h4>Work Experience</h4>
<p>Blah blah blah, professional with this and that.</p>
<h5>Royal National Lifeboat Institution</h5>
<p>Coastal Support Officer, blah blah blah.</p>
<h5>Pioneer Business Systems</h5>
<p>blah d, blah d, blah.</p>
<h5>JPMorgan Chase</h5>
<p>Some more blah d, blah.</p>
</div>
</div>


<div class="wrap">
<div class="container">
<img src="https://download.unsplash.com/photo-1424296308064-1eead03d1ad9">
<h3>Andrew Hardiman</h3>
<p>I have worked for one of the top Investment Banks in the world (JPMorgan) and loved every minute of it!</p>
<p>Today, I have left the world of high-finance to fulfill my curiosity in the freelance arena which has been amazing so far and I can't think of why I did not take the plunge sooner.</p>
</div>
</div>


<div class="contact">
<div class="container">
<div id="Username"></div>
<div id="Email"></div>
<div id="Cost or your project"></div>
<div id="Timeline"></div>

<h4>LOCATION</h4>
<p>Flat 1, 29 Markham Road, Bournemouth, BH9 1HZ</p>

<h4>I AM SOCIAL</h4>
<ul>
<li>Facebook</li>
<li>Twitter</li>
<li>Google+</li>
<li>LinkedIn</li>
<li>Flickr</li>
</ul>

<div class="Send'>
<a href="#">Send</a>
</div>

</div>
</div>


<div class="location">
</div>


<div class="top-footer">

<div class="footer-menu">
<h4>Menu</h4>
<ul>
<li>Home</li>
<li>Portfolio</li>
<li>Skills</li>
<li>Experience</li>
<li>Contact</li>
</UL>
</div>


<div class="footer-archives">
<h4>Archives</h4>
<ul>
<li>March 2014</li>
<li>April 2014</li>
<li>May 2014</li>
<li>June 2014</li>
<li>July</li>
</ul>
</div>


<div class="widget">
<p>Not really sure what this is for, you'll need to find out.</p>
</div>


<div class="follows">
<h4>Follows</h4>
<ul>
<li><a href="#"></li>
<li><a href="#"></li>
<li><a href="#"></li>
<ul>
</div>



<div class="footer-bottom">
<P>© 2015 Website by Andrew Hardiman</p>
</div> 

Answer №1

Here's a quick tip: To resolve the issue you're facing, make sure to clear your floats.

.nav { 
clear: both; 
overflow: auto; }

You can observe this solution in action by viewing a modified version of your jsfiddle. (By the way, I noticed some syntax errors in your HTML which I corrected in the forked link provided above, like missing closing tags.)

More detailed explanation: Within DIV.nav, you have 2 child elements that are floating. However, because these elements do not occupy the entire space, any content below DIV.nav will start moving up to fill any gaps.

I also suggest exploring beginner resources on platforms such as SitePoint or A List Apart. I hope this information proves useful for you.

Answer №2

To ensure that your floated content does not impact any subsequent divs, make sure to clear the float. Incorporate the following code into your CSS stylesheet:

.gallery{
    clear:both;
}

When you float an element either left or right, it creates a gap that allows other elements to occupy that space. By clearing the float, you can prevent these elements from filling that void.

Alternatively, you may address the affected element with this solution:

.menu{
    overflow:hidden;
}

Check out the JSFiddle demonstration here

Answer №3

Solution to the Issue:

To resolve this problem, you can clear your slider using the following CSS:

.slider{
    clear:both;
}

Explaining the Issue: The reason why your .slider moved between your ul and h1 is because the floated elements did not occupy 100% of the parent container's width. This allowed space for the .slider to fit in between them. Adding clear:both to the .slider class rectified this issue.

View Demo on jsFiddle


Alternative Solution:

An Alternative Approach: To avoid layout issues, consider utilizing div containers extensively as they are element containers. Set their width to 100% and nest your elements inside them. Here's an example demonstrating a solution:

<div class="row">
    <ul>
        <li>Home</li>
        <li>Portfolio</li>
        <li>Skills</li>
        <li>Experience</li>
        <li>Contact</li>
    </ul>
</div>

Apply these styles to the .row class:

.row {
    display: inline-block; /* or display: block;* display: table; */
    width:100%;
    clear:both;
}

This configuration ensures no other elements will interfere within that section.

Check out the demo with this alternative solution:

View Demo on jsFiddle


Responding to Concerns in Comments:

For further information, refer to this article from CSS-Tricks

div {
  display: inline;        /* Default of all elements, unless UA stylesheet overrides */
  display: inline-block;  /* Characteristics of block but aligns on a line */
  display: block;         /* UA stylesheet sets block-level behavior for tags like <div> and <section> */
  display: run-in;        /* Less common characteristic with limited support */
  display: none;          /* Conceal element */
}

A variety of display values exist to mimic table behaviors for non-table elements when needed. -css-tricks

div {
  display: table;
  display: table-cell;
  display: table-column;
  display: table-colgroup;
  display: table-header-group;
  display: table-row-group;
  display: table-footer-group;
  display: table-row;
  display: table-caption;
}

Answer №4

Include the following CSS snippet:

.navigation{
  overflow: hidden;
}

By adding this code to the container named .navigation, you will prevent it from collapsing. To visualize this, try adding a background or border to see the effect. If your menu items do not expand in a fly-out manner, using overflow: hidden should not cause any issues.

If there is a fly-out functionality present, consider inserting an additional div below both child elements (h1 and ul). Applying clear: both to that new div should yield the same outcome.

In scenarios where the container only consists of floating elements, collapsing may occur as a result.

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

Angular JS is having some issues with rendering the style correctly

How can I make input fields turn pink before values are entered and green after values are entered? Currently, the styling only applies to the first row. What could be causing this issue? <script src="https://ajax.googleapis.com/ajax/libs/angularjs ...

.comment {Visibility:hidden;} is only functioning on one specific page

What could be the reason behind this function only functioning on one page and not the others? The URL in question is . Specifically, the functionality is only active on the contact page while all other pages still display a comment section. ...

Focus on a specific div element while bypassing its parent

Can I directly target the element with .div-3 class, even if its parent does not have an id or class? <div class="wrapper"> <div> <div> <div class="div-3"> </div> </div> </div> </div> I am aware ...

Troubleshooting Symfony2 and Assetic: Why are my CSS-linked images not loading?

I've encountered an issue with my CSS sprite sheet that my CSS file seems unable to locate the image. Despite following the provided solution here, I'm still facing the same problem. The directory structure of my bundle is as follows: src/ v ...

Passing form values from JavaScript to a JSP page - a comprehensive guide

I am working on a sample HTML page that includes inline JavaScript for calculating geocodes and retrieving latitude and longitude values. My question is, how can I submit all the form values along with the latitude and longitude returned from the showlocat ...

What could be causing the issue with HTML not being printed upon button click in ReactJS?

My goal is to display the word "Hello" on the screen when the add button is clicked. However, I am encountering an issue where it is not showing up. Any insights or solutions would be greatly appreciated! import React, { Component } from 'react'; ...

Python: parsing comments in a cascading style sheet document

I am currently working on extracting the first comment block from a CSS file. Specifically, I am looking for comments that follow this pattern: /* author : name uri : link etc */ and I want to exclude any other comments present in the file, such as: /* ...

adjust the dimensions of the clickable icon's background

Is there a way to expand the pressable area of a close icon without altering its size? For example, if the icon is 19X19 pixels, can the pressable area be increased to 39X39 pixels? The concern lies with the div element containing the close button and the ...

What is the best way to make a hyperlink in HTML open in the same page or window, without relying on the <iframe> tag or JavaScript?

I have two html files and one css file. My question is how to open the link monday.html on the same page or window in my content section. For example, when someone clicks on the Monday navigation button, I want the result to show up in the content section ...

Guidance on marking a menu item as selected when choosing its sub-item

I've been tasked with creating a menu and I want to ensure that the menu item stays selected when its subchild is selected. Below is a snippet from my CSS file: #wrapper { width:100%; height:500px; } h2 { color:#787878; } // more ...

Is there a way to retrieve the left offset of a floating element even when it is positioned outside the viewport?

My current situation involves creating several panels that are stacked side by side within a main container. Each panel takes up 100% of the viewport width and height. I want to be able to horizontally scroll to each panel when clicking on their respective ...

Customizing the Twitter bootstrap navigation in Wordpress

I've been working on theming my navigation menu using Twitter Bootstrap. I want it to resemble the demo example of a fixed top banner, which can be seen here: http://getbootstrap.com/examples/navbar-fixed-top/ Although I have managed to get it to wor ...

Signal for a complete 360 degree rotation of an image

Looking to enhance my 360 image rotator with an indicator that fades out after the first image. I added this function to the end of my 360.js: (function(){ if( $('#first').css('visibility','hidden')) { $('#rotat ...

Troubleshooting: Custom icons not displaying in Next.js application

Every time I attempt to use icons that I have loaded into my source code, I keep getting this default icon displayed: I'm uncertain about what exactly is causing this issue. Here is the layout of my file tree for reference: When attempting to utiliz ...

What are the steps to designing an overlay using CSS?

Looking to generate a div with a custom size and then place something on top of it. How can I accurately position and resize the overlay to match the underlying div using CSS? ...

Exploring ReactJS: Combining JSON data with HTML elements

Recently, I started learning ReactJS. As I'm iterating through JSON data, I realized I need to incorporate some links into it. This is how my JSON data is structured: "text": ["For more information on the functionalities of the platform and services ...

Column-oriented and Slim-fit packaging

I have been working on designing a layout with a fixed height that will display multiple columns of specified size to accommodate flowing text. While I have successfully implemented this, I am facing an issue where the enclosing div does not adjust its siz ...

The resizing issue of Textarea during transitions

Whenever I add the transition property to a textarea, it automatically affects the resizing function of the textarea. Is it possible to disable the transition only when resizing the textarea, but not for the textarea itself? <textarea name="" id="" cla ...

Stop the iframe video when the modal is closed

I'm currently developing a website that incorporates the code showcased in this tutorial to launch a modal window featuring an iframe for playing YouTube or Vimeo videos. The issue arises when, as mentioned in the comments on the tutorial page, there ...

Showing an image on a blurred background-image at the top of the webpage

Is it possible to overlay an image on top of another image, with the top image appearing blurred in the background? .background-image { background-image: url('https://images.pexels.com/photos/366968/pexels-photo-366968.jpeg?w=1153&h=750&a ...