Is it possible to arrange buttons next to each other?

Here is the code for a specific page:

HTML:

<div class=main>
    <div class=bttn>
        <button>abcdef</div>
    <div class=content>
        <a href=#!>jksg</a>
        <a href=#!>jksg</a>
        <a href=#!>jksg</a>
        <a href=#!>jksg</a>
    </div>
</div>

<button>abcdef
    <button>abcdef
        <button>abcdef
            </div>

CSS :

button {
    border: 3px solid red;
    border-collapse: collapse;
    padding: 16px;
    background-color: blue;
    width: 25%;
    margin: 0px -1px 0px -1px display: inline;
}

.content {
    display: none;
}

.bttn + .content {
    display: none;
}

.bttn:hover + .content {
    display: block;
}

Upon running the code, the first button appears on a separate line while the remaining buttons align themselves next to each other on the following line. This behavior persists even if hidden divs are added to other buttons. I have attempted changing the display property of the button tag, bttn class, and content class to inline with no success. It could be related to the position property, but I am uncertain. Any insights into what might be going wrong here would be greatly appreciated.

Answer №1

If you want your buttons to appear on the same line, this solution will help you achieve that.

https://jsfiddle.net/2wwfxfqy/1/

However, a new challenge arises when you need to prevent hidden content from pushing the other buttons onto a new line once revealed.

Below is the CSS modification that has been implemented:

.main button,
.bttn button {
  float:left;
 }

Answer №2

Don't forget to properly close your <button> tag on the second line and at the bottom for correct functionality. I have made the necessary adjustments to your code below, and it now functions as expected. Additionally, I have enclosed "#" within quotation marks in the anchor tags.

<div class = main>
<div class = bttn><button>abcdef</button></div>
<div class = content>
<a href = "#">jksg</a>
<a href = "#">jksg</a>
<a href = "#">jksg</a>
<a href = "#">jksg</a>
</div>
</div>

<button>abcdef</button>
<button>abcdef</button>
<button>abcdef</button>

Answer №3

Not all HTML tags require a closing tag, but it is essential to close the <button> tag. Additionally, only one of the two selectors with display: none; is needed in this snippet, so I have removed one of them.

button {
  border: 3px solid red;
  border-collapse: collapse;
  padding: 16px;
  background-color: blue;
  width: 25%;
  margin: 0px -1px 0px -1px;
  display: inline;
}

.bttn + .content {
  display: none;
}

.bttn:hover + .content {
  display: block;
}
<div class="main">
  <div class="bttn"><button>abcdef</button></div>
  <div class="content">
    <a href="#">jksg</a>
    <a href="#">jksg</a>
    <a href="#">jksg</a>
    <a href="#">jksg</a>
  </div>
</div>

<button>abcdef</button><button>abcdef</button><button>abcdef</button>

In a side note, I have updated the HTML to include quotes for consistency. While not always necessary for attributes without spaces (unless using an XHTML doctype), forming good habits by maintaining consistency can prevent errors and make code easier to modify later on. It's best to choose either double or single quotes for markup and stick with it:

  • <button onclick="alert('hey');">

  • element.innerHTML = '<div class="my-div">hey</div>';

Changing quotes between HTML and JS code alters how the code is interpreted, so consistency is key. Double quotes are more commonly used in HTML, while single quotes are prevalent in JS.

UPDATE: When using data-* attributes that contain JSON, double quotes in HTML can pose issues. This may be an argument against the standard quote style outlined earlier. However, including quotes of some kind is still recommended.

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

SSI stands for Server Side Includes, a feature that allows

I have multiple versions of the same HTML page, each with only one hidden variable that is different. This variable is crucial for tracking purposes. Now, I am exploring options to rewrite this by incorporating a HTML file with a hidden variable. Here is ...

How can I customize the visibility toggles for the password input field in Angular Material?

Currently immersed in the Angular 15 migration process... Today, I encountered an issue with a password input that displays two eyes the first time something is entered in the field. The HTML code for this is as follows: <mat-form-field appearance=&qu ...

Is it acceptable to include a checkbox and a hidden input within a label element?

When creating a list of possible products for users to buy, I use the ul tag combined with li. Each product element includes a checkbox that allows users to select the product or not. For products with related information, I want to store this data in a h ...

Setting the transition time for adding or removing components in ReactJS CSS

As the list component changes in size based on its children, it tends to move around abruptly. Applying transition: 'all 0.3s ease' doesn't resolve this issue since it's the layout that is affected by the number of children rather than ...

Using Jquery to select a specific id for an input element that is a checkbox

I've been working on a snippet of jQuery code that I'd like to tailor to be more specific for one of two different input elements on my HTML page. <script> // ensure only one box is checked at a time $(document).ready(function() { ...

LESS — transforming data URIs with a painting mixin

Trying to create a custom mixin for underlining text, similar to a polyfill for CSS3 text-decoration properties (line, style, color) that are not yet supported by browsers. The idea is to draw the proper line on a canvas, convert it to a data-uri, and the ...

Error encountered when transitioning to TypeScript: Unable to resolve '@/styles/globals.css'

While experimenting with the boilerplate template, I encountered an unusual issue when attempting to use TypeScript with the default NextJS configuration. The problem arose when changing the file extension from .js to .tsx / .tsx. Various versions of NextJ ...

Use $.ajax to display the menu by capturing an array of elements {0}

Whenever I click on one of the DIV elements, a menu pops out on the side displaying more information about the clicked element. I can determine which element I'm clicking on, but I'm struggling to pass that information from jQuery to the PHP pag ...

AngularJS: When the expression is evaluated, it is showing up as the literal {{expression}} text instead of

Resolved: With the help of @Sajeetharan, it was pinpointed that the function GenerateRef was faulty and causing the issue. Although this is a common query, I have not had success in resolving my problem with displaying {{}} to show the outcome. I am atte ...

What is the expected output format for htmlspecialchars?

When I try the following: echo (htmlspecialchars("andreá")); I expect to see So, assuming that if I do echo (htmlspecialchars_decode("andreá")); I would get andreá. However, instead of the expected result, I see Additionally, when I run echo ...

Tips for Utilizing Environmental Variables within a Vue Application's index.html File

My website has an index file with all the necessary meta tags, stylesheets, and scripts: <!DOCTYPE html> <html lang="en"> <head> <!-- Required Meta --> <meta charset="UTF-8"> <meta http-equi ...

Developing an interactive single-page website

My goal is to develop a PHP web application with an overlay container that features dynamic fields/forms. The idea is for users to fill out the form without being redirected from the main page they are on. Imagine being able to report a page error on a w ...

Retrieving the value of a radio button using JavaScript

I am working with dynamically generated radio buttons that have been given ids. However, when I attempt to retrieve the value, I encounter an issue. function updateAO(id2) { var status = $('input[name=id2]:checked').val(); alert(status); ...

Rendering High-quality Images in Internet Explorer Browser

Currently, I am working on optimizing my website for high resolution monitors, particularly the new iPad. The site is already formatted to my liking, with images having increased resolutions while still fitting into specific DIVs. For instance, an image wi ...

Tips for sending a selected option value to jQuery via AJAX using the name attribute

Is there a way to send the section option value using ajax, but only for selection and checkboxes? The issue I'm facing is that when posting, the section and checkbox appear as undefined. Thanks in advance. PHP Code <?php $parameters = new P ...

Could a complex, intricate clipping path be created using CSS 3?

Here is an example: Can this be achieved using only CSS? I wish to create two divs: A circle without any background or border. A div with a background. I aim to have point 1 clip the background from point 2. This will allow me to rotate the backgroun ...

What is the most efficient way to remove all typed characters from fields when clicking on a different radio button? The majority of my fields share the same ngModel on a separate page

Is there a way to automatically clear all typed characters in form fields when switching between radio buttons with the same ngModel on different pages? I noticed that the characters I type in one field are retained when I switch to another radio button. ...

Counting the number of characters without including HTML tags in C#

I am currently exploring methods to calculate the number of characters in a string, shorten the string, and then return it without including HTML tags in the character count. It's important that HTML tags are not counted because if the truncation poin ...

The div is struggling to contain the image, can anyone assist with this issue?

Greetings all! I am a beginner with CSS and currently working on my website. You can check it out at this link. I am facing an issue where the image on my site is not fitting properly and it keeps repeating. Can anyone guide me on how to fix this using CS ...

PHP Solution: I'm working on a page that is accessed using the post method. However, I need to defer the execution of certain code until after the page has finished

Apologies for the confusing title, but I'm struggling to succinctly explain my issue: I have a web page that is accessed through a button. When this button is clicked, specific data (a unique identification name) is sent to the page I want to view. Ho ...