Ways to position an element at the edge of the browser either on the left or right side when the image is not in a centered container

Looking to create a unique layout that involves:

  • Utilizing a three-column structure
  • Incorporating a div element that spans two columns
  • Positioning an image within the two-column div so that it extends to the left edge of the browser window while staying "stuck" to the right side

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

Attempted implementing the following code on the image, but with limited understanding of its effect:

margin-left: calc( -100vw/2 + 100%/2);

HTML

<div class="wrapper">
<div class="col-1"><img src="blabla" /></div>
<div class="col-2">Content</div>
</div>

CSS

.wrapper {
max-width: 1400px;
display: block;
margin: auto;
}

.col-1 {
float: left;
width:66.666%;
}

.col-2 {
float: left;
width: 33.333%;
}

.col-1 img {
display: block;
width: 100%;
height: auto;
margin-left: calc( -100vw/2 + 100%/2);
}

Displayed result achieves the desired left alignment, but struggles with maintaining the image's position on the right.

Uncertain about how to modify the layout to achieve desired results, or if it is even technically feasible.

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

Answer №1

Understanding the calculation:

Two key parameters need to be calculated:
1. Determine the shift of the <img> -- (1/2 of the viewport width - 1/2 of the .wrapper width),
2. Calculate the width of the <img> -- (100% of .col-1 + the previously determined shift)

Consider the following:
1. The width of the viewport is denoted by 100vw
2. The width of .col-1 is represented as 100% of .col-1
3. The width of .wrapper is defined as 150% of .col-1

The <img> element is a child of .col-1, thus all percentage dimensions are relative to the width of .col-1.

Calculation steps:
1. Determine the shift value: (100vw/2 - 150%/2) = (50vw - 75%)
2. Calculate the width: (100% + 50vw - 75%) = (25% + 50vw)

Note the following:
The margin-left needs to be negative in order to shift left, hence
margin-left = (75% - 50vw)

For a responsive design, run the provided code snippet in fullscreen mode and observe its behavior when resizing the window.

html,
body {
  margin: 0;
  padding: 0
}

* {
  box-sizing: border-box
}

.wrapper {
  width: 80vw; /* Choose any value */
  max-width: 1400px;
  display: flex;
  margin: auto;
}

.wrapper div {
  border: solid 1px
}

.col-1 {
  display: flex;
  flex-flow: row wrap;
  align-items: flex-start;
  width: 66.666%;
  background: #693
}

.col-1-2 {
  width: 50%
}

.col-2 {
  width: 33.333%;
  background: #e43;
}

.col-1 img {
  display: block;
  width: calc(25% + 50vw);
  margin-left: calc(75% - 50vw);
  opacity: .75; /* Adjust as needed */
}
<div class="wrapper">
  <div class="col-1">
    <img src="https://picsum.photos/600/300" />
    <div class="col-1-2">Content</div>
    <div class="col-1-2">Content</div>
  </div>
  <div class="col-2">Content</div>
</div>

Answer №2

CSS-Grid seems like a good option for achieving what you want, but I have concerns about how the image will respond.

Just my two cents.

.super-con {
  display: grid;
  grid-template-columns: auto repeat(3, minmax(auto, 500px)) auto;
}

.super-con div {
  border: 1px solid red;
}

.image {
  grid-column: 1 / span 3
}

.image img {
  width: 100%;
  max-height: auto;
  display: block;
}

div:nth-child(3) {
  grid-column-start: 2;
}
<div class="super-con">
  <div class="image"><img src="http://www.fillmurray.com/150/150" alt=""></div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</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

Struggling to display blob URL on "pdf-viewer" component with "ng2-pdf-viewer" in Angular version 10

I am facing an issue with an API that returns uploaded files as blobs. When trying to bind the src with a blob URL, nothing is shown. However, if I bind it with a direct URL, the PDF file is displayed successfully. Below is my code snippet. My TypeScript ...

What is the process for specifying the content type as application/json in an Ajax request?

Do you have a smart contract named GovtContract that functions properly when utilizing curl commands? $curl -X POST -H "Content-Type: application/json" localhost:3000/govtcontract/set -d '{"value":"5"}' | jq $curl -X GET -H ...

`javascript pop up notification will only display one time`

I am currently developing a chrome extension with two different modes. When the user clicks on the icon, a pop-up message should appear to indicate which mode is active. However, I am facing an issue where the message does not always display when the pop- ...

Contact Form Appears to Be Functioning Properly, Yet Emails Are Not Being Received (HTML5 & PHP)

Hello everyone, I need some assistance with creating a contact form. This is my first time working on this, and I am facing an issue. I am using HTML5, which I have some knowledge of, and PHP, which I am not very familiar with. When I try to test the form ...

How can I stack two appbar components from Material Ui on top of each other without the vertical line separating them?

I am facing a challenge while trying to incorporate two AppBar components in Material-UI, stacked one on top of the other. The problem arises when a line appears at the end of the first AppBar, overlapping with the second one. Click here to view an image ...

Why does my console refuse to log the input entered for the search?

Looking to become proficient in web development, I am attempting to record HTML search queries in the console after storing them in a variable. However, when I try running the search procedure, nothing seems to be displaying in my browser's inspect co ...

A guide on automating the html5 color picker using input type="color" in selenium webdriver

When interacting with an HTML color input element, a color picker window pops up that prevents viewing the DOM. I am looking for a solution to either select a color and close the window or enter a hex code in the popup's text box. Can anyone provide m ...

Is there a way to use jQuery to verify if an LI element contains a parent LI element?

Struggling with customizing the comments section of a WordPress theme? Utilizing jQuery for styling can be tricky, especially when dealing with nested UL elements like admin comments. The challenge lies in traversing the DOM structure to target specific el ...

Is there a CSS3 Selector With Similar Functionality to jQuery's .click()?

For a few years now, I have been utilizing a pure CSS navigation system. However, with the recent increase in mobile site projects at my workplace, I am encountering issues with drop-down menus not functioning properly on mobile devices. Despite this chall ...

php code to show data in a single column of a table

I am working with a database record that includes an id and name, and I need to display it in a table where records are distributed in the pattern 1-2-1-2-4 across columns. Below is my current code that is generating the incorrect output: <table borde ...

The element does not have the 'ariaHidden' attribute present

I attempted to use the area-hidden property in a Vue file, but encountered an error. I also experienced a similar issue with the data-dismiss property. Do I need to perform additional setup to utilize the aria-hidden property? <button type="button& ...

Access and retrieve data from a string using XPath with JavaScript

My HTML page content is stored as a string shown below: <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <h1>This is a Heading</h1> <p>This is a paragraph.</p> </ ...

Steps to handle the change event of a p:inputText element

In my current setup, I am utilizing p:inputText and have the need to trigger a javascript function that will update the searchField in the backend bean. <p:inputText required="true" placeholder="#{cc.attrs.searchTip}" value="#{cc.attrs.queryProperty}" ...

Having trouble accessing the value of an item in a dropdown box on a website

I am currently developing a webpage using HTML5 and Knockout Js. I am facing an issue where I am unable to retrieve the ID of the selected item from a dropdown box in order to insert a value into an SQL table. As an example, consider the following SQL tab ...

What are the best situations to utilize bootstrap and when should you avoid it?

When it comes to web development, there is often a debate on whether to use Bootstrap or custom CSS for designing a website. For my school project, I am tasked with creating a homepage and contact page using Bootstrap primarily. However, we are also requir ...

How to create ::after pseudo-element with identical width as its preceding sibling

My HTML code includes a <div> container with an <img> and an ::after pseudo-element. Check it out: <div class="container" data-caption="The caption should match the width of the image and wrap accordingly"> <img> </div> . ...

How to adjust the size of the text on a button in jQuery mobile using custom

I am facing an issue with my jQuery mobile buttons that are placed inside a fieldset. Specifically, I need to adjust the font-size of one of the buttons. Despite attempting to add an inline style, it did not have the desired effect. Furthermore, I tried u ...

How do I add a "Switch to Desktop Site" link on a mobile site that redirects to the desktop version without redirecting back to the mobile version once it loads?

After creating a custom mobile skin for a website, I faced an issue with looping back to the mobile version when trying to add a "view desktop version" link. The code snippet below detects the screen size and redirects accordingly: <script type="text/j ...

Responsive menu not functioning properly with jQuery

I am currently working on a website located at . My goal is to incorporate the navigation plugin called MeanMenu into the site. I have followed all the instructions provided in the tutorial: adding the jQuery script to my HTML, including the CSS in the hea ...

Tips for adjusting the width of a field within an existing OpenERP7 form view utilizing percentages instead of pixels

In the process of modifying a form view on OpenERP7 through inheritance, I am attempting to adjust the width of a field to 50% (currently at 40%). While I have successfully altered the style and width of the field using pixels, any attempt to input a perce ...