Center the div element with a small left margin

In the process of developing a website, I am faced with the challenge of creating a navigation bar at the top. This navigation bar is designed to have a logo in the top left corner and a toolbar centered within the window. The logo is implemented using an img tag with the position:absolute property to ensure it appears in the correct location. On the other hand, the toolbar is contained within a div element with width:100% set, ensuring that its components are centralized within it.

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

However, when the window size is reduced significantly or when viewing on tablet devices in portrait mode, the toolbar ends up overlapping the logo, as shown below:

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

My goal is for the toolbar to stop moving once it reaches the logo's position, ensuring that when the window is resized, the layout appears as desired:

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

I came across a potential solution mentioned in this thread: css: how to build centered div with minimum spacing on the left. However, this approach relies on knowing the exact width of the centralized component (toolbar), which may not be feasible in my specific case.

Any assistance or insights would be greatly appreciated. Thank you!

Answer №1

To ensure that the logo and toolbar do not overlap, you must set both elements to have a position of relative. Specifically, you should set the toolbar to:

.toolbar {position: relative; float: right;}

and the logo to:

img {position: relative; float: left}

If you want to create a 'burger menu' for smaller screen sizes, consider using media queries. This will allow you to adjust the layout based on the screen width.

For a central top bar, place it in a div that occupies the remaining space on the right side. Here's an example:

.logo {position: relative; float: left; width:30%; background-color: red}
.topbar {margin-left: auto; margin-right:auto; width:50%;background-color:red;}
.topbarContainer {float:right; width: 70%}

Check out this Fiddle for reference:

https://jsfiddle.net/dbcv4yc7/1/

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

Learn the best practices for incorporating jQuery and other JavaScript libraries in your Angular2 projects

Is it possible to integrate a demo showcasing Bootstrap carousel with CSS3 animations in Angular2 using HTML, CSS, and JS? I have created my own implementation in Plunker with Angular2, but I am facing issues with the animated inner content of the carousel ...

Tips for vertically aligning elements within the body while excluding the navbar from being centered

On my webpage, I have a navigation bar and a lot of content. The code structure looks something like this: .container { display: flex; justify-content: center; align-items: center; } <div class="navbar"> <!-- Various links for navigation ...

Setting up a PHP email form for offline use

Here is the code I am currently working on: <?php if(isset($_POST['submit'])){ $to = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="086d65696164486d70696578646d266b6765">[email protected]</a>"; // ...

Prevent the onclick function of a span element from being triggered when the user clicks on a dropdown menu contained within

Just delving into the world of web development and currently tackling a React project. Encountered an issue where I need to make a span element clickable. However, there's a dropdown within that span that doesn't have any onClick event attached t ...

Is there a way to automatically close the Foundation topbar menu when a link is selected?

On my single page website, I am utilizing Zurb Foundation's fixed topbar which includes anchor links to different sections of the page. My goal is to have the mobile menu close automatically whenever a link inside it is clicked. As it stands now, whe ...

CSS-only countdown animation

I've been experimenting with a pure HTML/CSS countdown animation, but I'm facing challenges in getting it to run smoothly. Currently, I'm focusing on minutes and seconds only. One issue I encountered is that the digit representing tens of mi ...

Combining inline input fields and select buttons with Bootstrap 3

I've been exploring different ways to align form elements inline, including dropdown buttons and select buttons, but have only had success with smaller size buttons around 40px wide. My main challenge now is trying to create a search bar with an input ...

Ensure that the table container occupies the remaining viewport height without causing vertical scrolling in the window, while also enabling vertical scroll within the table itself

I am seeking a solution to create a table that fills the remaining height without causing a vertical scroll in the browser. My goal is for the table itself or its container to have a vertical scroll instead. I found guidance from another post on how to ma ...

I am facing a challenge with my data, as it includes start and end values representing character indexes for styles. I need to find a way to convert this information into valid HTML tags

I have some content from another source that I must transform into proper HTML. The content contains a string such as: "Hello, how are you?" Additionally, there is a separate section detailing styling instructions. For example: 'bold:star ...

Arranging buttons in a row with Bootstrap 5

Struggling to align the buttons on my webpage side by side in the center, I've tried two Bootstrap 5 classes but they're not achieving the desired look: https://gyazo.com/c23f2eade4614380aec547b11e61387a https://gyazo.com/e40a678b02c9f641f746b1c ...

Modify the POST data in PHP 8 if the HTML form submit results in an empty submission

When passing data from a form, I encountered an issue where the data in the hidden input field is not being properly accessed in the $_POST array. <?php> $color = "yellow"; $value = 8; echo "<form name='Form' action='' method=& ...

Restore original state on various elements using JavaScript

I recently asked a question about changing the height of images when clicking on them using vanilla JS. A helpful user named Butalin provided me with a working function, but there seems to be an issue - if I click on one image to change its height and then ...

The NVD3 tooltip is being obscured by other divs

Attempting to make the NVD3 tooltip appear above all other divs has presented a challenge. With three charts lined up horizontally and tooltips that exceed the boundaries of their divs, adjusting the z-index creates a dilemma. Regardless of which side&apos ...

Encountered an error while trying to filter markers and list using Knockout and Google Maps - Uncaught TypeError: Unable to locate property 'marker' of undefined

I am looking to enhance user experience by implementing a feature where clicking on a marker or a city name will display the info.window specific to that marker. HTML: <!DOCTYPE html> <html> <head> <title>Exploring Mag ...

Is it possible to overlay a div onto a Google Map?

Does anyone know of a plugin or method that can achieve this? I have a website at this link, and at the bottom there is a map. I'm looking to add a small overlay box on top of the map with contact information inside. Edit: The box should be position ...

CSS div elements not appearing in a row

I recently created an HTML page with the following structure: <div id="header-wrapper"> <header class="header" id="header" role="banner"> <div class="logo-slogan-wrapper"> <a href="/d7/" title="Home" rel="home" class="h ...

Is there a way to adjust the theme color in _variables.scss by recompiling Bootstrap while working with Angular and CSS?

I've been trying to update the primary color in my Angular project by editing the SCSS file node_modules/bootstrap/scss/_variables.scss. However, after stopping npm and running the front-end again, I noticed that the changes I made are not reflected i ...

Navigating a mobile-friendly menu anytime!

I'm in the process of creating a responsive "hamburger" menu for mobile devices. The HTML code I have implemented is as follows... .menu_closed { color: red; } .menu_open { color: blue; } <script src="https://ajax.googleapis.com/ajax/libs/jq ...

The information about the property is not appearing on the screen

I included the following CSS: nav label:AFTER { content: " ▾"; } However, when viewed in Chrome, it appears like this: content: " â–¾"; I have already added <meta charset="utf-8"/> to my JSP page and @CHARSET "utf-8"; in my CSS file. ...

Creating a user authentication portal

Looking for advice on creating a basic front end HTML login. I'm new to coding and would appreciate any suggestions! Additionally, how can I link the login system to my database? ...