The image persists between two floating elements, unable to be cleared

I have been trying to replicate the tutorial provided by this individual here. I timestamped it- and noticed that there is a text block that floats to the side. However, when I make one float left and the other float right, the image div continues to go in the middle and I cannot figure out why. view image here

*Also, if I place the image next to a textbox with a float, how can I ensure that they are the same height?

body {
  background-color: aquamarine;
  font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}

.navbar {
  list-style: none;
  color: red;
}

.container {
  width: 60%;
  margin: auto;
}

.box-1 {
  margin-bottom: 1%;
  padding: 1%;
  background-color: darkgray;
  color: white;
}

.box-2 {
  float: left;
  padding: 1%;
  width: 30%;
  color: black;
  background: white;
  border-style: none;
}

.sidebox {
  float: right;
  background-color: aliceblue;
  width: 60%;
}

img {
  float: none;
  margin: 10%;
  display: block;
  width: 300px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <title>help</title>
  <link href="Untitled-1.css" rel="stylesheet" />
  <meta charset="utf-8" />
  <meta name="description" content="Web Development" />
  <meta name="keywords" content="HTML and CSS" />
  <meta name="author" content="grace" />
</head>

<body>
  <header>
    <nav class="navbar">
      <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="study.html">Study</a></li>
        <li><a href="animation.html">Animation</a></li>
      </ul>
    </nav>
  </header>
  <div class="container">
    <div class="box-1">
      <h1>Achievements</h1>
      <p>Grace has completed these 4 achievements:</p>
      <ol>
        <li>Two decades of existence</li>
        <li>3+ jobs</li>
        <li>Graduated high school</li>
        <li>Failed hopes and dreams</li>
      </ol>
    </div>
    <div class="box-2">
      <h1>Hobbies</h1>
      <ul>
        <li>Rollerskating</li>
        <li>Crying</li>
        <li>Buying many books</li>
        <li>Reading some books</li>
        <li>Drawing and ctrl z</li>
        <li>Video Games</li>
      </ul>
    </div>
    <div class="sidebox">
      <p>Lorem ipsum ebfasuigfuie eufsiefh adfhuseifhuei efhnsihfeihf </p>
      <p>test</p>
      <p>text</p>
      <p>image</p>
    </div>
    <div class="astro">
      <img src="https://res.cloudinary.com/dvifiaohc/image/upload/v1651048486/fly_gpbrfe.png">
    </div>
  </div>
</body>

</html>

Answer №1

If you want your image elements to float and align to the right side of the page, you need to adjust your CSS.

img{
    float:none;
    margin:10%;
    display:block;
    width:300px;
}

To achieve the desired effect, change the margin value to a smaller number and set float: right instead:

img{
    float: right;
    margin:5px;
    display:block;
    width:300px;
}

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

Is it feasible to create a CSS selector that targets elements that are not the first child?

Is it feasible to use CSS to select all child elements except for the first child? ...

Creating ID cards using HTML, PHP, and JavaScript

Currently working on a web application that involves creating, updating, and deleting user data. One of the requirements is to print a PVC ID card with the information of the newly created user, similar to generating a report. I'm seeking a convenien ...

Tips on inline password field placement for a better user interface experience?

While creating a registration form, I ran into a user interface problem. There are two specific changes I'd like to make: The label for Confirm Password should be inline on the same line. The password input should have an eye icon embedded within it ...

Presentation: Troubleshooting Ineffective CSS3 Transitions

I have created a basic slideshow that can accommodate multiple images. Clicking on an image should move to the next slide, and once the last image is clicked, it should loop back to the first slide. The functionality of transitioning between slides seems ...

Shifting the navigation bar to the right to accommodate a logo on the left side of the bar

I'm currently working on adjusting the position of the navigation bar to the right in order to accommodate a logo on the left side of the navigation text. Despite trying to use float-right, it doesn't seem to be working as expected. Below is the ...

Styling and Div Container management with Bootstrap in ASP.NET Code Behind

I am currently in the process of developing an ASP.NET application that requires the dynamic creation of "TextBox" and "DropdownList" controls. Here is the method I have been using to create "TextBox" controls: private void CreateTextBox(string id, string ...

Arrange images/divs horizontally beside each other without specifying the width of the container div

Check out my code snippet <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <style type="text/css"> #image_container { position:absolute; top:100px; left:0px; he ...

Toggle the visibility of the search Div using Angular UI

When using angular UI buttons, users can select search criteria by choosing between Patient, ID, or Date. If Patient or ID is selected, the searchByText div will be shown. If Date is selected, the searchByText will be hidden and the SearchBydateRange wil ...

The novice image slideshow script in JavaScript is causing all images to disappear and generating errors

Trying to create a simple image slider that pulls information from various sources. CSS and HTML are set up properly, but adding the JavaScript logic causes all images to disappear. The console displays an error saying "Uncaught TypeError: Cannot read prop ...

Issue with Bootstrap Table Column Width Not Adjusting as Expected

Table columns are not adjusting width properly with the table-responsive class. I want to specify the width of the column containing Well Type to be 200px. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="st ...

Is it possible to trick the desktop browser into utilizing mobile-designed media queries?

While I've come across numerous solutions on Stack Overflow for displaying a desktop version on mobile devices, my inquiry is centered around the idea of forcing a browser to show a mobile version of a website on a desktop computer. Is there a way to ...

The bottom of the page refuses to remain anchored

I've exhausted all possible solutions and my footer just refuses to stay at the bottom of the page. Working with Opencart has made it challenging for me to pinpoint the root cause, leaving me utterly perplexed. The issue persists on pages with minim ...

Update to the viewport meta tag in iOS 7

Anyone experiencing problems with the viewport tag after updating to iOS7? I've noticed a white margin on the right side of some sites. Adjusting the initial scale to 0.1 fixed it for iPhone but made it tiny on iPad 3, which is expected due to the low ...

Display a pop-up window using a Bootstrap modal at random intervals

I have created a website that features a picture with various pop-up windows designed using bootstrap modal pop-ups. The image contains 34 clickable leaves, each triggering a different pop-up window when clicked. My goal is for a random pop-up window out ...

Disorganized jQuery Animation

Looking for some help with an animation I have on my website. The animation is supposed to smoothly move in and out when the mouse cursor hovers over it, but as you can see, it's a bit messy. This project involves HTML, CSS, and jQuery <!DOCTYPE ...

Ionic - Landscape Mode Causing Alignment Distortion

I'm currently working on designing a grid-based image display for an Ionic Mobile App. One of the main challenges I'm facing is adjusting the heights of the images based on the screen size. I want to set a maximum height for both portrait and lan ...

Auto-selecting the first item in a CSS menu when the switcher is tapped on mobile devices

Struggling with a responsive CSS menu on my website, . The switcher on mobile seems to automatically click the first item in the menu (Home) as I open it, redirecting me instantly. When setting the link to "#" everything is fine, but then I lose the home l ...

Tailwind (version 2) Experiencing Unresponsive Width Changes

I am currently working on fitting a series of elements into a panel, organizing them in rows of three. Here is how I have it set up: https://i.sstatic.net/DSd7v.png These elements are supposed to adjust their size based on the screen width. Even though t ...

Alter global table data attributes through device detection with Javascript

Initially, I assumed that setting the global font-size of all td's would be a simple task. However, I am having trouble finding a solution. In my existing stylesheet, I have defined the font-size for all td's. td { font-size: 20px; ...

Semi-fixed positioning with flex layout

I am encountering an issue with angular's flex-layout. I have implemented two divs with fxFlex, each containing a mat-card. My goal is to keep the right mat-card vertically fixed while maintaining responsive behavior when resizing the window. Does any ...