Tips for aligning an element at the center based on the viewport rather than the parent element

I'm trying to center an element within a header with five elements, consisting of a button on the left, a logo in the middle, and three buttons on the right. I want the logo to be centered based on the viewport. I've created a codepen to demonstrate: https://codepen.io/pier6dev/pen/WNvjJGX

However, the logo does not align perfectly even when using justify content center. Here is where the logo should be positioned:

https://i.sstatic.net/ddM9B.png

If my explanation is not clear, feel free to ask any questions. Here is the CSS code snippet:

header {
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 1rem;
}
.btn {
padding: 1rem;
border-radius: 0.5rem;
color: white;
}
...
<header>
  <div class="left">
    <div class="btn btn-home">Home</div>
  </div>
  <div class="center">
    <div class="logo">
      <svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" ... </svg>
    </div>
  </div>
  <div class="right">
    <div class="btn btn-shop">Shop</div>
    <div class="btn btn-subscribe">Subscribe</div>
    <div class="btn btn-login">Login</div>
  </div>
</header>

Answer №1

Successfully resolved the issue by changing the .center class to an absolute element. To the logo, a specific width has been assigned.

.center {
  position: absolute;
  left: 0;
  right: 0;
  margin: auto;
  width: 160px;
}

Visit codepen to view the solution

Answer №2

The issue you may encounter is that the buttons on the right side will consume too much space, causing problems when the screen size is reduced. The <div class="right"> group needs sufficient space to be visible on smaller screens.

One potential solution, although it may lead to overflow problems, is as follows:

.left, .right {
   width: 20%; // or 25% (totaling 50% width)
}

.center {
   width: 50%; // remaining 50%
   display: flex;
   align-items: center;
   justify-content: center;
   // center the .logo container using flex
}

Alternatively, consider determining which elements or buttons are essential for smaller screens and implement a Sidedrawer or Pullout menu. You can refer to W3Schools for guidance on this: https://www.w3schools.com/howto/howto_js_sidenav.asp

Furthermore, utilize Media queries to specify when a 'Full' Header menu should be available based on screen width.

Two common Media queries include:

// Tablet Media Query for screens with a minimum width of 768px
@media screen and (min-width: 768px) {}

// Desktop Media Query for screens with a minimum width of 1024px 
@media screen and (min-width: 1024px) {}

Answer №3

Your logo's alignment is determined by the flexbox it resides in, not the viewport. Unfortunately, there is no way to make the logo disregard its parent container and center itself based on the viewport.

One workaround is to ensure that the .right and .left classes (which wrap the home and other buttons) have equal width. This will create a perfectly centered space between them. In the given example, I've used display: flex and justify-items: start for the .left class to prevent the home button from stretching and set the width to match the larger div indicated in the inspector. However, be aware that you might need to switch to a different layout early on using a media query because the logo can begin to overlap with the buttons when the viewport width reaches around 768px, which can appear awkward.

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 1rem;
}

.btn {
  padding: 1rem;
  border-radius: 0.5rem;
  color: white;
}

.btn.btn-home {
  background: red;
}

svg {
  width: 10rem;
  height: auto;
}

.left, .right {
  width: 272px;
  display: flex;
}
.left {
  justify-items: start
}
.btn.btn-shop {
  background: blue;
  margin-right: 1rem;
}

.btn.btn-subscribe {
  background: orange;
  margin-right: 1rem;
}

.btn.btn-login {
  background: grey;
}
<header>
  <div class="left">
    <div class="btn btn-home">Home</div>
  </div>
  <div class="center">
    <div class="logo">
      <svg id="Layer_1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 5573.1 1990.2" width="5573.1" height="1990.2">
        <style>
          .st0 {
            fill: #ed1c40
          }
        </style>
        <path class="st0"
          d="M3283.8 1072.6c-7.7 11.6-12.6..............145.9-171.2 18.5-226.6 49.7-264.3 107 116.7 11.5 193. ..............09 85 181.2-55.7 261.9-114.8 82.6-62.9 138.3-..............1 93.7-10.1 350.8 3 63 1.2 123.4-7.8 179..............6-39.6 28-15 .6 48.1-45.9 54.9- 77.4 10-42.7 5-79..............

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

Unable to find and load the specified css-font formats (svg, eot, woff, woff2, and ttf) using webpack

Looking to incorporate a unique font into my project: @font-face { font-family: 'bikeshop'; src: url('/src/styles/bikeFont/font/bikeshop.eot?37006833'); src: url('/src/styles/bikeFont/font/bikeshop.eot?37006833#iefix') ...

Adjustable dimensions for a collection of squares based on screen size

I am currently designing a grid composed of 1:1 squares and allowing the user to continually add more squares. My main goal is to ensure that the size of each square maintains its aspect ratio while being able to resize accordingly. The challenge lies in k ...

HTML textarea content will update automatically whenever there is an input change in JavaScript

Everything seems to be working as expected, but I noticed that the onmouseover event only triggers once. Is there a way to make it work multiple times, i.e., every time the text array changes? <html> <head> <script> function myFunct ...

Different option for animated side slider based on location

Whenever I click on this div, it slides out smoothly. Check out these images to get a visual of what I am aiming for: This is how the Div looks when collapsed by default https://i.stack.imgur.com/zJIsA.png Once slid out, the Div looks like this (highlig ...

Arranging an image to appear directly underneath another image upon hovering the mouse over it

I want image two to appear below image one when the mouse hovers over image one. <ul> <li> <img id="img1" src="imageone.png"> <br> <img id="img2" src="imagetwo.png"> </li> </ul> Any assistance ...

jQuery code tailored specifically for desktop use

Hello, I could use some assistance with a jquery script. I have been able to accomplish what I need using a jquery script, but I would like it to only work on desktops and be disabled on tablets and mobile devices. The script I am currently using is as fo ...

How to eliminate the validation icon from a select tag in Bootstrap 5?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23414c4c57505751425363160d110d11" ...

Angular click event not functioning properly on elements loaded via http request

Check out my CodePen link: http://codepen.io/gauravcoder/pen/vLWEjJ?editors=101 I'm just getting started with Angular JS. I have some items that trigger an alert when clicked, it works fine for items that are not loaded via an HTTP request. However, ...

Is it possible to partially move the image outside of the MUI dialog?

Is it possible to move an image partially outside of the MUI dialog, with the bottom part inside and the top part outside the dialog? I attempted a solution found on Stack Overflow but it did not yield the desired result. This is the dialog code: <Dial ...

Easy selector for choosing jquery css backgrounds

Here is a simple background selector that I created. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <hea ...

Ways to position a dropdown submenu on the left-hand side in Bootstrap 4

I'm currently utilizing Bootstrap 4 with a multilevel dropdown feature as shown in the image below: https://i.sstatic.net/xgVLp.png My goal is to shift the submenus to the left side, in the opposite direction from how they are currently displayed. De ...

Tips for using jQuery to dynamically apply CSS styles to new content added to a webpage

I have encountered this question multiple times on stackoverflow and through google search, but unfortunately none of the suggested solutions have worked for me. My issue involves a page that loads an HTML page with empty sections, which are then dynamica ...

Customize the border style for the span element

I attempted to use the code below in JavaScript/jQuery to adjust the border thickness. Unfortunately, it seems to be ineffective. Can someone please assist me? //$("span").css({"border":"4px solid green"}); document.getElementById("192.168.42.151:8984_ ...

Troubleshooting Bootstrap Column Display Issues on Mobile Devices

I used bootstrap columns to organize my users' information. While they look good on larger screens like laptops and desktops, on smaller phone screens the second column appears to have some excess margin at the top. How can I fix this issue? <div ...

Create a dynamic calendar by integrating dates with Javascript and an HTML table

Recently, I decided to delve into web design again and embark on a challenging project for my father's baseball business. The main task at hand is creating a detailed calendar using HTML tables. After spending a considerable amount of time perfecting ...

The width of table cells expands even when using the box-sizing property set to border-box

Whenever I click the View button, I want to apply padding to the cells in my table. However, this action also increases the width of the cell. Despite researching various solutions like those found in this question, this one, and this post, I still encount ...

Unable to increase font size using CSS

I am struggling with adjusting the size of an h1 element along with other properties, but the only change I see is in the font family. Could it be the Bootstrap grid I am using causing this issue? I am still relatively new to Bootstrap. h1{ font-famil ...

Chrome fails to load webfont upon page initialization

Encountering a problem with loading a WebFont specifically on Chrome version "48.0.2564.82 m (64-bit)". The issue arises from the webfont not being applied upon page load or navigation, unless I manually refresh the page. Below is the snippet of code fro ...

What method is most effective for loading HTML content depending on the device being used?

Looking for the optimal method to load different div elements based on the device accessing my website without relying on user agent checks. As of now, I am utilizing CSS media queries to determine device specifications and display the appropriate div acco ...

When using font size in rem units, it will remain consistent across different screen sizes and not be subject to scaling

When setting padding, margin, and font size using rem units, I noticed that on larger screens the output looks good. However, on smaller screen devices, the sizes do not reduce proportionally - instead, the measurements from the larger screen persist. Wh ...