Issue with content not properly aligning next to the vertical navigation bar

I've set my vertical navigation to be 10% width and my content to be 90%, but for some reason, I can't get them to align properly next to each other. I've experimented with float: left and display: inline-block, but so far nothing has worked. Any assistance would be greatly appreciated.

@import url(http://fonts.googleapis.com/css?family=Play);
/* CSS Reset */

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
.clearfix:before,
.clearfix:after {
    content: "";
    display: table;
}
.clearfix:after {
    clear: both;
}

/* --------- */
.mainHeader {
background: rgb(255,50,50);
padding: 20px;
}
.mainHeader h1 {
font-family: "Play";
color: rgb(255,255,255);
font-size: 24px;
}
.mainNavigation {
padding: 12px;
width: 10%;
height: 900px;
font-family: "Play";
font-size: 18px;
background: rgb(255,50,50);
float: left;
}
.mainNavigation ul {
padding: 0;
margin: 0;
list-style-type: none;
list-style: none;
}
.mainNavigation ul li {
width: 100%;
padding: 2px;
}
.mainNavigation ul li a {
text-decoration: none;
color: rgb(255,255,255);
padding: 12px;
width: 100%;
-webkit-box-sizing : border-box;
    -moz-box-sizing : border-box;
    box-sizing : border-box;
display: inline-block;
border: 1px solid rgb(255,100,100);
}
.mainNavigation ul li a:hover {
background: rgb(255,255,255);
color: rgb(0,0,0);
}
.mainContent {
padding: 8px;
display: inline-block;
width: 90%;
margin: 0 auto;
-webkit-box-sizing : border-box;
    -moz-box-sizing : border-box;
    box-sizing : border-box;
}
.article {
background: rgb(222,222,222);
padding: 8px;
margin: 0 auto;
border: 1px solid rgba(222,222,222,0.5);
width: 90%;
font-family: "Play";
}
<!doctype HTML>
<html>
  <head>
    <link href="css/reset.css" type="text/css" rel="stylesheet"/>
    <link href="css/base.css" type="text/css" rel="stylesheet"/>
    <title>temp</title>
  </head>
  <body>
    <div class="mainHeader">
      <h1>Jackatron</h1>
    </div>
    <div class="mainNavigation">
      <ul class="clearfix">
        <li><a href="index.php">Home</a></li>
        <li><a href="index.php">Profile</a></li>
      </ul>
    </div>
    <div class="mainContent">
      <div class="article">
        <h1>Hello</h1>
      </div>
    </div>
  </body>
</html>

Answer №1

The #primaryMenu section is styled with a padding of 12 pixels. If the box-sizing property is not set to border-box, the width of #mainNavigation will be calculated as 10% plus an additional 24 pixels (2 times 12 pixels), causing it to not align properly with the 90% width of the #mainContent section.

Setting 'box-sizing: border-box' ensures that the element's width includes the specified padding or border (please note that this does not apply to margin).

Many developers nowadays opt for globally setting box-sizing:border-box as it is a more intuitive approach. Here is a code snippet that can be placed at the beginning of the main CSS file:

*,
*:before,
*:after {
    box-sizing: border-box;
}

Answer №2

To resolve the problem, try adjusting the max-width property for your navigation and content sections instead of just using width:

.mainNavigation {
  max-width:10%;
}

.mainContent {
  max-width: 90%;
}

There are other potential solutions depending on how you want the layout to appear. For example, you could leave the width of .mainContent unset.

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

I am attempting to dynamically align the images of two articles. The exact heights of the containers are not specified. Can anyone provide guidance on how to achieve this?

My code snippets: <article class="featured"> <img src="http://www.placehold.it/300x100/ff0000"> <p> <!--Text--> </p> </article> <article class="sub-featured"> <img src="http://www.placeh ...

Arranging a flex item below another flex item within a flexbox container

Is there a way to use flexbox to ensure that the 2.5 element is placed below the 2 element instead of beside it on the same row within the flex container? Given the HTML provided, how can I achieve this layout using flexbox? I attempted to accomplish thi ...

Position the background in CSS according to the class name

There are a total of 8 icons, with 4 in blue and 4 in grey. Therefore, product 1 has both a blue icon and a grey icon. When the user has product 1, they will see the blue icon (with an added class on the container called .gotProduct), and when they don&ap ...

adjusting iframe dimensions for iPads

I am dealing with an HTML file that contains the following code snippet: <table height="100%" cellspacing="0" cellpadding="0" border="0" width="646" class="data border"> <tbody> <tr> <td valig ...

Adjusting animation placement when resizing the window

Is it possible to utilize jquery window.resize() to ensure that the positioning of the two donuts never interferes with the text in the middle of the home? I am unsure of how to connect the x and y values of the window size to adjust the top/left and botto ...

Unusual alterations to HTML and CSS to meet specific needs

Struggling with bringing my website ideas to life! My header contains a navigation bar, followed by a centered chat box within a fixed <section>. Want the left and right side of the chat box to be content areas that scroll in sync while the chat box ...

What is the best way to eliminate the background color from one span after selecting a different one?

I created a span element that changes its background color when clicked, but I want the background color to switch to the newly clicked span while removing it from the previously clicked one. Can someone help me achieve this? CSS list-sty ...

The TreeView control displays as a <div> element, which results in an unexpected line break

I am currently working on designing a layout that includes an ASP.NET TreeView control on the left-hand side. However, I am facing an issue where the TreeView renders as a div, causing a line break. I have tried using display:inline, but it doesn't se ...

displaying a pair of inputs next to each other

Is it possible to display two input fields side by side? I am using Angular's matInput forms, but struggling to position the second input next to the first. What I would like to achieve is to have "input1 , input2" on the same line. Here is my code: ...

Utilize flexbox to keep a paragraph element centered even when the text wraps, without directly centering the

Before this gets down-voted into oblivion, let me mention that I have thoroughly searched through numerous similar questions on SO. However, none of them provided a solution to my specific issue. I am using flexbox to align <p> tags both horizontall ...

Looking to add elements to a specific div dynamically using jQuery? Let's explore how to insert comments seamlessly

I would like to implement a comment system that adds entered comments to a specific div. Here's the code I have so far: <ul class="comments"> <li> <a class="commenter_name" href="/">Dushyanth Lion</a> ...

Navigation bar's active area does not span the full height

I'm facing some issues with the navigation bar on my responsive website. One issue is that the clickable area for the links does not extend to the full height of the nav bar, and I would like it to cover the entire height. Another problem arises whe ...

Unable to delete the margin of Material-ui TextField

Having trouble removing the padding around material-ui's TextField component when using styled-components and inline styling. Any solutions to this issue? Interestingly, the inline width property is functioning correctly, unlike the styled-component w ...

Adjust the height attribute of a DIV element within a webpage that contains a "footer"

I'm hoping to achieve a scrollable middle section without the bottom scrollbar, and I prefer a CSS solution over using JavaScript for this. On my website, I have a 150px high div filled with icons and images that needs to be fixed while allowing the r ...

Bring to life a dashed slanted line

After incorporating the code from this post on Stack Overflow about animating diagonal lines, I was pleasantly surprised with how well it worked. However, my next goal is to style the line in such a way that it appears dotted or dashed, as opposed to one ...

displaying a video within an iframe without occupying the entire screen

After successfully implementing a fullscreen video background using the "video" tag with a video hosted on my own server, I decided to switch it out for a Vimeo video. However, after making the necessary code and CSS changes, the video now appears like thi ...

The image within the element is being cropped despite having a higher z-index

On my page, I am dynamically generating a table using an API request. Each row in the table has an icon that, when hovered over by the user, displays an image associated with that item. However, I seem to have misunderstood how z-index functions, as I have ...

How to Safely Swap Background Images on a Website Using CSS without Affecting the Body

How can I create a background similar to this one, but using an image instead of the blue background? Take a look at I noticed that resizing the browser window doesn't affect the background. What steps should I take to achieve this effect? ...

Unable to apply border-radius to a specific HTML table

I am facing an issue where the border-radius is not being displayed on a specific HTML table. I am having difficulty in applying rounded corners to the entire table so that the table edges have a 30px curve. Here is an example of what I am looking for: I ...

Is there a way to easily duplicate this and add a navigation bar and logo to every page?

Why is it that when I copy and paste this code into any .html document, it only runs correctly on the resources tab? The logo is only showing up on the resources page, even though it's using the same css stylesheet and html structure. <div class=" ...