I am struggling to assign the height style attribute to a div element

I have created the code below where I am attempting to set a div to 'display: flex' and include two divisions, one with a width of 200px and the other with a width of 100%.

<div style="clear:both;display:flex; height: 100%;">
    <div style="width:400px; background-color: red;
        height:auto;">
    </div>
    <div style="width:100%;
        background-color: black;
        height:auto;">
    </div>
</div>

However, I am encountering an issue in applying height to that div as it is displaying 0 height in the developer tools.

Even after trying the 'clear: both' style, the problem persists.

I would appreciate any guidance on the correct syntax and code to resolve this confusion I have with CSS.

Answer №1

When utilizing the auto parameter, the height of the content inside the element is considered. Since the div does not have any content, its height is set to 0.

To address this issue, you can use the min-height property:

<div style="clear:both;display:flex; height: 100%;">
    <div style="width:400px; background-color: red;
        min-height:20px;">
    </div>
    <div style="width:100%;
        background-color: black;
        height:auto;">
    </div>
</div>

If you prefer to use the auto parameter, you need to set the height for both html and

body</code elements because when using percentages for <code>div
, it needs to be relative to its parent elements.

html,body {
  height:100%;
}

html,body {
  height: 100%;
}
  <div style="clear:both;display:flex; height: 100%;">
    <div style="width:400px; background-color: red;
        min-height:auto;">
    </div>
    <div style="width:100%;
        background-color: black;
        height:auto;">
    </div>
</div>

Another approach is to use the vh unit like this:

<div style="width:400px; background-color: red; height:100vh;">

  <div style="clear:both;display:flex;">
    <div style="width:400px; background-color: red;
        height:100vh;">
    </div>
    <div style="width:100%;
        background-color: black;
        height:auto;">
    </div>
</div>

Answer №2

First and foremost, it is not recommended to use inline styles as they can be difficult to read and edit, making them hard to overwrite.

Furthermore, setting the height to 100% on the parent div (the one with display:flex) may cause issues. The height:100% property needs to be relative to the height of a container, such as html,body, which currently do not have any content.

Additionally, setting height:auto on the child div means that its height will depend on its content, but since there is no content present, this property won't have any effect.

If your intention is to make the parent div take up 100% of the screen height, you can use viewport height (100vh) or set the height of body and html elements to 100%, as shown in the example below:

body,
html {
  height: 100%;
}
<div style="clear:both;display:flex; height: 100%;">
  <div style="width:400px; background-color: red;
        height:auto;">
  </div>
  <div style="width:100%;
        background-color: black;
        height:auto;">
  </div>
</div>

Answer №3

To ensure that your entire screen is covered, make sure to set the height of your HTML tag to 100% and also give the body tag a height of 100%.

<html style="height:100%;">
  <body style="height:100%;">
  <div style="clear:both;display:flex; height: 100%;">
    <div style="width:400px; background-color: red;
        height:auto;">
    </div>
    <div style="width:100%;
        background-color: black;
        height:auto;">
    </div>
</div>
  </body>
</html>

Answer №4

If you want to adjust the height of an element, consider using vh as a unit instead of %. This way you can set the height with height: 100vh.

height: 100vh;

<div style="clear:both;display:flex; height: 100%;">
    <div style="width:400px; background-color: red;
        height:100vh;">
    </div>
    <div style="width:100%;
        background-color: black;
        height:width;">
    </div>
</div>

Answer №5

It's possible that the constraints of a more specific "height" attribute are causing this issue, especially if the div is nested within other tags. Consider creating a separate .css file for styling, as this is a recommended practice in HTML5. If you test the following code in a new html file, you should observe that the div has a 100% height:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div style="clear:both;display:flex; height: 100%;">
    <div style="width:400px; background-color: red;
        height:auto;">
        Hello 1
    </div>
    <div style="width:100%;
        background-color: black;
        height:auto;">
        Hello 2
    </div>
</div>

</body>
</html>

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 way to obtain the current cursor location in draft.js?

As part of my draftjs project, I'm working on implementing a feature that allows users to easily insert links. One approach I've taken is creating a popup that appears when the shortcut cmk + k is pressed. To enhance the user experience, I am cu ...

Modify an element upon clicking the mouse on an image

I am looking to dynamically change the paragraph element with className="details" to an editable input field when a user clicks on the image with className="edit-icon" within the same grid container. How can I achieve this functionality ...

Incorporating a line break within a string in specific situations

I am currently working on an older Sitefinity website that features a range of cut glass options on select product pages. These options are displayed as thumbnail images that can be clicked to open a popup window. The current code is structured to generate ...

Editing the dimensions of the input in Bootstrap 4

Is there a way to adjust the width of the input and select elements? I want the input element to take up more space while the select element occupies less space. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel=" ...

Overflow error in Rsuite table row cannot be displayed

Please see the image above Take a look at my picture. I am facing a similar issue. I am struggling to display my error tooltip over my table row. Has anyone else encountered this problem before? Any suggestions for me? I have attempted to set the overflow ...

Vue.js displaying incorrectly on Windows Server 2019 IIS post deployment

Once I run npm serve, everything runs smoothly. However, when I deploy my app with an API in an ASP.NET application, it doesn't scale properly. I am using Router and History for navigation. Anonymous user authentication is enabled and static content h ...

From structured CSS to streamlined LESS syntax

Being a UX designer, my goal is to streamline my workflow. I aim to transition from HTML to well-structured CSS to LESS as efficiently as possible. The concept is to input HTML, generate CSS, compile it to LESS, style it, and then recompile it back to CSS ...

Unable to access a PHP file within a JavaScript loop

In my HTML document, there is a button with an onclick method inside a JavaScript <script> tag. When this button is clicked, it triggers the deleteRow function, which is responsible for removing a row from a table on the page. Within the deleteRow f ...

It appears that the font in style.css is not being updated properly and seems to resemble

My issue lies within my CSS code. The text on my website is not displaying correctly and seems to be ignoring the styling that I have applied. Even though I have used similar styling on other buttons which look fine, this specific text element is causing p ...

Eliminate any empty spaces between the HTML tags

Is there a way to remove spaces between characters inside HTML tags? For example <option value="0" title="Advertising / Promotional Charges">Advertising / Promotional Charges</option> I tried using this code .replace(/\>\s+&bs ...

Not currently engaged with dynamic HTML components

I'm currently working on a drag and drop feature. After the drop event occurs, I dynamically create new HTML content and try to bind an event to it using the .on method. .on ( "event", "selector", function() { However, I'm encountering an issu ...

Why don't inline style changes implemented by JavaScript function properly on mobile browsers like Chrome, Dolphin, and Android?

View the jsFiddle here Issue on PC/Windows browser: When performing action, h1 header "gif" disappears and video starts playing. Problem on mobile devices: The gif does not disappear as expected but the video still plays indicating that JavaScript is fun ...

Where should I place my elements to ensure proper positioning in the Code Generator?

Hey everyone, I've been working on developing a code generator similar to Sketch2Code and I've hit a roadblock with my JSON structure. I'm trying to create an HTML page using the myData example provided with a predefined HTML structure. Thin ...

Implement automatic data replacement using PHP and MySQL triggers

The code provided below pertains to postar.php file include "conexao.php"; $consulta = mysqli_query($conexao, "SELECT titus.titus, titus.des, titus.link from titus"); while($postagem = mysqli_fetch_object($consulta)); echo "<d ...

"Upon invoking the console log, an empty value is being returned when attempting to access the

Why is console.log returning a blank line when trying to retrieve the value of a text input variable? HTML <label for="subject">Subject</label> <input type="text" id=" ...

What could be causing my bootstrap cards to not appear side by side?

Check out my code snippet where I've defined two Bootstrap cards: <div id="detailPanel" class="col-lg-12"> <div class="col-lg-12"> <div class="card"> <div class="col-lg-6"> <div class ...

Utilize Android accelerometer data to bring objects to life with animation

Utilizing an Android app, I am streaming accelerometer data to a Python script on my PC. The data is then saved to a text file. My goal is to utilize Javascript and jQuery to animate a 3D CSS cuboid (representing the device) to replicate the movements capt ...

What is the best way to superimpose two images with the same width but varying heights, where one remains static and the other scrolls seamlessly

I'm in the process of creating a webpage that will feature a .png image of a computer with a transparent screen. Upon this image, I plan to overlay a screenshot of a website and allow users to scroll through it as if they were navigating a real webpag ...

Is there a way to trigger a click event on an href using asp code behind?

Is there a way to programmatically trigger a click event on the href "shipping_question_ID_product_received_as_ordered_link" from the code behind of an ASP form? This href triggers a complex function in jquery when clicked by the user. I need to simulate ...

Translucent tonal backdrop hue

Is there a creative solution to dynamically increase the width of a border up to a certain point, using the thickness of the border as a progress bar indicator while reusing defined colors in stylesheets? Currently, I have the element with the border nest ...