What is the best way to position divs side by side?

I'm having trouble aligning these divs properly. I want them to look like this:

However, they either overlap each other (with .title taking up the full width of the container) or stack on top of each other. Any suggestions?

.wrapper{
    display: table;
    float: left;
    width: 1000px;
    height: 200px;
}
.pic{
    float: left;
    width: 100%;
    height: 20%;
}
.title{
    width: 100%;
    height: 20%;
}
.content{
    width: 100%;
    height: 20%;
}
.footer{
    width: 100%;
    height: 20%;
}

HTML:

<div class="wrapper">
    <div class="pic"><img src="..."></div>
    <div class="title"><p>title</p></div>
    <div class="content"><p>lorem ipsum</p></div>
    <div class="footer"></div>
</div>

Check out the JS FIDDLE here: http://jsfiddle.net/mmb84836/

Answer №1

Following the Recommended Approach:

To organize your layout, place an image in one box and the remaining three boxes on the right within another box using "float:left or **display:inline-block**.

Below is the example code for achieving this structure:

HTML

<div class="wrapper">
    <div class="leftBox">
        <div class="pic">pic</div>
    </div>
    <div class="rightBox">
        <div class="title">title</div>
        <div class="content">content</div>
        <div class="footer">footer</div>
    </div>
</div>

CSS

div {
    border:1px solid #000;
}
.wrapper {
    display: block; /*Default Property - You Can Remove Also*/
    width: 1000px;
    height: 200px;
}
.leftBox {
    float:left;
    width :20%;
    height:100%
}
.rightBox {
    width :79.5%;
    float:left;
     height:100%
}
.pic {
    width: 100%;
    height: 100%;
}
.title {
    width: 100%;
    height: 20%;
}
.content {
    width: 100%;
    height: 20%;
}
.footer {
    width: 100%;
    height: 20%;
}

View the Example Fiddle: http://jsfiddle.net/7xLyc3q1/

Answer №2

There are numerous responses provided here, yet none truly shed light on the actual situation at hand. When utilizing the float property, it is crucial to grasp the fact that floated elements are essentially removed from the box model and possess a width and height of near zero in relation to other elements. To address this issue, you can employ a workaround by specifying overflow:hidden within the parent element, preventing floated elements from "collapsing".

Take a look at this illustrative example. Observe how the title, content, and footer have a width:100%, filling only the available space allotted for them -- as one would likely anticipate. Also note that floating these elements to the right was unnecessary... they occupy the remaining space naturally.

Answer №3

To optimize the layout, try applying float: right to classes .title, .content, and .footer.

You may also want to explore utilizing frameworks like Foundation or Twitter Bootstrap. These frameworks offer grid systems to ensure that the divs adjust seamlessly to various screen sizes.

Answer №4

<div class="container">
<div class="image">image</div>
<div class="details">det1</div>
<div class="details">det2</div>
<div class="details">det3</div>
</div>

.container { width:100px; height:200px; }
.image { float:left; width:29%; height:100%; margin-right:1%; background-color:red; }
.details { float:left; width:70%; height:32%; margin-bottom:0.5%; background-color:green; }

Check out the code on jsfiddle http://jsfiddle.net/a23b45c6/

Answer №5

If you have a specific width for the image, here's one method you can use. I've assumed the image will be 200px wide in this example.

Give this CSS a try:

.wrapper{
    width: 600px;
    height: 200px;
    padding-left: 200px;
    border: 1px dashed gray;
}
.pic{
    float: left;
    width: 190px;
    margin-left: -200px;
    border: 1px dashed blue;
}
.pic img {
    display: block;
}
.title{
    width: auto;
    height: 20%;
    border: 1px dotted blue;
}
.content{
    width: auto;
    height: 20%;
    border: 1px dotted blue;
}
.footer{
    width: auto;
    height: 20%;
    border: 1px dotted blue;
}

The idea is to create space for the image by adding a 200px left padding to the .wrapper.

This padding will push .title, .content, and .footer 200px from the edge of the wrapper.

For .pic, set the width to 200px (or smaller) and apply a -200px left margin to move it into the padded area.

Lastly, ensure the correct width for .wrapper at 600px. The total width of .wrapper should end up at 800px (600px width + 200px left padding).

Check out the demo here: http://jsfiddle.net/audetwebdesign/mgg1stmc/

The key advantage of this method is that you won't need any additional wrapping elements. (Unlike when using floats which require extra wrappers.)

Answer №6

If you want to achieve the same result using just CSS and without altering your HTML structure, there is a simpler way:

See it in action http://codepen.io/examplelink/

Here's all you need:

.container {
    display: flex;
    justify-content: space-evenly;
}
.image {
    width: 20%;
}
.text {
    width: 80%;
}

Answer №7

This code can be utilized to suit your specific design requirements.

View Live Demo

HTML Markup:

<div class="container">
<div class="image"><img src="..."/></div>
<div class="title"><p>Title Here</p></div>
<div class="description"><p>Description Here</p></div>
<div class="footer-section"><p>Footer Section</p></div>
</div>

CSS Styling:

.container{
        position: relative;
        float: left;
        width: 1000px;
        height: 200px;
        border: 1px solid #000000;
    }
    .image{
        float: left;
        width: 300px;
        height: 200px;
        background-color: red;
        position: relative;
    }
    .title{
        width: 650px;
        height: 60px;
        background-color: green;
        position: relative;
        left: 350px;
        top:-16px;
    }
    .description{
        width: 650px;
        height: 60px;
        background-color: blue;
        position: relative;
        left: 350px;
        top: -22px;
    }
    .footer-section{
        width: 650px;
        height: 60px;
        background-color: gold;
        position: relative;
        left: 350px;
        top: -28px;
    }

Output:

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

Steps for inserting an image onto a blank page

Struggling with HTML and javascript - need help with displaying an image on a new page: Creating a new page and want to show an image on it: thepage= window.open('', '', 'height=700,width=800,left=100,top=100,resizable=yes,scroll ...

Generate a PHP array using data from a single column fetched from a MySQL database

As a beginner in PHP, MySQL, and HTML, I have a question regarding PHP arrays. I constructed a MySQL query that combines information from 2 tables in PHP. This query retrieves details about a single product with multiple JIGs. $sql = "SELECT product.prod ...

What is the method for including line numbers alongside a highlighted code section in highligh.js?

I want to ensure that the text rows are aligned perfectly with the numbers in the yellow vertical bar, without causing any issues with the mouse pointer as it moves across the rows. Adjusting margin or padding also affects the position of the mouse pointer ...

Automatically scroll to the top of a pop-up div that is scrollable with the help of jQuery

One of the first things to note is that there is a popup div containing a form. Below is the structure of the div: <div class="addnewrule" id="add_message_0"> <form method="post" action="" id="frmadd"> <input type="hidden" n ...

Using Javascript to trigger a setTimeout function after the user's mouse leaves

There is a div that pops up when the user's mouse exits the webpage, containing a survey pertaining to my website. I want to avoid prompting users to take the survey if they move their cursor out of the page within the first few minutes of browsing. ...

Page title missing from browser tab

I don't come from a design background, so the issue I'm experiencing is quite peculiar. Below is a snippet of code from MVC4 - <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-C ...

Achieve harmony in the If statement by accommodating one condition with multiple values

Currently, I have code written to exclude breadcrumbs from certain Prestashop 1.7 pages such as the index and order-confirmation pages. However, I am looking for a more efficient way to write this code as I plan to exempt breadcrumbs from additional web pa ...

What is the best way to modify foundation's small, medium, and large breakpoint values?

I am currently customizing a Drupal theme child that incorporates the Foundation framework from its parent theme. The predefined breakpoints in the Foundation classes and attributes do not perfectly align with the design of my website. Can anyone provide ...

The percentage of occurrences for each result with the specific id

In my database, I have a table that looks like this: Currently, in my PHP code, I am performing a SUM operation like this: function calculate_percentage() { $sql = "SELECT (sum(distance))/(25000)*(100) as totaldistance1 FROM axle WHERE train_id = ...

The loading feature of jQuery UI Autocomplete - .ui-autocomplete-loading is ingenious

When fetching the XML file for the search box, I would like to apply this css. It currently takes around 3 seconds to load the file. In the autocomplete.js file, I found these two functions: _search: function( value ) { this.term = this.element ...

modify the designation of a particular element's group

My goal is to create a webpage that features multiple tables with data. To prevent the page from getting too long, I want users to have the ability to collapse these tables by clicking on the header. I intend to use a + icon to signify expandable content a ...

Tips for implementing a hover transition effect in the navigation menu

I have a specific query regarding a particular issue that I am unsure how to address but would like to implement on my website. 1) My requirement is that when the parent li element has a child, I want the navigation to remain in a hovered state with the s ...

Positioning the final div at the bottom of a flexbox container

Situation : I'm struggling to align the last div, card-vat-fee, at the bottom of the container while creating a pricing comparison table. The issue arises because some tiers have longer lists than others, causing misalignment with the bottom of the ...

Customize your header and footer for printing to display on each page

Does anyone know how to use the window.print function to print a div content with custom header and footer that will show on every page? I have been able to add a header and footer, but they only appear at the top and bottom of the print document. How can ...

How to center align your application in the desktop using Ionic framework

Query: How can I ensure that my ionic mobile application renders in the center of the screen when viewed on a desktop browser? Attempt 1: I experimented with using a wrapper div with a fixed width, such as max-width:732px; and margin: 0 auto;, however Ion ...

Symfony: The Database Query Button with a Pop-up Feature

I am looking to create a button that will automatically search my database for all users with a "secouriste" attribute set and display their first name, last name, and phone number in a popup. After conducting research, here is what I have gathered: In m ...

How to use an if statement in Django when a button is clicked

I am trying to implement a functionality on my website where the user can click a button, and every time it is clicked the value of i should increase by 1. Currently, the code I have doesn't seem to work as expected. # HTML file snippet <form met ...

What is the best way to transfer files (html, js, css, and resources) while utilizing socket.io?

I am currently in the process of developing a web application that involves the server updating the HTML content on the browser page at specific time intervals, essentially creating an HTML slideshow. My project directory structure is as follows: project ...

Navigating a Multi-Page Website with Sleek Scrolling

I've been searching for a solution everywhere but haven't had any luck. I'm trying to implement a smooth scroll effect that works seamlessly across multiple pages. For instance, the smooth scroll effect is present on the homepage with naviga ...

Create a navigation bar for the homepage with a transparent background, while utilizing a black background for all other views on the website. Utilize

As a CSS novice, I've encountered an issue with my Bourbon Refills horizontal navigation bar in my application. The problem is that on the homepage, the navbar is semi-transparent and shows the background image, but I want it to be a different color o ...