Having trouble aligning my two divs next to each other

I'm still relatively new to HTML and understanding everything well, except for how the layout works. I want the text on the left and the image on the right, but for some reason, the float left and right aren't working.

Here is the code I've been using:

HTML:

  <!DOCTYPE html>
     <html>
     <head>

    <link rel = "stylesheet" type = "text/css" href = "glitch.css">
    <title> Data Bending </title>
    </head>
    <body>
    <header class = "horse" > Glitch Horse </header>
    <div class = "textContainer">

      <p>
        Horses' anatomy enables them to make use of speed to escape predators
        and they have a well-developed sense of balance and a strong fight-or-flight
        response. Related to this need to flee from predators in the wild is an unusual
        trait: horses are able to sleep both standing up and lying down. Female horses,
        called mares, carry their young for approximately 11 months, and a young horse,
        called a foal, can stand and run shortly following birth. Most domesticated horses
        begin training under saddle or in harness between the ages of two and four. They
        reach full adult development by age five and have an average lifespan of between 25-30 years.

        </p>

        <div class = "imageContainer">
          <img src="city.png" style = "width: 300px; height:250px" >
        </div>

        </div>
              <input  type = "button" name = "button1" value = " View Glitch Horse">

      <div class = "padding"></div>


      <div class = "padding">  </div>

      <div>

      </body>

      </html>

CSS:

.horse{
  background-color: #f3f3f3;
    padding-top: 10px;
    width: 900px;
    text-align: center;

}

.textContainer{

  float:left;
  width : 75%;
  height: 120px;

}

.imageContainer{

  float:right;
  width:20%;
}

p{

  background-color: #fff;
  width: 900px;
  height: 100px;
  float left:

}

.padding{

    padding-top: 10px;
    padding-bottom: 10px ;

}
   `

Answer №1

To ensure proper alignment, set both elements to float either left or right with a maximum width of 100%.

.left, .right {
  position:relative;
  width:20%;
  height:100px;
  background-color:red;
  float:left;
}

.right {
  background-color:blue;
  width:80%;
}
<div class="parent">
  <div class="left"></div>
  <div class="right"></div>
</div>

Answer №2

Avoid using float on both div elements, instead try the following CSS:

 .pictureWrapper{
   float:left;
   width:30%;
  }

Answer №3

Give this a shot:

.container{width: 100%; float: left;}

.car{
  background-color: #b3b3b3;
    padding-top: 15px;
    max-width: 800px;
    width: 100%;
    text-align: center;

}

.textArea{

  float:left;
  width : 70%;
  height: 130px;

}

.photoArea{

  float:right;
  width:30%;
}

p{

  background-color: #fff;
  width: 800px;
  height: 90px;
  float left:

}

.space{

    padding-top: 15px;
    padding-bottom: 15px ;

}
<!DOCTYPE html>
     <html>
  <head>
    <link rel = "stylesheet" type = "text/css" href = "car.css">
    <title> Car Show </title>
    </head>
    <body>
    <div class="container">
    <header class = "car" > Classic Cars </header>
    <div class = "textArea">
      <p>
        Vintage cars have unique designs and powerful engines, making them highly
        sought after by collectors. They represent a piece of automotive history
        that continues to captivate enthusiasts all over the world. Restoring and
        maintaining a classic car is a labor of love, requiring attention to detail
        and dedication. Whether it's a sleek sports car or a rugged muscle car,
        each vehicle has its own story to tell.</p>
        <input  type = "button" name = "button1" value = " See Classic Cars">
    </div>
    <div class = "photoArea">
      <img src="vintage-car.png" style = "width: 250px; height:200px" >
    </div>
</div>
    </body>
</html>

Answer №4

The issue at hand arises due to the presence of the following code snippets.

p{
  width: 900px;
}

Additionally, there is a conflict with the following code:

<img src="city.png" style = "width: 300px; height:250px" >

While the floats are functioning correctly, these particular lines of code are conflicting with the parent element's width specifications, resulting in the inability of the textContainer and imgContainer divs to be displayed side by side.

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

What is the best way to position one element on the far left and the other element on the far right within the same container?

I am facing an issue with aligning text and icons in a container. I want the text to align far left, and the icons to align far right. Also, the icons should be horizontally oriented according to the length of the text. I have already separated the text an ...

Is there a method to use media queries to dynamically switch JavaScript files based on the user's device?

I've been working on optimizing the mobile layout of my website, and while I have successfully implemented a media query with a different stylesheet for mobile devices, I'm struggling to determine if it's feasible to also load a separate Jav ...

Move cursor over the element with a specified attribute

Is it possible to apply a hover effect to an active element without using the disabled attribute? I attempted the code below, but unfortunately, it did not produce the desired hover effect. input.btn:not([disabled]):hover ...

The Modelform is throwing an error because it cannot iterate over a boolean object

Looking for some help with saving a boolean field in a Django model form. I keep encountering a type error 'bool' object is not iterable and I'm struggling to understand why. I initially thought that Django handled the fact that boolean valu ...

Swap out a web link for an embedded iframe

Currently working on updating internal links: <div class="activityinstance"> <a href="http://website.com/hvp/view.php?id=515512">activity</a> </div> The goal is to convert them into: <div class="activityinstance"> ...

Is it possible to apply a consistent character width to all glyphs, even the most obscure Unicode symbols, using CSS?

Is it possible to style a single character within a span element to have a specific width and height of 1em? Ideally, I would like the character to be stretched if it is not already fixed-width. This styling must support any character, including rare Unic ...

Ways to achieve text transparency with CSS without converting it to an image

Is there a way to have my navigation bar indicate the selected page by changing the text color and adding a black background, while making only the text inside the current page nav transparent? For example, you can see the effect here: sammarchant.co.uk/n ...

Determine the amount of unused vertical space within a block of text

Having applied CSS to a span element: font-height = 120px; height = 120px; line-height = 120px; The text inside the span does not completely fill the height of 120px. Is there a method to determine the offset of the text from the top and bottom boundar ...

Tips for locating and substituting a string in Javascript

Is there a way to locate a particular word within a string and substitute it using JavaScript? For instance Here is a lengthy sentence I want to locate the word "sentence" and modify it to "phrase", resulting in: Here is a lengthy phrase ...

Utilize Java to launch the HTML file in a web browser

I have a specialized need for my HTML files to be displayed on a platform that is browser-free. The solution that immediately comes to mind for me in this scenario is utilizing applet. Therefore, I am interested in having my HTML file (including CSS and JS ...

Troubleshooting: JSColor Is Failing to Function Properly in Volusion's

Recently, I've encountered some issues with the JSColor plugin () on the Volusion e-commerce platform. Despite having successfully used it before, getting it to load correctly seems to be a challenge. My current task involves working on a test produc ...

Extract Content from an Element Using Selenium in Python

How do I extract the text from an element using Selenium? Element Content: <p class="sc-168cvuh-1 cNxwvb"> <svg aria-hidden="true" focusable="false" data-prefix="fas" ...

Having issues with downloading the app from the website

Currently I am diving into the world of android development and I have encountered an issue while following a tutorial. The tutorial demonstrates how to download a web page and print it into logs using the AsyncTask class. However, the problem I am facing ...

Creating a nested div with a consistent image background

I have set an image background on the first div. Now, I am looking to create a div (illustrated here with an orange background) and another box placed "above" it for comments (referred to as the "comments box"). The issue I am facing is that I do not want ...

Is there a way to have a span update its color upon clicking a link?

I have 4 Spans and I'm looking to create an interactive feature where clicking a link in a span changes the color of that span. Additionally, when another link in a different span is clicked, the color of that span changes while reverting the previous ...

Filtering elements upon loading

I have successfully implemented data filtering on my website using data-filter. While everything is working great, I am interested in displaying items with specific attributes upon loading. For instance, I want to load only green objects by default inste ...

What is the best way to create a fixed sidebar in Bootstrap?

Is there a way to create a sticky sidebar similar to cpmta.org using Bootstrap? I'm struggling to figure it out... ...

What is the best way to implement a robust PHP Advanced Search feature using a straightforward HTML form combined with PHP and MySQL?

I'm looking to enhance the search functionality on my website by enabling users to search using multiple criteria. Below is the HTML code for the search form: <form id="form_search" action="search.php" method="post"> <input class="sea ...

Float Property for Aligning DIVs

Is there a way to properly align divs like the ones shown below? I tried setting a float left for both div1 and div2, but div3 ended up floating to the top right. How can I avoid this issue? <div1> <div2> How hot is it? ...

What is the process of defining a driver in Python in order to interact with a website

I have a task in my app that requires using Selenium to complete various functions. I am familiar with defining a driver and opening a webpage, but I need assistance regarding setting up the driver for an open net tab. Can anyone provide guidance on this ...