Ways to add a background color to the top portion of a div element?

.bg {
  background-color: gray;
  height: 200px;
}
<div class="bg">
  <div>
    <img src="https://s3.amazonaws.com/images.seroundtable.com/google-rainbow-texture-1491566442.jpg" style="width:200px">
  </div>
  <div>
    <img src="https://www.w3schools.com/w3css/img_lights.jpg" style="width:200px">
  </div>
</div>

Is there a way to add a background to only half of two separate div elements? For example, the bottom half of the first div and the upper half of the second one, without changing the height of the entire div?

https://i.sstatic.net/gJfkC.jpg

Similar to the brown background shown in the image. How can a shared background be applied in the middle of two distinct div sections?

Answer №1

To create an appealing background, consider using the linear-gradient property in CSS.

.bg {
  background: linear-gradient(to bottom, #ddd 50%, #9A8D6D 50%); /* This gradient covers half with #ddd and the other half with #9a8d60 */
  height: 200px;
}
<div class="bg">
  <div>
    <img src="https://s3.amazonaws.com/images.seroundtable.com/google-rainbow-texture-1491566442.jpg" style="width:200px">
  </div>
  <div>
    <img src="https://www.w3schools.com/w3css/img_lights.jpg" style="width:200px">
  </div>
</div>

If you desire margin between different sections of the background, utilize color stops within the gradient:

.bg {
  background: linear-gradient(to bottom, #ddd 25%, transparent 25%, transparent 50%, #9A8D6D 50%); /* Gradient covering various percentages to create margin */
  height: 200px;
}
<div class="bg">
  <div>
    <img src="https://s3.amazonaws.com/images.seroundtable.com/google-rainbow-texture-1491566442.jpg" style="width:200px">
  </div>
  <div>
    <img src="https://www.w3schools.com/w3css/img_lights.jpg" style="width:200px">
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporating an image into a list of checkboxes post-connection

Looking for a way to populate a checkbox list through datasource/databind and then integrate an image with each checkbox? I can do both separately but struggling to combine them successfully. My task is to create a checkbox list of students for teachers t ...

Having trouble with jQuery's show/hide methods not functioning properly

Check out my codepen for everyone to experiment with.... CodePen.io I'm having trouble displaying the #play-again button after a game is over using $('#play-again').show(). Any suggestions on this issue??? Update Included raw html link ...

What is the best way to adjust the layout for smaller screens using material ui?

Just starting out with responsive material design. On smaller screens, the Sports Status needs to remain in the right corner. 4Standard, 5Standard, 6Standard, 7Standard, 8Standard, 9Standard should wrap to the next line. I came across this code snippet f ...

What is the best way to split a jQuery tab into four equally sized divs?

I am currently working with jquery tabs and within one of the tabs, I have 4 google charts each enclosed in a separate div. My goal is to position each div in a different quadrant of the tab area (north, south, east, and west of the screen). Currently, my ...

When the <body onload="function()"> event is triggered, my animated image freezes in

I am working on a preload page that includes an animation. <body onload="showSpinner()"> <h3>Please Wait...</h3> <script> function showSpinner() { showProgress(); window.loc ...

Ways to Export HTML to Document without any borders or colorful text

How can I make a contentEditable area visible when filling text and then disappear when exporting the document? I found a script online that allows you to do this, but the issue is that the contentEditable area is not visible until clicked on. To address t ...

Struggling to line up text and image next to each other?

I have inserted an image and some text. The text consists of 1 <h6> tag and 2 <p> tags. I attempted to use the float:left property, but the content did not display side by side as intended. Here is the code from content.js: <div className ...

Is it possible to define the sequence of wrapped items in CSS flexbox?

I am using a <div> with CSS flex to maintain responsiveness. I want to control the order in which elements are wrapped. For instance, I need 1 - 2 - 3 To be rearranged as 1 3 2 Once fully wrapped. Here is my current code: https://jsfiddle.net/ ...

Adjusting AngularJS scroll position based on key presses

I am currently working on an autocomplete feature using AngularJS. I seem to be experiencing a problem with the scrollbar behavior. In the interactive gif provided, you can observe that the scrollbar remains stationary when navigating with the arrow keys. ...

Creating high-quality tabs for a gaming project

I tried following a tutorial on YouTube to create some professional tabs, but unfortunately my code didn't turn out the way I wanted. I was hoping to be able to scroll through different tabs and have a separate HTML file for each one. Although I&apos ...

a height of 100% with a minimum height of 100%

Is there a way to make the third and fourth rectangles have the same position while filling all the remaining height in %? Keep in mind that we can't change the min-height:100% on the body or html. Feel free to use CSS and HTML for your code solution. ...

What is the process for automatically activating the Enter key once moving to the next text input field?

Hey everyone, I am looking for a way to automatically trigger the enter button after the user switches to another HTML input type='text'. This is necessary for form validation. Below is the code snippet: <input type="text" class="form-contro ...

How can I duplicate or extract all the formatting applied to a specific text selection in Ckeditor?

I am currently utilizing CKEditor version 3.6 within my Asp.net MVC 3 Application. One of my tasks involves creating a Paint format option similar to Google Docs. I am looking to integrate this feature into CKEditor. Is there a way in CKEditor to transfe ...

Is there a way to enable scrolling on a page without editing the HTML? I have a table inside a div that is overflowing, and I want the page to scroll,

I'm facing an issue with a DIV that is 600px wide and contains a table. Due to long email addresses, the wrapping isn't working properly even after using word-wrap: break-word. I've experimented with changing the width, adding !important, a ...

Click on the link to access and activate the function

Currently, I am retrieving values from a text input field. Using jQuery: $('.submitLocation').click(function(){ // grabbing value from the input field var city = $(".city").val(); }); For HTML: <input type="text" name="city" class ...

Customizing Your WordPress Menu with HTML5 Features

Can someone assist me in creating a customized WordPress menu? I am facing issues with unnecessary classes added by WordPress and the lack of certain attributes on dropdowns. Below is the HTML code: <nav class="navbar navbar-default navbar-static- ...

Trouble arises when attempting to append a class through ng-class upon clicking

In a specific scenario, I am trying to change the border color from black to red in a div by appending a class using ng-class when clicked. However, when clicking a button, the modal opens but the class is not being appended as expected. <div ng-class ...

Trying to retrieve a CSS property using jQuery

Having trouble retrieving the correct CSS value for a background using a spectrum.js color picker. No matter which color is chosen, rgba(0,0,0,0) is always selected. Strangely enough, when checking the background in the console, it shows up correctly in th ...

Trouble with z-index when attempting to stack forms on top of one another

Currently, I am working on creating a login page and signup page for my program. The goal is to stack them on top of each other with only one being displayed at a time. However, I am facing an issue where they are not stacking properly. I attempted using t ...

Enclosing floated elements within a parent div

Looking for a way to make a div wrap around a changing number of child elements without specifying a width for the parent? I've managed to get the content wrapping by floating both the child and parent elements, but I'm facing an issue when the ...