Troubles with making display inline and float work properly

My layout is very simple and consists of an inline search bar and a logout button. However, I'm facing issues with displaying them inline correctly. Here's the code snippet:

HTML:

<div class="nav-bar">
<div class="search">
    <form action="" method="post">
        <input type="search" placeholder="Search here..." required>
        <input type="submit" value="Search">
    </form>
</div>
<div class="logout">
    <a href="logout.php" class="button-small">Logout</a>
</div>
</div>

CSS:

.nav-bar
{
display: inline-block;
background: #607D8B;
padding: 5px;
box-shadow: 0px 2px 2px rgba(0, 0, 0, 0.3);
width: 100%;
height:27px;
}

.search {
background-color: chartreuse;
width: 250px;
margin: 0 auto;
}

.logout {
display: inline-block;
float: right;
font-family:'Roboto',sans-serif;
}

.logout,
.username
{
background-color: chartreuse;
margin: 0;
padding: 0;
list-style: none;
}

.logout a,
.header-heading a
{
/*display: block;*/
padding: .7em 1em;
color: #607D8B;
text-decoration:none;
/*border-bottom: 1px solid gray;*/
font-family:'Roboto',sans-serif;
}

.logout a:link { color: #607D8B; }

.logout a:visited { color: #607D8B; }

.logout a:focus
{
color: #607D8B;
font-weight:bolder;
}

.logout a:hover
{
color: #607D8B;
font-weight:bolder;
}

.logout a:active
{
color: #607D8B;
}

I've attempted various solutions but the issue persists, as seen in the attached image.

Answer №1

The alignment isn't quite right because you forgot to include display: inline-block in the .search class. By making it inline-block, it will align horizontally with other inline or inline-block elements.

In addition, there is no longer a need for margin: 0 auto;. You can now use different techniques to position it.

I've prepared a fiddle with your code here

UPDATE

The .nav-bar class should not have the display: inline-block property. It needs to remain as a block to serve as a full-width container for your search form and button.

You can maintain your previous solution for centering the .search element.

To position the logout button on the top right corner, utilize position: absolute;

.logout {
  position: absolute;
  right: 0;
  top: 5px; //Equivalent to the padding-top of .nav-bar
  ...
 }

Updated fiddle

Answer №2

If you're aiming to ensure that everything lines up properly in the header, consider using absolute positioning on the .logout DIV. For example, you can check out this link. If you prefer to have the 'logout' button located closer to the bottom of the header, simply adjust the 'top' positioning value accordingly.

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

Is there a way to retrieve the name of the active tab in ngb-tabset when the component loads in Angular?

Incorporating ngbTabset for tab navigation in my current project has been a game-changer. However, I'm encountering an issue where the active tab name is not being displayed in the console upon app initialization. Any ideas on how to resolve this? You ...

The Challenge with jQuery's Click Event Handling

I am currently working on creating a dropdown menu list with jQuery and simple CSS. I have set up a Demo where the sliding animation is functioning correctly, but I am encountering an issue where the page refreshes when the item is clicked. Here is the cod ...

I have a feature where clicking a button subtracts 1 from the database, and every subsequent click adds 1 more

I have a button that, on click, decreases the value in the database by 1. Every time I click again, it alternates between increasing and decreasing the value by 1. HTML and PHP code: <form action="ind.php" method="post"> <inpu ...

Expanding the search field in my navbar to occupy the entire width of the

I am trying to customize the search field in the navbar below to make it full width without affecting any other elements. Any suggestions on how I can achieve this? Despite my efforts to set the width of various components, I have not been successful in m ...

Using the combination of Chrome browser, Lucida Grande font, and word-wrap

Recently, I encountered a unique situation in Chrome where an odd combination of styles led to an issue. To help illustrate the problem, I created a fiddle which can be viewed here: http://jsfiddle.net/C3CbF/1/ This particular issue is only visible if you ...

designing alternating rows within a table

Can I apply styles to alternating rows in an HTML table using only element hierarchy selectors and avoiding style names? I am tasked with styling the HTML output generated by a server component, which does not include styles for alternate rows. While I co ...

Personalizing CSS for showcasing bot icon next to bot reply container

I'm currently working on modifying the layout of this HTML page. I want to include a bot icon next to each bot response, and I also need to decrease the space between consecutive messages. https://i.sstatic.net/lBiMD.png Any suggestions on how I can ...

CSS: Alignment of Lists with Three Items

In my CSS, I am aligning two parts of a list item to the left and right side like this: ul li div.left { float: left; } ul li { text-align: right; } <ul> <li><div class="left">On the left</div>On the right</li> ...

implementing dynamic navigation bar, when transforming into a dropdown menu using ReactJS

I am currently utilizing a navigation bar from a Bootstrap theme within my React project. The navigation bar includes an in-built media query that determines whether it should display as a dropdown menu or not, with a toggler to handle the dropdown functi ...

Issue with Bootstrap 3 header display on Firefox browser

I'm facing an issue with coding the header on my website. The header looks fine in Chrome, but it's displaying incorrectly in Firefox. The problem seems to be with the responsiveness of the header, which was coded using Bootstrap 3. Here are som ...

Making changes to a specific row in a datatable within a Flask project

I need help with creating an edit function for my data table. I only require front-end and a bit of JS (Ajax) code to send the data. The picture below shows what I am trying to achieve: https://i.sstatic.net/9QDXl.png Here is how my data table looks like: ...

How to implement an external font in AngularJs

I am developing a multilingual website with AngularJS and need to load a font using a .woff file. However, I only want to load the font when it corresponds to the specific language being used on the site. function init(lang){ if(lang == 'eng') ...

issue with excessive content overflow

I'm working with a div structure where the outer div has the following CSS properties: position: relative; overflow: hidden; min-heigth: 450px; Inside this outer div, there is another div with the following CSS properties: position: absolute; top: ...

Tabs within the AutoComplete popup in Material-UI

Would it be feasible to showcase the corresponding data in the AutoComplete popover using Tabs? There could be potentially up to three categories of data that match the input value, and my preference would be to present them as tabs. Is there a way to in ...

Establish a predetermined selection for a drop-down menu

How can I set a default value for a dynamically filled dropdown menu featuring all 12 months in KnockoutJS? I want the default value to be the current month. This is how I am populating the dropdown with information: self.setMonthData = (data ...

The focusable attribute in SVG is malfunctioning

I've utilized the focusable attribute to ensure SVG elements receive focus within an HTML document. My goal is to navigate through SVG elements in the SVG tag using the TAB key, as indicated in this resource (http://www.w3.org/TR/SVGTiny12/interact.h ...

Ways to incorporate a scroll feature and display an iframe using JQuery

Is there a way to animate the appearance of hidden iframes one by one as the user scrolls down the website using jQuery? I have come across some solutions, but they all seem to make them appear all at once. I'm looking for a way to make the iframes s ...

What is the reason behind asp:Textbox occasionally appending "text" to the CSS class in the final HTML output?

Here's the code snippet: <asp:TextBox ID="txtAddress" CssClass="s175" runat="server" MaxLength="30" placeholder="Street"></asp:TextBox> After rendering, it looks like this: <input name="ctl00$LeftColumnContent$txtAddress" type="text" ...

Dynamic positioning causes the fixed navigation bar to overlay content

I am currently working on designing a single-page website that consists of multiple sections. However, I am encountering an issue where clicking on a navigation bar link causes the page to scroll to the desired section but covers some content in the proces ...

Is there a way to use flexbox to decrease the size of images?

Struggling to make my code mobile-friendly. The PC version looks good, but on mobile, the bio stretches and text goes off the page. I can't seem to get the picture to shrink when viewed on a mobile device, or have the content neatly boxed alongside it ...