Achieving precise alignment of div content through Css techniques

body {
  margin: 0;
  font-family: Arial,
  Helvetica,
  sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
    background-color: #ddd;
    color: black;
  }

  .topnav a.active {
    background-color: #04aa6d;
  color: white;
}

.topnav .icon {
  display: none;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child) {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
}

/* select dropdown css code */
select {
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  appearance: none;
  outline: 0;
  box-shadow: none;
  border: 0 !important;
  background: #5c6664;
    background-image: none;
    flex: 1;
    padding: 0 0.5em;
    color: #fff;
    cursor: pointer;
    font-size: 1em;
    font-family: "Open Sans", sans-serif;
  }
select::-ms-expand {
    display: none;
  }
  .select {
    position: relative;
    display: flex;
    width: 20em;
    height: 3em;
    line-height: 3;
    background: #5c6664;
  overflow: hidden;
  border-radius: 0.25em;
}
.select::after {
  content: "\25BC";
  position: absolute;
  top: 0;
  right: 0;
  padding: 0 1em;
  background: #2b2e2e;
    cursor: pointer;
    pointer-events: none;
    transition: 0.25s all ease;
  }
  .select:hover::after {
    color: #23b499;
}
<div class="topnav" id="myTopnav">
  <a href="#home" class="active">Red FMTuning</a>
  <a href="#news" class="aligntune">Choose a tune from ur favourite</a>
  <a href="#contact">
    <div class="select">
      <select name="format" id="format">
        <option selected>Choose a book format</option>
        <option value="pdf">PDF</option>
        <option value="txt">txt</option>
        <option value="epub">ePub</option>
        <option value="fb2">fb2</option>
        <option value="mobi">mobi</option>
      </select>
    </div>
  </a>
  <a href="#about" class="alignout">About</a>
  <a href="javascript:void(0);" class="icon" @click="myFunction()">
    <i class="fa fa-bars"></i>
  </a>
</div>

https://i.sstatic.net/08t0T.png

As shown in the image above, you can see that all the elements like "tuning", "Choose a tune from your favorite", "Choose a book format", "About". The contents are not properly aligned, especially the dropdown ("Choose a book format") is out of alignment with other content and overlaps when clicked on.

I want to align all the elements until it reaches approximately 1200px as shown below.

https://i.sstatic.net/6z5Rb.png

Code Link: https://jsfiddle.net/se8pa7m3/2/

Answer №1

I have encapsulated the elements "Choose a book format", "About" within

<div class="topnav-inner-wrap">
and implemented flexbox CSS on both .topnav and .topnav-inner-wrap

To see it in action, check out the demo here: https://jsfiddle.net/k2beLnrv/

function myFunction(){
      var x = document.getElementById("myTopnav");
      if (x.className === "topnav") {
        x.className += " responsive";
      } else {
        x.className = "topnav";
      }
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #333;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
} 

... (CSS code continues) ... 
<div class="topnav" id="myTopnav">
          <a href="#home" class="active">Red FMTuning</a>
          <a href="#news" class="aligntune">Choose a tune from ur favourite</a>
          <div class="topnav-inner-wrap">
          <a href="#contact">
          <div class="select">
              <select name="format" id="format">
                <option selected>Choose a book format</option>
                <option value="pdf">PDF</option>
                <option value="txt">txt</option>
                <option value="epub">ePub</option>
                <option value="fb2">fb2</option>
                <option value="mobi">mobi</option>
              </select>
            </div></a
          >
          <a href="#about" class="alignout">About</a>
          </div>
          <a href="javascript:void(0);" class="icon" onclick="myFunction()">
            <i class="fa fa-bars"></i>
          </a>
        </div>

Answer №2

Take a look at this coding example. Utilize the power of flexbox to tackle this particular case. Implement the following CSS properties in your topnav:

display: flex;
align-items: center;
justify-content: space-evenly;

The property align-items aligns items vertically, while justify-content evenly spaces them out. Both properties can only be used when display is set to flex.

Dive deeper into the world of flexbox by visiting this comprehensive guide.

Answer №3

To align items with equal space between them, apply the justify-content:space-between; property to the parent element

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

Using ion-icon inside a clickable ion-card while applying float: right does not render properly

I am facing an issue with a list of ion-cards that have clickable icons appearing when you hover over them. The problem is that due to the floating nature of the icons, they are not positioned correctly (as shown in the image below) and end up getting cove ...

What could be causing the hover effect to not work on other elements within the div?

I am working on creating a card that displays only the image when not hovered, but expands and reveals additional information in a div to the right of the image when hovered. Unfortunately, when I hover over the image and then move towards the adjacent div ...

Model-View-Controller (MVC) and

Apologies for my initial question, as I am new to this. I am having an issue with using a radiobutton for the first time. I want to create a radiobutton that looks like the image provided in the link below: My image When the radio button is checked an ...

Having trouble getting my Leaflet map to display even after meticulously following the quick-start guide

I am experiencing an issue where the map is not displaying at all, leaving a blank space where it should be. Despite following Leaflet's quick-start guide, I have been unable to determine the cause of this problem. Here is the code that I currently h ...

Button in HTML not responsive to clicks

I'm facing an issue with my HTML button as it's not clickable at all. I suspect it may be related to incorrect div settings or a background issue. Can someone advise on what setting needs to be changed for the "sign the petition" link to become ...

Finding the Absolute XPath of a Website Element

Using Javascript in the code below, I am retrieving the absolute XPath of a web element: public String getAbsoluteXPath(WebDriver driver) { return (String) driver.executeScript( "function absoluteXPath(element) {"+ "va ...

Mouse over the image link and update the CSS text effect

I need help with a WordPress plugin and I'm trying to avoid making too many changes to the HTML output. Is there a way to make both the image and text change color when hovered over? You can see what I'm working on here - http://jsfiddle.net/3JE ...

Display duplicated options with Bootstrap selectpicker and organize them into optgroups

I have encountered an issue while trying to create a selectpicker using Bootstrap 5 with optgroups. The problem is that all option elements under each optgroup are identical and do not match the HTML structure I have defined in my file. This inconsistency ...

Having trouble fetching information from form in a basic node.js program

Hey, so I'm not exactly a pro at backend development, but I gave it a shot and came up with this straightforward program: Here's my HTML file: <!DOCTYPE html> <head></head> <body> <form action = "http://lo ...

Fixing the vertical centering of content when printing HTML

My bootstrap 4-based HTML page is looking good on most screens, but when I try to print it, the content is vertically centered in the print layout. How can I ensure that the content is displayed at the top of the paper? Below is the HTML code snippet for ...

Transition smoothly with a fade-in effect as you scroll through the images, and proceed to

My Objectives: Implement a scrolling feature where images transition to the next one based on scroll movement. Create a cycle of images that automatically progress, with the view transitioning to the bottom section once all images are viewed. Currently f ...

Is it one form but with two buttons?

How can I identify which button was clicked in a form without using a hidden input field and JavaScript to set different values for each button? You can check out an example demonstrating this technique here: Is there a more straightforward way to achiev ...

Unable to trigger jQuery onclick event on nested div elements

I'm experiencing an issue with my web-page where two buttons at the top are not responding to a sorting function on the nested #byFilter. Despite trying to apply the onclick(function()) method, it doesn't seem to work. Javascript $(document).re ...

An ordered list placed within a div container in an HTML document

When I tried to format the ordered list with sub-items, I encountered an issue. There is a nested <ol> inside a <div>, but the numbering seems to be incorrect. Can you help me understand why? OL { counter-reset: item; padding-left: 10px; ...

I've been attempting to align the iframe element in the center without success, despite trying out various solutions from

Despite trying to center-align the div using style="text-align: center;", display: block, and align="center", none of these methods seemed to work for me. Here is the code including all attempts: <div align="center" style="text- ...

Embedding a stylesheet into an HTML document can be done automatically

Currently, I am working on creating standalone error pages (404/503) as individual HTML files. My server-side setup involves Node.js, but these specific files will be hosted directly in Nginx. One of the challenges I am facing is automatically including a ...

Prevent ui-select from being highlighted when clicked

here's a dilemma : (all in angular 1) I'm using a ui-select like this : https://i.stack.imgur.com/RzH2u.png Here's the code snippet : <div class="formZone-group"> <div class="mandatory-fl"> <div class="man ...

Switching background images with Javascript through hovering

I am currently working on implementing a background changer feature from removed after edits into my personal blog, which is only stored on my local computer and not uploaded to the internet. However, I am unsure of what JavaScript code I need to achieve t ...

Ways to ensure a row of floated images within a <div> container adjusts its size in sync with the browser

Struggling to resize four images inside a div as the browser window is resized? Frustrated that when the window is too small, the last image moves to the next row? Want them all to stay in one row? Despite searching for solutions, adjusting max-widths to 2 ...

The functionality of Ng-Show is acting up

$scope.stay = function() { alert("Inside Keep me In") $scope.timed = false; $scope.isLogStatus = true; } $scope.displayAlert = function() { $scope.timed = true; alert("inside display") } function idleTimer() { var t; $window.o ...