placing two divs side by side

Imagine having 2 separate divs

    width: 140%;
    padding: 13px;
    height: 20;
    float: left;

Is there a preferred method to position these divs next to each other?

Answer №1

Give this a shot:

<section>Hey</section>
<section>Earth</section>

section {
    border:1px solid #333;
    width: 250px;
    height:120px;
    margin: 15px;
    display: inline-block;
}

Answer №2

Imagine this scenario: if the page is 100%...

Can you picture how 2 divs totaling 280% are supposed to fit within a 100% page?

Furthermore, ensure that the height is specified as height: 20px;.

The recommended way to write this would be:

div {
  width: 50%;
  float: left;
  height: 20px;
  padding: 13px;
  box-sizing: border-box;
}

Alternatively, a more effective method could be:

div {
  width: 50%;
  display: inline-block.
  height: 20px;
  padding: 13px;
  box-sizing: border-box;
}

Answer №3

When setting the width of your divs to 140%, they may extend beyond the screen's boundaries. To ensure that they fit within 100% of the screen width, you should adjust the width accordingly. You can refer to this example where we set the width to 45%, taking padding into account so that they align correctly.

.box {
    width: 45%;
    padding: 13px;
    height: 20;
    background:blue;
    margin-right: 20px;
    float: left;
}

Answer №4

After carefully analyzing the scenario described in your inquiry, I have devised an optimal solution.

HTML

<div class="container">
    <div class="item"></div>
    <div class="item"></div>
</div>

CSS

*{
    box-sizing:border-box
}
.container{
    display:flex;
    width:280%;
}
.item {
    width:50%;
    flex:1;
    border:solid 2px red;
    padding: 13px;
    height: 20px;
}

By utilizing a flexbox layout with div elements, we can achieve the desired outcome effectively.

I set the wrapper div to 280% width and each box to 50%, resulting in 140% for each individual box to align with your requirements.

View the working demo

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

The issue of React hover style malfunctioning in conjunction with Radium and Material-UI

I am currently utilizing the Radium library for inline styling in my React project. I have encountered an issue where the styling does not apply properly to Material-UI components, specifically when hovering over a Paper element. The color should change ...

Adding a PHP file into an HTML page using JavaScript include

Recently, I was assigned to collaborate with a third-party vendor who displays movie times on their platform. We were given the opportunity to co-brand our website on their platform by creating a wrapper for our site. This wrapper would allow the movie tim ...

Vertical and right alignment of image within a div.container

I am trying to center the image with the unique ID of "image" to the right and vertically on the page. When using PHP, I generate my DIVs in the following way: echo "<div class=\"first\"> <div id=\"second\"><la ...

Create geometric designs using HTML and CSS

Looking to customize the positioning of an image inside a rectangle. The goal is to remove the icon border from a specific location and place it in the top left corner of the rectangle using css and html with bootstrap5. Currently, the image is not alignin ...

Issue with image position shifting due to Bootstrap 4 Modal opening and closing

In the code snippet below, a bs4 modal is opened through a button when hovered over. The problem arises when the modal is closed after being opened, causing the image to move unexpectedly. This issue can be resolved by hovering over the container again. T ...

When deploying a website with the default Hugo theme on Netlify, I encountered a strange issue where the webpage appeared blank. Sur

After coming across a question similar to mine about creating a static page using blogdown with the same Hugo theme as the main site on Stack Overflow, I still find myself struggling to understand the solution provided. As an absolute beginner, I am curren ...

Speeding up your responsive design with a single CSS file or multiple files is a crucial

There are numerous discussions on whether it's better to have many CSS files or just one, with the consensus being that using a single file reduces the number of HTTP requests. However, my question is a bit different. Personally, I tend to use two CS ...

Tips for integrating external JavaScript libraries and stylesheets into a widget

I am currently working on developing a custom Javascript widget that requires users to insert specific lines of code into their web pages. This code will then dynamically add an externally hosted javascript file, allowing me to inject HTML content onto the ...

What makes @font-face function on Safari but not on Firefox in Mac versions?

Can anyone help me troubleshoot a font issue on my website? I've added the following code to the CSS file, and it's working fine in Safari, but not in Firefox. I'm new to coding, so please bear with me if this seems like a basic question. I& ...

Can CSS styling be applied to lazy-loaded images using Gatsby and Sharp before they finish loading?

I am utilizing Sharp to implement lazy loading for images on my Gatsby website. Currently, the images appear blurry and only come into focus once they enter the screen. Is there a method to add CSS styles or filters to these blurry images while they are s ...

What is the best way to incorporate a top border image into the mainContent div within this current code structure?

I attempted to include 3 divs in order to apply a top and bottom border image to my mainContent div, but unfortunately I was unable to get it to align correctly. Is there a straightforward way to adjust this code to incorporate the blue border at the top a ...

I am struggling to implement the grid layout for my dashboard design in Tailwind CSS

Currently, I am in the process of developing a dashboard front-end utilizing Tailwind CSS. The grid structure has been established on the page, and various components have been integrated into it. However, there seems to be an issue with the uniformity of ...

Steps for showcasing stars (1-5) based on the data stored in the database

I recently completed a project where I created a user achievement page that displays their progress. Within the page, there is a table containing the user's level. The table structure looks like this: Users( id, username, password, sum_score, level) ...

What is the best way to display index.ejs when the input field is blank?

If the input field is left empty when I click the form button, I want to redirect to "/". After clicking the button, the desired URL should be: http://localhost:3000 Header.ejs <form action="/search" method="GET"> < ...

Struggling to make Tumblr Javascript function properly

I have integrated the following code snippet: <script type="text/javascript" src="http://underlineent.tumblr.com/api/read/json"> </script> Unfortunately, my Tumblr blog is not appearing on this site. Can anyone provide assistance? The provid ...

Wordpress easily adjusts post alignments for a seamless display

Can anyone help me figure out how to properly align posts in Wordpress to match the example image attached? I've tried using inline-block or float with CSS/HTML, but the posts are not aligning correctly. I'm unsure of what this method is called s ...

Dynamic columns not rendering correctly when using smart-table with ng-repeat

html <table st-table="ApplicationData.displayedCollection" class="table table-striped" st-safe-src="ApplicationData.source"> <thead> <tr ng-repeat="col in ApplicationData.columns"> <td st-sort="{{col.name}}"> ...

The email verification process is experiencing technical difficulties

Having trouble displaying error message when emails do not match I am relatively new to PHP and could use some assistance. The $email variable captures the user's email input, while the $VerifyEmail variable does the same for email verification purp ...

Issues with Collapse Feature on Bootstrap 3 Navbar

I need your help with a problem I'm facing. The navigation bar is not collapsing when I click the link in mobile view. Check out this video for more details: https://www.youtube.com/watch?v=2dBpcFMjqY0 ...

Display a background color behind an <img> element when the image's dimensions are smaller than the page

Currently, I am utilizing the following CSS for styling an image - check it out here img { border: 3px solid #ddd; margin-left: auto; display: block; margin-right: auto; background: red; } If you would like to view the output, click on this link - see it ...