我尝试使用 css display:inline 和 inline-block 排列我的标题元素,但我所做的一切都不起作用
无论我尝试什么,我似乎都无法让标题元素内的分区排列起来而不是堆叠起来。我希望徽标向左浮动,而社交媒体图标和搜索栏向左浮动。另外,我希望搜索栏图标(放大镜)也与文本区域对齐。
这是 HTML:
<!DOCTYPE html>
<html>
<head>
<title>Maj's Php Project 2022</title>
<!-- Jquery Link -->
<!-- CSS Stylesheet Link -->
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<!-- Bootstrap Links -->
<!-- Metadata Information -->
<!-- Website Comment Information -->
</head>
<body>
<div id="wrapper">
<header>
<div id="logo">
</div>
<div id="social-media-icons">
<img src="img/fb.png" />
<img src="img/fb.png" />
<img src="img/fb.png" />
<img src="img/fb.png" />
</div>
<div id="search-bar">
<img src="img/search.png" width="25px" height="25px" />
<form action="#" method="POST">
<input type="text" placeholder="Search" name="search" id="search" />
<input type="submit" value="GO" name="submit" id="submit" />
</form>
</div>
<nav>
<ul>
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li>
</ul>
</nav>
</header>
这是 CSS:
/* GLOBALS */
/* ELEMENT */
body {
background-color: white;
}
header {
width: 960px;
background-color: #336699;
border-radius: 15px;
padding: 10px;
}
main {
background-color: #33FF99;
border-radius: 15px;
padding: 10px;
color: #330099;
}
li {
display: inline;
padding: 5%;
}
li > a {
text-decoration:none;
color: #33CC99;
}
li > a:hover {
color: #330066;
}
footer {
background-color: #336699;
border-radius: 15px;
padding: 10px;
color: #33CC99;
}
header {
display: inline-block;
}
/* IDS */
#wrapper {
border-radius: 15px;
width: 960px;
clear: both;
margin: 0 auto;
padding-top: 5px;
}
#logo {
width: 100px;
height: 100px;
margin: 5px;
margin-top: 5px;
background-color: white;
border-radius: 15px;
}
#search-bar {
display: inline;
float: right;
}
#social-media-icons {
float: right;
}
/* CLASSES */
我需要 中的所有 div 来对齐内联,但浮动元素不起作用。我还缺什么啊!请并谢谢您。
No matter what I attempt, I cannot seem to get the divisions within the header element to line up instead of stack. I wanted the logo to float left, while the social media icons and the search bar float left. Also, I want the search bar icon (magnifying glass) to also fall inline with the text area.
Here is the HTML:
<!DOCTYPE html>
<html>
<head>
<title>Maj's Php Project 2022</title>
<!-- Jquery Link -->
<!-- CSS Stylesheet Link -->
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<!-- Bootstrap Links -->
<!-- Metadata Information -->
<!-- Website Comment Information -->
</head>
<body>
<div id="wrapper">
<header>
<div id="logo">
</div>
<div id="social-media-icons">
<img src="img/fb.png" />
<img src="img/fb.png" />
<img src="img/fb.png" />
<img src="img/fb.png" />
</div>
<div id="search-bar">
<img src="img/search.png" width="25px" height="25px" />
<form action="#" method="POST">
<input type="text" placeholder="Search" name="search" id="search" />
<input type="submit" value="GO" name="submit" id="submit" />
</form>
</div>
<nav>
<ul>
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li> |
<li><a href="">HOME</a></li>
</ul>
</nav>
</header>
And here is the CSS:
/* GLOBALS */
/* ELEMENT */
body {
background-color: white;
}
header {
width: 960px;
background-color: #336699;
border-radius: 15px;
padding: 10px;
}
main {
background-color: #33FF99;
border-radius: 15px;
padding: 10px;
color: #330099;
}
li {
display: inline;
padding: 5%;
}
li > a {
text-decoration:none;
color: #33CC99;
}
li > a:hover {
color: #330066;
}
footer {
background-color: #336699;
border-radius: 15px;
padding: 10px;
color: #33CC99;
}
header {
display: inline-block;
}
/* IDS */
#wrapper {
border-radius: 15px;
width: 960px;
clear: both;
margin: 0 auto;
padding-top: 5px;
}
#logo {
width: 100px;
height: 100px;
margin: 5px;
margin-top: 5px;
background-color: white;
border-radius: 15px;
}
#search-bar {
display: inline;
float: right;
}
#social-media-icons {
float: right;
}
/* CLASSES */
I need all the divs in the to align inline, but floating the elements is not working. What am I missing! PLease and thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能希望让自己变得更轻松,并使用 Bootstrap 或 Flexbox,因为这些工具可以帮助您实现对齐愿望。使用 Bootstrap,您需要包含外部资源,例如 bootstrap css;有了 Flexbox,它就已经存在了。我更喜欢 Flexbox,因为它提供了更大的灵活性。
https://css-tricks.com/snippets/css/a -guide-to-flexbox/
我使用您的代码模拟了一个快速示例,并进行了一些轻微的添加和修改。我还假设您希望社交媒体图标和搜索功能浮动“右”而不是“左”。如果没有,那很容易改变。
HTML
CSS
随意使用您想要的模拟。 https://jsfiddle.net/9g6ms7pz/
You may wish to make it easier on yourself and utilize either Bootstrap or Flexbox because those tools can help you with alignment desires. With Bootstrap, you'll need to include external resources like bootstrap css; with Flexbox, it's already there. My preference is Flexbox because it provides more flexibility.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I mocked up a quick example using your code and making slight additions and modifications. I also presume you wanted the social media icons and the search functionality to float "right" instead of "left". If not, that's easy enough to change.
HTML
CSS
Feel free to play around with the mock as you desire. https://jsfiddle.net/9g6ms7pz/