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

Placing the ul tag within a div at the top of the div for proper alignment

I am facing an issue with my code where I cannot get the UL element to appear at the top of the div as it is a child element. <!doctype html> <html> <head> <title>xxx</title> </head> <body> <div id="page" style ...

Mixing strings with missing slashes in links

console.log(mylink) When using the console to log mylink, the expected result is a normal link like http://example.com/something.jpg. I tried concatenating it as shown below: var html = '<div id="head" style="background:linear-gradient(rgba(0, 0 ...

Limitations of HTML and CSS on Android browsers

i have developed a website that can be found at this address : the site is experiencing some issues on android tablets browsers for instance : i suspect that some divs with the property margin:0 auto are not being centered on the page i am curious to k ...

Having trouble setting the image source in HTML with Node.js

I am a beginner with nodeJS and I am having trouble setting the src attribute of an img tag in my client side html. My node server is running on port 3000 and everything works fine when I visit http://localhost:3000. Here is the code from my server.js fil ...

Enhance your website with a stylish drop-down menu from inc.com

I am trying to create a drop and hide menu effect similar to the one on Here is my current demo: http://jsfiddle.net/elementhttp/56ThC/ $("#1").mouseover(function(){ //alert("aaaaaaaaaa"); just for testing $(".block2").stop().fadeToggle(700 ...

Unable to display images for Wordpress posts within list items

I am currently using this code to retrieve all images uploaded to a post within a specific category. The intention is to have these images displayed in a slider as list elements, but the current implementation looks like this: <li> <img...> &l ...

Steps for inserting a button within a table

Currently, I have implemented a function that dynamically adds the appropriate number of cells to the bottom of my table when a button is clicked. This is the JavaScript code for adding a row: <a href="javascript:myFunction();" title="addRow" class= ...

Tips for positioning elements on an HTML page by utilizing CSS styling

I have a primary container with the following CSS: .major { width:30%; height:100%; position:absolute; } Now, I want to insert web information at the bottom part which includes: About | Terms | Policies | Contact Us This content will be within the .web ...

Implement a unique InfoWindow for every individual polygon within the Google Maps API

Currently facing difficulties in implementing an infowindow for each polygon that has been created. Attempted various code samples from different sources without success. Below is the existing code snippet, but clicking on a polygon does not trigger any ac ...

How can you tap into local storage in CSS while utilizing a chrome extension?

Can I access local storage from a Chrome extension's options file using CSS? Is it possible to use JavaScript to define a variable that can be utilized in CSS, or is local storage only accessible through JavaScript? If local storage is restricted to J ...

What is the reason behind H1 tags not requiring a class or ID in CSS?

As I was reviewing my CSS code, I noticed that the h1 tag is defined like this: h1 { .... } Unlike everything else in my code, which either has an id "#" or a "." class preceding it. This made me wonder why header tags don't require this. Could it b ...

Improving performance by incorporating multiple SVGs or Icon Fonts onto a webpage

As I was planning to incorporate SVG for the numerous icons on my website, concerns have been raised about the potential impact on site speed. With multiple search results on each page and several icons per result, another developer advised against using S ...

PHP script not recognizing CSS styles

My coding dilemma involves a script that is supposed to display numbers from 1 to 100 in black font. For multiples of 3, it should show "Three" in green, and for multiples of 7, it should display "Seven" in blue instead of the number. If a number is a mult ...

What is the functionality of CSS radio tabs?

Can anyone provide a breakdown of how the final section of this code operates? Specifically: [type=radio]:checked { } [type=radio]:checked ~ .content { z-index: 1; } I am brand new to CSS and wanted to experiment with creating interactive CSS tabs, whi ...

Emphasize user-entered text using HTML and CSS

Here's a scenario: a text paragraph is displayed alongside an input box. The user must type the same text into the input box as shown in the paragraph. I'm aiming to highlight the paragraph text in color as the user types, to demonstrate progres ...

Aligning an image vertically within a division

I am currently working on aligning images vertically within an image rotator (SlideDeck). At the moment, all the images are aligned at the top but I need them to be centered. The heights of the images vary. I have tried several methods, including absolute ...

Retrieving information from CRM online utilizing the Web API

Recently, I developed a webpage to serve as a survey for end users to provide feedback on the helpdesk technician's performance in resolving tickets. The webpage was constructed using HTML, CSS, JS, and PHP. Currently, the page requires access to two ...

Responsive background image not displaying properly

The developer site can be found at http://www.clhdesigns.com/cliwork/wintermeresod/about.php I am encountering an issue where the background image on the home page scales perfectly, but on the about us page it does not scale at all. I am seeking a solutio ...

Turbolinks not allowing the addition of JavaScript classes for background images

While trying to merge a front end html theme with my Laravel app, I encountered an issue with turbolinks that is preventing Javascript from appending div classes. This is resulting in background images only being displayed on page refresh. <div class= ...

Converting text/plain form data to JSON using Node.js - step by step guide

I am currently working on a Node.js application to execute a POST call to an API for placing an order. However, the data I receive in our app is in text/plain format and not JSON. This is the current format of the data: TypeOrder=buy Coin=BTC AmountCoin= ...