How to properly position the h1 heading in relation to the navigation bar

Web developer specializing in server-side technologies here, venturing into the world of styling for the first time.

I recently attempted to recreate the header section with the id=topbar from a specific website. Despite my efforts to align the h1 element (Scott Hanselman) with the navigation list items (About, Blog, etc.), I struggled with using properties like float: left and display: inline to overcome the block nature of the h1. Unfortunately, my attempts were unsuccessful!

While experimenting with the CSS using Chrome Dev Tools, I couldn't figure out how the elements were perfectly aligned within the containerInner. Toggling the inherited margin property made the navigation items below the h1, behaving as expected for a block element.

My main question is, why isn't the h1 element taking up all available horizontal space? It seems like my assumptions about using the float and display properties were off the mark.

Here's what I've come up with so far:

HTML
<!doctype html>
<head>
    <meta charset="utf-8">
    <title>First Last</title>
    <link rel="stylesheet" href="blog_style.css" type="text/css" />
</head>

<body>
    <div class="container">
        <div class="top-ribbon-outer">
            <div class="top-ribbon-inner">
                <h1>First Last</h1>
                <nav>
                    <ul>
                        <li>Item 1</li>
                        <li>Item 2</li>
                        <li>Item 3</li>
                        <li>Item 4</li>
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</body>

</html>

CSS:

body {
    font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 15px;
    margin-left: 50px;
}

.top-ribbon-outer {
    width: 100%;
    height: 50px;
    color: white;
    background-color: black;
    position: relative;
}

.top-ribbon-inner {
    height: 20px;
}

h1 { 
    margin-bottom: 0px;
    margin-top: 0px;
    float: left;
    /*display: inline;*/
}

li { 
    float: left;
    padding-left: 15px;
    padding-right: 15px;
}

ul {
    list-style-type: none;
}

Answer №1

To ensure proper alignment, make sure to use display:inline-block for both the ul and h1 header elements. Here is how you can implement it:

h1 { 
    margin-left: 10px;
    margin-top: 10px;
    float: left;
    display: inline-block;
}

ul {
    list-style-type: none;
    display:inline-block;
    float:right;
}

Check out this JsFiddle link for a live demo.

Answer №2

Forget about adjusting the height of the parent element. Just apply overflow: hidden and it will automatically contain its floating children.

CSS:

body {
    font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-size: 15px;
    margin-left: 50px;
}

.top-ribbon-outer {
    width: 100%;
    color: white;
    background-color: black;
    position: relative;
}

.top-ribbon-inner {
    overflow: hidden;
}

h1 { 
    margin: 0;
    float: left;
    padding: 5px;
}
nav {
 float: right;
}

li { 
    float: left;
    padding-left: 15px;
    padding-right: 15px;
}

ul {
    list-style-type: none;
}

Check out the Demo here

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

Adjust the height of a sibling div using JavaScript with mouseover event

I need assistance with creating an audio visualizer effect where on mouseover, "this" div becomes the tallest one and the other divs adjust their heights accordingly. How can I specify the specific height for each sister div to change to? Code window ...

chrome side column must be set to 100% height

Having trouble with setting height to 100%. I have a list of items that need to be distributed to match the height of the side image. It works fine when I use a fixed height, but not with a percentage. The issue arises because the website is responsive a ...

Fixed first column in a spacious data grid

The layout I have created can be viewed in the Fiddle http://jsfiddle.net/5LN7U/. <section class="container"> <section class="field"> <ul> <li> Question 1 </li> <li> question 2 </ ...

how to style a page with a CSS arrow shape

Can someone help me figure out how to create an arrow similar to the one in this button using CSS? https://i.sstatic.net/gQi0l.png I've been able to create triangle-like arrows like the one below #triangle_arrow { top: 3pt; content: ...

Can the size of a linear gradient be predetermined in CSS?

When working with CSS, it's possible to set various properties of a linear gradient such as color, position, orientation, and width. However, is it feasible to define its length? I prefer not to use SVG and am specifically looking for a CSS-based solu ...

Reverting back to the specified CSS style

In my application, I have a common HTML component styled as follows: .box { min-width: 100px; padding: 20px 10px; } There are over 100 of these components and they each have a unique border style without a bottom border: .box:nth-child(1) { border ...

Discovering DOM elements positioned between two other well-established DOM elements can be achieved by utilizing a variety of methods

What is the most efficient and elegant way to select all elements between two specified elements using vanilla JavaScript? For instance, how can I target all elements between: <h2> and <hr> .. and the result should be an [ p, p, ul ] ... but & ...

Arranging Nav Items in a Stack with Bootstrap 4

Struggling to create a unique navigation bar design with fixed elements on both the left and right sides. The challenge arises when scaling down to mobile, as the icons on the right end up stacking. Any suggestions on how to resolve this issue? <div ...

Enhancing Jquery Accordion with a new attribute for seamless transitions

I'm looking to adjust the font size for the title section of the accordion found here body { font: 16px Sans-Serif;<br> } The challenge I'm facing is that I already have a "body" attribute in my site that overrides the code above. Is th ...

Shift the "Login link" to the right side of the Bootstrap navbar

Currently working on a Django website and incorporating Bootstrap/CSS/HTML to enhance the design. I've set up a navbar menu and want the login tab on the right side. However, despite my attempts, I'm not achieving the desired outcome: The HTML ...

Can the background color of HTML header text be altered using JavaScript in JQGRID?

Can the background color of HTML header text be modified using JavaScript? Updated: Oops, I forgot to mention that it is for the header text in jqGrid. My apologies for that oversight. ...

Verify whether the division contains any elements and does not include specific elements

I previously opened a similar thread, but with the condition that the div does not contain a second element within it. So my previous question was as follows: I have some code that looks like this: <p class="elementWrap"> <label>Phone</l ...

What is the best way to create a website cover image with a set height and a width that expands?

Is there a way to have a cover image on a web page that expands in width as the screen size increases, but maintains a fixed height? I found a page with the desired effect that I want to replicate: Below is the link to my page where I want to implement t ...

When I switch to smaller devices, the content in my div gets covered by the following div

I'm struggling with my bootstrap setup. When I switch to small devices, my content overlaps with the next div. I've tried adjusting margins and positioning, but nothing seems to solve the issue. I suspect it has something to do with the relative ...

Steps to create interconnected circles using CSS

Currently, I am attempting to create a set of steps using only CSS. Here is what I have achieved so far: https://i.sstatic.net/sCRzr.jpg However, I am facing an issue with removing the excess line where the red mark is located. <ul class="step&q ...

Tips on how to align text vertically next to an image

I am struggling to vertically center the text alongside an image on my website. Despite my efforts, I can't seem to get it right. Do you have any suggestions on why this might be happening? I've tried searching for a solution but haven't had ...

Increased Z-index on an unfamiliar site

I'm in the process of enhancing the Moodle platform at my school with a userscript, and I want to create a sleek UI for it. One feature I want to add is a progress bar that stays fixed at the top of the browser viewport so that it remains visible as ...

What is the best way to ensure that when a div is opened, the information to the right does not get pushed down?

I have a functioning menu bar, but when it expands, the content below is pushed down even though it should not be affected. How can I adjust my code to prevent this issue? View my website here: <div id="menu_wrapper"> <div id="menu"> &l ...

Issue with fixed positioning not behaving as intended while scrolling downwards

I am facing an issue with an element that has the .it class and I want to keep it fixed on the screen using the position: fixed CSS property. However, as I scroll down the page, the element moves along with the screen instead of staying in place. Below is ...

rearranging nodes from multiple arrays into a single array and then repositioning them within the parent array using angularjs

[![enter image description here][1]][1]I am working with AngularJS and I have multiple arrays named list1, list2, and list3. Each array is used in a different list. Within my application, there are two buttons and two divs - the left div displays elements ...