Height of a div

2019-02-27 15:55发布

Height of a div

I'm trying to set the size of a div according to its content. See the complete code here. Basically my problem is in the div: content

In the HTML code look for the comment: wrongly

There had to add a tag p, I'm giving it a clear that the div align to the end. Without this clear, the div will cut the image.

Actually I wonder just a better way to do this task.

There seemed a good a place p with nothing in it only with a clear class so that this problem does not happen. What do you recommend me? There is a better way to do this task?

The goal is not to let the rope div content:

Html

<div id="header">
    <div class="headerLeft" />
</div>

<div id="waycontact">
    <div class="contactPaula">
        <p>paulaMACHADO</p>
        <p>crea 11111/D-MG</p>
        <p>(00) 0000 0000</p>
    </div>
    <div class="contactBeatriz">
        <p>beatrizDAMAS</p>
        <p>crea 22222/D-MG</p>
        <p>(00) 0000 0000</p>
    </div>
    <div class="address">
        <p>av. xxxxxxxl, yyy, sl. zzz, centro - belo horizonte - mg</p>
        <p>cep: 00000-000 - telefax: (00) 0000 0000</p>
        <p><a href="mailto:xxxxx@yyyy.com.br">xxxxx@yyyy.com.br</a></p>
    </div>
</div>

<div id="content">
    <img class="left" alt="Arquitetura" src="http://cdn.archdaily.net/wp-content/uploads/2009/05/1265154380_107-silos-090511-west-view-a4-allard-architecture-528x304.jpg" />
    <h2>Sobre a empresa</h2>
    <p>A AMSD é focada em qualidade..</p>
    <p class="clear" /> <!--Wrongly!! -->
</div>

<div id="footer">
    <a class="current" href="#">home</a>
    |
    <a href="#">quem somos</a>
    |
    <a href="#">blog</a>
    |
    <a href="#">na mídia</a>
    |
    <a href="#">fale conosco</a>
</div>

CSS

body
{
    font-size: 0.87em;
    font-family: Calibri, Arial, Georgia, Verdana, Tahoma, Microsoft Sans Serif;
    margin: 0;
    padding: 0;
    color: #666666;
}

a:link
{
    color: rgb(124,71,111);
    text-decoration: underline;
}
a:visited
{
    color: rgb(41, 12, 36);
}
a:hover
{
    color: rgb(91,25,79);
    text-decoration: none;
}
a:active
{
    color: #AB6D9C;
}

p
{
    margin:2px;
}

ul
{
    list-style-type: square;
}
li
{
    line-height: 165%;
}
div#header
{
    width:1024px;
    margin:5px auto;
    height:150px;
    background-color: rgb(91,25,79);
}
div.headerLeft
{
    height:100%;
    width:900px;
    border-right:10px solid rgb(169, 171, 174);
    background-color: rgb(124,71,111);
}
div#footer
{
    margin-top:10px;
    text-align:center;
    position:relative;
    font-size: 1.27em;

}
div#content
{
    margin:auto 40px;
    width:200;
    border:2px solid red;
}


/* Others
-----------------------------------------------------------*/
div.contactPaula p, div.contactBeatriz p
{
    padding:0px;
    margin:0px;
}
div.contact, div.contactPaula, div.contactBeatriz
{
    margin-bottom:15px;
    margin-top:0px;
}
div.contactBeatriz
{
    float:right;
    text-align:right;
}
div.contactPaula
{
    float:left;
    text-align:left;
}

div.address
{
    text-align:center;
    margin-bottom:30px;
}

div#waycontact
{
    width:340px;
    margin:20px 40px;
}
.clear, div.address, div#footer, div#waycontact
{
    clear:both;
}
.left
{
    float:left;
}

a.current
{
    font-weight:bold;
}

标签: css layout html
3条回答
时光不老,我们不散
2楼-- · 2019-02-27 16:34

To make a container expand to fill floated elements, give it an overflow property:

div.wrapper {
    overflow: auto;
}
查看更多
狗以群分
3楼-- · 2019-02-27 16:42

A better method than using an empty <div class="clear"> is to use the 'cearfix' method.

This is an example borrowed straight from HTML5Bolierplate , just apply it to the div that is giving you trouble.

In your case #content will become #content.clearfix

/* The Magnificent Clearfix: Updated to prevent margin-collapsing on child elements.
   j.mp/bestclearfix */
.clearfix:before, .clearfix:after { content: "\0020"; display: block; height: 0; overflow: hidden; }
.clearfix:after { clear: both; }
/* Fix clearfix: blueprintcss.lighthouseapp.com/projects/15318/tickets/5-extra-margin-padding-bottom-of-page */
.clearfix { zoom: 1; 
查看更多
神经病院院长
4楼-- · 2019-02-27 16:44

Add a "overflow:hidden;" to the div#content so it will look like the following. You won't need a separate clear property and a p tag. Good luck :)

div#content
{
    margin:auto 40px;
    width:200;
    border:2px solid red;
    overflow:hidden;

}
查看更多
登录 后发表回答