2

I am trying to get a section up and running on my website. I want to add a background image instead of a color. I have tried reading here and other websites and nothing I try seems to work. I am using this section code:

https://codepen.io/ckor/pen/lBnxh

!* {
box-sizing: border-box; 
}

ebody {
margin: 0; 
font-weight: 500;
font-family: 'HelveticaNeue';
}

esection {
width: 100%;
padding: 0 7%;
display: table;
margin: 0;
max-width: 100%;

background-image: url('https://s15.8cb2jiu3/banner_test.jpg');
background-repeat: no-repeat;
background-size: cover;
height: 100vh;

&:nth-of-type(2n) {
background-color: #D55B79;
}
}

.eintro {
 height: 90vh;
 }

 .econtent {
  display: table-cell;
  vertical-align: middle;
  }

  eh1 {
  font-size: 3em;
  display: block;
  color: white;
  font-weight: 300;
  }

  ep {
  font-size: 1.5em;
  font-weight: 500;
  color: #C3CAD9;
  }

  ea {
  font-weight: 700;
  color: #373B44;
  position: relative;

  e&:hover{ 
  opacity: 0.8;
  }

  e&:active {
  top: 1px;
  }
  }

  efooter {
  padding: 1% 5%;
  text-align:center;
  background-color: #373B44;
  color: white;

  ea {
  color: #FE4B74;
  font-weight: 500;
  text-decoration: none;
  }
  }

I have added this to the code:

 section {
    width: 100%;
    padding: 0 7%;
    display: table;
    margin: 0;
    max-width: 100%;

    background-image: url('https://s15.banner_test.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    height: 100vh;

The goal is to add images that are left or right justified and then add some text to the section. I am aiming for a left image then the next box is a right image and so on. I have seen the effect on other websites and it looks good if done correctly.

Larry Larry
  • 25
  • 1
  • 1
  • 5

2 Answers2

2

In order to add background image to any html element you will need the following line added to your css:

background-image: url("pathtoimage");

In your css code there is :

section {
  width: 100%;
  padding: 0 7%;
  display: table;
  margin: 0;
  max-width: none;
  background-color: #373B44;
  height: 100vh;
  }
  section:nth-of-type(2n) {
    background-color: #FE4B74;
  }

this code here will fetch for all section html tags and apply to them the styles.

in here you can even notice the first background-color:#373B44; that is applied to all sections and the second background-color: #FE4B74; that is only applied to sections with that are 2n

for example:

(1st section html element in your page) Section 1 will be color #373B44 (2nd section html element in your page) Section 2 will be color #FE4B74 (3rd section html element in your page) Section 3 will be color #373B44 (4th section html element in your page) Section 4 will be color #FE4B74 and so on

Now in order to add a background-image , all we have to do is add the code I provided above to your section in that way

    section {
      width: 100%;
      padding: 0 7%;
      display: table;
      margin: 0;
      max-width: none;
      background-color: #373B44;
      height: 100vh;

      /*added here*/
      background-image:url("https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&h=350");
       /*added here*/ }

      section:nth-of-type(2n) {
        background-color: #FE4B74;
      }

However, now you have two problems : 1- same background image in all sections (no alternating) 2- the image might be too small , not fit or might be repeated in a bad looking way

so in order to solve problem 1 all you have to do is just the same thing as what happens with background-color . So we just add another background-image under the 2n like so :

 section {
      width: 100%;
      padding: 0 7%;
      display: table;
      margin: 0;
      max-width: none;
      background-color: #373B44;
      height: 100vh;
        background-image:url("https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&h=350");}

      section:nth-of-type(2n) {
        background-color: #FE4B74;
        /*this will overwrite the other background image for your 2n sections*/
        background-image:url("https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&h=350");
      }

In order to solve the 2nd issue , you might want to use the following:

  background-repeat:no-repeat; /*removes repetition*/
  background-size:cover; /*allows picture to take up whole section space*/

and the final code will look like this

section {
  width: 100%;
  padding: 0 7%;
  display: table;
  margin: 0;
  max-width: none;
  background-color: #373B44;
  height: 100vh;
    background-image:url("https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&h=350");
    background-repeat:no-repeat;
  background-size:cover;}

  section:nth-of-type(2n) {
    background-color: #FE4B74;
    background-image:url("https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg?auto=compress&cs=tinysrgb&h=350");
  }

To better understand the background properties in CSS , I would really suggest going into the following link : https://www.w3schools.com/css/css_background.asp

And this link will help you understand the section:nth-of-type(2n) https://www.w3schools.com/cssref/sel_nth-of-type.asp

The codepen link https://codepen.io/sara-kat/pen/gKrVpg

Have a good day

Sara Kat
  • 378
  • 2
  • 19
  • Thanks a lot for the help. I added the code and the flower image is huge. Does the box/section grow with the image size I choose or do I choose a max height? So like where the height is set to 100% if I set it to like 80% or maybe 450 pixels it will only be that big? – Larry Larry Jun 05 '18 at 17:46
  • Also lets say the image looks good on the desktop but on mobile it is not correctly placed. Can I add some code so on mobile the image is reduced? – Larry Larry Jun 05 '18 at 17:53
  • hey again @LarryLarry background-size is what you are looking into for the size of the image , in fact some images might be smaller than the container (section) therefore we add background-size:cover ; to make it fill up , you can also try background-size: auto or background-size:contain or background-size: 80% 100% ; (where first value is x and second is y) you can see more details on the following link: https://www.w3schools.com/cssref/css3_pr_background-size.asp – Sara Kat Jun 05 '18 at 18:00
  • As for the mobile , yes you can add some media queries : (see here if you are not familiar with them https://www.w3schools.com/css/css_rwd_mediaqueries.asp) and then you can try different values of background-position or background-size until you are satisfied . It all depends on your ends.. see here for background position https://www.w3schools.com/cssref/pr_background-position.asp If you need anything else , please do not hesitate :) – Sara Kat Jun 05 '18 at 18:02
  • Lets say my wording I want in white text but the background image has white in it. Is there a way to add a "blur" to the background of just where the text is so that my wording is readable? I have seen these blurred backgrounds around text before. I tried an image I like but sadly the image makes the font wash out. Not too handy of a text block. I will for sure go look at the w3 schools. I play around there a lot as I am new to coding. I am so excited to see this working on the website. – Larry Larry Jun 05 '18 at 18:12
  • there sure is a way @LarryLarry look at this https://codepen.io/aniketpant/pen/DsEve from https://stackoverflow.com/questions/20039765/how-to-apply-a-css-3-blur-filter-to-a-background-image?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa if you find my answer helpful please mark it as accepted ^^ – Sara Kat Jun 05 '18 at 18:26
0

There is a really simple solution to this, actually. The link to the image isn't working. You don't have to change anything about the rest.

I gave your second section the class bg and put your code to style that section in your CSS. It works if you use a working link:

Codepen

section.bg {
    width: 100%;
    padding: 0 7%;
    display: table;
    margin: 0;
    max-width: 100%;

    background-image: url('//unsplash.it/1200/800');
    background-repeat: no-repeat;
    background-size: cover;
    height: 100vh;
}
Jos van Weesel
  • 2,128
  • 2
  • 12
  • 27
  • I will give this a try. Thanks for your comment and help. – Larry Larry Jun 05 '18 at 17:44
  • Let me know how it goes. – Jos van Weesel Jun 05 '18 at 18:01
  • 1
    It actually works now. I am very excited about this. Now to get my images to work properly on mobile. You throw an image in there and it looks great on the desktop but is not perfect for mobile. That and the fon't needs a box around it that is maybe blurred so the text can be read easily no matter the background image. – Larry Larry Jun 05 '18 at 18:14
  • Great :) You'll have to add media-queries for smaller screen-sizes. As for the text, you can add a background color (for example: rgba(0, 0, 0, 0.5) and some padding to increase the size of the "box". – Jos van Weesel Jun 05 '18 at 18:43