What impact does the order of the element I'm specifying in my .css file have on its appearance after being rendered?

(An update has been added below)

In my project, I am working with a .css file and an index.html document. The goal is to create a panel of buttons on the screen [to prototype the usability of a touchscreen interface], with each button assigned a specific position on the screen (referencing the "margin-top" and "margin-left" tags in the .css file). Later, I will be adding onclick functionality, among other things.

Currently, I am focused on getting the buttons to display correctly on the screen. What's puzzling is that the first element in the .css section (i.e. the .aButton #....) always appears at position (0,0) when viewed in the browser. For example, right now, button1 is displayed at (0,0) while all the other buttons are in their correct positions. If I rearrange the css code and place the button2 section above the button1 section, then button1 displays in the correct place but button2 ends up at (0,0).

I would appreciate any suggestions or ideas!

Here are the relevant parts:

From index.html

<div id="buttonPanel" class="aButton">
            <div id="button1" class="aButton"></div>
            <div id="button2" class="aButton"></div>            
            <div id="button3" class="aButton"></div> 
            <div id="button4" class="aButton"></div>
            <div id="button5" class="aButton"></div>
</div>

From styles.css:

.aButton #button1
{
    margin-top: 262px;
    margin-left: 110px;
}

.aButton #button2
{
    margin-top: 24px;
    margin-left: 347px;
}

.aButton #button3
{
    margin-top: 32px;
    margin-left: 114px;
}

.aButton #button4
{
    margin-top: 524px;
    margin-left: 104px;
}
.aButton #button5
{
    margin-top: 392px;
    margin-left: 106px;
}

An Update: I have updated Fr0zenFyr's jsfiddle with the complete html and css files and encountered the same issue! http://jsfiddle.net/b4NYd/ I realize now that omitting them both was a mistake, and I suspect it may have something to do with how the .aButton #buttonx {} definitions interact strangely with the #buttonx {} definitions preceding them. I can temporarily resolve the problem by combining the two, but I would like to understand why this issue arises, especially since I plan to use different classes for prototyping various layouts later on.

Another Update: Problem solved! It turns out, it was due to my comments. Lesson learned!

Answer №1

One issue lies in your comments. The correct way to write comments in CSS is by using the /* notation.

Here's the corrected format:

 /* style guide 2 */
 /* make necessary adjustments based on the data in my notebook.*/

Answer №2

Changing the order of your CSS styles shouldn't impact how your HTML elements are displayed. You have the flexibility to rearrange the class order for your buttons without affecting anything.

Have you made any other modifications to your CSS? Have you designated the elements as inline-block, or as btevfik suggested, are you applying any positioning to them? Are you potentially overriding the button styles later in your stylesheet?

It's likely that there is another underlying issue causing the buttons to shift on the screen.

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

How can you make the contents of a <DIV> expand?

Picture a scenario where I have a navigation bar in the header of my web page, consisting of several links. What I want is for these links to expand horizontally to fill the parent container without me having to hard-code based on the number of links. In o ...

Video from YouTube keeps playing in the background even after closing Bootstrap modal

I'm having trouble with playing an embedded YouTube video inside a Bootstrap modal. When I close the modal, the video continues to play in the background. Can someone help me with this issue? <!-- Button trigger modal --> <button type=&q ...

What is the best way to choose the final XHTML <span> tag with a specific class using XPath?

My target XHTML document structure is as follows: <html> <head> </head> <body> <span class="boris"> </span> <span class="boris"> </span> <span class="johnson"> </span> </body> </html> ...

The jsPdf library performs well on medium-sized screens like laptops, but when downloaded from larger monitor screens, the PDF files do not appear properly aligned

In the code snippet below, I am using html2canvas to convert HTML to PDF. The PDF download works fine on medium screens, but when viewed on larger screens, some pages appear blank and the content order gets shuffled. How can I resolve this issue so that th ...

Converting HTML to formatted text in C#

I have a field in my database (SQL Server 2014) that contains HTML formatting, such as <p><strong>Content</strong></p> When I display this field in my Table View using MVC 5, the HTML tags show up as text. I want to actually render ...

Tips for optimizing the placement of div elements for top advertisements on Amazon's website

I'm looking to overlay some divs on top of Amazon.com ads, similar to the image above. This is part of a personal project where I need to position these divs precisely over the ads. To achieve this effect, I've been using getBoundingClientRect t ...

When the width is set to 100vh, there is excessive white space beneath the div

I'm dealing with a unique situation where I have a horizontal scrollable div containing different elements. To achieve the horizontal scroll, I've set the width: 100vh and rotated the div. However, this is causing an unwanted white space below th ...

Creating a dynamic progress bar that scrolls for multiple elements

I am currently working on implementing a scrolling progress bar to show users how much of an article within a div they have read. For reference, something similar can be seen on this site. I have created my custom progress bar and coded it on fiddle, whe ...

When Socket.emit is called, it outputs <span> elements within a well-structured message

Currently working on a small chat application using Node.js, Express.js, and Socket.IO. However, I'm encountering an issue with formatting. Instead of displaying the message content within <span> tags as intended, it is printing out the actual t ...

When the text starts to overlap the column completely at approximately 700 pixels wide

Previously, everything was working fine until it suddenly broke. The column system switches to a 100% width single column at 1500px, but for some reason, the text overflows off the screen around 700px and I can't figure out why. The text at the bottom ...

Adjust the size of a div using absolute positioning

Can a div with absolute position be resized? I need this pink modal to maintain the same width as the input and resize accordingly. This modal will be utilized in a dropdown component, so it should resize along with the input. Moreover, is using absolute ...

HTML 4.01 character and character offset attributes

I'm curious about the purpose of the charoff attribute in HTML 4.01 Transitional. Specifically, consider a table column that contains the following cells: <td>1.30</td> <td>10</td> <td>100.2</td> <td>1000 ...

The video attribute in HTML is malfunctioning on the react webpage

Currently working on setting up a basic portfolio layout with a video playing in the background on loop. However, despite making some adjustments and modifications, the video is not showing up as expected. Any insights or suggestions on what might be causi ...

Content Division with Sticky Footer

I have successfully made my footer stay at the bottom of the page by following this method: Now, I am facing an issue where my #content div has borders on the left and right sides but does not expand to match the height of the #wrapper. Your help in reso ...

Different placeholder text rendered in two text styles

Can an input box placeholder have two different styles? Here's the design I have in mind: https://i.stack.imgur.com/7OH9A.png ...

Accordion menu causing Javascript linking problem

After following a tutorial, I managed to create an accordion menu in JavaScript. In the current setup, each main li with the class "ToggleSubmenu" acts as a category that can hide/show the sub-lis. Now, my query is this: How can I retain the link functio ...

Collecting information from form submissions, accessing data from the database, and displaying the results

I am currently working on a project using nodejs, express, and mongodb within the cloud9 IDE. My goal is to create a page with a form that allows users to input data, search for corresponding information in the database, and display that data on the same ...

get the value of the last selected element using jQuery

I am facing an issue where I cannot retrieve the selected value from my second select element. Even though I have selected the second option in the dropdown, it keeps returning the value of the first option as empty. Here is a snippet of the HTML code: & ...

Arrange the "See More" button in the Mat Card to overlap the card underneath

I'm currently working on a project that involves displaying cards in the following layout: https://i.stack.imgur.com/VGbNr.png My goal is to have the ability to click 'See More' and display the cards like this: https://i.stack.imgur.com/j8b ...

When attempting to load a CSS file, Java automatically redirects to the login page

EDIT** After successfully loading CSS, I encountered an issue where the page kept redirecting back to login.jsp instead of displaying the CSS. Any insights on what might be causing this?https://i.sstatic.net/Ij7Oh.png I attempted to incorporate a custom ...