I’m a student working on a responsive header layout using flexbox, and I’m encountering an issue. When I try to move the logo to the left by a few pixels using ‘margin-right’, it also affects the positioning of the other elements. I want the navigation elements and the other link to remain in their positions.
Please ignore the borders, as they are there for my understanding.
* {
border: 1px solid black;
box-sizing: border-box;
}
header {
display: flex;
padding: 20px 0;
font-family: sans-serif;
justify-content: space-around;
}
.logo {
color: rgb(31, 31, 58);
margin-right: 20px;
}
<body>
<header>
<div class="logo">Logo</div>
<nav>
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Portfolio</a>
<a href="#">Contact</a>
</nav>
<div class="Download"><a href="#">Download</a></div>
</header>
</body>
I tried moving the logo to the left using the ‘margin-right’ property and also tried ‘padding-right’. In both cases, the logo moves to the left, but the other elements also shift from their positions.
I know I can use the ‘position: relative’ property, but I believe it’s not the best approach for a responsive website.
2
Answers
.logo {translate: -your px, 0);
A transform is purely visual and does not affect layout.Since adding a margin to the right makes the element "wider" in the flow what you MAY have intended is to move the element to the left half the amount so
10px
and that is 10/16 for the default16px
font size on most modern browsers we can use andem
so 10/16 = 0.625 so use-0.625em
to move it that much to the left here.I put TWO header elements and hacked the logo class just for a visual reference of the after with the unchanged element flex.