Place items along the perimeter of a div on each of its four sides

My goal is to add an element on all four sides of the div without using box-sizing. The challenge is that I need to apply event listeners to these elements once they are placed.

Currently, I can add elements to the left and right sides, but I'm struggling to figure out how to position them on all four sides in a more elegant way.

.box {
      width: 100px;
      height: 100px;
      background: yellow;
      text-align: center;
      font-size: 0.8rem;
      position: relative;
    }

    #sideEl {
      background-color: red;
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      width: 4px;
      height: 100%;
      cursor: w-resize;
    }

    #sideEl2 {
      background-color: red;
      position: absolute;
      right: 0;
      bottom: 0;
      width: 4px;
      height: 100%;
      cursor: w-resize;
    }
<div class="box">
      <div id="sideEl">
      <div id="sideEl2">
    </div>

https://i.sstatic.net/Um0KM.png

I came across a post about Placing 4 html elements in every corner of a div, but I haven't found a solution yet for placing them along all sides of the borders.

Answer №1

I made a simple adjustment to ensure that horizontally aligned elements display properly by swapping the height and width parameters. Remember, when aligning an element to one of the four sides, you should only use one of the options - top, right, bottom, or left. Here's the revised code snippet:

.box {
  width: 100px;
  height: 100px;
  background: yellow;
  text-align: center;
  font-size: 0.8rem;
  position: relative;
}

#sideEl {
  background-color: red;
  position: absolute;
  top: 0;
  height: 4px;
  width: 100%;
  cursor: w-resize;
}

#sideE2 {
  background-color: brown;
  position: absolute;
  left: 0;
  width: 4px;
  height: 100%;
  cursor: w-resize;
}
#sideE3 {
  background-color: blue;
  position: absolute;
  right: 0;
  width: 4px;
  height: 100%;
  cursor: w-resize;
}
#sideE4 {
  background-color: green;
  position: absolute;
  bottom: 0;
  height: 4px;
  width: 100%;
  cursor: w-resize;
}
<div class="box">
  <div id="sideEl"></div>
  <div id="sideE2"></div>
  <div id="sideE3"></div>
  <div id="sideE4"></div>
</div>

Answer №2

To create a visually appealing layout, you can add classes to identify different sides of your div elements. Here's how you can achieve this:

  • Create a class called side to style the sides of your div elements collectively. This class will contain properties like background-color and position.
  • Introduce two additional classes:
    • side-h for horizontal sides (top and bottom), defining specific values for width, height, and applying cursor: n-resize.
    • side-v for vertical sides (right and left), with unique values for width, height, and setting cursor: w-resize.
  • Assign identical values for #side-top and #side-left regarding the top and left rules.
  • Set common values for #side-bottom and #side-right in terms of bottom and left rules.

.box {
  width: 100px;
  height: 100px;
  background: yellow;
  text-align: center;
  font-size: 0.8rem;
  position: relative;
}

.box .side {
  position: absolute;
  background-color: red;
}

.box .side.side-h {
  width: 100%;
  height: 4px;
  cursor: n-resize;
}

.box .side.side-v {
  width: 4px;
  height: 100%;
  cursor: w-resize;
}

#side-top, #side-left {
  top: 0;
  left: 0;
}

#side-right, #side-bottom {
  bottom: 0;
  right: 0;
}
<div class="box">
  <div class="side side-h" id="side-top"></div>
  <div class="side side-v" id="side-right"></div>
  <div class="side side-h" id="side-bottom"></div>
  <div class="side side-v" id="side-left"></div>
</div>

You have the flexibility to rearrange elements inside .box without impacting the overall design.

Answer №3

.box {
  width: 100px;
  height: 100px;
  background: yellow;
  text-align: center;
  font-size: 0.8rem;
  position: relative;
}

#sideTop {
  background-color: green;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 4px;
  cursor: w-resize;
}

#sideRight {
  background-color: green;
  position: absolute;
  top: 0;
  right: 0;
  width: 4px;
  height: 100%;
  cursor: w-resize;
}

#sideBottom {
  background-color: green;
  position: absolute;
  right: 0;
  bottom: 0;
  width: 100%;
  height: 4px;
  cursor: w-resize;
}

#sideLeft {
  background-color: green;
  position: absolute;
  left: 0;
  top: 0;
  width: 4px;
  height: 100%;
  cursor: w-resize;
}
<div class="box">
  <div id="sideTop"></div>
  <div id="sideBottom"></div>
  <div id="sideRight"></div>
  <div id="sideLeft"></div>
</div>

Answer №4

I trust you have grasped the concept...

.main-container {
  width: 400px;
  height: 400px;
  background-color: #ccc;
}
.child {
  float: left;
}
.child:nth-child(odd) {
  height: calc(100% - 50px);
  width: 50px;
  background-color: blue;
}
.child:nth-child(even) {
  height: 50px;
  width: calc(100% - 100px);
  background-color: red;
}
.child:nth-child(4) {
  width: 100%;
}
<div class="main-container">
  <div class="child"></div>  
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></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

Lag experienced in Chrome browser on Mac due to Bootstrap CSS framework

I created a basic HTML and CSS file using Bootstrap that runs smoothly on my work computer with Chrome version 27.0.1453.110. However, when I attempt to open the file in Chrome on my personal Mac (unknown Chrome version), it loads very slowly and experienc ...

Issues with Display on IE 11 and Edge

Currently, I am in the process of working on a website that was initially created from a template by someone else. Since I am not a professional web designer, some elements on the site are not my own creation. Everything seems to be running smoothly on Fir ...

Creating a cascading layout with React Material-UI GridList

Can the React Material-UI library's GridList component be used in a layout similar to Pinterest's cascading style? I have utilized Material-UI Card components as children for the GridList: const cards = props.items.map((item) => <Card ...

Utilizing AngularJS: Running a controller function within a directive

I'm brand new to AngularJS and I'm trying something new: I want to bind a scroll event to an element using a custom directive. Here's the code snippet: First, let's look at my controller: var officeUIApplication = angular.module(&apo ...

Hide the text if there is not enough space to show it fully

Currently working on a website where quotes are featured within a specific section. I am seeking a solution for ensuring that if the text cannot be fully displayed within the div (leading to words being cut off), the entire div becomes invisible. I would ...

Is it possible to echo a CSS class using PHP?

I have a PHP structure and I would like to embed my div in PHP. I need to write CSS with echo class, but I am unsure of how to do it. Here is my div: <div class="mydiv"> <img src="catalog/view/theme/default/img/mypng.png" alt=""> </div ...

Establish the default bootstrap theme mode specifically for print formatting

Customizing the theme in Bootstrap is made easy by setting the data-bs-theme attribute on the body or parent element. While this feature is useful, we discovered that printing in light mode produces the best results. Instead of creating an entire set of st ...

Transforming Jade into HTML

While working on an application with my team using nodejs and jade, we made the decision to switch to Django. However, I would prefer to keep the web pages written in jade without having to manually rewrite them. Is there a tool available that can convert ...

Get rid of the Parallax background feature in the Brooklyn theme

I am attempting to utilize an Index.php file as a custom page that incorporates some of the themes styles and elements such as Navigation, Footer, Contact Form, etc. However, I have encountered difficulties with the default header calling the body, making ...

I am having difficulty with my ajax code and I am unsure of how to resolve it

Here is the code snippet. I am encountering an issue where pressing the button does not trigger any action, while I simply want to ensure that the AJAX functionality is working properly. The expected behavior is for an alert message to be displayed when th ...

Issue with page break functionality during print preview

div.pagebreak { page-break-after: always; page-break-inside: avoid; } HTML <!-- Page separator --> <div class="pagebreak" style="float: none;"><hr class="hidden-print" /></div> <app-mud-chec ...

Organizing HTML input elements in a single row

Despite trying various solutions found in similar questions, I am still facing the same issue. I have two HTML controls - an anchor tag and an input button. I have attempted using vertical-align: top;, float: right;, display: inline-block; both independe ...

JavaScript Calendar and Clock Selector

I've been scouring every corner of the internet, but I can't seem to locate what I'm after. My quest is for a basic Date and time picker along with a standalone time picker that harnesses the power of JavaScript. My webpage is built using ...

Instructions for updating the Modal Value using ajax

This is the script I use to retrieve the data <script> $(document).ready(function() { $('.tmpsurat').click(function() { var id=$(this).data('id'); var url ='{{URL('/cekSuratKelengkapan')}}/'+id; ...

I am having trouble retrieving multiple li elements within a ul tag, as I am only able to access one li element and not

I'm in the process of creating a data extract for my organization, and I am facing an issue with retrieving data from an HTML list. Despite my attempts, when I run my code, I only manage to get the text content of a single li element. Below, you can f ...

Transferring information from an HTML file to Python

How can I retrieve information from a selected cell in an HTML file and then process it in views.py? Any suggestions on how to achieve this would be greatly appreciated. Thank you! <tbody> {% for element in qz %} <tr> ...

How can I maintain circle radius while resizing SVG to 100% width?

Our line graphs are drawn using SVG technology: https://i.stack.imgur.com/PLpPT.png When generating SVG, the width is set to 100%, but the exact value remains unknown. To ensure that the SVG fills the entire width without distortion, we use preserveAspec ...

Changing the background color of a clicked checkbox can be done by using CSS

Currently, I am working on creating an exam page that features choices with checkboxes. My goal is to have the background of a selected choice change color. However, I am encountering some obstacles. To view the codes and screen, please click here Here i ...

Can the width of an offcanvas panel be adjusted?

Currently implementing the offcanvas component in my project, but I'm looking to make the panel wider than the standard size. Specifically, I want it to be 35% width. I've attempted to utilize the panelClass NgbOffcanvasOptions property, however, ...

Is it necessary to utilize container or container-fluid within the grid system layout?

While similar questions may already exist on Stackoverflow, I believe the exact answer has yet to be found. Many suggest: "You should nest .col within .row, and ensure that .row is inside of .container." However, this concept still eludes me. I underst ...