What are the basics of floating elements and how can it be effectively utilized in CSS3?

I can't wrap my head around this property. First, let me share what I know about it. I'm focusing on the <p> element with id="amazing" for this discussion.

Let's set a width for it

#amazing{
   width: 200px
}

The outcome is:

Since paragraphs are block elements, other elements won't move up beside them as they have line breaks before and after.

Next, I float it by:

#amazing{
       width: 200px;
       float: right
 }

My understanding so far:

(1) The browser displays elements on the page from top to bottom
(2) When encountering a floated element, the browser positions it all the way to the right and removes it from the flow

Please correct me if I'm wrong at this point...
Moving on,

Using Float Property with CSS Dropdown Menus

Consider this code snippet from the web :
An unordered list with list items like this

<ul id="menu">
    <li>
        <a href="">Home</a>
    </li>
    <li>
        <a href="">About Us</a>

        <ul id="menu-about-us">
            <li>
                <a href="">The Team</a>
            </li>
            <li>
                <a href="">History</a>
            </li>
            <li>
                <a href="">Vision</a>
            </li>
        </ul>
    </li>
    ...

Now, if I do this...

ul {
    font-family: Arial, Verdana;
    font-size: 14px;
    margin: 0;
    padding: 0;
}

ul li {
    display: block;      
    position: relative;
    float: left;
}
li ul {
    display: none;
}

For ul li, why does setting display: block make them act as inline elements? And what purpose does the float property serve here?

ul li {
    display: block;      
    position: relative;
    float: left;
}

I'm quite baffled by this. Can someone clarify how float interacts with block and inline elements?

Answer №1

The outcome varies depending on the specific situation and what you aim to accomplish, Shirgill. I have prepared a quick comparison for you on JS Fiddle --> http://jsfiddle.net/Lt7cftyc/

You are correct. When you apply display:inline-block; to a list with the float attribute, it behaves similarly:

ul li {
display:inline-block;
}

will behave similarly to:

ul li {
display:block;
float:left;
}

however, there are some distinctions. For instance, you must always clear floated elements to avoid unexpected behavior:

clear:both;

In the example below, you can observe different scenarios with inline and floated menus. Feel free to experiment and observe how the behavior changes when you add margins or paddings to inline vs. floated elements. It may surprise you.

ul.inline li {
 display:inline-block;
}

ul {
    font-size:14px;
    font-family: Arial;
    border: 1px solid black;
    background: lightblue;
    margin: 20px 0;
}

ul li {
    padding: 0 5px;
}

ul.floated2 {
    height: 20px;    
}

ul.floated li, ul.floated2 li {
    display:block;
    float:left;
}

.clr {
clear:both;
}
<p>This is an example of an inline list</p>
<ul class="inline">
    <li>menu 1</li>
    <li>menu 2</li>
    <li>menu3</li>
</ul>
<br />
<p>This is an example of a floated list. Note the lack of background - first difference!</p>
<ul class="floated">
    <li>menu floated 1</li>
    <li>menu floated 2</li>
    <li>menu floated3</li>
</ul>

<br />
<p>This is an example of a floated list with visible background after setting height:20px;. </p>
<ul class="floated2">
    <li>menu floatedSecond Version 1</li>
    <li>menu floated 2</li>
    <li>menu floated3</li>
</ul>

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

Image floating next to a table

Trying to achieve something that I assumed would be straightforward, but CSS always has its surprises! The issue I'm facing is with an image floated to the left. Next to the image, I have a title, and below the title, but still alongside the image, I ...

Having trouble with clicking on an icon link within a list

Seeking assistance with creating a social media icon/link list for the first time on this platform. Any help is appreciated! <div class="social-links"> <ul> <li class="m-link"> <a href="&q ...

CSS properties for a div element resembling a button

I am attempting to add a popup window feature to my project in order to simulate a pop-up window where I can later incorporate content from other pages within my application. The code I currently have is as follows: html <div id="box"> <div ...

Troubleshooting navigation bar problems: dealing with overlapping and padding challenges in

I'm exploring how to create a hover effect with a bottom border that overlaps with the parent div (navbar) bottom border. I've applied margin:0px and padding:0px to the navbar container. I'm unsure how to make the hover bottom border effec ...

CSS Resizing the screen leads to misalignment of the footer layout

I am currently working on a page located at However, I have encountered an issue where the footer is not displayed properly when the screen size is changed. Below is the CSS code for the footer: #footer_1 { background: url("../images/bgfooter2.png") ...

Create a moving background gradient with styled-components

Currently, I am working on setting the background of the <Paper /> component using Material-UI version 1.0.0-beta.25 to display a gradient of colors. The colors are dynamically added by clicking the Add button and selecting one from the color picker. ...

Discover the steps to position the bootgrid pagination control above the table

Is there a way to customize the pagination control so that it appears at the top of the table, across its heading row, rather than at the bottom? It's likely a CSS solution... I attempted to make adjustments based on the advice in this unanswered qu ...

What steps can be taken to stop a list from exceeding the boundaries of its parent <div>?

Currently, I am dealing with a list (#this-list) that has an input box below it. Whenever a user submits something in the input box, it gets added to the list #this-list (all of this happens within react.js). The issue arises when the list grows too long a ...

Leveraging the power of CSS Target to emphasize the parent container

I utilize Ckeditor on my website for adding articles. These articles are typically sourced from Word documents and contain footnotes. In the latest Ckeditor build (7125), I've managed to link each article to its respective footnote using automatic anc ...

Is there a way to validate td content without considering spaces or new lines?

I am working with a table that has dynamic values in its td cells. Some td cells will be empty while others will have values. I want the empty td cells to have a red background, and the filled td cells to have a green background. I attempted to achieve thi ...

Issue in Chrome when using CSS clip property on nested div elements

Take a look at this fiddle Here is the HTML structure: <div class="parent"> <div class="child"></div> </div> This is the corresponding CSS code: .parent { position: absolute; width: 100px; height: 100px; background: re ...

Menu toggle vanishes suddenly

Whenever I click the toggle button, I notice that the menu (which I have set to appear on toggle) only shows for a brief moment before disappearing. As a beginner in bootstrap, I am sure I might have made some obvious mistakes. Any assistance would be gr ...

The design became distorted when the fonts were resized in every browser

Looking for assistance with a simple layout issue. When I scale the font in browsers by using CTRL and scrolling up (zooming), the layout does not behave as expected. I want #allWrapper to scale its dimensions with the body so that the white background ex ...

Attempting to change the primary color in Bootstrap is ineffective following a button click

I have been attempting to customize the primary color of Bootstrap 5 using SCSS. Everything works perfectly until I click a button that opens an external link. Below is the content of my SCSS file: $primary: #ae217aff; @import "../node_modules/boots ...

Difficulty positioning CSS, aligning flex-box grid with floating text

My attempt to include an aside section is causing alignment issues with the navigation menu and the aside itself. The problem seems to be related to using float:left for the 200x200 images. I want the layout to be well-positioned like the expected image. I ...

Selenium with Python: A class focused on link text

Currently, I am encountering a challenge while using Python and Selenium to scrape content from a specific webpage. The issue lies in the presence of multiple div-classes with identical names but distinct content. My requirement is to extract information f ...

Activate SwipeBox gallery and launch an external link

<li> <a rel="gallery-3" href="images/photos/photo7.jpg" title="Photo title" class="swipebox"> <img src="images/photos/photo7.jpg" alt="image"/> </a> </li& ...

Problem with text alignment in Internet Explorer 7

I'm facing an issue with the alignment of columns on my website. Each item is represented by an <a> tag within an <li> element, which is part of a larger <ul>. The topmost <li> in each list has a unique class compared to the lo ...

Troubleshooting problems with CSS menus and submenus, along with addressing browser pixel issues specific to Web

Find my custom css menu here: Custom CSS Menu On hovering over "about us", the "our clergy" sub-menu appears automatically. I need it to show only when hovering over "our clergy". This screenshot is from Firefox, but webkit browsers like Chrome show a sl ...

Conceal a Div Element on a Website

My website's home page features a CSS animation that slides two images with a high z-index off the screen to reveal the content below. I want this animation to only run the first time the page is accessed during a session, and not every time the user ...