tips for organizing the <div> element horizontally within a design

I have reviewed some similar questions that have been posted and attempted to apply their solutions to my code. However, I am still encountering difficulties. Below is the code that I need help with. I am looking to organize the main content along with three smaller contents beneath it, as well as position the footer at the bottom of the page.

<!DOCTYPE html>
<html>
<head>
    <title> CSS Attempt </title>
     <style type="text/css">
#container {width: 960px; margin: 0 auto; background:#ffffff; border:1px solid black; height:500px;}
#header { background:#ffffff; height:60px;}
#navigation {width:960px;background:#555555;text-align:left; height:35px;}
#sidebar {width:170px; height: 300px; text-align: left; border-right: 5px solid grey; list-style: none; margin-top: 20px; margin-left: 20px; float:left;}
#content {width:750px; height:150px; background-color: #333333; margin-left:190px; float: right; margin-top:0px   }
#display {width: 220px;height:200px;background-color: #cccccc }
#center {width: 220px;height:200px; border:2px solid black }
#footer {width:960px;text-align:right; height:35px; float: right;}
 </style>
</head>


<body>
<div id="container">
        <div id="header">
        <p><img src="logo.png" alt = "Image not available"></p>
        </div>

    <div id="navigation">
    <p>Home</p>
    </div>
    <div id="sidebar">
    <p> Left Content </p>
    </div>
    <div id = "content">
    <p> Main Content </p>
    <div id = "display">
    <p> Content 1</p>
    </div>
    <div id = "center">
    <p> Central Content </p>
    </div>
    <div id = "display">
     <p> Content 2</p>
    </div>
    </div>
    <div id = "footer">
     <p> copyright </p>
    </div>

</div>  
</body>
</html> 

Answer №1

Utilizing CSS3 flex box allows for easy alignment of boxes next to each other:

<!DOCTYPE html>
<html>
<head>
    <title> CSS Attempt </title>
     <style type="text/css">
#container {width: 960px; margin: 0 auto; background:#ffffff; border:1px solid black; height:500px;}
#header { background:#ffffff; height:60px;}
#navigation {width:960px;background:#555555;text-align:left; height:35px;}
#sidebar {width:170px; height: 300px; text-align: left; border-right: 5px solid grey; list-style: none; margin-top: 20px; margin-left: 20px; float:left;}
#content {width:750px; height:200px; background-color: #333333;margin: 20px 0 0 20px; float: right; }
#display {width: 220px;height:200px;background-color: #cccccc }
#center {width: 220px;height:200px; border:2px solid black }
#footer {width:960px;text-align:right; height:35px; float: right;}
.leftAndMain {display: flex;}
.mainInnerContents {display: flex;}
 </style>
</head>


<body>
<div id="container">
        <div id="header">
        <p><img src="logo.png" alt = "Image not available"></p>
        </div>

    <div id="navigation">
    <p>Home</p>
    </div>
<div class="leftAndMain">
    <div id="sidebar">
    <p> Left Content </p>
    </div>

    <div id = "content">
    <p> Main Content </p>
    <div class="mainInnerContents">
        <div id = "display">
        <p> Content 1</p>
        </div>
        <div id = "center">
        <p> Central Content </p>
        </div>
        <div id = "display">
         <p> Content 2</p>
        </div>
    </div>

    </div>
</div>
    <div id = "footer">
     <p> copyright </p>
    </div>

</div>  
</body>
</html> 

Only two additional divs have been added with classes class="leftAndMain" and class="mainInnerContents".

Additionally, two lines have been included in the <style> tag:

.leftAndMain {display: flex;}
.mainInnerContents {display: flex;}

Minor adjustments were made to the #content section as well.

Answer №2

There seems to be a glitch in the HTML code structure you provided. It is crucial to ensure that each element has a unique ID, but in your case, there are two elements with the same ID - display. I have rectified this by renaming the second instance to display2. However, it might be more advisable to utilize classes for styling purposes instead of relying solely on IDs.

In the following snippet, the three blocks are set to display as table cells, positioning them next to each other. The footer is cleared to ensure it appears below the left floated sidebar. Additionally, the float property of the content itself has been removed to allow it to display adjacent to the sidebar.

/* Display the three block as cells, so they are next to each other. 
   You may specify a width for each of them. */
#display,
#center,
#display2 {
  display: table-cell;
}
#container {
  /* Removed float here. Content is just adjacent to side bar. */
  width: 960px;
  margin: 0 auto;
  background: #ffffff;
  border: 1px solid black;
  height: 500px;
}
#header {
  background: #ffffff;
  height: 60px;
}
#navigation {
  width: 960px;
  background: #555555;
  text-align: left;
  height: 35px;
}
#sidebar {
  width: 170px;
  height: 300px;
  text-align: left;
  border-right: 5px solid grey;
  list-style: none;
  margin-top: 20px;
  margin-left: 20px;
  float: left;
}
#content {
  height: 150px;
  background-color: #333333;
  margin-left: 190px;
  margin-top: 0px
}
#display {
  width: 220px;
  height: 200px;
  background-color: #cccccc
}
#center {
  width: 220px;
  height: 200px;
  border: 2px solid black
}
#footer {
  width: 960px;
  text-align: right;
  height: 35px;
  /* Clear to force the footer to the bottom. Add a border to show that this is working */
  border: 1px solid red;
  clear: both;
}
<!DOCTYPE html>
<html>

<head>
  <title>CSS Attempt</title>
</head>


<body>
  <div id="container">
    <div id="header">
      <p>
        <img src="logo.png" alt="Image not available">
      </p>
    </div>

    <div id="navigation">
      <p>Home</p>
    </div>
    <div id="sidebar">
      <p>Left Content</p>
    </div>
    <div id="content">
      <p>Main Content</p>
      <div id="display">
        <p>Content 1</p>
      </div>
      <div id="center">
        <p>Central Content</p>
      </div>
      <div id="display2">
        <p>Content 2</p>
      </div>
    </div>
    <div id="footer">
      <p>copyright</p>
    </div>

  </div>
</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

Stopping all animations with JQuery animate()

I have a question about stopping multiple animations. Here's some pseudocode to illustrate my situation: CSS #div1 { position: absolute; background-image: url("gfx/cat.jpg"); width: 60px; height: 70px; background-size: 50%; b ...

The display of website content across various screens

I'm relatively new to creating websites using scripts, CSS, etc. But I feel like I'm getting the hang of it pretty well... Now I've reached a point where I want my site to look good on different screen resolutions. Currently, I have somethin ...

Tips for achieving a seamless appearance with box-shadow on the left and right sides of various elements

.row { height: 300px; width: 300px; margin: 0 auto; box-shadow: 0 -20px 0px 0px white, 0 20px 0px 0px white, 12px 0 30px -4px rgba(0, 0, 0, 0.3), -12px 0 30px -4px rgba(0, 0, 0, 0.3); } <div class="row"></div> <div class="row">< ...

Identify Bootstrap modal to modify destination of keypress input

I am working on a piece of code that detects keypress events and focuses input towards a searchbar with the id "search". $( document ).keypress(function() { $("#search").focus(); In addition, I have a bootstrap modal dialog containing input fields ...

Identify the significance within an array and employ the filter function to conceal the selected elements

I'm in the process of filtering a list of results. To do this, I have set up a ul list to display the results and checkboxes for selecting filter options. Each item in the ul list has associated data attributes. When a checkbox with value="4711" is c ...

Having trouble with a basic jQuery selector not functioning as expected

Having trouble parsing this HTML code: <tr id="a"> <td class="classA"> <span class="classB">Toronto</span> </td> <td class="classC"> <span class="classD">Winnipeg</span> </ ...

``There seems to be an issue with implementing the SlideDown feature in JavaScript

I am having an issue with a code where the expected behavior is to slide down a div when a person hovers over text, but it's not working. Can someone please review and identify the cause of this problem? <script type="text/javascript" src="/js/jqu ...

Storing Selenium WebElement in memory for extended periods to retrieve its details: A guide in C#

I am using Selenium and C# to monitor a website for any new items that may appear. The website contains a list of <div class="myClass"> elements displayed in a table format with multiple sub-divs. Periodically, I scan all the displayed divs to check ...

Two divs positioned on the left side and one div positioned on the right side

Hey there, I am attempting to align 2 divs on the left side with one div on the right. #div1 #div2 #div1 #div2 #div3 #div2 #div3 #div3 The challenge is to have #div2 positioned between #div1 and #div3 when the browser window is resized smaller. Currentl ...

What could be causing the malfunction of the .setAttribute() and .removeAttribute functions in my JavaScript program?

Below is a flashcard game I have developed: Once a user enters the correct answer in the text box, "Correct!" is expected to be displayed in bold green font. The CSS attributes are then supposed to be removed shortly after. Here is the HTML code for the i ...

Sometimes the downloaded xlsx file may become corrupted

Currently, I am working on developing a project using Angular4 with Typescript. One of the tasks involved creating a blob utilizing the XLSX-populate library. Below is an example showing the code snippet for generating a valid xlsx object: var url = wind ...

Embedding a CSS class within the primary class selector

Can someone help me with this HTML challenge? <div class="span3"> <div class="abc">code for first div</div> <div class="abc">code for second div</div> </div> I'm trying to hide the first "abc" division within th ...

Boosting Your SCSS (SASS) Productivity

I'm curious if there are any ways to make the code below more efficient or dynamic. I've already achieved the desired result, but I'm interested in optimizing it further. The Script Any suggestions would be highly appreciated, Cheers, ...

Accumulation of style from various directives in AngularJS on a single element

Currently, I am working on developing a component framework and find myself in need of a method to apply styles from multiple directives to an element in a way that they can accumulate. The priority of the styles should be determined by the directive creat ...

Email confirmation section

I'm in the process of setting up a subscription newsletter page, and everything seems to be working correctly except for the part where I need users to enter their email address twice. The second email field is meant for confirmation purposes, but I&a ...

Combine going to an anchor, performing an AJAX request, and opening a jQuery-UI accordion tab all within a

My goal is to have the user click on the hyperlink below, which will (1) anchor to #suggestions, (2) navigate to the url suggestions.php?appid=70&commentid=25961, and (3) open the jquery-ui accordion #suggestions. However, I am facing an issue where c ...

Can someone provide assistance on how to add an icon to a button?

Help needed with adding icons to buttons! I am new to coding and struggling with adding icons to each button. If any classes need to be changed, please let me know. I am currently learning HTML and CSS and could use some guidance. Thank you! Example: ...

What is the best way to dynamically update or display unique CSS styles when a service is invoked or provides a response in AngularJS using JavaScript

Seeking to display a unique CSS style on my HTML FORM prior to invoking a service in my code and then reverting back after receiving the response. I have implemented the use of ng-class to dynamically add the class when the boolean activeload1 is set to tr ...

Position tables on the right side of adjacent tables

There are two tables named id="BFCategories" and "BMICategories". Additionally, there are two other tables with id="BFTable" and "BMITable" BFCategories should come after BFTable, while BMICategories should follow BMITable. How can I a ...

Resolving the issue: "Unable to destructure the 'Title' property of 'fac' as it is undefined" in React JS

I'm facing an issue with react-bootstrap mapping in my component whereby I encounter the following error: Cannot destructure property 'Title' of 'fac' as it is undefined. It seems like there's an error when trying to de ...