Can elements be floated within an absolutely positioned div?

I've encountered a small issue with my code and need some guidance. Is it possible to float elements to the left and right inside an absolute div?

Do I need to make each element within this div absolute as well, positioning it relative to another element?

Alternatively, is there a way to achieve this using floating or any other methods?

Here is the CSS code snippet:

#story{
position: absolute;
margin-top:180px;
margin-left:10px;
width: 450px;
}

#story .img-left{
    float:left;
}
#story p{
    float:left;
}

#sidebar{
    position: absolute;
    margin-top:180px;
    margin-left:550px;
    background-color: white;
    border-style: solid;
    border-color:brown;
    border-width: 5px;
}

My index file currently looks like this:

The desired final result should look something like this:

I'm uncertain if achieving this layout without using the (position) property is possible, but I tend to rely on it as it fixes most of my issues.

Thank you for taking the time to read my query! Your help is greatly appreciated :)

Answer №1

To move one element to the left and another element to the right, you can implement the following technique:

You can create an inner div that has the same dimensions as the outer div by setting its height and width to 100%.

.abs-div {
  position: absolute;
  top: 0;
  left: 0;
  border: 1px solid green;
  width: 50%;
  height: 50%;
}

.inside-div {
  margin: 0 auto;
  display: block;
  width: 98%;
  height: 98%;
  border: 1px solid red;
}

.inside-div #right-elm {
  float: right;
}

.inside-div #left-elm {
  float: left;
}
<div class="abs-div">
  <div class="inside-div">
    <p id="right-elm">Right Element</p>
    <p id="left-elm">Left Element</p>
  </div>
</div>

Answer №2

You have the option to include an image within content and align it using float:right

.abs {
  position: absolute;
  top: 0;
  right: 0;
  border: 1px solid #999999;
  width: 40%;
}

.inside {
  margin: 0 auto;
  display: block;
  text-align:justify;
}

.inside .leftdiv {
  display:inline-block;
}

.inside .rightimg {
  float: right;
  width:145px;
}

.inside .rightimg img{  
  width:100%;
}
<div class="abs">
  <div class="inside">
    <div class="leftdiv"><div class="rightimg"><img src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" /></div>Only an entrepreneur can decode this? Below are some pieces in the form of my thoughts, try and see if you create a picture out of it. Charles Darwin once said, “It is not the strongest of the species that survives, nor the most intelligent, but the one most responsive to change."</div>
    
  </div>
</div>

Answer №3

Avoid using float:left with the p tag. Instead, use float:right in .img-left and remove it from the paragraph tag. Remember to keep the image tag inside the paragraph tag.

<p>paragraph contents<img src="" style="float:right" ></p>

Follow these instructions to achieve the desired result.

Answer №4

Absolutely, it is possible to float the element inside the absolute div.

<html>
    <head>
        <style>
            .content
            {
              position: absolute;
              top: 50%;
              left: 50%;
              max-width: 300px;
              width: 500px;
              padding: 100px 0px;
              transform: translate(-50%,-50%);
              border: 1px solid red;
            }
            .element
            {
             float: right;
            }
        </style>
    </head>
    <body>
        <section class="card">
          <div class="content">
            <p class="element">
               Element 
            </p>
          </div>
        </section>
    </body>
</html>

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

Prevent the occurrence of a fixed height problem associated with the Bootstrap Navbar (Mega Menu)

In my project, I have a Custom Mega Menu that dynamically creates code to display all Nav bar entries. Since changing the logic of Mega menu creation is difficult, I am looking for a CSS solution to avoid fixed height in bootstrap columns. My code structu ...

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

Toggle the visibility of radio buttons by displaying a hidden div class instead. When the div is clicked, automatically select

I'm attempting to make this functionality work differently. Instead of displaying the standard radio button, or replacing it with an image, I want to use a styled div as a button. When hovered over, the class should change, and when clicked, the class ...

You are unable to define a module within an NgModule since it is not included in the current Angular compilation

Something strange is happening here as I am encountering an error message stating 'Cannot declare 'FormsModule' in an NgModule as it's not a part of the current compilation' when trying to import 'FormsModule' and 'R ...

Python Selenium Timeout Exception Handling

Looking for a solution: def get_info(url): options = webdriver.ChromeOptions() options.headless = True chrome_browser = webdriver.Chrome('./chromedriver', chrome_options=options) chrome_browser.get(url) name = chrome_browser.f ...

Scrolling Horizontally with a <div> containing <a> and <img> elements

I currently have a table with three columns. The third column consists of multiple images that are clickable to perform an action, so I need this column to be horizontally scrollable. To achieve this, I inserted a <div> in the third column and added ...

The minimalist responsive menu created using Pure CSS is experiencing issues on the default browser in Android

In one of my projects, I implemented the Minimal Responsive Menu (Pure CSS) as the navigation menu. http://codepen.io/nickisix/pen/julqA Although the navigation menu works fine in popular browsers like Google Chrome, Mozilla Firefox, and Opera, there see ...

CSS failed to load on HTML page

Having trouble loading a CSS file. I've attempted: <link href="./css/style.css" rel="stylesheet" type="text/css"> And: <link href="css/style.css" rel="stylesheet" type="text/css"> And even tried without the "css" directory in the name ...

Issues with displaying images in IE 11 when using Flex

After testing this code, I found that it works perfectly in both Firefox and Chrome, displaying a row as expected. https://i.sstatic.net/29wgb.png However, when viewing the same row in IE 11, the image is not visible, and the width seems to be missing in ...

Convert your flexible designs into dynamic HTML with our Flex to HTML/A

Looking for a solution to transform Flex code into Html/Ajax code. Most of my Flex code includes datagrids, buttons, and text labels. ...

Conceal the scroll bar while the page preloader is active

Is there a way to hide the scroll bar while the preloader is loading on a webpage? I want to prevent users from scrolling until the preloader disappears. I have tried using CSS and setting the body overflow to hidden, but it's not working as expected. ...

Learn how to save an image from the uploads folder located in the same directory as your PHP program

After saving an image in the upload folder and storing its link in a SQL table, what steps should be taken to create a program that downloads the images from that folder? ...

HTML5 Embedding a redirect iframe for connection status validation [UPDATE]

I've created an offline HTML file and embedded an iframe within it that redirects to a site if there is an available internet connection. However, in the event of no internet connection, the iframe will redirect to an offline page. Could someone plea ...

Mobile devices are having issues with CSS rendering

I've encountered an issue with my CSS code. It seems to be working fine until the @max-width:767 media query, but then it stops working on the resolution immediately below it, which is @425. However, it starts working again on resolutions below that. ...

file_put_contents - store user-defined variables

When I execute this script, it successfully generates the html page as expected. However, I am encountering challenges in incorporating variables, such as the $_GET request. The content is enclosed in speech marks and sent to a new webpage on my site usin ...

Achieving vertical alignment of a flexbox that is stretched without relying on padding or margin

I recently implemented flexbox with align-items-stretch to ensure the borders extend to the top and bottom. However, I am now facing a challenge with vertically aligning the text within the flexbox. Due to restrictions on using margins and paddings, I cann ...

Interactive Click Actions on Rotated Divs

Does anyone know why, when clicking on the back side of a div that has been rotated with rotateY, the clicks pass through to the next div inside the body tag instead of registering on the intended div? And is there a way to fix this issue? You can view th ...

Display and conceal different sections using hyperlinks

Hey there, I'm back with another issue related to this code snippet on jsfiddle: https://jsfiddle.net/4qq6xnfr/9/. The problem I'm facing is that when I click on one of the 5 links in the first column, a second div appears with 3 links. Upon clic ...

Drag a dropdown menu to the bottom left corner and set it to full width on mobile using CSS

Check out this code snippet with CSS and HTML: CSS .flyout { position: absolute; top: 30px; right: 15px; background-color: #FFF; padding: 20px; border: 1px solid #eaeaea; z-index: 999; width: 400px; border-top: 3px solid #337AB7; disp ...

Customizing the color of buttons in MUI 4

Currently, I am utilizing mui V4 for my styling and attempting to incorporate an additional color for my buttons. I understand that it only accepts these predefined colors: expected one of ["default", "inherit", "primary", "secondary"]. To achieve this, I ...