viernes, 3 de septiembre de 2021

CSS Walk 3 Box Position

CSS Walk 3 Box Position

Box Position

Start with an html file with <div>s

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CSS course</title>
    <link rel="stylesheet" type="text/css" href="./css/styles.css" />
  </head>
  <body>
    <div>
      <h2>#1</h2>
    </div>
    <div>
      <h2>#2</h2>
    </div>
    <div>
      <h2>#3</h2>
    </div>
    <div>
      <h2>#4</h2>
    </div>
    <div>
      <h2>#5</h2>
    </div>
    <div>
      <h2>#6</h2>
    </div>
    <div>
      <h2>#7</h2>
    </div>
    <div>
      <h2>#8</h2>
    </div>
    <div>
      <h2>#9</h2>
    </div>
    <div>
      <h2>#10</h2>
    </div>
  </body>
</html>


and a css file

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

the browser output

 


Now lets change the position of the second box with the help of a class (movebox).

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CSS course</title>
    <link rel="stylesheet" type="text/css" href="./css/styles.css" />
  </head>
  <body>
    <div>
      <h2>#1</h2>
    </div>
    <div class="movebox">
      <h2>#2</h2>
    </div>
    <div>
      <h2>#3</h2>
    </div>
    <div>
      <h2>#4</h2>
... 

by css change the position with position:relative 

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.movebox {
  positionrelative;
  top-50px;
  bottom0;
  left150px;
  right0;
}

browser output:


 The relative position refers the start point where the top,bottom,left,right will refer as a start point, look the space of the #2 box is respeted.

With absolute, the box take the start reference point from the parent in this case <body>

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.movebox {
  positionabsolute;
  top-50px;
  bottom0;
  left150px;
  right0;
}

browser output, check the -50px is considering the body reference.

moving top 150px in absolute.

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.movebox {
  positionabsolute;
  top150px;
  bottom0;
  left150px;
  right0;
}

browser output:


Let´s put the box inside a parent box to check the effect of relative, absolute code.

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.parentbox {
  width300px;
  height300px;
  backgroundrgb(878383);
}

.movebox {
  positionabsolute;
  top0px;
  bottom0;
  left0px;
  right0;
}
 
 
Putting relative in parentbox, the child box take his reference of position

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.parentbox {
  width300px;
  height300px;
  backgroundrgb(878383);
  positionrelative;
}

.movebox {
  positionabsolute;
  top0px;
  bottom0;
  left0px;
  right0;
}

 
 

parent box relative and movebox relative

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.parentbox {
  width300px;
  height300px;
  backgroundrgb(878383);
  positionrelative;
}

.movebox {
  positionrelative;
  top0px;
  bottom0;
  left0px;
  right0;
}

 
...

Moving box inside parentbox
div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.parentbox {
  width300px;
  height300px;
  backgroundrgb(878383);
  positionrelative;
}

.movebox {
  positionabsolute;
  top50px;
  bottom0;
  left25px;
  right0;
}

...

Position: fixed
Let remove the parentbox, and the parent <div>

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.movebox {
  positionfixed;
  top50px;
  bottom0;
  left75px;
  right0;
}
...
The box #2 stay in the same palce even when moving the vertical scroll.
scrolling...
 

Position: static

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
}

.movebox {
  positionstatic;
  top50px;
  bottom0;
  left75px;
  right0;
}

 
with static, top, bottom, left, right doesn't work.
 

float: left

To ordening the position of the elements in a float way, use float

div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
  floatleft;
}

.movebox {
  positionstatic;
  top50px;
  bottom0;
  left75px;
  right0;
}
 
...
float right
div {
  backgrounddarkcyan;
  width100px;
  height100px;
  colorwhite;
  font-size30px;
  floatright;
}

.movebox {
  positionstatic;
  top50px;
  bottom0;
  left75px;
  right0;
}

...

 Using float to order images and paragraphs

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CSS course</title>
    <link rel="stylesheet" type="text/css" href="./css/styles.css" />
  </head>
  <body>
    <img src="images/css3.png" alt="css3" />
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla commodi et
      omnis cumque nam maiores ducimus similique libero, beatae magni
      accusantium exercitationem. Consequatur quo, voluptatibus quis rerum
      maxime sapiente modi.
    </p>
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla commodi et
      omnis cumque nam maiores ducimus similique libero, beatae magni
      accusantium exercitationem. Consequatur quo, voluptatibus quis rerum
      maxime sapiente modi.
    </p>
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla commodi et
      omnis cumque nam maiores ducimus similique libero, beatae magni
      accusantium exercitationem. Consequatur quo, voluptatibus quis rerum
      maxime sapiente modi.
    </p>
    <p>
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Nulla commodi et
      omnis cumque nam maiores ducimus similique libero, beatae magni
      accusantium exercitationem. Consequatur quo, voluptatibus quis rerum
      maxime sapiente modi.
    </p>
  </body>
</html>

 
browser output:
setting img to float: left;
 
img {
  floatleft;
}
...

 

Setting margin to the images

img {
  floatleft;
  margin-right30px;
  margin-bottom20px;
}

...
 
 
Braking the float starting from the second paragraph. clear

img {
  floatleft;
  margin-right30px;
  margin-bottom20px;
}

.clear {
  clearboth;
}

...

 
eot

No hay comentarios:

Publicar un comentario