English 中文(简体)
使孩子的div 留在父母的div cs
原标题:Making child div stay inside parent div css
  • 时间:2024-05-19 02:27:36
  •  标签:
  • html
  • css

我试图让孩子的Div(div in other) 留在父母的体内,而不是从中出来。 我想让Div(div)随时留在头版Div(the header Div)内。

<!DOCTYPE html>
<html>
<head>
    <title>Noah</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <style>
        body {
            background-color: #0B0F25;
            font-family: Times New Roman , Times, serif;
            
            color: white;


            margin: 0;
            padding: 0;

        }

        #header {
            background-color: #0B0F25;
            width: 100vw;

            border-bottom: .5px solid black;
            border-top: 1.5px solid black;
            height: 5vw;
            background-color: purple;
            
        }

        #cmdLine {
            padding: 10px;
        }

        #cmdTitle {
            text-align: center;
            font-size: 16px;
        }

        #options {
            position: absolute;
            width: 10vw;
            background-color: pink;
        }

        #help {
            padding: 5px;
            display: inline-block;

        }
        
        #action {
            padding: 5px;
            display: inline-block;
        }

    </style>
    
</head>

<body>
    <div id = "header">
        <h3 id = "cmdTitle"> Noah@Noah: ~</h3>

        <div id = "options">
            <h2 id = "action">Actions</h2>
            <h2 id = "help">Help</h2>
        </div>

    </div>
    <p id = "cmdLine">$</p>
</body>

</html>

This displays this: code output. each div has an ID. I have tried setting position: absolute on the header div then once again on the options div but no luck.

问题回答

当您将 # 选项设置为绝对时, 它会设置相对于 h3 的 # 选项, 添加一个顶级属性会将其置于更高的位置 。

#options {
            position: absolute;
            top: 5px;
            width: 10vw;
            background-color: pink;
        }

只需删除父身身高和选择宽度, 这样就可以默认地使其自动, 让孩子留在父身内 。


        body {
            background-color: #0B0F25;
            font-family: Times New Roman , Times, serif;
            color: white;
            margin: 0;
            padding: 0;
        }

        #header {
            width: 100vw;
            border-bottom: .5px solid black;
            border-top: 1.5px solid black;
            background-color: purple;
        }

        #cmdLine {
            padding: 10px;
        }

        #cmdTitle {
            text-align: center;
            font-size: 16px;
        }

        #options {
            background-color: pink;
        }

        #help {
            padding: 5px;
            display: inline-block;
        }
        
        #action {
            padding: 5px;
            display: inline-block;
        }
    </style>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签