How to align content to the right in CSS without causing it to overflow

Hey there, I'm currently working on creating a menu with both side and top elements. However, I've encountered an issue while trying to align the icons and names to the right using position:absolute and right:0px. The problem arises because my body has a margin-left: 280px.

let menuToggleButton = document.getElementById('menu_toggle_button');
let side_menu = document.getElementById('side_menu');
let body = document.getElementById('body');

menuToggleButton.addEventListener('click', function() {
    side_menu.classList.toggle('resize-side-menu');
    body.classList.toggle('resize-body');
});
*{
    margin: 0px;
    padding: 0px;
}
body{
    display: flex;
}
.side-menu{
    position: fixed;
    display: inline-flex;
    background: #ff000061;
    width: 280px;
    height: 100vh;
    transition: .5s;
}
.resize-side-menu{
    width: 50px;
    transition: .5s;
}
.body{
    display: inline-flex;
    width: 100%;
    height: 60px;
    margin-left: 280px;
    background: blue;
    height: 2000px;
    transition: .5s;
}
.resize-body{
    margin-left: 50px;
    transition: .5s;
}
.body .content-body{
    display: block;
    width: 100%;
    height: 100%;   
}
.body .content-body .header{
    width: 100%;
    height: 50px;
    display: block;
    position: fixed;
}
.content-items-header{
    display: flex;
    align-items: center;
    background: brown;
    position: relative;
    height: 50px;
}
.body .content-body .header .content-items-header .content-search-input{
    background: white;
    color: rgb(0, 0, 0);
    border: 2px solid blue;
}
.body .content-body .header .content-items-header .icons-content{
    position:absolute;
    right:0px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.0/css/all.min.css">
</head>
<body>
    <div class="side-menu" id="side_menu">
        
    </div>
    <div class="body" id="body">
        <div class="content-body">
            <div class="header">
                <div class="content-items-header">
                    <button class="fad fa-bars" id="menu_toggle_button">open</button>
                    <div class="content-search-input">
                        <input type="text" class="search" id="search">
                        <i class="fad fa-search"></i>
                    </div>
                    <div class="icons-content">
                        <i class="fas fa-bell"></i>
                        <i class="fas fa-envelope"></i>
                        <i class="fas fa-cog"></i>
                    <img src="brain.jpg" alt="" height="35px" width="35px">
                    <span>Aldahir Ruiz Valdez</span>
                    </div>

                </div>
            </div>
            <div class="workspace">
    
            </div>
        </div>
    </div>
    <script src="main.js"></script>
</body>
</html>


I am looking for a solution where I can keep them aligned to the right without overflowing off the screen

Answer №1

consider using either overflow: hidden; or overflow: auto based on your desired outcome. With overflow: hidden;, all overflowing content will be concealed, while overflow: auto will automatically add a scroll bar if necessary.

Answer №2

Modifications were made to the width of .body and .header, adjusting it to calc(100% - 280px). Upon clicking the button, the width changes to calc(100% - 50px). Additionally, a transition: .5s; was added for a smooth width transition on the .header element (you'll notice the difference when the transition is removed). Hopefully, this achieves the desired outcome!

let menuToggleButton = document.getElementById('menu_toggle_button');
let side_menu = document.getElementById('side_menu');
let body = document.getElementById('body');
let header = document.querySelector('.header');

menuToggleButton.addEventListener('click', function() {
    side_menu.classList.toggle('resize-side-menu');
    body.classList.toggle('resize-body');
    header.classList.toggle('resize-header');
});
* {
    padding: 0;
    margin: 0;
}

body{
  display: flex;
}
.side-menu{
  position: fixed;
  display: inline-flex;
  background: #ff000061;
  width: 280px;
  height: 100vh;
  transition: .5s;
}
.resize-side-menu{
  width: 50px;
  transition: .5s;
}
.body {
  width: calc(100% - 280px);

  display: inline-flex;
  height: 60px;
  margin-left: 280px;
  background: blue;
  height: 2000px;
  transition: .5s;
}
.resize-body{
  margin-left: 50px;
  transition: .5s;
  width: calc(100% - 50px);
}

.body .content-body{
  display: block;
  width: 100%;
  height: 100%;   
}
.body .content-body .header{
  width: calc(100% - 280px);
  transition: .5s;
  height: 50px;
  display: block;
  position: fixed;
}
.body .content-body .header.resize-header {
  width: calc(100% - 50px);
  transition: .5s;
}

.content-items-header{
  display: flex;
  align-items: center;
  background: brown;
  position: relative;
  height: 50px;
}
.body .content-body .header .content-items-header .content-search-input{
  background: white;
  color: rgb(0, 0, 0);
  border: 2px solid blue;
}
.body .content-body .header .content-items-header .icons-content{
  position:absolute;
  right:0px;
}
<div class="side-menu" id="side_menu">
        
  </div>
  <div class="body" id="body">
      <div class="content-body">
          <div class="header">
              <div class="content-items-header">
                  <button class="fad fa-bars" id="menu_toggle_button">open</button>
                  <div class="content-search-input">
                      <input type="text" class="search" id="search">
                      <i class="fad fa-search"></i>
                  </div>
                  <div class="icons-content">
                      <i class="fas fa-bell"></i>
                      <i class="fas fa-envelope"></i>
                      <i class="fas fa-cog"></i>
                  <img src="brain.jpg" alt="" height="35px" width="35px">
                  <span>Aldahir Ruiz Valdez</span>
                  </div>

              </div>
          </div>
          <div class="workspace">
  
          </div>
      </div>
  </div>

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

Error in electron-builder: Module 'dmg-license' was not found

Seeking a straightforward method to create an electron app for macOS from a Linux machine. Unfortunately, the electron-builder -m command is not functioning properly. Here is the complete output of the command: electron-builder -m • elec ...

Store user input in a paragraph

I want to create a unique program that allows users to input text in a field, and when they click "Start", the text will appear in a paragraph backwards. I plan to use Html, jQuery, and CSS for this project. Can anyone provide guidance on how to achieve th ...

Having trouble with syntax highlighting on Node.js Vash view files in Visual Studio 2015 Update 3?

While working on a Node.js application using Vash as the view engine in Visual Studio 2015 Update 3, I encountered an issue with syntax highlighting. When writing HTML in the vash files, the code appears as plain text without any syntax highlighting. For e ...

What is the best way to merge multiple arrays nested within another array while removing the final character from each word?

We have a 2D array: let userGroup = [ ['user1-', 'user2-', 'user3-'], ['user4-', 'user5-', 'user6-'], ['user7-', 'user8-', 'user9-'] ]; How can we c ...

Currently seeking assistance in replacing an existing Wordpress object with a new one

I'm looking to swap out the current slider image with a new slider. Whenever I try to replace it with the TOP 5 Games, they disappear or don't show up at all. <?php if( function_exists('cyclone_slider') ) cyclone_slider('24&ap ...

Properly setting up event handling for a file input and Material UI Button

In my attempt to create a customized form using material UI elements, I am facing an issue. The form allows users to upload files and add notes for each option, which are stored in an array in the component's state. Here is a simplified version of th ...

What sets BoxBufferGeometry apart from BoxGeometry in Three.js?

I'm currently diving into the world of Three.js and I've hit a roadblock trying to distinguish between BoxBufferGeometry and BoxGeometry. Can anyone shed some light on this for me? ...

Space between elements in a horizontal arrangement

While working with Bootstrap 5 and using paddings in columns, I encountered issues when some of my columns had backgrounds or when adding an embed element. How can I create margins in such cases? https://i.sstatic.net/EhF7y.png To address this issue, I i ...

Utilizing CSS for Page Orientation

For my current project, I was tasked with designing a print view using HTML & CSS. This view is then converted into a PDF on the server and sent to an A5 printer for physical output. One of the specific requirements of this assignment is that the first pa ...

Navigating directly to a page in ReactJS and Redux without needing to login if a token is found in the local storage

Currently in the main component, it is set to automatically redirect to the Login page. However, I want to implement a check to see if a token is already stored in the local storage. If a token exists, I want the application to skip the Login page and dire ...

Guide on implementing a template within a form using Vue.js

I have set up a Vue instance for handling the form data var formInstance = new Vue({ el: '#amount_form', data: { logdate: '', amount:'', description:'' }, methods: { ...

Trigger Vue method when the size of a div element changes

Is there a way to trigger a method every time the dimensions (width or height) of a div element change? <template> <div> </div> </template> <script> export default { methods: { updateSize() { // ...

What is preventing me from positioning my image on the left side of my Bootstrap website?

I've experimented with various classes like ml-auto, float: left, float-left, and fixed-left, but none have successfully aligned my image to the left as desired. I'm hesitant to use margin adjustments as they are not always responsive across diff ...

What is the best way to manage an empty JavaScript object?

I'm feeling stuck. Check out this code snippet: const clientInfoPromise = buildPromiseMethod clientInfoPromise.then((clients) => { console.log('clients ' + JSON.stringify(clients)) console.log(clients.typeOf) console.log(_.k ...

Slowly revealing sticky navigation with slideDown animation using JQuery

Here is the code for a .JS file: $(document).scroll(function (){ var menuheight= $('header').height(); var y = $(this).scrollTop(); if (y>(menuheight)) { $('.menu_nav_features').addClass ...

Retrieving data from a remote source repeatedly based on user selections on a single page

In my current project, I am utilizing next js to build a web application. One of the pages in this app has the following structure: <page> <component_1> This component fetches data from a remote server using `getInitialProps` (alternat ...

Limiting the number of characters allowed in an HTML5 number input field on an iPhone

We are facing an issue where the "maxlength", "min", and "max" HTML attributes do not work as expected on an iPhone when using the following markup: <input type="number" maxlength="2" min="0" max="99"/> On iPhone 4, instead of restricting the numb ...

What is the best way to incorporate variables into the useState hook in React?

I want to create multiple useState hooks based on the length of the userInputs array. The naming convention should be userInput0, userInput1, userInput2,...userInputN. However, using "const [userInput+index.toString(), setUserInput] = useState('' ...

At what point is the ajax call considered fully executed?

Whenever a client makes an AJAX call, they upload data to the server (HTTP request) and then receive data from the server (HTTP Response). Once the clients have received all the data, the AJAX request is considered successful, and the success callback func ...

Currently, I am encountering a problem as I attempt to iterate through a dynamic table

I have a table containing various elements. An example row is Jack Smith with multiple rows like this: col1 col2 col3 col4 col5 col6 col7 col8 jack smith 23 Y Y error error_code error_desc The table is ...