Challenges of positioning two div elements next to each other without adhering to the width of the container

Apologies for my limited knowledge of CSS and HTML; I am relatively new to this.

I have gone through various questions and tutorials on how to align two divs side by side, but I seem to be facing difficulties in controlling the width of the divs in my code.

Here is the URL of the test site for reference.

Below is the HTML code:


/* HTML code goes here */

And here is the corresponding CSS:


/* CSS code goes here */

The two divs that I am trying to align are named highlights and ads. They are enclosed within a wrapper div called maincontent, located below another div named menu_area. Within the maincontent div, there is also a div for social buttons on the right, labeled socialgels.

In the demo page, highlights and ads are aligned well after adjusting their widths. However, for consistency purposes, I need the site to maintain a fixed width of 983px. Despite setting the widths of highlights to 662px and ads to 321px, with highlights floating left and ads floating right, they do not align correctly as expected. Sometimes, the ads div positions itself below the highlights div or shifts to the far right unexpectedly.

View the page with specified widths for both divs here.

In summary, I urgently need to set these specific widths to ensure the layout remains intact when additional content is added below them. Your assistance is greatly appreciated. Thank you.

Answer №1

The issue lies in forgetting to account for padding. When setting the width of the highlight div to 662px, you need to remember to include the additional 17px for padding, resulting in a total width of 679px.

To address this problem, there are two possible solutions:

One option is to subtract the padding and/or margin from your intended width.

662px - 17px = 645px

321px - 17px = 304px

Alternatively, you can apply box-sizing: border-box to both the highlight and ads divs. By doing so, when you set the width to 662px, the padding will be included within the specified width.

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

Is there a way to set the image result retrieved from the API call as the background image for my app?

In my current project, the user is able to input a city and the background of the page changes to display an image of that specific city. This image is obtained through an API call to Pixabay API, with the result stored in a variable named "background". Ho ...

What could be causing the Contact anchor element to not function properly?

I've been working on creating a dropdown menu for my API, but I'm having trouble getting the anchor links to function properly. I even attempted changing the "a" element to an onclick call with javascript:void(0) and added a function to open Gmai ...

Implementing multiple button functionalities and conditional statements in PHP

Despite my limited experience in PHP, I am working on a page with a basic setup that includes 4 buttons to send email reports regarding printer issues in our print labs to our service desk email. Although I have successfully coded the email sending functio ...

Is there a way to adjust the image dimensions when clicked and then revert it to its original size using JavaScript?

Is there a way to make an image in an HTML file change size by 50% and then toggle back to its normal size on a second click using JavaScript? I've attempted to do it similar to the onmouseover function, but it doesn't seem to be working. Any sug ...

How can CSS files be shared among multiple projects within Visual Studio 2012?

I am working on a Visual Studio Project Solution that includes 3 separate projects. To streamline the process and avoid duplication, I aim to have a shared CSS file for all 3 projects since they all follow the same CSS rules. Despite trying the Add As Lin ...

Looking to authenticate a CSS span class within an XML document using Oxygen Program?

This is in connection with my initial query. The XML does not recognize the CSS span class. <l rend="indent"> <span class="face_dropcap_ ">C</span><hi rend="initial_roman">E</hi>stoit alors que le prefent des <span class= ...

My website keeps crashing because the "wheel" event is being constantly triggered

import React, { useEffect, useState } from "react"; import "./Skill.css"; import { Fade } from "react-reveal"; function Skill({ name, color }) { const [style, setStyle] = useState({ borderBottom: `4px solid ${color}` }); ...

Transforming a sequence of characters into a multidimensional array

I have a nested for loop that generates an empty string value representing a multidimensional array. After the loops finish, the output looks like this: "[[0,0,0,0],[0,0,0,0]]" Now, I want to insert this into a multidimensional array within my code. How ...

Problem with Bootstrap 3 navbar on mobile devices - not tappable or responsive

After years of using Bootstrap, I've come across a new issue with my implementation of a Bootstrap 3 Nav. While testing on a desktop browser with device emulation, the nav collapses and functions properly. However, when tapping on the header on an ac ...

After a refresh, jQuery's mousenter event is not functioning

I am currently working on a fun project called Etch-a-Sketch for The Odin Project, and I have a jquery grid that can be redrawn with the click of a button. Initially, the grid allows the user to choose the size and then draw on it using .mouseenter. Howev ...

Toggle the display of slide numbers in RMarkdown xaringan / reveal.js presentations

In preparation for a workshop, I am creating HTML slides using the xaringan package in R, which is based on remark.js. These slides are made with RMarkdown, and while this approach has been effective, I have encountered a need to disable slide numbers on s ...

Concerns with textbox placement while scrolling with an absolute position style

I have an ASP:textbox on a page with various controls and divs, where I am setting the style to position:absolute on the "onkeyup" event. The height of the textbox increases dynamically based on the characters entered, but the issue is that the textbox alw ...

Is it possible to integrate ngx Pagination with Bootstrap?

Is it possible to integrate Bootstrap's pagination with ngx Pagination? I'm interested in using the functionality of ngx Pagination but styling it with the CSS from Bootstrap. ...

What is the user-agent string for the Safari home screen?

Is there a unique user-agent string specifically for Safari on IOS that identifies it as being in "Home screen" or "app mode"? I've observed a bug on IOS8 where the browser window appears incorrectly, with the time and battery information overlapping ...

Trouble with CSS styling for focused input fields

Attempting to style a form by Patriot with CSS, I encountered an issue. input#card-month:focus ~ label.card-label.label-cardExpiryYear { font-size: 20px !important; /* This works fine */ } input#card-month:focus ~ label.card-label.label-cardExpiryMont ...

How can CSS be leveraged to generate a fluid and adaptable grid layout for icons?

Please note that using a script is not an option for me My goal is to include a set of 12 100X100 icons at the bottom of emails sent to customers. These icons should be displayed with 100% width and evenly spaced, like this: X X X X X X X X X X X X If t ...

How to stop parent event propagation in jQuery

I am facing a frustrating issue with my code. Every time I toggle a checkbox, the parent element's click event also triggers. I have experimented with various solutions like: event.stopImmediatePropagation(); event.stopPropagation(); event.preventD ...

Enhancing File Control Validation Feedback with Bootstrap 4-alpha6

I have successfully integrated a new custom file control in my forms and I am pleased with how it looks. Has anyone discovered a way to apply validation feedback (such as success, warning, danger states with icon) to this custom file control? (I wouldn&ap ...

What is the best way to input a value into the search bar within an Iframe?

I've created a basic website using node and a pug page where I inserted an iframe from wikipedia.com. My goal is to have the search input in the Wikipedia iframe populated with a value based on a button click within the same pug page. Is this feasible ...

Mixing without Collections

After initially posting this question yesterday, I realized that I needed to clean up my code before proceeding. However, for my assignment, I am required to create a JavaScript quiz where the questions and answer choices are shuffled every time a user ret ...