The CSS float property and text alignment only apply when elements are displayed inline

I am facing an issue with my ASP .net core 2.1 app. I have customized the scaffolded crud details page, but I am encountering hurdles with the dl, dt, and dd elements.

The text inside dt elements, which display the field names, is appearing next to them on the right side. No matter what changes I make, I can't seem to adjust this layout without resorting to using inline styles. I added the following code to the style section of the file:

<style>
    dd {
        min-width: 120px;
        background: #dd0
    }

    dt {
        float: left;
        background: #cc0;
        text-align: left;
    }
</style>

Although this code doesn't affect the float and text alignment as expected, it does change the background color, as shown in the image below. https://i.sstatic.net/EJzny.png

However, when I apply these display settings inline like the example below, it works perfectly for those lines as illustrated in the picture above.

<dt style="text-align: left; max-width: 70px;">
                        @Html.DisplayNameFor(model => model.PatientName)
                    </dt>
                    <dd>
                        @Html.DisplayFor(model => model.PatientName)
                    </dd>
                    <dt style="text-align: left;min-width: 70px;">
                        @Html.DisplayNameFor(model => model.PatientAddress)
                    </dt>

This has led me to speculate that maybe it's a hierarchy of settings issue, but even trying Bootstrap didn't yield any better results.

Furthermore, I prefer not to use inline styles as it is considered a bad practice.

Answer №1

Understanding CSS Specificity is crucial as it determines the hierarchy in which CSS rules are applied. To achieve the desired styling, it's recommended to incorporate selectors with increasing specificity.

Explained on the provided webpage:

The order of selector types based on specificity is as follows:

  1. Type selectors (e.g., h1) and pseudo-elements (e.g., ::before).
  2. Class selectors (e.g., .example), attribute selectors (e.g., [type="radio"]), and pseudo-classes (e.g., :hover).
  3. ID selectors (e.g., #example).

Additional rules to consider:

  • The universal selector (*), combinators (+, >, ~, ' ', ||), and negation pseudo-class (:not()) do not impact specificity directly. The selectors within a :not() statement do influence it.
  • Inline styles assigned to an element inline (e.g., style="font-weight: bold;") always have precedence over external stylesheet styles, representing the highest specificity level.
  • The use of !important: Applying !important to a style declaration ensures that it takes precedence over other declarations.

To experiment with selectors aligned with the aforementioned specificity hierarchy, consider these examples:

/* Type selectors (current approach): */

dd
dt

/* Class selectors (either directly on elements or on parent <table class="my-table">): */

dd.my-class
dt.my-class
/* or */
table.my-table dd
table.my-table dt

/* ID selectors (either directly on elements or on parent <table id="my-table">): */

dd.my-dd
dt.my-dt
/* or */
table#my-table dd
table#my-table dt

If troubleshooting proves challenging, resorting to less favorable techniques where all other CSS selectors are overridden can be considered: usage of inline styles or the !important rule:

<style>
    dd {
        min-width: 120px !important;
        background: #dd0 !important;
    }

    dt {
        float: left;
        background: #cc0 !important;
        text-align: left !important;
    }
</style>

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

What is the best method for binding variables in Vue.JS using moustache syntax and passing parameters to them?

If I have an HTML structure like this: <div> {{ CUSTOM_MESSAGE }} </div> And in my data: data() { return { CUSTOM_MESSAGE: 'Hey there! This message is for {{name}} from {{location}}' } } Is there a way to dynamically p ...

Is there a way to retrieve the left offset of a floating element even when it is positioned outside the viewport?

My current situation involves creating several panels that are stacked side by side within a main container. Each panel takes up 100% of the viewport width and height. I want to be able to horizontally scroll to each panel when clicking on their respective ...

Is it possible to dynamically alter a CSS property using styled components when an onClick event occurs?

Hey there, I'm pretty new to React and currently exploring styled components for the CSS styling of my components. One of the components I've created is a button called SummonButton: const SummonButton = styled.button` height: 50px; borde ...

Is there a way to retrieve a list of li tags using BeautifulSoup4 in Python?

I am attempting to extract information from a Persian webpage where I need to retrieve only 3 li tags out of a ul element that contains 6 of them. The issue is that each li tag also has nested li tags within it, so when I use soup.find_all('li'), ...

Sending a jQuery variable to an external PHP script

Utilizing the HTML5 Geolocation API, I am able to retrieve latitude and longitude coordinates which I then need to pass to an external PHP file. This PHP file (getPosition.php) retrieves JSON variables from my database, allowing me to utilize them in geo.j ...

Leveraging jQuery functions with dynamically generated DOM elements in JavaScript

On my website, I have a button that dynamically generates a dropdown menu, a textfield, and two buttons when clicked. The purpose of the textfield is to track the quantity selected, while the two buttons allow users to increase or decrease the value by 1. ...

Responsive background image not displaying properly

The developer site can be found at http://www.clhdesigns.com/cliwork/wintermeresod/about.php I am encountering an issue where the background image on the home page scales perfectly, but on the about us page it does not scale at all. I am seeking a solutio ...

Having trouble loading background color or image in three.js

I've been struggling to load a background image or color on my webpage. Despite trying various methods, including commenting out javascript files and checking the stylesheet link, nothing seems to work. Even when I load the three.js scene with javascr ...

Tips for centering fontawesome icons in your design

https://i.sstatic.net/Atian.pngI'm facing an issue with aligning 3 icons. My goal is to align them, but every time I attempt something, the first icon ends up in the middle of the page instead of the second icon where I want it to be. .item-icon { ...

Extract the content from a <Span> element without specifying its ID

Is there a way to make it so that when a user clicks on an HTML tag, the text is copied to their clipboard? I also need to ensure that this functionality does not apply to a specific tag ID/name as I am unable to add those to my span. Is there a way to aut ...

Creating a React.js component and setting an initial value within it

Recently delved into the world of React.js and currently attempting to create a reusable header that can switch between two states: one for when the user is logged in, and another for when the user is not logged in. // Header.js var Header = React.createC ...

Change the text inside a container without losing any associated event listeners using JavaScript or jQuery

Here is the HTML code: <div id="div001"> This is ${Row.1}. <p>${Row.2} explain something else.</p> <p>${Row.3} welcome you. <span>${Hello.World}, this is a new world.</span></p> </div> My objective is ...

Creating interactive <td> elements in HTML with JavaScript editor capabilities

Currently, I am working on creating an editable table feature and managed to find a good example to follow. However, I have encountered some issues along the way. <td contenteditable="true" class="hover" onBlur="saveToDatabase(this,'question' ...

Accordion Element using HTML and CSS

Seeking assistance with a website project! I am in need of help in implementing the following feature: I would like the "About" link to expand into a panel when clicked, and retract back when the user clicks "hide" within the panel. Check out the attached ...

Best practices for resolving classlist.toggle issues with my dark mode button

The code seems to work only the first time, but after activating light mode again, the sun stops appearing. How can I ensure that the 'bi-sun' class is added back every other click or when dark mode is not activated? function theme() { do ...

Toggle among 10 divs, but automatically revert back to the initial div if there is no mouse activity for a short period

My website has 9 links in the header and 10 divs in the body. The first div serves as the main page, while the remaining 9 divs contain different content. I would like to implement a feature where when users hover over the 9 links, it displays one of the 9 ...

Concealing forms within an Angular 5 application

I'm currently working on displaying the terms of use on the initial screen along with two buttons. If the user clicks the accept button, they will be directed to the authentication form. However, if they click refuse, the "Refused Terms" screen will a ...

How to Create a Speech Bubble in SVG Using SnapSVG

In the process of developing a chat program, I have animated figures moving across the screen engaging in conversations. One crucial aspect I am yet to implement is creating scalable speech bubbles for when users interact. Being relatively new to SVG and ...

Avoid reloading the page in PHP when the browser back button is clicked

I've built an HTML form where the values are passed to a second page using POST method. On the second page, I have an edit button that, when clicked, redirects back to the HTML form page with the user's typed values. However, my dilemma is figuri ...

Hiding Button in code behind

I have around 100 buttons in a list. Initially, I have set their visibility to false in the ASPX page. Now, I need to change the visibility to true from the code-behind when required. Since there are too many buttons, it's not feasible to individuall ...