Problem with adjusting the height of divs in CSS and HTML

Welcome to my JSFiddle project: https://jsfiddle.net/mkdjetkq/1/

I am currently working on developing a website that showcases a stunning region as part of a personal challenge.

I have put in a lot of effort trying to consolidate everything I've accomplished so far onto a single page. My goal is to eliminate scrollbars and ensure the entire webpage fits within one browser window without the need for scrolling, purely for aesthetic reasons.

Despite experimenting with div heights and various other elements, I haven't been able to achieve the desired outcome. Adjusting div heights using percentages hasn't yielded the results I want. What changes do I need to make to my code in order to accomplish this?

CSS:

    body,td,th {
        font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
    }

    body {
        width: 100%;
        height: 100%;
        margin: 0px;
    }

    #container {
        width: 100%;
        height: 100%;
    }

    #navigation {
        color: white;
        background-color: #292526;
        width: 99%;
        padding: 0.5% 0.5%;
    }

    #navigationLeft {
        width: 24.5%;
        display: inline-block;
        vertical-align: middle;
        font-size: 150%;
    }

    #navigationRight {
        width: 74.5%;
        display: inline-block;
        vertical-align: middle;
    }

    #navigation ul {
        float: right;
    }

    #navigation ul li {
        display: inline;
    }

    #navigation a {
        font-size: 100%;
        color: white;
        text-decoration: none;
    }   

    #banner {
        line-height: 0;
        height: 70%;
    }

    #cliffPic {
        width: 100%;
        height: 70%;
    }

    #footer {
        color: white;
        background-color: #181818 ;
        width: 99%;
        padding: 0.5% 0.5%;
        text-align: center;
    }

HTML:

<div id="container">

    <div id="navigation">
        <div id="navigationLeft">
            <a href="#">Visit Clare Ireland</a>
        </div><!--

        --><div id="navigationRight">
            <ul>    
                <li><a href="#">Home |</a></li>
                <li><a href="#">Maps |</a></li>
                <li><a href="#">Hotels |</a></li>
                <li><a href="#">Appartments |</a></li>
                <li><a href="#">Attractions |</a></li>
                <li><a href="#">Essentials |</a></li>
                <li><a href="#">Bars & Clubs |</a></li>
                <li><a href="#">Transport</a></li>
            </ul>
        </div>
  </div>

    <div id="banner">
        <img src="http://i.imgur.com/VsIRZNZ.jpg" alt="The Cliffs of Moher" id="cliffPic"/> 
    </div>

  <div id="footer">
        <p>Placeholder Text</p>
    </div>

</div>

Answer №1

Ensure your html height is set to 100% with html {height: 100%}

Make sure your box-sizing is set to border-box using * {box-sizing: border-box}. This will prevent padding from affecting the overall width/height of elements.

Set the height of your navigation to 20% with #navigation {height: 20%}

Adjust the banner height to 70% with #banner {height: 70%}

Set the footer height to 10% using #footer {height: 10%}

If you notice image stretching, consider setting it as a background within the div and setting background-size to cover like so:

#banner {background: <background>; background-size: cover}

Remember that for percentage heights to work properly, the immediate parent element of a child element must have a set height. Ensure this cascades all the way back to the first set height ancestor, in this case, the html element needed the 100% height to resolve the issue.

Answer №2

If you wish to hide the scroll bar while maintaining a height of 100%, simply add html { overflow-y: hidden; } to your stylesheet. Just remember, if you go over 100% height, the scroll bar will disappear entirely so be sure to utilize appropriate media queries for responsiveness.

Answer №3

Check out this updated css:

1) The height was not being set properly due to the html tag not having a 100% width and height. To ensure control over element heights, always specify that both the html tag (the outermost container of the html document) and body tag have a height of 100%.

2) As the size of the menu may vary on different screen sizes, using a fixed height ratio for elements is not ideal. To address this, I implemented css flexbox in your code, allowing the menu, banner, and footer to adapt to fill the remaining space on the page without overflowing.


body,td,th {
    font-family: Consolas, "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", Monaco, "Courier New", monospace;
}
body, html {
    width: 100%;
    height: 100%;
    margin: 0px;
}
#container {
    width: 100%;
    height: 100%;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-direction: column;
    flex-direction: column;
}
#navigation {
    color: white;
    background-color: #292526;
    width: 99%;
    padding: 0.5% 0.5%;
}
#navigationLeft {
	width: 24.5%;
	display: inline-block;
	vertical-align: middle;
	font-size: 150%;
}
#navigationRight {
	width: 74.5%;
	display: inline-block;
	vertical-align: middle;
}
#navigation ul {
	float: right;
}
#navigation ul li {
	display: inline;
}
#navigation a {
	font-size: 100%;
	color: white;
	text-decoration: none;
}
#banner {
    line-height: 0;
    -webkit-flex: 1; 
    flex: 1;
}
#cliffPic {
	width: 100%;
}
#footer {
	color: white;
	background-color: #181818 ;
	width: 99%;
	padding: 0.5% 0.5%;
	text-align: center;
}

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 set the width of a nextjs Image using CSS styles

Do you think it's more beneficial to use 'next/image' rather than the traditional img tag? If so, why? I am trying to display an image by specifying width and height in styles using a class called 'img', like this... <img class ...

What is the best way to ensure items are aligned to the top of the navbar as a picture is enlarged?

I have searched through various similar questions, but none of them have provided a solution to my specific issue. I have attempted multiple suggestions, but none have been successful. (I am working with bootstrap4) Here is the html code I am using: < ...

Achieving vertical centering for content within :before/:after pseudo-elements

I'm striving to replicate a similar layout as shown in the image: Within a div element containing an image as part of a slideshow, I have utilized :before and :after pseudo-elements to create controls for navigating to the next (>>) or previous ...

Tips for identifying HTML tags that include a specific attribute automatically

Is there a way to automatically identify an HTML tag that contains a specific attribute like data-wow-delay? This query revolves around wow.js. The goal is to have the classes wow bounce added automatically to any HTML tag that has this particular attribu ...

The Jquery Ajax .load() function is not working properly

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Tabs - Expanding content</title> <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> < ...

Display endless data within a set window size

I am looking to create a fixed-size window for displaying text from the component <Message/>. If the text is longer, I want it to be scrollable within the fixed window size. See screenshot below: Screenshot export default function AllMessages(){ ...

Highchart displays text centrally on legend hover

I am struggling with the code provided here. My goal is to make text appear in the center of the donut chart when hovering over the legend. Similar to how it works when hovering over a single piece of the donut chart. var chart = new Highcharts.Chart ...

Implement a trigger to activate toggling functionality

<span id="test">​click</span>​​​​​​​​​​​​​​ $('#test').toggle(function() { alert('true'); }, function() { alert('false'); }); $('#test').trigger('click') ...

The functionality of CSS scroll snap does not seem to be compatible with the CSS Grid

When utilizing CSS scroll snap with Flexbox, the snapping functionality works adequately: * { box-sizing: border-box; margin: 0; padding: 0; } .slider { font-family: sans-serif; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vw ...

Storing HTML table data in a MySQL database will help you

Operating a website focused on financial planning where users can input various values and cell colors into an HTML table. It is crucial to uphold the integrity of these HTML tables. How can I store the complete HTML table (including values and colors) i ...

Is there a way to reveal multiple inputs by simply pressing a button?

Currently, I am working on developing a website using HTML, CSS, and JavaScript. As part of the process, I am looking to implement a login interface. I have already included some input fields and written a sign-in.js script that verifies whether a user h ...

Responsive CSS for symmetrically-aligned DIVs that float on screens of varying widths

[ 1 ] [ 2 ] [ 3 ] [ 4 ] My design features four floating DIVs, each set to float left with a width and height of 128px. When the screen is narrowed, the last DIV correctly moves to the next line: [ 1 ] [ 2 ] [ 3 ] [ 4 ] However, I want the last two bl ...

css center arrow-aligned underline text

Trying to emphasize my text with an additional arrow centered on the line is proving to be a challenge. When attempting this, the text does not align in the center of the page. .souligne { line-height: 17px; padding-bottom: 15px; text-transform: u ...

Formatting and designing text within anchor tags

I need assistance with properly displaying a list of links in a navigation structure. Here is the current HTML code I am using: <nav> <ul> <li><a href="#"><span class="code">1</span><span class="name">One< ...

Unable to adjust the size of the icons sourced from https://github.com/erikflowers/weather-icons

While integrating openweathermap with these icons, I encountered an issue where I couldn't adjust the size of the icons. They are used similarly to font-awesome icons, like this: <i class="wi wi-omw-100"></i> I have tried changing various ...

I am utilizing React to pull data from the database with a one-day discrepancy in the backend

Despite trying numerous solutions, I am still puzzled as to why this issue persists. For instance, in my database, there is a date listed under the "date_of_birth" column as 2023-12-29. However, when examining my backend code, it displays as "date_of_birth ...

Adding a bootstrap label on the corner of a div using overlay technique

I'm attempting to superimpose a bootstrap label in the corner of a DIV. My goal is to position the label in the top left corner of the div, partially overlapping it. Thank you <div class="span4" style="border-bottom: none; border-top: 4px s ...

How to Insert a Hyperlink Beside a Button Using HTML and CSS

Is there a way to add a link next to the button on the right side of the screen, and keep them on the same line? I've tried different methods but the link always ends up being displayed above or below the button. .dropdown-menu { position: absolu ...

What initiates the call for CSS?

During his instructional video, the creator visits the CodeIgniter website at . When he initially arrives at the site (at 1:32), it appears somewhat odd, almost as if it lacks CSS styling. However, after refreshing the page (1:37), it displays properly. ...

Guide to achieving a powerful click similar to a mouse

I've been struggling to get an audio file to play automatically for the past three days, but so far I haven't had any luck. The solutions I've tried didn't work in my browser, even though they worked on CodePen. Can anyone help me make ...