简体   繁体   中英

How to align text within div?

In this fiddle http://jsfiddle.net/QLCeH/ the text is not aligning with the header "Google" and the text is wrapping beneath the image.

here is fiddle code :

HTML :

<div style="margin-left:auto;margin-right:auto; width: 600px;">
<div id="block">
<img height="50" style="max-width: 50px;background-position: top left;" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />

<div style="font-size:20px;font-weight:bold;">
<a href="http://www.google.com">Google</a>
</div>
<div>
This is some multi line text to show that multiple lines to not align very well.
</div>

CSS :

*{
    margin:0;
    padding:0;
}
#block { 
    border-width: 2px; 
    border-color: #4682B4; 
    background-color: WHITE; 
    width: 200px; 
    text-align: center; 
    line-height:30px;
    padding:3px 0;
    padding-right: 100px;
    float:left;
}
img{
float:left;
}
#block:hover {
  background-color: #C2DFFF ;
}

How can the text be aligned with the header "Google" and also prevent it from wrapping beneath the image ?

So that it appears like this :

在此输入图像描述

I've tried text align but it doesn't seem to provide this kind of option ?

Update :

This is the code I am now using :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>

<style type="text/css">
#block img{
    display:inline-block;
    vertical-align: top;
    width: 50px;
}
#text{
    display:inline-block;
    vertical-align: top;
    width: 350px;}


</style>

</head>

<body>

<div style="float: left;display: inline;width=450px" id="block">
    <img height="50" style="max-width: 50px;background:pink;;" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />

    <div style="font-size:20px;font-weight:bold;" id="text">
        <a href="http://www.google.com">Google</a><br />
        <p>This is some multi line text to show that multiple lines to not align very well.</p>
    </div>

</div>


<div style="float: left;display: inline;width=450px" id="block"> 
    <img height="50" style="max-width: 50px;background:pink;;" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />

    <div style="font-size:20px;font-weight:bold;" id="text">
        <a href="http://www.google.com">Google</a><br />
        <p>This is some multi line text to show that multiple lines to not align very well.</p>
    </div>
<div>


</body>
</html>

When I paste this code into IE8 this is the output :

在此输入图像描述

On Chrome this is the output : 在此输入图像描述

The Chrome output is correct, is the css used not supported on IE8 ?

You have to shuffle your html a bit:

<div id="block">
    <img height="50" src="http://socialmediababe.com/wp-content/uploads/2010/12/administrator.jpg" />

    <div style="font-size:20px;font-weight:bold;" id="text">
        <a href="http://www.google.com">Google</a><br />
        <p>This is some multi line text to show that multiple lines to not align very well.</p>
    </div>
<div>

and matching style (you dont need floats):

#block img{
    display:inline-block;
    vertical-align: top;
    width: 50px;
}
#text{
    display:inline-block;
    vertical-align: top;
    width: 150px;;
}

And a jsFiddle example

Add a selector id or class to your text, and add a padding to it.

.aligntext{
    padding-left: 40px;
}

JSFiddle: http://jsfiddle.net/QLCeH/4/

Another (probably cleaner) way is grouping the right part (Google title and text below) in a div and floating it: http://jsfiddle.net/QLCeH/11/

Add only this line to your css and it will solve your problem :

 div#block > div { text-align:left; margin-left:45px; }

DEMO HERE:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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