skip to Main Content

Hello!
I’m having problems with my title bar not taking up the intended space. Here’s a diagram:
Diagram of my problem
The area I highlighted in red is where I want my title bar to be (Next to the existing buttons) but my title bar instead moves to the next line.
Here’s the code:

body, html{
    margin: 0px;
    height: 100%;
}
div.titlebar{
    background-color: rgb(226, 147, 0);
    color: white;
    padding: 5px;
    font-size: 20px;
    line-height: 30px;
    height: 30px;
    font-family: 'Segoe UI', 'Arial', 'Helvetica', sans-serif;
    display: inline-block;
    cursor: default;
}
div.titlebutton{
    cursor: pointer;
    margin-left: 2px;
    background-color: orange;
    color: white;
    padding: 5px;
    font-size: 20px;
    line-height: 30px;
    height: 30px;
    font-family: 'Segoe UI', 'Arial', 'Helvetica', sans-serif;
    display: inline-block;
}
div.titlebutton:hover{
    background-color: rgb(226, 147, 0);
}
#editor{
    border: none;
    resize: none;
    font-size: 20px;
    width: 100%;
    height: calc(100% - 45px);
    box-sizing: border-box;
}
div#rest {
    background-color: orange;
    color: white;
    margin-left: 2px;
    padding: 5px;
    height:30px;
    display: inline-block;
    cursor: default;
    width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Text Editor</title>
    <link rel="stylesheet" href="text.css">
</head>
<body>
    <div class="titlebar">Text Editor</div><div class="titlebutton" id="save">Save</div><div class="titlebutton" id="load">Load</div><div id="rest"></div>
    <br/>
    <textarea id="editor"></textarea>
    <script src="text.js"></script>
</body>
</html>

Attempts to fix

! (Please keep in mind that I’m a CSS beginner)
I tried using width:100%; along with display:inline-block; to make it go next to the buttons. I’m not familiar with other ways to do it, so that’s the only way I tried aside from display:inline;, that ended up breaking the whole bar.

2

Answers


  1. Have you tried Wrapping the content in a div?
    <!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>
        <style>
            .titlebutton{
                display:inline-block;
            }
        </style>
    </head>
    <body>
    <div class="titlebar">Text Editor<div class="titlebutton" id="save">Save</div><div class="titlebutton" id="load">Load</div><div id="rest"></div></div>
    </body>
    </html>
        
    Like this?
    

    If you wrap it in a container that is block formatted and make the buttons inside inline-block, it should work as a base point.

    Login or Signup to reply.
  2. You’ve made this quite hard for yourself by not including a wrapper element for the titlebar. The layout you’re going for is very flexbox and so naturally you’d want a flexbox parent for the items inside the titlebar:

    body, html{
        margin: 0px;
        height: 100%;
    }
    div.titlebar-wrapper {
        display: flex;
        background-color: rgb(226, 147, 0);
    }
    div.titlebar{
        color: white;
        padding: 5px;
        font-size: 20px;
        line-height: 30px;
        height: 30px;
        font-family: 'Segoe UI', 'Arial', 'Helvetica', sans-serif;
        display: inline-block;
        cursor: default;
    }
    div.titlebutton{
        cursor: pointer;
        margin-left: 2px;
        background-color: orange;
        color: white;
        padding: 5px;
        font-size: 20px;
        line-height: 30px;
        height: 30px;
        font-family: 'Segoe UI', 'Arial', 'Helvetica', sans-serif;
        display: inline-block;
    }
    div.titlebutton:hover{
        background-color: rgb(226, 147, 0);
    }
    #editor{
        border: none;
        resize: none;
        font-size: 20px;
        width: 100%;
        height: calc(100% - 45px);
        box-sizing: border-box;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Text Editor</title>
        <link rel="stylesheet" href="text.css">
    </head>
    <body>
        <div class="titlebar-wrapper">
          <div class="titlebar">Text Editor</div><div class="titlebutton" id="save">Save</div><div class="titlebutton" id="load">Load</div>
        </div>
        <br/>
        <textarea id="editor"></textarea>
        <script src="text.js"></script>
    </body>
    </html>

    This also allows you to get rid of the <div id="rest">, by simply giving the parent the appropriate background-color.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search