Disregard for flex direction observed

I have been working on configuring settings for a specific application. The main container for the settings is set as a flex element with a column direction. Within this, I included a div with the class .option, containing an h2 heading and a <select> element. Despite setting the .option container to display as flex and using a row direction, the elements still stack up instead of aligning horizontally.

I experimented with making both the h2 and <select> inline-block elements and tried adjusting their width to 50%, but nothing seems to change the layout. Below you will find a screenshot highlighting the issue, along with the relevant HTML and CSS snippets.

#SettingsOverlay {
  display: block;
  position: fixed;
  z-index: 3;
  width: 100%;
  height: 100%;
  background-color: rgba(48, 48, 48, 0.7);
  background-size: cover;
}

#SettingsOverlay div {
  width: 80%;
  height: 80%;
  background-color: purple;
  margin: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
}

#Options {
  display: flex;
  flex-direction: column;
}

.option {
  display: flex;
  flex-direction: row;
}

.option h2 {
  display: inline-block;
  width: 50%;
}

.option select {
  display: inline-block;
  width: 50%;
}
<div id='SettingsOverlay'>
  <div>
    <h1>Settings</h1>
    <div id='Options'>
      <div class='option'>
        <h2>Timezone</h2>
        <select>
          <option value="GMT-12">GMT-12</option>
          <option value="GMT-11">GMT-11</option>
          <option value="GMT-10">GMT-10</option>
         ...
        </select>
      </div>
    </div>
  </div>
</div>

Click here to view UI problem image

Answer №1

Your rule .option is being overridden by #SettingsOverlay div. To resolve this, you can change it to #SettingsOverlay > div or increase the CSS specificity of .option by including the parent ID as well, like #SettingsOverlay .option

https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity

Specificity refers to how browsers determine which CSS property values are the most relevant for an element and will be applied. It is based on the matching rules determined by different types of CSS selectors.

How is specificity calculated?

Specificity assigns a weight to a CSS declaration based on the selector types in the matching selector. When multiple declarations have equal specificity, the last one found in the CSS will be applied. Specificity matters when the same element is targeted by multiple declarations. In CSS, directly targeted elements always take precedence over inherited rules from ancestors.

#SettingsOverlay {
 /* display: none;*/
  position: fixed;
  z-index: 3;
  width: 100%;
  height: 100%;
  background-color: rgba(48, 48, 48, 0.7);
  background-size: cover;
}

#SettingsOverlay div {
  width: 80%;
  height: 80%;
  background-color: purple;
  margin: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
}

#Options {
  display: flex;
  flex-direction: column;
}

#SettingsOverlay  .option {
  display: flex;
  flex-direction: row;
}

.option h2 {
  display: inline-block;
  width: 50%;
}

.option select {
  display: inline-block;
  width: 50%;
}
<div id='SettingsOverlay'>
  <div>
    <h1>Settings</h1>
    <div id='Options'>
      <div class='option'>
        <h2>Timezone</h2>
        <select>
          <option value="GMT-12">GMT-12</option>
          <option value="GMT-11">GMT-11</option>
          <option value="GMT-10">GMT-10</option>
          <option value="GMT-9">GMT-9</option>
          <option value="GMT-8">GMT-8</option>
          <option value="GMT-7">GMT-7</option>
          <option value="GMT-6">GMT-6</option>
          <option value="GMT-5">GMT-5</option>
          <option value="GMT-4">GMT-4</option>
          <option value="GMT-3">GMT-3</option>
          <option value="GMT-2">GMT-2</option>
          <option value="GMT-1">GMT-1</option>
          <option value="GMT">GMT</option>
          <option value="GMT+1">GMT+1</option>
          <option value="GMT+2">GMT+2</option>
          <option value="GMT+3">GMT+3</option>
          <option value="GMT+4">GMT+4</option>
          <option value="GMT+5">GMT+5</option>
          <option value="GMT+6">GMT+6</option>
          <option value="GMT+7">GMT+7</option>
          <option value="GMT+8">GMT+8</option>
          <option value="GMT+9">GMT+9</option>
          <option value="GMT+10">GMT+10</option>
          <option value="GMT+11">GMT+11</option>
          <option value="GMT+12">GMT+12</option>
        </select>
      </div>
    </div>
  </div>
</div>

Answer №2

The reason behind the issue lies in the hierarchy of CSS selectors. IDs are given the highest priority, followed by classes, attributes, and pseudo-classes, and then elements and pseudo-elements.

In your case, the #SettingsOverlay div CSS selector holds a higher priority compared to both the #Options selector and the .option selector. This is causing all div elements within the id of SettingsOverlay to inherit a flex-direction of column.

To resolve this issue, you can assign a class to the div (which will lower its priority) and use that class as a CSS selector instead of #SettingsOverlay div, as shown in the example below.

If you ever need to determine the specificity of your CSS, you can utilize Keegan's Specificity calculator for assistance.

#SettingsOverlay {
  display: none;
  position: fixed;
  z-index: 3;
  width: 100%;
  height: 100%;
  background-color: rgba(48, 48, 48, 0.7);
  background-size: cover;
}

#Options {
  display: flex;
  flex-direction: column;
}

.option {
  display: flex;
  flex-direction: row;
}

.option h2 {
  display: inline-block;
  width: 50%;
}

.option select {
  display: inline-block;
  width: 50%;
}

.settingsOverlay {
  width: 80%;
  height: 80%;
  background-color: purple;
  margin: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
}

<div id='SettingsOverlay'>
  <div class="settingsOverlay">
    <h1>Settings</h1>
    <div id='Options'>
      <div class='option'>
        <h2>Timezone</h2>
        <select>
          <option value="GMT-12">GMT-12</option>
          <option value="GMT-11">GMT-11</option>
          <option value="GMT-10">GMT-10</option>
          <option value="GMT-9">GMT-9</option>
          <option value="GMT-8">GMT-8</option>
          <option value="GMT-7">GMT-7</option>
          <option value="GMT-6">GMT-6</option>
          <option value="GMT-5">GMT-5</option>
          <option value="GMT-4">GMT-4</option>
          <option value="GMT-3">GMT-3</option>
          <option value="GMT-2">GMT-2</option>
          <option value="GMT-1">GMT-1</option>
          <option value="GMT">GMT</option>
          <option value="GMT+1">GMT+1</option>
          <option value="GMT+2">GMT+2</option>
          <option value="GMT+3">GMT+3</option>
          <option value="GMT+4">GMT+4</option>
          <option value="GMT+5">GMT+5</option>
          <option value="GMT+6">GMT+6</option>
          <option value="GMT+7">GMT+7</option>
          <option value="GMT+8">GMT+8</option>
          <option value="GMT+9">GMT+9</option>
          <option value="GMT+10">GMT+10</option>
          <option value="GMT+11">GMT+11</option>
          <option value="GMT+12">GMT+12</option>
        </select>
      </div>
    </div>
  </div>
</div>

Answer №3

#SettingsOverlay div has a specificity of (1, 0, 1) and .option has a specificity of (0, 1, 0), indicating that the flex-direction in #SettingsOverlay div is given priority.

To ensure the style only applies to direct children, consider changing #SettingsOverlay div to #SettingsOverlay > div. It is generally recommended to avoid using IDs due to their high specificity.

For more information on specificity in CSS, check out https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity.

Answer №4

section, it appears that the issue you are experiencing is a result of the #SettingsOverlay div causing unwanted behavior on all subsequent div elements within the main #SettingsOverlay. To resolve this, consider changing the name of the rule to ensure it functions as intended. Additionally, some parameters have been added to the .option div for further customization. Here is a snippet of the CSS code: #SettingsOverlay { /*display: none;*/ position: fixed; z-index: 3; width: 100%; height: 100%; background-color: rgba(48, 48, 48, 0.7); background-size: cover; } #Options { width: 80%; height: 80%; background-color: purple; margin: auto; padding: 10px; display: flex; flex-direction: column; } .option { display: flex; flex-direction: row; justify-content: space-between; align-items: center; } .option h2, .option select { display: inline-block; width: 50%; } In addition, here is an example of how the HTML structure should look like:

Settings

Timezone

By making these adjustments, you should be able to address the issues with the #SettingsOverlay div and enhance the functionality of your webpage.

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

Navigating through dropdown menus using webdriver

I am having trouble selecting a specific element from a dropdown menu. The element I am trying to select is labeled Edit/ViewResume 1st Attempt For my initial try, I attempted to use the Action and Select class methods to choose the desired webelement ...

Setting Aggrid style height to 100% in JustPy made easy

I am attempting to adjust the Aggrid height style to 100%, but unfortunately, the table is not displaying as expected. I have tried using viewport and pixel measurements, which worked fine. Interestingly, percentage width is working perfectly well. My goa ...

Tips for adding and removing active class with navbar links using JavaScriptonclick

I need help with toggling the "li-active" class on my navigation bar links. Currently, when I click on a link, the "li-active" class is transferred from the previous link to the newly clicked link instead of removing it first. Can someone please assist me ...

Is it possible to operate a mobile site automatically alongside a desktop site?

I'm currently working on a website that looks fantastic on desktop, but doesn't quite function properly when viewed on mobile devices. I'm curious if there is a method or system I could implement to automatically load the mobile version of t ...

Can selectors be grouped together for easier organization?

Seeking to style all child elements like div, table, ul, dl, under a specific selector using LESS. It would be great if I could do something along these lines. .myclass { ... &.otherClass > (div, ul, dl, table) { // define some rules... ...

developing a responsive website that can effortlessly adjust to any screen size, whether big or small

As I develop a website on my 13-inch MacBook, I encountered an issue with the background image being too large for my screen. Is there a way to maintain the aspect ratio while designing so that it looks consistent when scaled up or down? I tried using the ...

Grails 3 - Resource Management Extension

Can anyone recommend a great Grails plugin for the latest version of Grails 3 (specifically working with 3.0.4)? I'm looking for something like this one () that allows me to create resource bundles for JS, CSS, and other dependencies. I think having ...

Page designed for displaying small star ratings in JSP

In the realm of my star.jsp page, the style.css is its trusty companion, faithfully executing its duties. However, a desire has crept into my heart - I yearn for the stars to shrink in size. The Chronicles of My Code star.jsp <!DOCTYPE html> ...

bootstrap used for creating horizontal radio buttons

I'm attempting to horizontally align radio buttons within a form without relying on the Bootstrap library. The following code achieves this: <form id="test_form"> <div class="control-group"> <label for="Q1">First question</ ...

Detecting When an Element is About to Be Deleted by Pressing Backspace in a Contenteditable HTML Element

As I develop a text editor using <div contenteditable>, one of my goals is to implement a confirmation message for users before they delete an image element within the editor. My idea is that when a user presses the backspace key while attempting to ...

Instructions for passing a JavaScript variable to a PHP MySQL database

I am attempting to integrate a JavaScript variable into PHP MySQL, but I'm encountering issues with the insertion. Currently, it is being inserted as <javascript>document.write(window.outerWidth); </javascript> x <javascript>document ...

Enable the bottom footer to expand along with the content to support a dynamically growing page

I followed a tutorial on keeping footers at the bottom of a webpage (http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page) to create a site with a fixed footer. However, I encountered an issue when there is more content than can fit ...

Elements on the page are being truncated beyond the visible viewport

Encountering an issue with a web page I'm currently developing that is proving challenging to explain. The problem occurs when the page is viewed in a small browser window and cuts off when scrolling horizontally to content outside of the viewport. Wh ...

Clicking on the element will not cause the body to scroll

For some reason, I need to keep the onclick function, but I'm struggling to make it work as intended. What I want is for the body to slide to the next logo when the previous logo is clicked. This is a simplified version of what I have: <div class ...

I am looking to transfer the data stored in a variable within a JavaScript function to a different PHP page

Here is my form featuring the summernote editor: <form class="form-group" action="upload.php" style="width: 700px;" method="post" enctype="multipart/form-data"> <label> Title: </label> <input name="title" class="form-c ...

Leverage predefined JavaScript functions within an Angular template

I have been attempting to execute an eval function within my angular template in the following manner: <div *ngFor="..."> <div *ngIf="eval('...')"></div> </div> You understand what I'm trying to ...

How can I determine if the td I clicked on is in the first row or the last row, and how do I find out its row and column

I am working with a table that looks like the one in this link. https://i.sstatic.net/LCoLw.png Is there a way for me to determine if the td (input) I clicked on is in the first row or last row? Additionally, how can I figure out the column index and ro ...

Finding a Div ID with Python and Selenium

Currently, I am facing an issue where I have to interact with an image of a dropdown arrow in order to select an item that will automatically fill the input box. Both the dropdown arrow and input box are components within the dev id:decision. I require ass ...

The angular-block-ui feature fails to run my HTML code as a personalized message

I'm looking to enhance my UI by implementing a blocking feature with a spinner display. My attempt involved the following code: blockUI.start("<div class='dots-loader'>Minions are Working!</div>"); // code for fetching data ...

Unable to alter the css of a jQuery object

I have been attempting to modify the CSS of two child elements of a specific element using Backbone and jQuery. However, my code does not seem to be working as expected. I suspect that the issue lies in distinguishing between a DOM element and a jQuery obj ...