简体   繁体   English

相邻显示Div元素

[英]Displaying Div Elements next to each other

I'm looking to make a simple game of hangman, and am stuck at this step. 我正在寻找一个简单的子手游戏,并且被困在这一步。 I'm trying to display the alphabet in a horizontal line and make each letter clickable. 我试图在水平线上显示字母并使每个字母都可单击。 Here is what I have right now in my html file. 这是我现在在html文件中拥有的内容。

 #letters { height: 10%; width: 50%; right: 30%; display: inline-block; } 
 <p1> Guess the Word in 5 or less guesses <br>Click on each letter to guess the word </p1> <div id="letters"> <div id="a"> <p>a</p> </div> <div id="b"> <p>b</p> </div> <div id="c"> <p>c</p> </div> <div id="d"> <p>d</p> </div> <div id="e"> <p>e</p> </div> <div id="f"> <p>f</p> </div> <div id="g"> <p>g</p> </div> <div id="h"> <p>h</p> </div> <div id="i"> <p>i</p> </div> <div id="j"> <p>j</p> </div> <div id="k"> <p>k</p> </div> <div id="l"> <p>l</p> </div> <div id="m"> <p>m</p> </div> <div id="n"> <p>n</p> </div> <div id="o"> <p>o</p> </div> <div id="p"> <p>p</p> </div> <div id="q"> <p>q</p> </div> <div id="r"> <p>r</p> </div> <div id="s"> <p>s</p> </div> <div id="t"> <p>t</p> </div> <div id="u"> <p>u</p> </div> <div id="v"> <p>v</p> </div> <div id="w"> <p>w</p> </div> <div id="x"> <p>x</p> </div> <div id="y"> <p>y</p> </div> <div id="z"> <p>z</p> </div> </div> <div id="spots"> <p>_ _ _ _ _ _</p> </div> 

I've looked at other posts, but they don't seem to solve my problem. 我看过其他帖子,但它们似乎无法解决我的问题。

Also I'm a bit confused on the difference between a div class and a div id . 我也对div classdiv id之间的区别感到困惑。

You are only making the wrapping div inline-block you need to apply this style to each of the letters. 您仅使包装div inline-block您需要将此样式应用于每个字母。

You could do this by adding a class to all of the letters such as: 您可以通过向所有字母添加一个class来实现此目的,例如:

<div id="a" class="letter">a</div>
<div id="b" class="letter">b</div>

Then in your css apply inline-block 然后在您的CSS中应用inline-block

.letter{ display: inline-block }

You could also do this without the classes like so: 您也可以在没有这样的classes情况下执行此操作:

#letters > div { display: inline-block }

You can have multiple elements using the same class and an element can have multiple classes (separated by spaces) where as ids should be unique for each element and elements may only contain 1 id 您可以使用同一个class来包含多个elements ,一个element可以具有多个classes (以空格分隔),因为ids对于每个element都是unique ,并且elements只能包含1个id

Answering your doubt, regards to ID vs Class you can read this great article The differences between ID and class 回答您的疑问,关于ID与类,您可以阅读这篇很棒的文章ID与类之间的区别

Where one of the big difference is: 其中最大的区别之一是:

ID IS UNIQUE - CLASS ARE NOT UNIQUE ID是唯一的-类不是唯一的


Regards to your issue in hand 关于您的问题

you need to make your letters inline, because you are using div - which is a block level element - so a way to do that is set to inline-block to it. 您需要使字母内联,因为您使用的是div (这是块级元素),因此将其设置为inline-block即可。


EDIT 编辑

Great thank you. 太好了谢谢。 That works. 这样可行。 How would I now move this whole element to a specific spot on the screen. 我现在将如何将整个元素移动到屏幕上的特定位置。 The usual options don't seem to work. 通常的选项似乎不起作用。 When I use right, top, bottom, it makes the letters go vertical again. 当我使用右,上,下时,它使字母再次垂直。

the snippet was updated, you can achieve that using position:relative/absolute 代码段已更新,您可以使用position:relative/absolute来实现

 #letters > div { display: inline-block; } .hangman-game-wrap { position: relative; } /*move the game here*/ .hangman-game { position: absolute; top:0; right:0; bottom:0; left:0; margin: auto; /*change the width as you wish*/ width: 300px } 
 <div class="hangman-game-wrap"> <div class="hangman-game"> <p1> Guess the Word in 5 or less guesses <br>Click on each letter to guess the word </p1> <div id="letters"> <div id="a"> <p>a</p> </div> <div id="b"> <p>b</p> </div> <div id="c"> <p>c</p> </div> <div id="d"> <p>d</p> </div> <div id="e"> <p>e</p> </div> <div id="f"> <p>f</p> </div> <div id="g"> <p>g</p> </div> <div id="h"> <p>h</p> </div> <div id="i"> <p>i</p> </div> <div id="j"> <p>j</p> </div> <div id="k"> <p>k</p> </div> <div id="l"> <p>l</p> </div> <div id="m"> <p>m</p> </div> <div id="n"> <p>n</p> </div> <div id="o"> <p>o</p> </div> <div id="p"> <p>p</p> </div> <div id="q"> <p>q</p> </div> <div id="r"> <p>r</p> </div> <div id="s"> <p>s</p> </div> <div id="t"> <p>t</p> </div> <div id="u"> <p>u</p> </div> <div id="v"> <p>v</p> </div> <div id="w"> <p>w</p> </div> <div id="x"> <p>x</p> </div> <div id="y"> <p>y</p> </div> <div id="z"> <p>z</p> </div> </div> <div id="spots"> <p>_ _ _ _ _ _</p> </div> </div> </div> 

I would do it like this. 我会这样做。 Each element has its click event. 每个元素都有其click事件。 Also it can be simplified even more if you create each element dynamicaly using jQ. 如果使用jQ动态创建每个元素,它甚至可以进一步简化。

 $(".letter").each(function() { // gets id from attribute var letter = $(this).attr("id"); // create element <p> inside .letter $(this).append("<p>" + letter + "</p>"); // assign click event for each .letter $(this).on("click", function() { // TO DO when clicked $("#letterClicked").text("Clicked " + letter); }); }); 
 #letters{ height: 10%; width: 100%; right: 30%; display: flex; justify-content: flex-start; align-items: center; } .letter { background: black; color: white; width: 30px; height: 30px; margin: 1px; display: flex; justify-content: center; align-items: center; } 
 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> <div id="letters"> <div class="letter" id="a"></div> <div class="letter" id="b"></div> <div class="letter" id="c"></div> <div class="letter" id="d"></div> <div class="letter" id="e"></div> <div class="letter" id="f"></div> <div class="letter" id="g"></div> <div class="letter" id="h"></div> <div class="letter" id="i"></div> <div class="letter" id="j"></div> <div class="letter" id="k"></div> <div class="letter" id="l"></div> <div class="letter" id="m"></div> <div class="letter" id="n"></div> <div class="letter" id="o"></div> <div class="letter" id="p"></div> <div class="letter" id="q"></div> <div class="letter" id="r"></div> <div class="letter" id="s"></div> <div class="letter" id="t"></div> <div class="letter" id="u"></div> <div class="letter" id="v"></div> <div class="letter" id="w"></div> <div class="letter" id="x"></div> <div class="letter" id="y"></div> <div class="letter" id="z"></div> </div> <div id="letterClicked"></div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM