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

Design interactive elements such as buttons and input fields inspired by the layout of Google websites

Does anyone know how to achieve the sleek, lightweight buttons and text boxes seen on Google's web pages like Google Search, Google Plus, and Google Play? I am currently using JSP and CSS, but I am open to incorporating jQuery if necessary. Any help w ...

How can you make the browser window scroll horizontally when a div is clicked using jQuery?

I have an image of an arrow inside a div. The div is positioned fixed in the bottom right corner of an extremely wide page. Is there a way to utilize jQuery to scroll the window 600px to the right each time the arrow is clicked? Also, can I detect when th ...

Is there a way to create an X shape by rotating two divs when I click on the container div?

I currently have this setup: code Below is the HTML code: <div id="box"> <div id="line1" class="line"></div> <div id="line2" class="line"></div> <div id="line3" class="line"></div> </div> My go ...

Concealed Sub Menus Within a Vertical Navigation Bar

I've successfully created a Vertical Menu with hidden submenus, but I'm struggling to make the submenu appear on hover. Any suggestions on how to achieve this? Also, I'd like to align the text all the way to the left and remove the bullets f ...

Ways to customize the default home icon

My current theme is Redmond, but I would like to change the color of the home icon in the breadcrumb. To achieve this, I have included the hot-sneaks theme in the pom.xml file: <dependency> <groupId>org.primefaces.themes</groupId&g ...

Discovering the method for retrieving JavaScript output in Selenium

Whenever I need to run JavaScript code, the following script has been proven to work effectively: from selenium import webdriver driver=webdriver.Firefox() driver.get("https:example.com") driver.execute_script('isLogin()') However, when I atte ...

How to use CSS to insert a line break after the fourth child in a

At this moment, the example displays an unordered list containing 8 list items. I am curious if there is a way to add a line break after the 4th li item using only CSS (no HTML or JavaScript). Perhaps something like: ul li:nth-child(4n):after { conte ...

Is it possible to utilize a separate, standalone stylesheet for media queries?

My understanding of media queries evolved when I discovered them yesterday during my research on creating mobile-optimized pages. I learned that a media query refers to a stylesheet that complements and supersedes the current one, which differed from what ...

Iterating over a table and selecting a specific button on the rows that meet a specific condition

I'm having a bit of trouble and would really appreciate your help. I'm attempting to write a script that will scan through this table and delete any row that contains the word "active". The script needs to check every row in the table. The code ...

Trouble experienced with the window.open() function on Safari

When using Safari, it can sometimes block the opening of a new tab through the window.open() function during an ajax call. To bypass this blocking, we must first call window.open() to open a new tab before making the ajax call. Refer to this Stack Overflow ...

From HTML to Python CGI to communication with a Serial port

I am having an issue running a Python CGI script to send data to a serial port. Whenever I try to open and set the serial port to a variable, the HTML server crashes. Below is the code snippet that receives a color (red, blue, green) from the HTML page: ...

Tips for relocating a popup window''s position

A situation has arisen in my application where a popup window is opened with the following code: function newPopup(url, windowName) { window.open(url,windowName,'height=768,width=1366,left=10,top=10,titlebar=no,toolbar=no,menubar=no,location=no,d ...

Is there a quick way to import all available font weights from a Google font?

I am looking to experiment with importing all weights and styles of a Google font, such as Roboto or Roboto Slab, in a CSS file. Is there a more efficient way to do this rather than listing out each individual style in the @import? ...

What is the best way to resize my hamburger menu and achieve a flawlessly circular border for it?

I've been struggling to reduce the size of my hamburger menu (both height and width) for some time now. I managed to make it a bit smaller, but I can't seem to figure out how to shrink it further. Additionally, I'm having trouble creating a ...

Make the button appearance change when being clicked different from when being hovered over

I'm trying to create a button that changes background color when hovered over, but maintains the button color without a background color when clicked. This is what my current code looks like: .windowButton:hover { background-color:#1a82b8; } .win ...

The JavaScript function is not being activated by clicking on Selenium in Chrome

Take a look at the HTML snippet below: <table class="table-main "> <thead> <tbody> <!----> <tr class="" ng-if="mapCtrl.isAdded" style=""> <td/> <td> <td> <t ...

Issue with CSS positioning: Struggling to place images outside a div with floating properties

Here is what I need to design along with the code I have written, HTML: <div className='relative mt-64 mb-40 md:h-56 h-36 w-full gradient z-10'> <img className='bg-transparent w-20 h-28 absolute md:top-40 md:lef ...

I am looking to edit specific lines of HTML code on a website in order to automatically select a checkbox. Is this possible?

I'm currently attempting to automate the process of increasing bids on a particular website every two hours. However, the site requires users to manually select checkboxes next to the lots they want to raise their offers on. The issue is that the che ...

Stop scrolling on the body of the webpage

I am struggling to find a solution to completely disable scrolling on the HTML body. I have experimented with different options, but none seem to work as desired: overflow: hidden; did not disable scrolling, only hid the scrollbar. position: fixed; w ...

Is there a way to conceal the horizontal divider in the login form of the ionic HTML mobile user interface?

While designing this login form on the HTML ionic mobile UI, everything looks good except for one thing - I want to hide the horizontal line that appears between the "Forgot Password" label and the LOGIN button. Is there a way to do this? Login.html: < ...