Preventing elements from moving downwards in CSS

Looking for a simple solution here - how can I prevent elements from moving to the next row and keep them within the same div, side by side?

 .navcontainer {width: 1300px; margin-left: auto; margin-right: auto; height: 130px; background-color:red;}
 .grid1{width: 33.33%; height: 130px;}
 .grid2{width: 33.33%; margin-left: 33.33%; height: 130px; background-color: blue;}
 .grid3{width: 33.33%; margin-left: 66.66%; height: 130px;  background-color: green;}

View this screenshot for clarity

Answer №1

Using the float property is necessary.

.navcontainer {width: 1300px; margin-left: auto; margin-right: auto; height: 130px; background-color:red;}
.grid1{width: 33.33%; height: 130px; float:left;}
.grid2{width: 33.33%; float:left; height: 130px; background-color: blue;}
.grid3{width: 33.33%; float:left; height: 130px; background-color: green;}

Alternatively, you can use display:inline-block;

.navcontainer {width: 1300px; margin-left: auto; margin-right: auto; height: 130px; background-color:red;}
.grid1{width: 33.33%; height: 130px; display: inline-block;}
.grid2{width: 33.33%; display: inline-block; height: 130px; background-color: blue;}
.grid3{width: 33.33%; display: inline-block; height: 130px; background-color: green;}

Answer №2

To arrange the boxes side by side in a single row, apply the float: right style to both of them.

Alternatively, you can position the boxes at the leftmost side using position: relative / absolute; and setting left: 0px;.

I trust this solution will be beneficial for your layout design.

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

Does setting white-space:nowrap enlarge the div?

My CSS code for div .name sets the width to 75% to truncate text if it doesn't fit. However, resizing the window causes the text to remain the same size and my entire website structure falls apart. This is the HTML code snippet: <tr> <t ...

Is the display of the 'div' not working when hovering over Bootstrap navigation items?

I am attempting to display the div (#list) when hovering over the navbar item #categories, but for some reason it is not working. None of the items are displaying the list as intended. I have tried using #main #div #this:hover + #list {} but still no succ ...

Removing border of the top and bottom of jspdf pages

In my project, I am utilizing a combination of html2canvas, canvg, and jspdf to convert HTML content (which includes SVG graphs) into canvases for the purpose of creating a PDF file. To address some page-break issues, I have resorted to creating multiple c ...

The viewport width in NextJS does not extend across the entire screen on mobile devices

I'm currently tackling a challenge with my NextJS Website project. It's the first time this issue has arisen for me. Typically, I set the body width to 100% or 100vw and everything works smoothly. However, upon switching to a mobile device, I not ...

Is Javascript the new CSS:hover killer?

Hello, I'm currently working on implementing the Product Categories menu on this specific page: Currently, when the page loads, the CSS hover effect works correctly to display a rollover effect. However, I attempted to use JavaScript to set the backg ...

Is there a way to access just the concealed text within an element?

Is there a way to create a JavaScript function that can specifically extract hidden text from an element? Are there any existing libraries with this capability, and if so, how efficient are they? In order for an element to be considered visible ac ...

Identifying mobile devices with exceptionally high resolutions

I'm currently working on optimizing my website for different devices, with a focus on responsiveness. While I understand the concept of media queries, I've noticed that high-resolution screens like the Samsung Galaxy Note 4 tend to display my web ...

Unable to shift to the side while in full flexibility mode

I ran into an issue with my code, shown in the image below. I am trying to position 1 on the left and 2 on the right, but something seems off. Can you spot the problem? <div className="mt-5 mx-5 py-3 shadow bg-white"> &l ...

What is the best way to ensure that text and SVG elements are properly aligned?

I am facing an issue where the text is not aligned with the SVG icons in my project. Here is an example of the code causing the problem: <span class="cardSpan"> <svg style="width:24px;height:24px" viewBox="0 0 24 24"> <path fi ...

Issue with navigation links on HTML website not functioning as expected

I would like the dropdown menu items to be clickable. For example: Menu item: Services Sub items: - branding } These already have working links - marketing } However, when I try to replace '#' with a link for Services, it d ...

"Enhance the visual appeal of your Vue.js application by incorporating a stylish background image

Currently, I am utilizing Vue.js in a component where I need to set a background-image and wrap all content within it. My progress so far is outlined below: <script> export default { name: "AppHero", data(){ return{ image: { bac ...

The font size in the Android Chrome browser appears different when switched between portrait and landscape modes

I have uploaded a screenshot here, but I am puzzled as to why the font size appears differently on the Android browser in portrait and landscape modes. $(document).ready(function(e) { var x=$(".main").css("font-size"); alert(x) }); .main{ font-size: ...

Customize the delete icon margin styles in Material-UI Chip component

Currently, I have implemented the V4 MUI Chip component with a small size and outlined variant, complete with a specified deleteIcon. However, I am encountering difficulties when attempting to override the margin-right property of the deleteIcon due to MUI ...

Dynamic column group in Datatable - toggle visibility based on user selection

In my application, I am utilizing Jquery datatable with a table that includes the following columns: Name, Office, A1, B1, Diff1, A2, B2, Diff2, A3, B3, Diff3, A4, B4, Diff4 Additionally, there is a select box containing the options: 1. All 2. Diff1 3. D ...

Is it possible to have a variable number of dynamic CSS Grid columns with the option of including a header or footer

Implementing a dynamic number of columns with CSS Grid is a breeze. .grid { grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); } // These .cells are beautifully aligned <div class="grid"> <div class="cell"></div> ...

Executing a Javascript function post AJAX page loading

I'm not a coding expert, so I hope my question is still clear. Essentially, I have an index.php page with filters (by project, year, month) that send variables to filterData.php after clicking submit. These variables are then used in SQL statements t ...

Why aren't the JavaScript stars shining once I've selected one?

I recently set up a star rating feature on my website by following the tutorial "Creating an Ajaxified Star Rating System in Rails 3". Although I managed to get everything working on the index page, I noticed that the stars do not remain lit up after bein ...

Price Adjuster Tracker

Recently, I coded a small JavaScript program with the purpose of: incrementing or decrementing the quantity of an item by 1 adjusting the price of an item based on the initial price However, my excitement turned to disappointment when I encountered these ...

Determining the width of an element in terms of percentage

My issue involves input elements with widths set as percentages in CSS under the common class 'display-class' For example: input.test1 { width: 60% } In JavaScript, I am attempting to wrap a div around these input fields with the same width ...

What could be causing the <a> tag inside the <nav> element to lose a few pixels in width?

I'm currently working on crafting a responsive menu for a website, but I've encountered an issue: The a tag (red color) seems to lose a few pixels when setting the width of the div (blue color). To spot this discrepancy, you'll need to acce ...