1

I'm trying to make it so that the text in each DIV below is vertically centred so there is an equal amount of space above and below the text. I'd also like it so that I could change the colour of the URL so that it's not blue. I would also like for the two of them to be touching, they were but I can't figure out how to get that back.

<style>
  .message1 {
    height: 100px;
    width: 100%;
    background-color: #f0f0f0;
  }

 .message2 {
    height: 100px;
margin-top: 50px;
    width: 100%;
    background-color: #3a3a3a;
  }
</style>

<style>
.messagetext {
  margin-top: 0px;
  margin-bottom: 0px;
  margin-right: 0px;
  margin-left: 20px;
  padding: 0px;
</style>

<div class= "message1">

<div class= "messagetext"><font size="4" color= "#6d1a76"><p class="Roboto"><a href="URL">URL TEXT</p></font></a></div>

  </div>

<div class= "message2">

<div class= "messagetext"><font size="3" color= "#ffffff"><p class="Roboto">TEXT</p></font></div>

  </div>

It would be good also if they could go to each edge of the screen. There is still space on either side.

How it should look.

I'd like for it to look like this and have the colour go all of the way to the edge of the screen. I've got nowhere in hours.

Thanks in advance.

HTMLHelp
  • 51
  • 1
  • 1
  • 5

3 Answers3

3

You can go with flex and some CSS for color :

body {
 margin:0;
}

/* I added an extra element as the container so you may consider an existing element in your site */
.container {
  display: flex;
  height: 100vh; /* change this value to the height needed (ex: 100px) */
  margin: 0;
  flex-direction: column;
}

.message1 {
  flex: 1;
  align-items: center;
  display: flex;
  background-color: #f0f0f0;
  padding-left: 20px;
}

.message2 {
  flex: 1;
  align-items: center;
  display: flex;
  background-color: #3a3a3a;
  padding-left: 20px;
}

a {
  color: #000;
  /*Change color of link*/
}

p {
  color: #fff;
  /*change color of text*/
}
<div class="container">
  <div class="message1">
    <div class="messagetext">
      <p class="Roboto"><a href="URL">URL TEXT</a></p>
    </div>
  </div>
  <div class="message2">
    <div class="messagetext">
      <p class="Roboto">TEXT</p>
    </div>
  </div>
</div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • How would I make it so that they're a maximum of 100px high? I can no longer add content below them because they fill up the remainder of the screen. – HTMLHelp Dec 14 '17 at 22:27
  • @HTMLHelp as you can see in my code i used the body as container and used 100vh in the height ... so simply use another container and put 100px in the height ;) – Temani Afif Dec 14 '17 at 22:28
  • @HTMLHelp i updated my answer for more clarification :) – Temani Afif Dec 14 '17 at 22:30
  • I can't add any more content below it, the content sits behind them. How can I fix this? – HTMLHelp Dec 14 '17 at 22:40
  • @HTMLHelp is there a way i can see a complete code ? a link to the site ? maybe you have more CSS in conflcit – Temani Afif Dec 14 '17 at 22:43
  • It isn't live and I'd have to redact a lot. It works if I add a few breakers, I think they're on top of everything. – HTMLHelp Dec 14 '17 at 22:44
  • @HTMLHelp check if you have some absolute/fixed position, and consder adding maring than breakers – Temani Afif Dec 14 '17 at 22:46
  • I've isolated just the code for this and it still doesn't work to add things below it. – HTMLHelp Dec 14 '17 at 22:48
  • https://stackoverflow.com/questions/47823519/no-more-content-will-display-below-div I made a new question there. – HTMLHelp Dec 14 '17 at 23:10
1

I'm not sure why you are using the font tag, in HTML5 it's deprecated, but here's the code you are wanting to use.

<style>
  * {
    margin: 0;
    padding: 0;
  }
  
  .message1 {
    height: 100px;
    width: 100%;
    background-color: #f0f0f0;
  }
  
  .message2 {
    height: 100px;
    width: 100%;
    background-color: #3a3a3a;
  }
  
  .messagetext {
    margin-top: 0px;
    margin-bottom: 0px;
    margin-right: 0px;
    margin-left: 20px;
    padding: 0px;
    position: relative;
    top: 50%;
    transform: translateY(-50%);
  }
  
  a {
    text-decoration: none;
    color: purple;
  }
</style>

<div class="message1">

  <div class="messagetext">
    <font size="4" color="#6d1a76">
      <p class="Roboto"><a href="URL">URL</a></p>
    </font>
    </a>
  </div>

</div>

<div class="message2">

  <div class="messagetext">
    <font size="3" color="#ffffff">
      <p class="Roboto">TEXT</p>
    </font>
  </div>

</div>
leveeee
  • 70
  • 1
  • 7
0

First some general coding practices:

  1. (Assuming this isn't a copy-paste error) You do not need multiple <style> tags for your CSS.
  2. All the work you are doing with the html font tags can be handled easily in CSS. This makes it easier to edit and see what you are doing. There is a bit of conversion from the sizing you do to what is used in css. I used this SO answer: How to convert to px?

  3. You have HTML tags that are not nested correctly. The rule is "first opened, last closed". Your message1 div includes a paragraph and anchor set of tags that are intermeshed, not correctly nested.

First Version, Mostly Cleaning:

<style>
  .message1 {
    height: 100px;
    width: 100%;
    background-color: #f0f0f0;
    font-size: 1.13em;
    color: #6d1a76;
  }

 .message2 {
    height: 100px;
    margin-top: 50px;
    width: 100%;
    background-color: #3a3a3a;
    font-size: 1em;
    color: #ffffff;
  }

 .messagetext {
    margin-top: 0px;
    margin-bottom: 0px;
    margin-right: 0px;
    margin-left: 20px;
    padding: 0px;
  }
</style>


<div class= "message1">
   <div class= "messagetext">
      <p class="Roboto">
         <a href="URL">URL TEXT</a>
      </p>
   </div>
</div>

<div class= "message2">
   <div class= "messagetext">
      <p class="Roboto">TEXT</p>
   </div>
</div>

Ok, now that this is a bit more readable, let's start changing your CSS to get the actual output you want.

  1. The gap between your top div and the bottom one is because of the margin you are adding to the class .message2, margin-top: 50px;. Margin adds distance between the element it is applied to and the surrounding elements. In this case, it is pushing everything away from its top, including .message1 div.
  2. A super fun thing about HTML in the browser is that we get some styles applied to our elements "for free" or, without us explicitly asking for them. This is why links look the way they do AND why we still have space between the divs after removing the margin-top: 50px; This can be done through a reset.css file (see: CSS reset - What exactly does it do? for more info). To do this ourselves, I've added a little bit of reset just for this. I've set the padding and margin of div, p, and a (the three elements you use in this html) to 0. This is the first style in the style tags. Note the commas between element names: you can apply 1 set of styles to multiple elements at once that way :)
  3. To get rid of that color on the links, we need to add another bit of reset. To do this, we'll create another style rule, this time just for a tags. We'll set the text-decoration: none; to get rid of the underline. Next, we'll set the color: #6d1a76; to get that straightened out.
  4. You dont need that messagetext class, as you are only using it to set a margin on the content of your divs. Instead, add padding-left: 20px; to your message1 and message2 classes. Padding on an outer element pushes inner elements away from the parent's edges. Same effect, in this case, as adding margin to the inner element.
  5. Finally, we need to get that text centered vertically. There are several ways of doing this, but the best (IMO at least) is using CSS flex. For both message divs, we'll add two styles: display: flex; and align-items: center; to fix:

New Version:

<style>
   div, p, a {
     margin: 0;
     padding: 0;
   }

   a {
     text-decoration: none;
     color: #6d1a76;
   }
   .message1 {
     height: 100px;
     width: 100%;
     background-color: #f0f0f0;
     font-size: 1.13em;
     color: #6d1a76;
     margin: 0;
     padding-left: 20px;
     display: flex;
     align-items: center;
   }

  .message2 {
     height: 100px;
     width: 100%;
     background-color: #3a3a3a;
     font-size: 1em;
     color: #ffffff;
     margin: 0;
     padding-left: 20px;
     display: flex;
     align-items: center;
   }
 </style>


 <div class= "message1">
    <p class="Roboto">
      <a href="URL">URL TEXT</a>
    </p>
 </div>

 <div class= "message2">
    <p class="Roboto">TEXT</p>
 </div>

There is more work you can do with cleaning up your CSS as well so you aren't re-writing so many styles:

<style>
   div, p, a {
     margin: 0;
     padding: 0;
   }

   a {
     text-decoration: none;
     color: #6d1a76;
   }

   div {
     height: 100px;
     width: 100%;
     padding-left: 20px;
     display: flex;
     align-items: center;
   }

   .message1 {
     background-color: #f0f0f0;
     font-size: 1.13em;
     color: #6d1a76;
   }

  .message2 {
     background-color: #3a3a3a;
     font-size: 1em;
     color: #ffffff;
   }
 </style>


 <div class= "message1">
    <p class="Roboto">
      <a href="URL">URL TEXT</a>
    </p>
 </div>

 <div class= "message2">
    <p class="Roboto">TEXT</p>
 </div>
Andrea
  • 839
  • 8
  • 21
  • Wow! Thank You! At least I've learned more than I was expecting to. I've mostly solved it, I can no longer add additional elements below them though? – HTMLHelp Dec 14 '17 at 22:33
  • Hmmm ... it should be as simple as adding another div with content underneath the existing. Suggestion: before adding new content, try copy-paste one of the existing message divs (since we know that they both work) and see if you still get the error. If you do, either comment on the person whose solution you used, or post a new question (this allows the most other ppl to benefit from the discussion taht will follow) – Andrea Dec 14 '17 at 22:36
  • Yeah it's still happening. I've tried creating another div but everything just displays below it. – HTMLHelp Dec 14 '17 at 22:39
  • 1. Check to make sure that all your tags are opening/closing where you expect them to, including the style tag 2. Make sure that you have all your tag names spelled correctly (such a common error ... even if it is silly :) ) 3. Learn how to use dev tools. For Chrome, its : https://developer.chrome.com/devtools Or 4. Post a new question so that we can get code snippets &c. again – Andrea Dec 14 '17 at 22:41