There seems to be a problem with displaying two div elements side by side using Html/CSS

Recently, I've delved into the world of html/css/javascript just for fun. I've encountered a little roadblock and I could use some help. In my html code, I'm trying to display two div elements on the same line but for some reason, it's not working.

The specific pairs of divs that I want to be on the same line and at the same height are the combinations of "button" and "text".

Below you can find the code snippet I'm working with:

html {
  background-color: #e6eeff;
  font-family: Verdana;
}
* {
  border: 1px solid black;
}
#bgCal {
  background-color: #004466;
  color: white;
  width: 500px;
  margin: 0px auto;
  border-radius: 10px;
  padding: 10px 10px 10px 10px;
}
#display {
  background-color: #d7d7c1;
  color: black;
  text-align: right;
  width: 400px;
  height: 100px;
  font-size: 30px;
  margin: 10px auto;
  line-height: 100px;
  border-radius: 5px;
}
#numberTot {
  background-color: #d7d7c1;
  vertical-align: middle;
  line-height: normal;
  margin-right: 5px;
}
.button {
  background-color: #0099e6;
  width: 150px;
  height: 60px;
  border-radius: 5px;
  margin: 10px 50px;
  display: inline-block;
  text-align: center;
  overflow: hidden;
}
.text {
  background-color: #004466;
  color: white;
  height: 60px;
  width: 180px;
  margin: 10px 50px 10px 0px;
  display: inline-block;
  text-align: center;
  font-size: 12px;
  overflow: hidden;
}
<div id="bgCal">
  <div id="display">
    <span id="numberTot"> 0 </span>
  </div>
  <button class="button" onclick="numberClick(1)">+</button>
  <div class="text">
    Every click increase of 1 the total number.
  </div>
  <br>
  <button class="button" onclick="buyStudent()">
    Buy Student
    <br>Cost: <span id="studentLUCost">10</span>
    <br>Students owned: <span id="studentLevel">0</span>
  </button>
  <div class="text">
    A student can click instead of yourself, but they are slower.
    <br>
    <span id="studentProd">0</span> num/sec
  </div>
</div>

Answer №1

According to Leo the lion in the comments, adding float:left and clear:both to your .button class will achieve the desired outcome.

To understand more, Clear is used to ensure floating elements do not sit alongside the specified element and would instead be pushed below, either right or left. For further insights, check out this related QA here.

html {
    background-color: #e6eeff;
    font-family: Verdana;
}

* {
    border: 1px solid black;
}

#bgCal {
    background-color: #004466;
    color: white;
    width: 500px;
    margin: 0px auto;
    border-radius: 10px;
    padding: 10px 10px 10px 10px;
}

#display {
    background-color: #d7d7c1;
    color: black;
    text-align: right;
    width: 400px;
    height: 100px;
    font-size: 30px;
    margin: 10px auto;
    line-height: 100px;
    border-radius: 5px;
}

#numberTot {
    background-color: #d7d7c1;
    vertical-align: middle;
    line-height: normal;
    margin-right: 5px;
}

.button {
    background-color: #0099e6;
    width: 150px;
    height: 60px;
    border-radius: 5px;
    margin: 10px 50px;
    display: inline-block;
    text-align: center;
    overflow: hidden;
    float:left;
    clear:both;
}

.text {
    background-color: #004466;
    color: white;
    height: 60px;
    width: 180px;
    margin: 10px 50px 10px 0px;
    display: inline-block;
    text-align: center;
    font-size: 12px;
    overflow: hidden;
}
<div id="bgCal">
            <div id="display">
                <span id="numberTot"> 0 </span>
            </div>
            <button class="button" onclick="numberClick(1)">+</button>
            <div class="text">
                Every click increase of 1 the total number.
            </div>
            <br>
            <button class="button" onclick="buyStudent()">
                Buy Student
                <br>
                Cost: <span id="studentLUCost">10</span>
                <br>
                Students owned: <span id="studentLevel">0</span>
            </button>
            <div class="text">
                A student can click instead of yourself, but they are slower.
                <br>
                <span id="studentProd">0</span> num/sec
            </div>
        </div>

Answer №2

If you are utilizing the CSS property inline-block, it is important to consider the usage of vertical-align

html {
  background-color: #e6eeff;
  font-family: Verdana;
}
* {
  border: 1px solid black;
}
#bgCal {
  background-color: #004466;
  color: white;
  width: 500px;
  margin: 0px auto;
  border-radius: 10px;
  padding: 10px 10px 10px 10px;
}
#display {
  background-color: #d7d7c1;
  color: black;
  text-align: right;
  width: 400px;
  height: 100px;
  font-size: 30px;
  margin: 10px auto;
  line-height: 100px;
  border-radius: 5px;
}
#numberTot {
  background-color: #d7d7c1;
  vertical-align: middle;
  line-height: normal;
  margin-right: 5px;
}
.button {
  background-color: #0099e6;
  width: 150px;
  height: 60px;
  border-radius: 5px;
  margin: 10px 50px;
  display: inline-block;
  text-align: center;
  overflow: hidden;
  vertical-align:top;/* reset here vertical-align to next inline content: top, bottom, middle, 1em, etc ... */
}
.text {
  background-color: #004466;
  color: white;
  height: 60px;
  width: 180px;
  margin: 10px 50px 10px 0px;
  display: inline-block;
  text-align: center;
  font-size: 12px;
  overflow: hidden;
}
<div id="bgCal">
  <div id="display">
    <span id="numberTot"> 0 </span>
  </div>
  <button class="button" onclick="numberClick(1)">+</button>
  <div class="text">
    Every click increase of 1 the total number.
  </div>
  <br>
  <button class="button" onclick="buyStudent()">
    Buy Student
    <br>Cost: <span id="studentLUCost">10</span>
    <br>Students owned: <span id="studentLevel">0</span>
  </button>
  <div class="text">
    A student can click instead of yourself, but they are slower.
    <br>
    <span id="studentProd">0</span> num/sec
  </div>
</div>

Answer №3

To achieve vertical alignment, simply include the following CSS property:

vertical-align: middle;

This should be applied to both the .button and .text classes in your stylesheet.

For a live demonstration, feel free to visit my fiddle link: https://jsfiddle.net/1fm1cpqh/

Answer №4

To align your buttons to the left, simply include float:left in your .button class .button{ float:left;}

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 copying of array values is not supported by Chromium

While utilizing Webworker to process an array of pixels from Canvas and then assign it back, I encountered a strange behavior. Firefox works flawlessly in this scenario, however, Chromium seems to insert an empty pixel array into the Canvas. Upon further ...

Is it common for the `col` class in Bootstrap 4 Grid System to have no padding?

This is my first time using Bootstrap version 4. Currently, I have a footer that utilizes the new flex col classes. It looks great on desktop, but on mobile, the columns are stacked so closely together that there is no vertical margin or padding. Is this ...

Tips for extracting title and image from someone else's blog posts to share on your own website

Currently, I am in the process of creating a website where I can showcase my personally curated content. I have been struggling to figure out how to seamlessly integrate this content into my site without relying on an API. My initial idea was to manually ...

Tips for emphasizing active tabs in Angular 6

**I have everything displaying perfectly, but I am wondering how to highlight the active tab without using any third-party components. Any assistance would be greatly appreciated. Thank you.** <ul class="tabs"> <li [ngClass]=" {'active-tab ...

Save React code as a single HTML file

My current project involves creating one-page websites for a specific service that only accepts single inline HTML files. To make these webpages as modular as possible, I am utilizing React to create ready components. The challenge is exporting the final R ...

Tips for effectively overlaying one container on top of another in a responsive manner

There are a pair of containers. The main container functions as the parent, while the child container acts as a tag to categorize the content. The objective is to position the tag container at the top right corner of the main content container, slightly of ...

The children's className attribute can impact the parent element

As I work on creating a card object, I envision it with the className .card that is styled in CSS as follows: .card img{position:absolute; width:150px; height:160px} I want only the images inside my div to overlap each other while not affecting the divs ...

Properly extending the functionality of an HTML widget

Understanding HTML and CSS layout is still a mystery to me, so any guidance on what I'm trying to achieve would be greatly appreciated. I am currently developing a JavaScript API for our company's "widget" so that users can integrate it into the ...

How to preserve alternating row styles when exporting Angular Data Table to Excel with .withButtons?

Utilizing Angular DataTable to display a list of data, with alternate row background color and border styles defined. An issue arises when exporting the Data table to excel as the alternate row style and border styles are lost. Only the raw data is includ ...

Conceal the icon for the Dropdown Menu arrow

Is there a way to hide the old arrow icon in the Select (drop down) after changing it with a new one? HTML: <div class="row"> <div class="col-sm-12"> <select name="country" class="form-control SearchBar"> <option ...

The HTML form does not function properly on OSX when it contains buttons with Cyrillic names in windows

After installing a site locally on my OSX, which was developed under Windows with cyrillic encoding for some string values, I encountered an issue. I noticed that certain buttons with cyrillic values were not functioning properly until I changed the value ...

Unable to find any matches when conducting a search through Google Apps Script

After spending days analyzing the code, I am encountering an error that states "uncaught reference error: google is not defined." This issue seems to occur specifically in line 360. Curiously, when inspecting the original code editor, line 360 simply conta ...

Handling the width and borders of CSS cells that are positioned outside of a table

Welcome! I recently created a simple gantt organizer as part of a demo. My goal was to make the main table scrollable while keeping certain columns "frozen" for easy visibility when navigating through the data. To achieve this, I followed some advice found ...

Is there a way to upload an image without utilizing or concealing the `<input type='file' />` element?

Currently, I am developing a PHP script that retrieves the directories and files from specified local computer drives. The goal I am striving for is to enable users to upload an image to my server by clicking on its name from the listing, without using th ...

Tips for eliminating extra blank space under the footer on an HTML website

Just beginning my journey with bootstrap and I am encountering an issue on my website where there is space after the footer when resizing to a mobile size. I've tried setting margin: 0; padding: 0; but it hasn't resolved the problem. Here's ...

Adjust the sizing of all fonts following the switch in font styles

Seeking help for adjusting font-size across a responsive web page design using different font families. font-family:Cambria, "Hoefler Text", "Liberation Serif", Times, "Times New Roman", serif; To achieve responsiveness, various media queries were applie ...

Would you like to learn how to dynamically alter a button's color upon clicking it and revert it back to its original color upon clicking it again?

My Upvote button starts off transparent, but when I click on it, the background color changes to green. What I need is for the button to become transparent again when clicked a second time. I've attempted using the following code snippet: function ...

Unable to interpret the JSON reply from the server

I am currently developing a web application that sends data to a website, which then updates its database and returns a JSON array to replace my web app page. I am using AJAX for this query, but I am facing an issue with preventing the overwriting of my we ...

Text fields do not show a cursor icon

On the website http://www.legrandclub.net, there are two text fields. Everything works fine in all web browsers except for Internet Explorer. When I click on one of the text fields, the cursor is not displayed, although it is possible to write text. What c ...

Container displaying zero height due to masonry issue

Currently, I am developing a project that requires a responsive masonry layout. The container has a maximum width set to 960px, but it is supposed to adjust based on the browser's size. Each grid item has a percentage-based width (100%, 66.666666etc%, ...