简体   繁体   English

如何在 HTML 中将不同的文本位放在同一行而不影响彼此?

[英]How do I have separate bits of text in HTML on the same line without them affecting one-another?

I'm wanting to create a mini-website to gain a general grip on how to code in HTML/CSS, and I've decided to make a main box with a sidebar, and when I try to create two separate portions of text on the same line under different tags, it automatically moves down a line and affects the position of the other tag, making the whole (albeit very simple) site look like a mess.我想创建一个迷你网站来全面了解如何使用 HTML/CSS 进行编码,并且我决定制作一个带有侧边栏的主框,当我尝试在上创建两个单独的文本部分时同一行在不同标签下,它会自动下移一行影响另一个标签的 position,使整个(尽管很简单)站点看起来一团糟。

The code which I tried looks like this:我试过的代码如下所示:

 .insidebar{ float: right; margin-right: calc(33.5%); position: relative; }
 <ul><a href="https://rateyourmusic.com/~Xerunox">my rym!!</a></ul><p class="insidebar">test</p ```

Note the second piece of text is what I'm trying to move into the sidebar, if that information is needed.请注意,如果需要该信息,第二段文字就是我要移入侧边栏的内容。

Since my knowledge is quite limited, please try and simplify a lot of the wording.由于我的知识非常有限,请尽量简化很多措辞。

To be honest, from the description, I'm still not sure I understand what you want to achieve but let me try to give some hints.老实说,根据描述,我仍然不确定我是否理解你想要实现的目标,但让我试着给出一些提示。

ul and p are block elements meaning they will be one above the other, even if in HTML you write them in one line. ul 和 p 是块元素,这意味着它们将一个在另一个之上,即使在 HTML 中你将它们写在一行中。

Also, using floats to make layouts is an outdated practice.此外,使用浮动进行布局是一种过时的做法。 Better read about flexbox.最好阅读 flexbox。

Also, you're using tag but you actually don't have a list.另外,您正在使用标签,但实际上没有列表。 Why that tag then?为什么那个标签呢?

To make a layout with a main box and a sidebar I'd have a container for both of them.要制作带有主框和侧边栏的布局,我需要为它们都准备一个容器。 In that a div that would be the sidebar and another one that would be the main div.其中一个 div 作为侧边栏,另一个 div 作为主 div。

And the text I want in the main div I would put inside that, and whatever is supposed to be in the sidebar would go inside that div.我想要在主 div 中放置的文本会放在其中,侧边栏中应该包含的任何内容都会在该 div 中放置 go。

I don't know how how your whole layout should look like, but a safe way to achieve a main vs sidebar approach in contemporary HTML/CSS is with the flexbox model:我不知道你的整个布局应该是什么样子,但在当代 HTML/CSS 中实现主栏与侧边栏方法的安全方法是使用flexbox model:

 * { box-sizing: border-box; } html { height: 100%; } body { font-family: sans-serif; margin: 0; display: flex; height: 100%; } aside { width: 200px; background-color: dodgerblue; padding: 1rem; } main { flex: 1; background-color: orange; padding: 1rem; }
 <body> <aside> <h2>This is the aside section</h2> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Contact</a></li> </ul> </aside> <main> <h1>This is the main section</h1> <p>Welcome to this document</p> </main> </body>

By setting display: flex on the <body> , you are positioning its children next to each other horizontally (being row the default flex-direction ).通过在<body>上设置display: flex ,您可以将其子项水平放置在一起(默认为row flex-direction )。

Inside each body child, elements are arranged vertically (default block model).在每个 body child 中,元素垂直排列(默认块模型)。

Floats are a reminder of the pre-flexbox era, are confusing and should be avoided nowadays.浮动是前 flexbox 时代的提醒,令人困惑,现在应该避免。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何将 position 一个单独类型的文本放入我制作的另一个文本的同一行? - How do I position a separate type of text into the same line of another text I made? 如何更改一个元素更改而又不影响同一类的其他元素? - How do I change one element change without affecting other elements with the same class? 如何在不影响网页的情况下将文本向左移动? - How do I move the text to the left without affecting the web page? 如何在一行上有3张图像而在另一行上有2张图像? - How do I have 3 images on one line and 2 on another? 如何在不影响文本的情况下制作透明框? - How do I make a transparent box without affecting text? 如何在同一行上有两个DIV,一个不带HTML表格,一个左对齐,一个右对齐? - How can I have two DIV s on the same line , one aligned to the left and one aligned to the right without an HTML table? 如何将按钮与文本放在同一行? HTML - How do i put a button on the same line as text? HTML 如何在书签中包含来自另一个文件的HTML位? - How do I include HTML bits from another file in a bookmarklet? 如何将图像堆叠在同一div中并彼此叠加,然后在悬停时将它们与动画分开? - How do I stack images on top of each other wrapped in the same div, and then have them separate with animation when I hover? CSS:同一行上有2位文字,左右对齐? - CSS: Have 2 bits of text on same line, aligned left and right?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM