Position a div in the center and add color to one side of the space

I am seeking a way to create a centered div with the left side filled with color, as shown in examples.

I have devised two solutions without using flexbox, but both seem somewhat like hacks.

body {
    margin: 0;
    padding: 0;
}

.header {
    width: 100%;
    height: 60px;
    position: fixed;
}

.center-part {
    width: 500px;
    margin: 0 auto;
    height: inherit;
    background-color: rgba(0,255,0,0.8);
    position: relative;
}

.blue-big {
    background-color: blue;
    width: 9999px;
    height: inherit;
    position: absolute;
    right: 500px;
}

.equal-side {
    display: table-cell;
}
<div class="header" style="top: 0px">
    <div class="center-part">
        <div class="blue-big">
        
        </div>
    </div>
</div>

<div class="header" style="top: 70px; display: table;">
    <div class="equal-side" style="background-color: blue">
    </div>
    <div class="center-part" style="display: table-cell;">
    </div>
    <div class="equal-side">
    </div>
</div>

The first solution involves a large div and positioning, while the second one relies on "display: table".

I am uncertain if either of these approaches is considered best practice or if there is a better way to achieve this layout?

The green DIV will not completely fill the height, ruling out the option of placing a div in the background with 50% width.

Answer №1

Using a linear-gradient is a simple solution, as shown in the code snippet below:

.container {
  background: linear-gradient(to right, green 50%, transparent 0) 0 0/100% 40% no-repeat;
  height: 100px;
  background-color:rgba(255,0,0,0.5);
}

.container>div {
  width: 300px;
  height: 40%;
  background:blue;
  margin: 0 auto;
}
<div class="container">
  <div></div>
</div>

Alternatively, you can use a pseudo element with overflow to create a similar effect:

.container {
  overflow:hidden;
  height: 100px;
}

.container>div {
  width: 300px;
  height: 100%;
  background:blue;
  margin: 0 auto;
  position:relative;
}
.container>div:before {
  content:"";
  position:absolute;
  top:0;
  right:0;
  left:-1000%;
  bottom:0;
  background:green;
  z-index:-1;
}
<div class="container">
  <div></div>
</div>

Answer №2

This specific solution has been quite effective for me, and you can check it out here.

Below is the HTML code snippet:

    <div class="test-header">
  <div class="equal-side left-side">
  </div>
  <div class="center-part">
     <div class="center">
       INSERT TEXT HERE
    </div>
  </div>
  <div class="equal-side right-side">
  </div>
</div>

CSS styling used in this particular scenario:

    .test-header {
  position: relative;
  width: 100%;
  height: 60px;
  text-align: center;
}

.equal-side {  
  display: inline-block;
  height: 100%;
  width: 49%;
}

.left-side {
  background: blue;
}

.right-side {
  background: red;
}

.center-part {
  background: white;
  width: 500px;
  height: 60px;
  display: block;
  position: absolute;
  margin-left: -250px;   /*half of center element's width*/
  left: 50%;
  top: 0;
}

.center {
  width: 100%;
  border: 1px dashed;
}

Answer №3

If you're looking to create a linear gradient effect with only one element, check out the informative resources at CSS-tricks. They provide detailed explanations on how to achieve this design technique.

In my initial response, I overlooked mentioning the container needed to center the div. I have now updated with two examples - one utilizing flexbox and the other without. Whether you prefer not to use flexbox or are unable to, both options are provided for your convenience.

.container {
  display: flex;
  justify-content: center;
}

.bar {
  height: 50px;
  width: 400px;
  background-image:
    linear-gradient(
      to right, 
      red,
      red 50%,
      orange 50%,
      orange 100%
    );
}

.bar-noflexbox {
  height: 50px;
  width: auto;
  margin: 1rem 20%;
  background-image:
    linear-gradient(
      to right, 
      red,
      red 50%,
      orange 50%,
      orange 100%
    );
}
<div class="container">
  <div class="bar"></div>
</div>

<div>
  <div class="bar-noflexbox"></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

Dynamically insert <td> elements into <tr> element using both jQuery and JavaScript

I am facing an issue with adding a new table data (td) element dynamically to the first table row (tr) in my JavaScript code. Here is the original table before adding the new td element: <table> <tbody> <tr> <t ...

What could be the reason for the state not initializing with the provided value?

It seems that the latest value is being passed through props, but when it is set as the initial value for state, it doesn't appear. https://i.stack.imgur.com/7dVf2.png import React, { useRef, useState } from "react"; //importing config fr ...

Make sure to keep the <div> element nested within the image

I'm working on creating a photo album that displays images in full screen and allows users to navigate using arrows. Check out my progress here: https://jsfiddle.net/q49wzey6/ Here's the HTML code I'm using: <div class="main"> &l ...

Unsuccessful CORS on whateverorigin.org with YQL

Despite trying all three methods, I keep getting an error regarding cross domain access denied. Previously, I had successfully used YQL in different parts of my applications, but now it seems to have stopped working as well. JSFIDDLE <script> $(fun ...

Ensure that the text in the table with a width of 100% remains stable

Currently, I am encountering an issue with the inside text moving within my application on a webpage. In my previous tables, I utilized the min-width option to prevent the text or td from shifting when resizing the website page. For example, when stretchin ...

Preventing Height Stretch in CSS Flex-Wrap: A Guide

Is there a way to eliminate the large gaps between box3, box1 and box4, box6? How can we ensure each box adjusts its height dynamically? .wrap { display: flex; align-items: baseline; align-content:flex-start; justify-content: space-between; fl ...

Showcasing Items Within JSON Object in Angular

I am dealing with a JSON database that contains nested Objects: { "characters2": [ { "campaign":"Menagerie Coast", "index": 1, "name": "Mark Whithershmitz", "portrait": "assets/pics/markW.jpg", "affiliation": "Kryn Dynast ...

Using JQuery to Enhance the Highlight Effect: Tips and Tricks

Exploring the functionality of the "highlight" JQuery effect: http://docs.jquery.com/UI/Effects/Highlight You have the ability to modify the background color of any DIV element with a fade in/out effect. However, the provided example demonstrates trigge ...

Using routerLink for linking to multiple components

Below is the anchor tag provided: <a class="uppercase" routerLink="settings/pressure" routerLinkActive="text-success" (click)="closeModal()" > <div class="pad-btm"> PRESSURE </div> </a> I need to include another route ...

What is the best way to trigger a new css animation on an element that is already in the midst of

Let's talk about an element that already has an animation set to trigger at a specific time: .element { width: 100%; height: 87.5%; background: #DDD; position: absolute; top: 12.5%; left: 0%; -webkit-animation: load 0.5s ease-out 5s bac ...

I have implemented a custom-built sidebar component and I'm unsure about the process of adding data to it

After successfully incorporating this SideBar into my Vuex/Laravel project, I encountered a problem. The example code provided used: <div :class="$style.sidebar"/> However, when I tried to modify it to: <div :class="$style.sidebar">Content&l ...

Guide on integrating jQuery for AJAX responses to gracefully transition into view within an MVC3 View

Is there a way to create a smooth fade in effect for a <p> element on my website? Currently, I am using Ajax to retrieve the server time and display it. However, the elements appear abruptly and I would like to make the transition more gradual by ad ...

Revising text for real-time editing

Most of us are familiar with the functionality on StackOverFlow that allows you to preview your text before posting a question. I'm interested in implementing a similar feature on my website and am looking for some guidance on how to get started. I ...

Step-by-step guide on crafting a scrolling marquee with CSS or Javascript

I am in need of creating two marquees for a website project. The first marquee should showcase a repeating image while the second one should display links, both spanning the browser window at any size. It is crucial that the marquee items are displayed fro ...

Bring to life the div that has been clicked

There are three divs labeled as "left", "active", and "right" in my setup: Whenever the left div is clicked, it animates to become active and the previously active one moves into its place. I want this same functionality to apply to the right div as well. ...

Unusual behavior exhibited by ng-if within a widget

Hey there, seeking some assistance here. I'm currently making edits to a widget and within the client HTML code, I've come across two ng-if statements (the first one was already there, I added the second). <li> <a ng-if="data.closed ...

Adding and removing attributes with Jquery upon clicking a button

How can I make my listed items add an ID when clicked, or what am I doing incorrectly? $('.ex-menuLi #tt').attr('id', 'test'); $('.ex-menuLi').on('click', function(){ $(this).attr('id', &apos ...

Using Javascript to Conceal Button for Unauthenticated Users

Our website is currently running on an outdated e-commerce CMS platform, which limits my options due to my beginner level skills in JavaScript and jQuery. One specific issue we are facing is the need to hide Prices and Add to Cart buttons for users who ar ...

Attempting to recreate the dynamic banner featured in the video

Looking to replicate a setup similar to what was demonstrated in the video. I have two div blocks - one with a random image and the other with a video, and I want them to be as flexible and identical as possible to the video layout. How should I go about a ...

Ways to Retrieve the Index of Elements in the DOM Array Generated by the querySelectorAll() Function in JavaScript

const items = document.querySelectoAll('.class') console.log(items) // Array with 15 elements (This array contains a total of 15 elements) ...