0

So I'm having a little issue with my code and wanted to ask if I can float left and right elements inside an absolute div.

Should I make each element of this div absolute as well and position it to the relative element?

Or is there a way to get around this using floating or other methods?

CSS code:

#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 looks like that: Image 1

It should look like this:(final result): Image 2

I'm not sure if this can be done without using the (position) thing but I can't really avoid it since I find it fix most of my problems

Thank you for reading my question! and thanks in advance :)

Mark
  • 143,421
  • 24
  • 428
  • 436
Ali Zgheib
  • 128
  • 2
  • 11

4 Answers4

0

You should not use float:left with p tag Just make float:right in .img-left and remove it from paragraph tag.. And keep the image tag inside the paragraph tag

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

In order to get your desired result

Vijay
  • 13
  • 1
  • 8
  • https://ibb.co/bPoScA https://ibb.co/ekAvVV I understood what you are trying to do but i applied it and it wont work. the text is still under the image – Ali Zgheib Nov 23 '18 at 04:25
  • @Ali Zgheib Try writing image tag nested within paragraph tag bro.. IT will work.. First write the contents of the paragraph then add the image tag – Vijay Nov 23 '18 at 04:28
0

If you simply just want to make one element go left and one element go right, you can use the following:

You can achieve this by making an inner div which you can expand to be the same size as the outer div using height and width 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>
Ben Tegoni
  • 219
  • 1
  • 10
0

You can set image with content and apply to 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>
Hiren Vaghasiya
  • 5,454
  • 1
  • 11
  • 25
-1

Yes you can 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>
Ben Tegoni
  • 219
  • 1
  • 10