首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在HTML + CSS中对齐Div

在HTML + CSS中对齐Div
EN

Stack Overflow用户
提问于 2014-10-15 18:25:52
回答 5查看 150关注 0票数 0

我试图在第四个div中对齐三个div,以创建类似于您在这个页面上看到的内容:http://www.thedistillerydistrict.com/

我似乎不能让里面的div (#娱乐,#社区,#欢迎)在#HomeMain div内并排排列

这是html中的

代码语言:javascript
复制
<div id = "HomeMain">
        <div id="welcome">
            <p>Finest Booze around, come taste for yourself Home to many of Toronto's hottest designer boutiques, unique cafes, artisan shops, breathtaking art galleries, performance venues and award-winning restaurants, The Distillery District is the place to see and be seen. An internationally acclaimed pedestrian-only village, The Distillery features more than 70 ground-floor cultural and retail establishments in the restored red brick, Victorian-era buildings of the renowned Gooderham & Worts whiskey distillery. One of Canada's hottest tourist attractions, centrally-located and just a short walk from downtown Toronto there is always something happening at The Distillery.</p>
            <div class = "Oldman"></div> 
        </div>
        <div id = "Entertainment">
            <img src="images/Entertainment1.jpg" id="EntSlide" width=125 height=70 />
        </div>
        <div id = "Community">
            <img src="images/Victoria1.jpg" id="ComSlide" width=125 height=70 />
        </div>
    </div>

这是CSS

代码语言:javascript
复制
#HomeMain{
width: 100%;
float: left;
overflow:hidden;
margin:0 auto;
padding:5px;
border-style: groove;
border-width: 3px;
border-colour: white;
border-radius: 5px 5px 5px 5px;
}

#Entertainment #Community{
float: left;
width: 25%;
border-style: groove;
border-width: 3px;
border-colour: white;
border-radius: 5px 5px 5px 5px;
}
#welcome{
float: left;
width:50%;
position: relative;
border-style: groove;
border-width: 3px;
border-color: white;
border-radius: 5px 5px 5px 5px;
font-weight: bold;
padding:15px;
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-10-15 18:43:06

检查这个小提琴链接http://jsfiddle.net/hek7fLy2/

我所做的就是使用方框大小的css属性来实现所需的结果。另外,我假设你想让2个较小的div中的图像居中,所以这就解决了这个问题。

我没有更改您的HTML代码,但是我只调整了一点css代码,包括错误..。

代码语言:javascript
复制
#HomeMain{
width: 100%;
float: left;
overflow:hidden;
margin:0 auto;
padding:5px;
border-style: groove;
border-width: 3px;
border-colour: white;
border-radius: 5px 5px 5px 5px;
box-sizing:border-box;
}

#Entertainment, #Community{
float: left;
width: 25%;
border-style: groove;
border-width: 3px;
border-colour: white;
border-radius: 5px 5px 5px 5px;
box-sizing:border-box;
}
#welcome{
float: left;
width:50%;
position: relative;
border-style: groove;
border-width: 3px;
border-color: white;
border-radius: 5px 5px 5px 5px;
font-weight: bold;
padding:15px;
box-sizing:border-box;
}
img{
    display:block;
    margin: 0 auto;
}
票数 1
EN

Stack Overflow用户

发布于 2014-10-15 18:34:05

检查这个小提琴

首先应该是

代码语言:javascript
复制
#Entertainment,#Community{

而不是

代码语言:javascript
复制
#Entertainment #Community{

接下来,您的div不对齐是因为您为3 div指定了3px的边框,这使得div跳到下一个line.So,这里我对每个div都使用了box-sizing: border-box属性。

试试..。

票数 1
EN

Stack Overflow用户

发布于 2014-10-15 18:37:28

您忘记了第二条规则中的逗号,为了使其正确,您必须使用box-sizing: border-box;

代码语言:javascript
复制
#HomeMain {
  width: 100%;
  float: left;
  overflow: hidden;
  margin: 0 auto;
  padding: 5px;
  border-style: groove;
  border-width: 3px;
  border-colour: white;
  border-radius: 5px;
  box-sizing: border-box;
}
#Entertainment,
#Community {
  float: left;
  width: 25%;
  border-style: groove;
  border-width: 3px;
  border-colour: white;
  border-radius: 5px;
  box-sizing: border-box
}
#welcome {
  float: left;
  width: 50%;
  position: relative;
  border-style: groove;
  border-width: 3px;
  border-color: white;
  border-radius: 5px;
  font-weight: bold;
  padding: 15px;
  box-sizing: border-box
}
代码语言:javascript
复制
<div id="HomeMain">
  <div id="welcome">
    <p>
      Finest Booze around...
    </p>
    <div class="Oldman"></div>
  </div>
  <div id="Entertainment">
    <img src="http://dummyimage.com/125x70" id="EntSlide">
  </div>
  <div id="Community">
    <img src="http://dummyimage.com/125x70" id="ComSlide">
  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26389516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档