Preserving spacing using minimum widths

Looking for some help with a page layout issue. I have a header on my page with a title and menu. Currently, the header has a margin and a minimum width set to the width of the menu. However, when the user resizes the window and reaches the minimum width, the right-hand margin disappears. I want the margin to be included in the minimum size of the header. Any suggestions?

Check out the HTML:

    <!DOCTYPE HTML>
    <html>
        <body>
            <div id="header">
                <ul id="title">
                    <li>
                        Title
                    </li>
                </ul>
                <ul id="menu">
                    <li>Home</li>
                    <li>Profile</li>
                    <li>Projects</li>
                    <li>News</li>
                    <li>Contact</li>
                    <li class="stretch"></li>
                </ul>
            </div>
        </body>
    </html>

Below is the CSS:

    html, body {
        margin: 0;
        border: 0;
        padding: 0;
        width: 100%;
        height: 100%;
        font-family: Sans-Serif;
        font-size: 1em;
    }

    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
    }

    #header {
        background-color: orange;
        margin: 1em;
    }

    #title {
        font-size: 2em;
        font-weight: bold;
        background-color: red;
        display: inline-block;
    }

    #title li {
        position: relative;
    }

    #menu {
        width: 40em;
        background-color: blue;
        text-align: justify;
    }

    #menu li {
        display: inline;
    }

    #menu .stretch {
        display: inline-block;
        width: 100%;
    }

And finally, the JavaScript:

    $(function() {
        //SET MINIMUM WINDOW WIDTH TO MENU WIDTH
        var menu = $('#menu'),
            header = $('#header');

            header.css('min-width', menu.width());

        $(window).resize(function() {
        });
    });

If you want to see a live example, check out this JSfiddle: http://jsfiddle.net/L3u7nLgz/

Answer №1

Your .menu class has been modified to:

#menu {
    /*width: 40em;*/
    margin-right: 1em;
    background-color: blue;
    text-align: justify;
}

It might be best not to set a width here, and simply apply a margin to the ul element to achieve the spacing you desire.

http://jsfiddle.net/R2y9pQmK/1/

Update: Instead of using margin, consider using padding on the .header element.

#header {
    background-color: orange;
    margin: 1em;
    padding-right: 1em;
}

http://jsfiddle.net/R2y9pQmK/2/

Answer №2

After exploring various options, I came up with a unique solution to the issue at hand. By incorporating transparent borders into the header element, I was able to address the problem in a way that may not be conventional, but proved to be effective and consistent for my needs.

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

Obtain the WordPress footer while applying a specific CSS rule

I need to customize my Wordpress template so that a specific css class is applied to a certain element. Currently, I am loading the footer in my templates using the <?php get_footer(); ?> function. However, I would like to load the footer with the . ...

Stopping the <nav> element from displaying as "unlabeled section" on HTML5 webpages

As I work on incorporating proper sectioning in HTML5 with sectioning elements and headlines to meet my customer's design goals (while adhering to certain restrictions), I have encountered a challenge. The basic layout requested is as follows: <b ...

Issues with the animation of the navbar menu button are preventing it from functioning

I have been attempting to incorporate animation when the navbar-button is toggled on smaller screen sizes. Inspired by the design of .navbar-toggle.larr in this particular template, I tried to implement a similar effect. /* ANIMATED LEFT ARROW */ .navbar- ...

Displaying an error or success message upon submitting a form is currently not functioning as

I have implemented a registration/login system on my website, and currently, it shows errors and success messages at the top of the page. I want to display these messages under the form instead. However, every time I attempt to place them below the form, i ...

HTML - What steps can I take to ensure my website displays appropriately on various screen sizes?

Currently, I'm having some trouble with the appearance of my website. Let me explain: I've written some basic code (Please note that this code includes margins. I'm not sure if this is causing the issue) but the content doesn't display ...

Experiencing difficulty inputting data into database using PDO

Attempting to save user input data to a database using PDO, I have created a form for users to input their information. However, when I submit the form, it redirects to proses_input.php and displays a blank page. I tried combining the code into one file, b ...

Attempting to execute an SQL query in order to populate a dropdown menu by utilizing AJAX technology

Here is the code snippet I am working on: //Logic.... $companyId = $_POST['var1']; global $db; $query = "SELECT firstname, surname FROM contact WHERE directorycompany_id = " . $companyId; $result = $db->query($query); $total = $result->num ...

Flex and Bootstrap 5.1.3 divs are not aligned with the prices

I have implemented flex from here Whenever the content text increases in size, the div and the price do not remain aligned. See image here. My goal is to achieve the following: Make sure the div maintains the same size whether the content text is long ...

Looking for help with aligning an icon to the right side of my text

I'm struggling with aligning the icon to the right of the quantity. When I use float:right, it places the icon at the far right of the cell instead of next to the text. Is there a way to get it to be on the right side of the text but directly adjacent ...

What is the best way to determine the midpoint index of an array?

As a newcomer, I have a question regarding a recent assignment. The task involves creating a global array where objects are imported as they are entered into an input field on the HTML document. The challenge is to update the current list on the document ...

Exploring the Benefits of Using a Try-Catch Block for Creating an Audio Element

While reviewing the Jbox plugin code, specifically focusing on the audio addition part, I encountered the following snippet of code: jBox.prototype.audio = function(options) { options || (options = {}); jBox._audio || (jBox._audio = {}); // ...

Eliminating the need for horizontal scrolling in the window causes a change in the height of an internal DIV element

Check out this snippet of code, a simplified version of a larger piece: html, body { margin: 0 !important; padding: 0 !important; /* overflow-x: hidden; /* uncomment this line */ } .app { position: relative; width: 100%; text-align: center !important; } ...

Incorporate row numbering feature into jQuery DataTables

Is there a way to have jQuery datatables generate a row number column in the first column, similar to a datagrid in VB? This is what I'm aiming for: If you have any insights on how this can be achieved, please share! ...

Choosing the Right Project for Developing HTML/Javascript Applications in Eclipse

Whenever I attempt to build a webpage using eclipse, I am presented with two choices: -- A Javascript project -- A Static web project If I opt for the former, setting up run to open a web browser can be quite challenging. If I decide on the latter ...

What is the best way to send the $_SESSION['var'] array to jquery and initiate an ajax request?

I'm dealing with an issue here. I need to retrieve all the items within the $_SESSION['cart'] array and pass it to jQuery so that it can be used in a php-ajax file. My question is, how can this be accomplished? This is what I have in mind: ...

When initializing a new instance of Stripe using cartalyst/stripe-laravel, an error occurs stating "The Stripe API key has not been properly defined!"

I am eager to explore Stripe and learn how to process payments. Currently, I am utilizing the cartalyst/stripe-laravel package to create a new instance of Stripe following the instructions provided here. The code snippet should resemble this: $stripe = ...

Tips for customizing the back button in Ionic 3

<ion-header> <ion-navbar> <ion-buttons left> <button ion-button navPop icon-only> <ion-icon ios="ios-arrow-back" md="nbsons-arrow-back"></ion-icon> </button> </ion-buttons> <ion-title> ...

Choosing descendant elements in CSS styling

Is there a way to select child elements in sequence? For instance: <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li> ...

limiting jquery selector to target only the td elements within a table

I have a table generated by asp.net that functions as a slide show. To make the slide show work properly, I need to select only the root td elements of the table. This becomes an issue when the table has multiple rows. The jquery cycle plugin is being used ...

What could be causing the issue of $.ajax being undefined while utilizing jQuery in Node.js?

I'm currently testing a module on Node.js that is meant for client-side use. In order to make $.ajax work, I need some guidance. To begin with, I have installed jQuery on the project using: $ npm install jQuery Despite this, when I try to access $. ...