Setting the height to "auto" will take precedence over any other

I recently designed a responsive webpage with two distinct sections. In order to ensure all elements in the right section were responsive, I utilized the following CSS code:

#rightSection * {max-width:100%; height:auto;}

Despite this, I noticed that any additional styling related to height was not taking effect, as evident in the code snippet.

Avoiding the use of !important is important for me due to the presence of numerous inline styles within the HTML code. So, I am seeking an alternative method to establish heights subsequent to using height:auto.

    #rightSection * {max-width:100%;height:auto;}

    .mydiv {
    width:534px;
    height:37px;
    box-sizing:border-box;
    }
<div id="rightSection">
  <div class="mydiv" style="background:#ff0000"></div>
</div>

The red div remains invisible due to the ignored height property!

Answer №1

Everything that's occurring in your code is correct. CSS stands for Cascading Style sheet, which means the last rule takes precedence, especially if it is more specific. In this case, the first rule has a higher specificity than the second one, so height:auto is being applied.

For more details on Specificity, refer to the link provided.

In your code, you can increase the specificity of the second rule in various ways, as explained in the above link. Below is an example:

 #rightSection * {max-width:100%;height:auto}


    #rightSection div {
    width:534px;
    height:37px;
    box-sizing:border-box;
    }
<div id="rightSection">
  <div class="mydiv" style="background:#ff0000"></div>
</div>

The red div is invisible because the height is ignored!

Edit: As highlighted by @connexo, I recommend avoiding the use of Id selectors. You can refer to this for more information on why.

You can utilize css classes instead, as they help in making your code more universal. For instance:

.outerDiv * {max-width:100%;height:auto}


    .outerDiv .mydiv{
    width:534px;
    height:37px;
    box-sizing:border-box;
    }
<div class="outerDiv">
  <div class="mydiv" style="background:#ff0000"></div>
</div>

Now, the red div is visible since the rule is more specific.

I hope this explanation helps :)

Answer №2

    #rightSection * {max-width:100%;height:auto}


    #rightSection .mydiv {
    width:534px;
    height:37px;
    box-sizing:border-box;
    }
<div id="rightSection">
  <div class="mydiv" style="background:#ff0000"></div>
</div>

The crimson colored division remains hidden as its height is overlooked!

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 hover effect is not extending the full width of the dropdown menu

I'm currently working on my first WordPress theme, trying to style the _'s theme. I've been focused on creating a dropdown menu, but I'm facing an issue - if one of the dropdown item headers is too wide, the hover color won't cover ...

Signal for a complete 360 degree rotation of an image

Looking to enhance my 360 image rotator with an indicator that fades out after the first image. I added this function to the end of my 360.js: (function(){ if( $('#first').css('visibility','hidden')) { $('#rotat ...

Is it possible to adjust the dimensions of the number input spinner?

When working with a number input that includes a spinner, I have encountered challenges in changing the size of the spinner itself. Specifically, I am referring to the <input type='number'> element. Despite my efforts to adjust its size thr ...

Converting a table into JSON format

I'm working on developing a live application that will showcase data in a table format like the one below: Machine Production Scanned Delta Mach 1 2500 1000 1500 Mach 2 1500 1300 200 Mach 3 6500 5000 1500 Mac ...

How can I validate a password input in a form on click and reveal a hidden div on the same page without refreshing?

I've been attempting to create a password-only form that reveals a hidden div on the same page when the correct password is entered. I want to avoid reloading the page, but I'm unsure of how to achieve this. function checkPassword() { var co ...

Jquery allows for the toggling of multiple checkboxes

I have a group of check-boxes that I would like to toggle their content when checked. Currently, I am using jQuery to achieve this functionality, but I am searching for a way to optimize my code so that I do not need to write a separate function for each ...

Unable to place a div below another div

I am in the process of creating a website for my mother, using my limited knowledge. Despite numerous attempts, I have not been able to achieve the desired outcome. Currently, the website looks like this: https://i.stack.imgur.com/ItOcp.jpg However, I e ...

What is the method for obtaining the WordPress user ID?

I am looking to incorporate a Twitter follow button for all site authors on every post. Below is the structure of the Twitter button: <a href="https://twitter.com/twitterapi" class="twitter-follow-button" data-show-count="false" data-lang="en">Foll ...

Pandoc has removed all the line breaks in this text

I am currently utilizing pandoc for converting HTML to Markdown format. However, I have noticed that Pandoc tends to remove line breaks in the final output. Below is the command I am executing: pandoc -f html -t markdown_phpextra myfile.html Does anyo ...

Table for maximizing space within a cell (Internet Explorer 8)

I am attempting to make the "innerTable" fill up all available space within another table. The issue is with IE8 (no compatibility mode, IE8 browser mode, Doc mode IE8 Standards). The table does not expand to fit the containing TD. I tried enclosing the ta ...

The tabs are visible in the drawer, but the transition is not occurring

I am a beginner when it comes to using material ui. After clicking on advanced sports search, a drawer pops up. I have been trying to incorporate tabs in the drawer, but every time I click on tab two, the drawer closes. In reality, it should switch between ...

The validation tool from Google Rich Results indicates that no enriched content was found on this particular website link

My website () is designed to showcase rich results by implementing JSON-LD schemas on every page, which are validated correctly by the Schema.org validator. However, Google's Rich Results Test seems to be failing to recognize them for some reason. Cu ...

Mobile Safari on iOS devices is known for its capability to overlay HTML5 <video> on top of everything

Although a similar question was asked 6 years ago without any answers, I am currently facing the same issue. Problem: The <video> tag in my Angular/Ionic application overlaps my image and two buttons in iOS Mobile Safari - no matter what I try! It ...

Implementing drag-and-drop functionality for text on an image using javascript

Hey there, I'm looking to develop a super simple meme creator tool. I have some code for adding text to an image and dragging it around. Can anyone help me out? <html> <body> <div id="draggable-element">Drag me!</div> <styl ...

Setting up ng-show in AngularJS

Looking for a solution to use ng-repeat and ng-show to organize data into two columns effectively <div class="col-md-4"> <div class="row" infinite-scroll="eventSearchService.getMoreEvents()"> <div ng-repeat="event in eventSearchService ...

Secure User Authentication using HTML and JavaScript Box

In the given code, I am attempting to implement a functionality where a message is displayed next to the select box if it does not have a value of male or female. This message should not be shown if one of these values is selected. However, this feature ...

How can I modify the background color of the datePicker?

I have a couple of inquiries about customizing the DatePicker: Is there a way to modify the background color of the datePicker using CSS? For instance, I would like to change the current green color to blue. Link to datepicker image (Just to note, JS ca ...

Consistent dimensions for columns in a table, automatically adjusting based on content

Can we ensure that all rows and columns maintain equal height, whether or not they contain an image? If you need a visual representation of this concept, refer to this example: [] [] [] ...

Displaying only the Active field in tabs in Bootstrap Django

I have a question about two tabs: Active Projects Inactive Projects The application model has a field called {{app.is_active}}. Would using a script to filter the tabs be the best way to display "Active projects" in the active tab and ...

How can I use Jquery to loop through each combobox on the page and delete those with a specific class name?

I am currently dealing with a page that contains multiple combo-boxes. Each combo-box has a default option set as empty, which is given the class name 'hide'. This functionality seems to work perfectly in Chrome and Firefox, as the hidden options ...