Duplicating Youtube's navigation bar, but, I have a habit of using position: absolute; I need a more effective way of doing this - html

This is the epitome of DRY code, I need to find an effective way of writing this code. I am currently using The Odin Project and the guidelines were to Duplicate Youtube's video section. I completed the navigation bar, but I am unhappy with it since I am just using position: absolute all the time. I was about using Grid or flexbox but I do not really know if it will help.
* {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
body {
background-color: #f8f8f8;
}
.navigation {
height: 70px;
width: 100%;
background-color: #fff;
box-shadow: 0px 1px 4px #ebebeb;
}
.menu {
position: absolute;
left: 30px;
top: 22px;
opacity: .4;
cursor: pointer;
}
.menu:hover {
opacity: 1;
}
.youtube-logo {
width: 90px;
position: absolute;
left: 70px;
top: 10px;
}
.search-bar {
position: absolute;
top: 21px;
left: 390px;
width: 600px;
height: 33px;
background: url(images/search.svg) no-repeat 95% 50%;
background-size: 16px;
border-radius: 2px;
border: 1px solid #b3b3b3;
padding-left: 20px;
box-sizing: border-box;
outline: none;
}
::placeholder {
font-family: Arial, Helvetica, sans-serif;
font-size: 1.2em;
padding-left: 3px;
}
.video {
position: absolute;
left: 1150px;
top: 25px;
cursor: pointer;
opacity: .4;
}
.stack {
position: absolute;
left: 1210px;
cursor: pointer;
width: 18px;
opacity: .4;
top: 27px;
}
.message {
position: absolute;
left: 1265px;
top: 24px;
;
opacity: .4;
cursor: pointer;
}
.bell {
position: absolute;
left: 1320px;
top: 23px;
opacity: .4;
cursor: pointer;
}
.icon {
position: absolute;
top: 16px;
left: 1374px;
cursor: pointer;
width: 36px;
opacity: .4;
}
<nav class="navigation">
<img src="images/menu.svg" alt="menu for the top left, shaped like a hamburger" class="menu">
<img src="images/youtube.logo.png" alt="youtube logo" class="youtube-logo">
<input type="text" name="searchbar" placeholder="Search" class="search-bar">
<img src="images/video.svg" alt="video icon" class="video">
<img src="images/google-app-button.png" alt="square stacks" class="stack">
<img src="images/message-square (1).svg" alt="message forr youtube" class="message">
<img src="images/bell.svg" alt="bell for top right bar" class="bell">
<img src="images/icons8-male-user-512.png" alt="profile picture for the bar" class="icon">
</nav>

Flexbox is pretty straight forward. You only need to set display: flex; on the parent element and the children elements will be affected.
.parent-flex {
display: flex;
}
<h2>Without Flexbox</h2>
<div>
<div>Child 1</div>
<div>Child 2</div>
<div>Child 3</div>
</div>
<h2>With Flexbox</h2>
<div class="parent-flex">
<div class="child-1">Child 1</div>
<div class="child-2">Child 2</div>
<div class="child-3">Child 3</div>
</div>
Since you are trying to replicate the youtube navbar, I'd recommend using flexbox to setup the "regions" of the navbar and then applying justify-content: space-between; to space out the regions equally.
.flex {
display: flex;
}
.parent {
justify-content: space-between;
}
.child-1 {}
.child-2 {}
.child-3 {
justify-content: flex-end;
}
<div class="flex parent">
<div class="flex child-1">
<div class="menu">[Menu Icon]</div>
<div class="logo">[Logo]</div>
</div>
<div class="flex child-2">
<div class="search-box">[search box]</div>
<div class="search-button">[search button]</div>
</div>
<div class="flex child-3">
<div class="user-photo">[the user's photo]</div>
<div class="etc">[whatever other icons...]</div>
</div>
</div>
You can check out this great article on css-tricks to learn more about the ins and outs of flexbox.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

Related

positioning a div in above another div

This is challenging for me to position the share div like in the picture above. well, I tried the position absolute with bottom and left it's so frustrating adjusting the px but the output is always either stacked on top or bottom. how can I achieve that similar output in the picture?
:root {
--VeryDarkGrayishBlue: hsl(217, 19%, 35%);
--DesaturatedDarkBlue: hsl(214, 17%, 51%);
--GrayishBlue: hsl(212, 23%, 69%);
--LightGrayishBlue: hsl(210, 46%, 95%);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Manrope", sans-serif;
}
body {
background-color: var(--GrayishBlue);
display: flex;
flex-direction: column;
min-height: 100vh;
padding: 20px;
}
.container {
display: grid;
grid-template-columns: 2fr 3fr;
max-width: 1150px;
max-height: 390px;
margin: auto;
background-color: white;
overflow: hidden;
border-radius: 0.8em;
}
.img-box {}
.img-box img {
width: 100%;
height: 100%;
object-fit: cover;
}
.text-box {
padding: 8%;
}
.text {
padding-bottom: 30px;
}
.title {
color: var(--VeryDarkGrayishBlue);
padding-bottom: 10px;
}
.subtitle {
color: var(--GrayishBlue);
font-size: 1.1em;
}
.writer img {
width: 50px;
height: 50px;
border-radius: 50%;
}
.footer {
display: flex;
flex-direction: row;
align-items: center;
}
.name {
margin-left: 12px;
}
.name h4 {
color: var(--VeryDarkGrayishBlue);
}
.name p {
color: var(--GrayishBlue);
}
.share {
margin-left: auto;
}
.share-icon button {
border: none;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: var(--LightGrayishBlue);
cursor: pointer;
}
.share-option {
width: 250px;
height: 40px;
background: var(--VeryDarkGrayishBlue);
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
color: white;
position: absolute;
bottom: ;
}
<main class="container">
<div class="img-box">
<img src="/images/drawers.jpg" alt="" />
</div>
<div class="text-box">
<div class="text">
<h1 class="title">
Shift the overall look and feel by adding these wonderful touches to furniture in your home
</h1>
<p class="subtitle">
Ever been in a room and felt like something was missing? Perhaps it felt slightly bare and uninviting. I’ve got some simple tips to help you make any room feel complete.
</p>
</div>
<div class="footer">
<div class="writer">
<img src="/images/avatar-michelle.jpg" alt="" />
</div>
<div class="name">
<h4>Michelle Appleton</h4>
<p>28 Jun 2020</p>
</div>
<div class="share">
<div class="share-icon">
<button><img src="/images/icon-share.svg" alt=""></button>
</div>
<div class="share-option hidden">
<span>Share</span>
<a href="#"> <img src="/images/icon-facebook.svg" alt=""> <a/>
<a href="#"> <img src="/images/icon-pinterest.svg" alt=""> <a/>
<a href="#"> <img src="/images/icon-twitter.svg" alt=""> <a/>
</div>
</div>
</div>
</div>
</main>
I have made some changes in the code and made the popup visible with absolute.
:root {
--VeryDarkGrayishBlue: hsl(217, 19%, 35%);
--DesaturatedDarkBlue: hsl(214, 17%, 51%);
--GrayishBlue: hsl(212, 23%, 69%);
--LightGrayishBlue: hsl(210, 46%, 95%);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Manrope", sans-serif;
}
body {
background-color: var(--GrayishBlue);
display: flex;
flex-direction: column;
min-height: 100vh;
padding: 20px;
}
.container {
display: grid;
grid-template-columns: 2fr 3fr;
max-width: 1150px;
max-height: 390px;
margin: auto;
background-color: white;
border-radius: 0.8em;
}
.container:after {
display: block;
content: '';
clear: both;
}
.img-box {}
.img-box img {
width: 100%;
height: 100%;
object-fit: cover;
}
.text-box {
padding: 8%;
}
.text {
padding-bottom: 30px;
}
.title {
color: var(--VeryDarkGrayishBlue);
padding-bottom: 10px;
}
.subtitle {
color: var(--GrayishBlue);
font-size: 1.1em;
}
.writer img {
width: 50px;
height: 50px;
border-radius: 50%;
}
.footer {
display: flex;
flex-direction: row;
align-items: center;
}
.name {
margin-left: 12px;
}
.name h4 {
color: var(--VeryDarkGrayishBlue);
}
.name p {
color: var(--GrayishBlue);
}
.share {
margin-left: auto;
position: relative;
padding: 20px 0 0;
}
.share-icon button {
border: none;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: var(--LightGrayishBlue);
cursor: pointer;
}
.share-option {
width: 250px;
height: 40px;
background: var(--VeryDarkGrayishBlue);
border-radius: 10px;
color: white;
position: absolute;
bottom: 100%;
right: 50%;
transform: translatex(50%);
visibility: hidden;
opacity: 0;
transition: visibility 0s, opacity 0.5s linear;
}
.share-option:after {
top: 100%;
left: 50%;
border: solid transparent;
content: "";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-color: rgba(136, 183, 213, 0);
border-top-color: var(--VeryDarkGrayishBlue);
border-width: 10px;
margin-left: -10px;
}
.share:hover .share-option {
visibility: visible;
opacity: 1;
}
<main class="container">
<div class="img-box">
<img src="/images/drawers.jpg" alt="" />
</div>
<div class="text-box">
<div class="text">
<h1 class="title">
Shift the overall look and feel by adding these wonderful touches to furniture in your home
</h1>
<p class="subtitle">
Ever been in a room and felt like something was missing? Perhaps it felt slightly bare and uninviting. I’ve got some simple tips to help you make any room feel complete.
</p>
</div>
<div class="footer">
<div class="writer">
<img src="/images/avatar-michelle.jpg" alt="" />
</div>
<div class="name">
<h4>Michelle Appleton</h4>
<p>28 Jun 2020</p>
</div>
<div class="share">
<div class="share-icon">
<button><img src="/images/icon-share.svg" alt=""></button>
</div>
<div class="share-option hidden">
<span>Share</span>
<a href="#"> <img src="/images/icon-facebook.svg" alt=""> <a/>
<a href="#"> <img src="/images/icon-pinterest.svg" alt=""> <a/>
<a href="#"> <img src="/images/icon-twitter.svg" alt=""> <a/>
</div>
</div>
</div>
</div>
</main>
I think without javascript you won't be able to do it.
You could include your share-option (position absolute) inside share-icon (position relative). This way option would be positioned relative to the button.
<div class="share">
<div class="share-icon">
<button>
<img src="/images/icon-share.svg" alt="">
</button>
<div class="share-option hidden">
<span>Share</span>
<img src="/images/icon-facebook.svg" alt="">
<img src="/images/icon-pinterest.svg" alt="">
<img src="/images/icon-twitter.svg" alt="">
</div>
</div>
</div>
and css
.share-icon {
position: relative;
}
.share-icon button {
border: none;
width: 40px;
height: 40px;
border-radius: 50%;
background-color: var(--LightGrayishBlue);
cursor: pointer;
}
.share-option {
width: 250px;
height: 40px;
background: var(--VeryDarkGrayishBlue);
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
color: white;
position: absolute;
top: -40px;
z-index: 100;
}
Now the problem is options are in the boundary of the relative, so it doesn't extend on the right!
I suppose this container is responsive, meaning size can change. So I think the only way would be to put the option in the body (display none), when a click (or hover) happens on the button, you take the position of the button and position options on top of it with display flex

Popup over sticky components

I have two sticky boxes. On hovering over a particular area inside the sticky boxes a popup opens. I want these popups to appear always on top of the sticky boxes. But increasing the z index of one hides the other. Any solution ?
Note - Removing sticky from the boxes and keeping z index of both the box same solves the problem but I need the boxes to be sticky.
.box {
margin: 40px;
padding: 20px;
background: yellow;
border: 1px solid red;
position: sticky;
}
.innerBox {
width: 100px;
height: 50px;
background: lightgreen;
position: relative;
}
.popup1 {
position: absolute;
width: 50px;
height: 150px;
top: 25px;
left: 35px;
background: red;
display: none;
}
.popup2 {
position: absolute;
width: 50px;
height: 150px;
bottom: 25px;
left: 35px;
background: black;
display: none;
}
.box1:hover .popup1 {
display: block;
}
.box2:hover .popup2 {
display: block;
}
.boxUp {
z-index: 3;
}
.boxDown {
z-index: 3;
}
<div>
<div class="box boxUp">
<div class="innerBox box1">
<p>
Hover Here
</p>
<div class="popup1">
</div>
</div>
</div>
<div class="box boxDown">
<div class="innerBox box2">
<p>
Hover Here
</p>
<div class="popup2">
</div>
</div>
</div>
</div>
Set the z-index on hover the .box
.box {
margin: 40px;
padding: 20px;
background: yellow;
border: 1px solid red;
position: sticky;
}
.innerBox {
width: 100px;
height: 50px;
background: lightgreen;
position: relative;
}
.popup1 {
position: absolute;
width: 50px;
height: 150px;
top: 25px;
left: 35px;
background: red;
display: none;
}
.popup2 {
position: absolute;
width: 50px;
height: 150px;
bottom: 25px;
left: 35px;
background: black;
display: none;
}
.box1:hover .popup1 {
display: block;
}
.box2:hover .popup2 {
display: block;
}
.box:hover {
z-index: 2;
}
<div>
<div class="box boxUp">
<div class="innerBox box1">
<p>
Hover Here
</p>
<div class="popup1">
</div>
</div>
</div>
<div class="box boxDown">
<div class="innerBox box2">
<p>
Hover Here
</p>
<div class="popup2">
</div>
</div>
</div>
</div>

Text when hover on HTML

I would like to place a text on an image when hovering it. So far, I have created a hover effect to zoom the picture in and reduce the opacity which looks smooth. My problem now is to place text on the image because I am not sure how to place it in the picture.
Here is what I have so far: enter link description here
Code:
#portfolio {
background-color: : white;
padding-bottom: 100px;
}
#portfolio h1 {
font-size: 30px;
font-weight: 400px;
letter-spacing: 5px;
text-align: center;
color: #000;
}
#portfolio h2 {
font-size: 15px;
letter-spacing: 2px;
text-align: center;
color: #000;
}
.project {
display: inline-block;
width: 33.33%;
margin-right: -4px;
}
.img-box {
padding: 20px;
}
.project img {
width: 100%;
display: block;
border-radius: 12px;
}
.img-box img:hover {
transform: scale(1.1);
transition: 0.5s;
opacity: 0.5;
}
<section id="portfolio">
<div class="container">
<h1>MY WORK</h1>
<h2>Below you will find my favorite projects & school assignments</h2>
<!--CPU-->
<div class="project">
<div class="img-box">
<img src="./img/cpu.png">
</div>
</div>
<!--JAVA-->
<div class="project">
<div class="img-box">
<img src="./img/JSON.png">
</div>
</div>
<!--PHOTOSHOP-->
<div class="project">
<div class="img-box">
<img src="./img/photoshop.png">
</div>
</div>
</div>
</section>
Add another DIV as a child element to .img-box with settings as follows which contains the text. The most important part is position: absolute, plus the top, left and transform settings for the text position.
.img-box .hovertext {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.img-box:hover .hovertext {
display: block;
}
Also make sure to add position: relative to .img-box to create an anchor element for the absolutely positioned text DIV:
#portfolio {
background-color: : white;
padding-bottom: 100px;
}
#portfolio h1 {
font-size: 30px;
font-weight: 400px;
letter-spacing: 5px;
text-align: center;
color: #000;
}
#portfolio h2 {
font-size: 15px;
letter-spacing: 2px;
text-align: center;
color: #000;
}
.project {
display: inline-block;
width: 33.33%;
margin-right: -4px;
}
.img-box {
padding: 20px;
position: relative;
}
.project img {
width: 100%;
display: block;
border-radius: 12px;
}
.img-box img:hover {
transform: scale(1.1);
transition: 0.5s;
opacity: 0.5;
}
.img-box .hovertext {
display: none;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.img-box:hover .hovertext {
display: block;
}
<section id="portfolio">
<div class="container">
<h1>MY WORK</h1>
<h2>Below you will find my favorite projects & school assignments</h2>
<!--CPU-->
<div class="project">
<div class="img-box">
<img src="./img/cpu.png">
<div class="hovertext">Test Text 1</div>
</div>
</div>
<!--JAVA-->
<div class="project">
<div class="img-box">
<img src="./img/JSON.png">
<div class="hovertext">Test Text 2</div>
</div>
</div>
<!--PHOTOSHOP-->
<div class="project">
<div class="img-box">
<img src="./img/photoshop.png">
<div class="hovertext">Test Text 3</div>
</div>
</div>
</div>
</section>
You can use pseudo-element and you can either add the text you want as an attribute value or add it manually which would be hideous. and then style it as you would, but of course add position:absolute to take it off of the flow so it can be put on top of the image. And don't forget your position:relative on the parent.
I used the div, because <img> doesn't have a closing tag therefore no pseudo-elements
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#portfolio {
background-color: : white;
padding-bottom: 100px;
}
#portfolio h1 {
font-size: 30px;
font-weight: 400px;
letter-spacing: 5px;
text-align: center;
color: #000;
}
#portfolio h2 {
font-size: 15px;
letter-spacing: 2px;
text-align: center;
color: #000;
}
.project {
display: inline-block;
width: 33.33%;
margin-right: -4px;
}
.img-box {
padding: 20px;
position: relative;
}
.img-box:after {
/* the text will be coming from the custom attribute data-text*/
content: attr(data-text);
background: #000000ba;
width: 100%;
font-size: 1.5em;
padding: 10px 0;
left: 0;
bottom: 0;
text-align: center;
position: absolute;
opacity: 0;
transition: opacity .2s linear;
}
.img-box:hover:after {
opacity: 1;
}
.project img {
width: 100%;
display: block;
border-radius: 12px;
}
.img-box img:hover {
transform: scale(1.1);
transition: 0.5s;
opacity: 0.5;
}
<section id="portfolio">
<div class="container">
<h1>MY WORK</h1>
<h2>Below you will find my favorite projects & school assignments</h2>
<!--CPU-->
<div class="project">
<div data-text="Text" class="img-box">
<img src="http://via.placeholder.com/350x150">
</div>
</div>
<!--JAVA-->
<div class="project">
<div data-text="text on image" class="img-box">
<img src="http://via.placeholder.com/350x150">
</div>
</div>
<!--PHOTOSHOP-->
<div class="project">
<div data-text="i'm a text too" class="img-box">
<img src="http://via.placeholder.com/350x150">
</div>
</div>
</div>
</section>

unable to get height as 100 percent

I have created a div and inside the div I have made bars for which I have to set max-height and height as 100% but I do not know what is the problem as the height 100% is not showing. Can anyone help me out with this. I want to show the bars height to be placed according to the values but the problem is the height is only showing when I am setting height in pixels it is not working when I place height as 100% I want to show the height to be 100% can anyone help me out with fixing this issue for me please would be grateful for your help.
Here is my code -
.bar_graph {
width: 341px;
height: 258px;
background-color: #fff;
position: absolute;
top: 15px;
left: 15px;
padding: 20px 12px;
display: block;
border: 1px solid #a3a3a3;
}
span.line_gr {
display: block;
background-color: #3d4a7b;
margin: auto;
width: 24px;
max-height: 177px;
margin-left: 0;
height: 100%;
}
.bars_area {
width: 238px;
float: right;
position: absolute;
bottom: -8px;
right: 0;
}
.bars {
float: left;
margin: 0 10px;
margin-top: 20px;
height: 100%;
width: 27px;
}
.bars >p {
font-size: 12px;
margin: 0;
line-height: 30px;
font-weight: bold;
}
.head_graph > h2 {
font-size: 14px;
font-weight: bold;
margin-bottom: 6px;
color: #000;
}
.bar_main {
height: 200px;
position: relative;
}
.bar_main > .line {
height: 60px;
width: 100%;
border-top: 1px solid #ccc;
line-height: 20px;
font-size: 12px;
}
<div class="bar_graph">
<div class="head_graph"><h2>Heading</h2></div>
<div class="bar_main">
<div class="line">3000,000 Mil</div>
<div class="line">200,000</div>
<div class="line">100,000</div>
<div class="line"></div>
<div class="bars_area">
<div class="bars">
<span class="line_gr"></span>
<p>2016</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2015</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2014</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2013</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2012</p>
</div>
</div>
</div>
</div>
I do not know what is the problem as the height 100% is not showing
A height in percent only works if the parent element also has a height (either set explicitly, or implicitly based on other properties, such as an absolute positioned element with top and bottom given).
Your .bars_area does not have such a height, so trying a height in percentage on the children won’t work either.
As others says it won't work. I can suggest you a different approach if you like you can do it.Use div instead of span.Remember I am just suggesting you another approach according your code not an answer.
.bar_graph {
width: 341px;
height: 258px;
background-color: #fff;
position: absolute;
top: 15px;
left: 15px;
padding: 20px 12px;
display: block;
border: 1px solid #a3a3a3;
}
.inner {
position: absolute;
width:5px;
height:200px;
background: grey;
bottom:25px;
}
span.line_gr {
display: block;
background-color: #3d4a7b;
margin: auto;
width: 24px;
max-height: 177px;
margin-left: 0;
height: 100%;
}
.bars_area {
width: 238px;
float: right;
position: absolute;
bottom: -8px;
right: 0;
}
.bars {
float: left;
margin: 0 10px;
margin-top: 20px;
height: 100%;
overflow: hidden;
}
.bars >p {
font-size: 12px;
margin: 0;
line-height: 30px;
font-weight: bold;
}
.head_graph > h2 {
font-size: 14px;
font-weight: bold;
margin-bottom: 6px;
color: #000;
}
.bar_main {
height: 200px;
position: relative;
}
.bar_main > .line {
height: 60px;
width: 100%;
border-top: 1px solid #ccc;
line-height: 20px;
font-size: 12px;
}
<div class="bar_graph">
<div class="head_graph"><h2>Heading</h2></div>
<div class="bar_main">
<div class="line">3000,000 Mil</div>
<div class="line">200,000</div>
<div class="line">100,000</div>
<div class="line"></div>
<div class="bars_area">
<div class="bars">
<div class="inner"></div>
<p>2016</p>
</div>
<div class="bars">
<div class="inner"></div>
<p>2015</p>
</div>
<div class="bars">
<div class="inner"></div>
<p>2014</p>
</div>
<div class="bars">
<div class="inner"></div>
<p>2013</p>
</div>
<div class="bars">
<div class="inner"></div>
<p>2012</p>
</div>
</div>
</div>
</div>
First set .bars_area to height 100% and then set position relative on .bars
I hope it is what you want.
.bar_graph {
width: 341px;
height: 258px;
background-color: #fff;
position: absolute;
top: 15px;
left: 15px;
padding: 20px 12px;
display: block;
border: 1px solid #a3a3a3;
}
span.line_gr {
display: block;
background-color: #3d4a7b;
margin: auto;
width: 24px;
max-height: 177px;
margin-left: 0;
height: 100%;
}
.bars_area {
width: 238px;
float: right;
position: absolute;
bottom: -8px;
right: 0;
height: 100%;
}
.bars {
float: left;
margin: 0 10px;
margin-top: 20px;
height: 100%;
width: 27px;
position: relative;
}
.bars >p {
font-size: 12px;
margin: 0;
line-height: 30px;
font-weight: bold;
}
.head_graph > h2 {
font-size: 14px;
font-weight: bold;
margin-bottom: 6px;
color: #000;
}
.bar_main {
height: 200px;
position: relative;
}
.bar_main > .line {
height: 60px;
width: 100%;
border-top: 1px solid #ccc;
line-height: 20px;
font-size: 12px;
}
<div class="bar_graph">
<div class="head_graph"><h2>Heading</h2></div>
<div class="bar_main">
<div class="line">3000,000 Mil</div>
<div class="line">200,000</div>
<div class="line">100,000</div>
<div class="line"></div>
<div class="bars_area">
<div class="bars">
<span class="line_gr"></span>
<p>2016</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2015</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2014</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2013</p>
</div>
<div class="bars">
<span class="line_gr"></span>
<p>2012</p>
</div>
</div>
</div>
</div>
The parent element should have margins and padding set to 0. Then, the child element can be given that property of height:100%;

make responsive a css + html design

I created a layout with 4 columns centered with margin and a triangle as an arrow-down with a star in the middle...
this is working nice in my computer:
But triangle and star are far away to be responsive, only way I achieved to position it correctly is with absolute position:
.triangle-down {
display: block;
position: absolute;
top: 0;
left: 318px;
width: 0;
height: 0;
border-left: 532px solid transparent;
border-right: 532px solid transparent;
border-top: 400px solid blue;
}
.star {
display: block;
position: absolute;
vertical-align: middle;
font-size: 250px;
text-align: center;
color: white;
top: -434px;
left: -109px;
}
How can I put the element in top of the others and make it responsive in the same way columns and it's margins?
NOTES:
layout is a countdown, but javascript is not important for the question.
You can find a functional (no JS) fiddle here
You can see actual result (with JS) here
I can use sass if necessary
How about this updated fiddle?
https://jsfiddle.net/ondrakoupil/0rtvcnon/11/
Basically, these are the changes:
use flexbox for column layout
sizes are measured using viewport-relative units (vw)
triangle is created as standard rectangular <div> and rotated via CSS - you have better control over its position and size
There are some CSS3 techniques used. For IE, you'll need to prefix them in CSS (use Autoprefixer). Other browsers should handle it "as it is".
html, body {
height: 100%;
margin: auto;
padding-left:1%;
background: yellow;
font: normal 16px 'Roboto', sans-serif;
}
.container {
margin: auto;
width: 80%;
height: 100%;
position: relative;
display: flex;
justify-content: space-between;
}
.bar {
background: red;
font-weight: 700;
text-align: center;
color: yellow;
width: 15%;
}
.init {
position:absolute;
bottom: 10px;
right: 20px;
background: yellow;
margin-left: 0px;
font-weight: 700;
text-align: right;
color: red;
}
a:link, a:visited {
color: red;
text-decoration: none;
}
::-moz-selection {
color: yellow;
background: red;
}
::selection {
color: yellow;
background: red;
}
p.numbers {
font-size: 8vw;
margin-top: 45vw;
margin-bottom: 20px;
}
p.meta, p.strings {
font-size: 2vw;
}
h1 {
font-size: 4.5em;
}
.triangle-down {
position: absolute;
top: 0;
left: 0;
right: 0;
pointer-events: none;
}
.triangle-color {
margin: auto;
width: calc(80vw / 1.4142);
height: calc(80vw / 1.4142); /* sqrt of 2 */
background-color: blue;
transform: translateY(calc(-1 * 80vw / 1.4142 / 2)) rotate(45deg);
}
.star {
position: absolute;
vertical-align: middle;
font-size: 15vw;
text-align: center;
color: white;
top: 5vw;
left: 0;
right: 0;
z-index: 1;
}
<body>
<div class="container">
<div class="triangle-down">
<div class="triangle-color"></div>
<div class="star">★</div>
</div>
<div class="bar">
<p id="d" class="numbers days">00</p>
<p class="strings">DIES</p>
</div>
<div class="bar">
<p id="h" class="numbers hours">00</p>
<p class="strings">HORES</p>
</div>
<div class="bar">
<p id="m" class="numbers minutes">00</p>
<p class="strings">MINUTS</p>
</div>
<div class="bar">
<p id="s" class="numbers seconds">00</p>
<p class="strings">SEGONS</p>
</div>
</div>
<div class="init">
ENTRA
</div>
</body>
Take a look at this. I was need to rewrite it from scratch, because you've got a lot of absolutes and they all calculated through js, as I understood. Hope, this will satisfy your requirements.
html, body {
margin: 0;
height: 100%;
}
.container {
width: 100%;
height: 100%;
background-color: yellow;
font: normal 16px 'Roboto', sans-serif;
position: relative;
}
.triangle-aspect-keeper {
width: 50%;
padding-bottom: 50%;
position: relative;
margin-left: auto;
margin-right: auto;
}
.triangle-container {
}
.triangle-down {
width: 100%;
height: 100%;
background-color: blue;
transform: rotate(45deg);
position: absolute;
top: -50%;
left: 0;
}
.star {
font-size: 1100%;
text-align: center;
color: white;
width: 100%;
line-height: 200%; /* control star vertical position */
position: absolute;
top: 0;
left: 0;
}
.bar-container {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
text-align: center;
}
.bar-inner-container {
margin-left: auto;
margin-right: auto;
width: calc(50% * 1.41); /* sqrt(2) = 1.41. Length of the diagonal of the square*/
height: 100%;
display: flex;
justify-content: space-between;
}
.bar:first-child {
margin-left: 0;
}
.bar {
background-color: red;
height: 100%;
width: 15%;
color: yellow;
font-weight: 700;
}
p.numbers {
font-size: 5em;
margin-top: 350%;
}
p.meta, p.strings {
font-sie: 1.5em;
}
a:link, a:visited {
color: red;
text-decoration: none;
}
.init {
position: absolute;
bottom: 0;
right: 0;
padding: 10px;
font-weight: 700;
}
<body>
<div class="container">
<div class="bar-container">
<div class="bar-inner-container">
<div class="bar bar-first">
<p id="d" class="numbers days">00</p><br>
<p class="strings">DIES</p><br>
</div>
<div class="bar bar-second">
<p id="h" class="numbers hours">00</p><br>
<p class="strings">HORES</p><br>
</div>
<div class="bar bar-third">
<p id="m" class="numbers minutes">00</p><br>
<p class="strings">MINUTS</p><br>
</div>
<div class="bar bar-fourth">
<p id="s" class="numbers seconds">00</p><br>
<p class="strings">SEGONS</p><br>
</div>
</div>
</div>
<div class="triangle-aspect-keeper">
<div class="triangle-container">
<div class="triangle-down"></div>
<div class="star">★</div>
</div>
</div>
<div class="init">
ENTRA
</div>
</div>
</body>
#import url('https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css');
html,
body {
background: yellow;
height: 100%;
}
.container {
position: relative;
height: 100%;
}
.row {
height: 100%;
}
.col-xs-3 {
height: 100%;
}
.col-xs-3,
.col-xs-12 {
padding: 0 6.25%;
}
.bar {
color: yellow;
background: red;
min-height: 100%;
padding-top: 333%;
}
.triangle-down {
position: absolute;
width: 100%;
height: auto;
z-index: 1;
}
.triangle-color {
margin-bottom: -30%;
}
.triangle-color * {
fill: blue
}
.star * {
fill: white
}
.numbers {
font-size: 5vw;
padding-bottom: 2vw;
}
.strings {
font-size: 1.5vw;
}
<div class="container text-center">
<div class="row triangle-down">
<div class="col-xs-12">
<svg class="triangle-color" viewBox="0 0 100 40">
<polygon points="0,0 100,0 50,40"/>
</svg>
<svg class="star" viewBox="0 0 1400 188">
<polygon points="700,0 640,188 790,68 610,68 760,188"/>
</svg>
</div>
</div>
<div class="row">
<div class="col-xs-3">
<div class="bar">
<div class="numbers">00</div>
<div class="strings">DIES</div>
</div>
</div>
<div class="col-xs-3">
<div class="bar">
<div class="numbers">00</div>
<div class="strings">HORES</div>
</div>
</div>
<div class="col-xs-3">
<div class="bar">
<div class="numbers">00</div>
<div class="strings">MINUTS</div>
</div>
</div>
<div class="col-xs-3">
<div class="bar">
<div class="numbers">00</div>
<div class="strings">SEGONS</div>
</div>
</div>
</div>
</div>
You can simply do this.
HTML:
<div class="parent">
<div class="triangle">
<div class="star">
...
</div>
</div>
</div>
CSS:
.parent {
width: xx%; /*Whatever percentage*/
height: yy%; /*Whatever percentage*/
margin: 0 auto;
position: relative;
}
.triangle {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.star {
font-size: xx%; /*some percentage (assuming star is a font icon)*/
position: absolute;
top: 15vh;
margin: 0 auto;
}