Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill only half of the container with a background color

Tags:

html

css

I have something like this:

enter image description here

#container {
  width: 300px;
  background-color: red;
  display: flex;
  justify-content: center;
}

#child {
  background-color: green;
  width: 250px;
  height: 150px;
  position: relative;
  top: 75px;
}
<div id="container">
  <div id="child"></div>
</div>

The red box is the container and it's height is equal to its content - in this case 150px. What I'd like to achieve is to make the container height equal half the size of its content size.

The effect that I want to achieve is to have the background-color fill half of the containers content.

If this can be made in some other way than changing the container height - feel free to propose a solution.

like image 891
sarneeh Avatar asked Dec 23 '22 07:12

sarneeh


1 Answers

Use a linear gradient:

#child {
  background: linear-gradient(to bottom, green 50%, white 0%); 
}

change white with body color or any color you want.

Source: CSS-Tricks

like image 54
Shahriar Avatar answered Feb 05 '23 06:02

Shahriar