How to create a box with two separate texts

Asked

Viewed 272 times

4

Hello,

I am a beginner of XHTML and I ask you to tell me about this code and not Html5.

I want to make a header but I’m not getting it. The goal is for the first text to be higher than the first. See the image from the example sf.

I am in doubt in both xhtml and css. The box is to put as the first element of the site.

2 answers

5


A small example of how to achieve this using CSS only.

There are a multitude of other ways to achieve the same result.

HTML:

<div class="FUNDO">
    <span class="CIMA">Título do site</span>
    <span class="BAIXO">Outro texto</span>
</div>

CSS:

.FUNDO {
    margin: 0px;
    padding: 0px;
    position: relative;
    font: normal 30px sans-serif;
    color: #fff;
    background-color: #ccc;
    height: 220px;
}
.CIMA {
    position: absolute;
    left: 25px;
    top: 25px;
}
.BAIXO {
    position: absolute;
    right: 25px;
    bottom: 25px;
}

The example can be tested live in that Fiddle.

  • Carlosraelgn I really appreciate your help. 5 stars. Thank you very much. I only had one question: why can’t we use div id and p class? Div id is not unique?

  • @David can use the id, yes, and instead of span, p. It was just an example :)

5

As already answered by @carlosrafaelgn, there are several ways to do what you intend.

I used the same technique proposed by him, and made only a few structural changes. Originally I was going by how comment in the existing answer, but it was going to be unreadable, so I ended up posting separately. Anyway, the original answer already won my +1.

The difference is basically the use of semantic tags instead of divs, to preserve the logical structure of the page regardless of the CSS.

In case, I used the <p> for informative text in the second line of text, but you can use <h2> if it is a subtitle, for example. The important thing is to use the tag that best identifies the meaning of the content there.

HTML:

<div id="cab">
   <h1>Título do site</h1>
   <p>Outro texto</p>
</div>

CSS:

#cab    { position: relative;
          margin: 0;
          padding: 0;
          height: 200px; }

#cab h1 { position: absolute;
          top: 25px;
          left: 25px; }

#cab p  { position: absolute;
          bottom: 25px;
          right: 25px; }

As we use different tags for the two lines, it was unnecessary to specify classes, and to do so just use the descending selector.

See working on Jsfiddle.

  • Yes, I agree with you! It is better not to use spans generic, but elements with more specific meanings :) But if it were to use p, would have to configure the text-align, for right ;)

  • @carlosrafaelgn got to see the fiddle? I tested on Opera/Presto, Chrome, IE 11, IE 6(!), and FF, and everyone behaved well. The <p> with absolute theoretically it does not get 100% width of Parent, so it works as expected. But if you find some browser "dissident" in this aspect let me know, which I review accordingly.

  • That’s right! I didn’t notice it, no! It was right! My fault :(

  • @carlosrafaelgn grateful anyway. I understood your concern, usually need yes, because of the 100%. It’s just that in this particular case, the exception ended up helping.

  • Thank you all for the help :) I’m actually making a liquid layout with xhtml and so I don’t think I can use pixels. Instead I think I have to use the percentage. I confess it’s giving me some headache.

  • @David believes that you should not worry so much about it. It is very simple to use px and % in the same layout, and the media queries solve most of the problem on different devices. Do not fall into the illusion of wanting the site to simply "expand whole" in the same proportion, because in addition to differences in size, there are differences in proportion. The best way is to outline how it should look on the different screens, and then decide which "tool" to use on which part of the site. Look at the sites that behave as you would like, and study their techniques. This is independent of being (X)HTML, CSS is the same.

  • @Bacco thanks for the advice. You guys are 1000 grade. I’m learning xhtml and css. I’m walking back from a liquid site to practice the technique. After I dominate it I make another example gelatin website. I know that today there are tools more suitable for the final work of the liquid layout in xhtml after all we already use Html5 in conjunction with other tools. My goal is more to learn at this point. Veja - http://answall.com/questions/21302/como-cria-um-layout-liquido-com-xhtml-1-0-strict

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.