What's the reason for the element not inheriting margins from its parent?

I am facing an issue with centering an h1 header within a .banner element. When I try to apply margin to the header, it appears to indent not from the top border of the banner, but rather from the nav element located above the banner.

After inspecting the screenshot using Chrome's tool, it is evident that the heading indents based on where the nav element ends.

I am seeking advice on identifying the problem and finding a solution. Any help or tips would be greatly appreciated.

screenshot

.container {
width: 1024px;
margin: auto;

overflow: hidden;
}


a {
text-decoration: none;
}

/*
***********
* =banner
***********
*/

.banner {
background: url(../img/banner1.jpg);
width: 1024px;
height: 580px;
margin: auto;
}

.banner h1 {
text-align: center;
font-size: 100px;
font-weight: normal;
margin: 200px;

}
/*
***********
* =nav
***********
*/

nav {
margin-left: -35px;
margin-top: 0;
margin-bottom: 0px;
position: relative;
}
nav li {
display: inline;
}

nav .left-nav {
margin-top: -20px;
margin-right: 0px;
width: 200px;
float: right;
position: relative;
padding-top: 20px;
}

#login {
position: absolute;
top: 11px;
right: 0;
}

header h3 {
text-indent: -9999px;
}

header h3:after {
clear: both;
display: block;
content: ' ';
visibility: hidden;
height: 1%;
}

header h3 a {
background: url(../img/cart.png) no-repeat;
width: 48px;
height: 44px;
position: absolute;
top: 10px;
left: 0;
padding: 0;
}

header h3 a:hover {
background-color: white;
}

nav a {
font-family: 'Indie Flower', cursive;
font-size: 26px;
letter-spacing: 3px;
margin-right: 5px;
color: #0d0d0d;
padding: 10px 5px;
}

nav a:hover {
background-color: #e8e8e8;
}
<body>
<div class="container">
<header>
<nav>
<ul>
<li><a href="#">Прайслист</a></li>
<li><a href="#">Подбор букета</a></li>
<li><a href="#">Контакты</a></li>
<li><a href="#">Доставка</a></li>
<div class="left-nav">
<li><h3><a href="#" ></a>Корзина</h3></li>
<li><a href="#" id="login">Войти</a></li>
</div>
</ul>
</nav>
<div class="banner">
<a href="#" id="left"></a> <!-- image replacement лучше сделать в css без картинки -->
<a href="#" id="right"></a> <!-- image replacement -->
<h1>Цветочный Дом Полины</h1>
<ul>
<li id=visited><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
</div>
</header>

<div class="main">
<div class="news">
<h2>Новости нашего магазина</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
</p>
<img src="#" alt="Heart-shaped box of flowers">
</div>

<div class="portfolio">
<h2>Наши последние работы</h2>
<div class="image-container">
<img src="#" alt="Пример 1">
<img src="#" alt="Пример 2">
<img src="#" alt="Пример 3">
</div>
<p>
Во все времена цветы олицетворяют счастье, улыбку, радость, прекрасное настроение и чудеса. Именно цветами Вы можете рассказать о своих чувствах дорогим сердцу людям, поэтому многие тысячелетия люди стараются украсить свою жизнь и жизнь близких этими ароматными малышками. В любой момент жизни цветы порадуют и вызовут искреннюю улыбку на лице.
</p>
</div>
</div>
</body>

Answer №1

Instead of using margin inside the .banner h1, you can opt for padding-top:185px;

.container {
width: 1024px;
margin: auto;

overflow: hidden;
}


a {
text-decoration: none;
}

/*
***********
* =banner
***********
*/

.banner {
background:url('https://images.designtrends.com/wp-content/uploads/2015/12/10131036/Yellow-Flower-Background.jpg') !important;
width: 1024px;
height: 580px;
margin: auto;
}

.banner h1 {
text-align: center;
font-size: 100px;
font-weight: normal;
margin: 200px;
padding-top:185px;
}
/*
***********
* =nav
***********
*/

nav {
margin-left: -35px;
margin-top: 0;
margin-bottom: 0px;
position: relative;
}
nav li {
display: inline;
}

nav .left-nav {
margin-top: -20px;
margin-right: 0px;
width: 200px;
float: right;
position: relative;
padding-top: 20px;
}

#login {
position: absolute;
top: 11px;
right: 0;
}

header h3 {
text-indent: -9999px;
}

header h3:after {
clear: both;
display: block;
content: ' ';
visibility: hidden;
height: 1%;
}

header h3 a {
background: url(../img/cart.png) no-repeat;
width: 48px;
height: 44px;
position: absolute;
top: 10px;
left: 0;
padding: 0;
}

header h3 a:hover {
background-color: white;
}

nav a {
font-family: 'Indie Flower', cursive;
font-size: 26px;
letter-spacing: 3px;
margin-right: 5px;
color: #0d0d0d;
padding: 10px 5px;
}

nav a:hover {
background-color: #e8e8e8;
}
<body>
<div class="container">
<header>
<nav>
<ul>
<li><a href="#">Прайслист</a></li>
<li><a href="#">Подбор букета</a></li>
<li><a href="#">Контакты</a></li>
<li><a href="#">Доставка</a></li>
<div class="left-nav">
<li><h3><a href="#" ></a>Корзина</h3></li>
<li><a href="#" id="login">Войти</a></li>
</div>
</ul>
</nav>
<div class="banner" style="background:#ccc;">
<a href="#" id="left"></a> <!-- image replacement лучше сделать в css без картинки -->
<a href="#" id="right"></a> <!-- image replacement -->
<h1>Цветочный Дом Полины</h1>
<ul>
<li id=visited><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
</div>
</header>

<div class="main">
<div class="news">
<h2>Новости нашего магазина</h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. 
</p>
<img src="#" alt="Heart-shaped box of flowers">
</div>

<div class="portfolio">
<h2>Наши последние работы</h2>
<div class="image-container">
<img src="#" alt="Пример 1">
<img src="#" alt="Пример 2">
<img src="#" alt="Пример 3">
</div>
<p>
Во все времена цветы олицетворяют счастье, улыбку, радость, прекрасное настроение и чудеса. Именно цветами Вы можете рассказать о своих чувствах дорогим сердцу людям, поэтому многие тысячелетия люди стараются украсить свою жизнь и жизнь близких этими ароматными малышками. В любой момент жизни цветы порадуют и вызовут искреннюю улыбку на лице.
</p>
</div>
</div>
</body>

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

Flashing text compatibility across all browsers

I'm looking for a way to create text that blinks on all major web browsers. Initially, I attempted using the <blink> HTML tag, but unfortunately, it only works in Mozilla Firefox. Next, I experimented with CSS: <style> .blink {text-deco ...

Trouble with selecting a color from the picker tool

Working with HTML markup often presents challenges, especially when it comes to using a color picker on Mac OS. I find that trying to match the exact background color of a div element by picking up color from an image can be tricky. After using the Mac co ...

What are the reasons behind the lack of smooth functionality in the Bootstrap 4 slider?

My customized bootstrap4 slider is functional, but lacks smoothness when clicking on the "next" and "prev" buttons. The slider transitions suddenly instead of smoothly. Any suggestions on how to fix this? Here is the code for the slider: $('.carous ...

Scraping the web using Python for HTML data retrieval

I'm working on a Python program to retrieve the card sub-brand and brand based on a card BIN number. The URL for this information is: . The code snippet below fetches the card type from the page. page = requests.get('https://www.cardbinist.com/s ...

What is the best way to prevent the navbar from covering content? The navbar-static-top option helps, but it doesn't allow for fixed-scroll

I am facing an issue with my navigation bar while using asp.net. When I apply the class navbar-fixed-top, the navigation bar becomes fixed and scrolls with the page, but it overlaps the content in the contentplaceholder However, when I switch to the clas ...

How can you use C# code to control the visibility of a table row in HTML?

I am trying to display or hide a table row based on the current month using DateTime.Now.Month in my HTML code, but I can't seem to remember the correct syntax. The code I have tried is not working as expected. Can anyone help me with the correct synt ...

Displaying XML data in an HTML table

Encountered a challenge while fetching data from an external XML document using JS. Following the w3schools tutorial for AJAX XML, but faced an issue I couldn't resolve. The XML structure is as follows: <root> <document-id> <author ...

From Table to DIV: A Simple Conversion

I have encountered a major issue that has stumped me so far. Perhaps you can assist me. I am dealing with a table that appears as follows: __________________________________ | | time1 | time2 | time3 | +--------+-------+-------+-------+ | John | ...

When the mouse leaves, the background image will revert back to its original setting

https://i.stack.imgur.com/Ojd0Q.pngI need assistance in reverting a div's background image back to its default state using the onmouseleave method. Below is the HTML and JS code I have been working on: <script type="text/javascript"> functi ...

Begin the div at a width of 0 pixels and expand it towards the right

My current project involves opening a row of blocks with an animation. The desired outcome is to transition from 2 blocks to 3 blocks by clicking a button. Specifically, the block that needs to be added should appear in the middle and fade in from left to ...

Ensure that the div expands to accommodate the content

I'm experiencing an issue where the content within a div with a blue border exceeds the boundaries of the div when it expands. How can I make sure that the div extends to contain all the content? Here is a demonstration of the problem: When I expand ...

An unusual 1px variance appears in the background-image on Internet Explorer

Having trouble with a sprite of two images showing a 1px difference in ALL versions of Internet Explorer, yet working perfectly in Firefox. Check out the demonstration here: http://jsfiddle.net/bHUs3/6/ I'm feeling frustrated. What could be causing ...

Reading Properties in VueJS with Firebase

<template> <div id="app"> <h1 id="title"gt;{{ quiz.title }}</h1> <div id="ques" v-for="(question, index) in quiz.questions" :key="question.text"> <div v-show="index = ...

What could be causing my AJAX script to not execute upon form submission?

My form's ajax functionality is not working properly. When I try to submit the form, nothing happens. Can someone please assist me with this issue? $( '#my-form' ) .submit( function( e ) { $.ajax( { url: 'u_mainslid ...

What is the best way to create an answer label box that validates your response?

I am looking to design a question box label that resembles a search box. In this unique setup, users will have the option to input their answer into the question label. For example, if the user types 'kfuffle' as the answer, they will automatical ...

The custom attribute in jQuery does not seem to be functioning properly when used with the

I am currently working with a select type that includes custom attributes in the option tags. While I am able to retrieve the value, I am experiencing difficulty accessing the value of the custom attribute. Check out this Jsfiddle for reference: JSFIDDLE ...

Use the Segoe UI typeface on Internet Explorer for a sleek

I have implemented a custom Segoe UI font in my CSS file. While it displays correctly in Chrome, I am facing an issue where IE and Firefox are showing the default font in certain areas. Below is the snippet of the CSS code: body { margin:0px auto; ...

Manipulate classes based on scrolling (Wordpress)

Trying to manipulate a widget by adding and removing classes based on user scroll behavior has proven to be quite challenging. Specifically, I aim to add one class when the user scrolls down by 50px and remove it when they scroll back up. Website: Check o ...

Tips for transferring the input text box value to angularjs when clicking on the text box

Here is the HTML code snippet: <tr ng-repeat="c in client"> <td><input type="text" id="id" value="{{c.id}}" onclick="display();"></input></td> <td><input type="text" value="{{c.fname}}"></td> </ ...

Creating a new row with a dropdown list upon clicking a button

I want to include a Textbox and dropdown list in a new row every time I click a button. However, I seem to be having trouble with this process. Can someone assist me in solving this issue? Thank you in advance. HTML <table> <tr> ...