Two child DIVs within a single parent DIV

I am struggling to keep two divs side-by-side within a parent div. Initially, I was able to position the image in the right div successfully, but when resizing the elements, everything became disorganized and I can't seem to fix it. As a beginner, I would appreciate if you could provide a simple solution.

div.surf2
{
position:absolute;
background: #41c3ac;
height: 600px;
}

div.surfleft
{
display: inline-block;
width: 45%;
padding-top: 80px;
padding-left: 20px;
height: 600px;
}

div.surf2right
{
display: inline-block;
height: 600px;
width: 55%;
}
<div class="surf2">
    <div class="surfleft">
    <p class="title1">Interactive games</p>
        <ul style="font-size: 1.5em">
        <li>We offer various games for you to enjoy, including object recognition exercises, multiple-choice quizzes, and error-spotting challenges.</li>
            <li>These games are designed to make learning English engaging and rewarding.</li>   
          </ul>
    </div>
    <div class="surf2right">
    <img src="console.png">
    </div>
</div>

Answer №1

The inline-block property always creates a space. You can either utilize float: left; or eliminate the space:

div.surf2 {
  position: absolute;
  background: #41c3ac;
  height: 600px;
}
div.surfleft {
  display: inline-block;
  width: 45%;
  padding-top: 80px;
  height: 600px;
}
div.surf2right {
  display: inline-block;
  height: 600px;
  width: 55%;
}
<div class="surf2">
  <div class="surfleft">
    <p class="title1">Interactive games</p>
    <ul style="font-size: 1.5em">
      <li>Explore various game types such as object recognition, multiple-choice exercises, and spot-the-mistake challenges.</li>
      <li>These games are designed to make learning and practicing English enjoyable yet challenging.</li>
    </ul>
  </div><<!--
  --><div class="surf2right">
    <img src="console.png">
  </div>
</div>

Also, consider eliminating the padding-left or using box-sizing: border-box.

div.surf2 {
  position: absolute;
  background: #41c3ac;
  height: 600px;
}
div.surfleft {
  display: inline-block;
  width: 45%;
  padding-top: 80px;
  height: 600px;
  padding-left: 20px;
  box-sizing: border-box;
}
div.surf2right {
  display: inline-block;
  height: 600px;
  width: 55%;
}
<div class="surf2">
  <div class="surfleft">
    <p class="title1">Interactive games</p>
    <ul style="font-size: 1.5em">
      <li>Explore various game types such as object recognition, multiple-choice exercises, and spot-the-mistake challenges.</li>
      <li>These games are designed to make learning and practicing English enjoyable yet challenging.</li>
    </ul>
  </div><<!--
  --><div class="surf2right">
    <img src="console.png">
  </div>
</div>

Answer №2

<html>
<head>
<style type="text/css">
#content {

width: 980px;
height: 500px;
padding: 20 20 20 20;
background-color: #00fcff }
#package_update {

width: 680;
height: 500;
float: left;
background-color: #aaaaaa }
#previous_update {

width: 280;
height: 500;
float: right;
background-color: #ffcc00 }
</style>
</head>
<body>
<div id="content">

<div id="package_update"></div>
<div id="previous_update"></div>

</div>
</body>
</html>

Answer №3

To fix the issue of the div going over 100%, remove the padding-left property and add float: left to both divs. You can also consider removing the display: inline-block property.

div.surf2
{
position:absolute;
background: #41c3ac;
height: 600px;
}

div.surfleft
{
width: 45%;
padding-top: 80px;
height: 600px;
}

div.surf2right
{
height: 600px;
width: 55%;
}

div.surf2right, div.surfleft {
    float: left;
}
<div class="surf2">
    <div class="surfleft">
    <p class="title1">Interractive games</p>
        <ul style="font-size: 1.5em">
        <li>We have different types of games you can play, testing your abilities to recognise objects, multiple choice exercises and also putting you to the test of spotting mistakes.</li>
            <li>Those games are designed to help you learn and practice English combining fun with hard work.</li>   
          </ul>
    </div>
    <div class="surf2right">
    <img src="console.png">
    </div>
</div>

Answer №4

Give this a shot:

.surf2 {
    width: fit-content;
    position:absolute;
    background: #41c3ac;
    height: 600px;
}


.surf2 div.surfleft {
    float:left;
    display: block;
    width: 45%;
    padding-top: 80px;
    height: 600px;
 }

.surf2 div.surfright {
    float:right;
 }

 <div class="surf2">
    <div class="surfleft">
        <p class="title1">Interactive games</p>
         <ul style="font-size: 1.5em">
            <li>Explore various types of games that challenge your object recognition, test your multiple-choice skills, and assess your ability to spot errors.</li>
            <li>These games are designed to help you enhance your English proficiency through an engaging learning experience.</li>   
          </ul>
    </div>
    <div class="surf2right">
    <img src="console.png">
    </div>
</div>

The use of width: fit-content will adjust the div's width based on the content inside it

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

Utilizing JSON and select for dependency management

Let's say we have a JSON object: { "A": { "1": "1", "2": "2", "3": "3" }, "B": { "4": "4", "5": "5", "6": "6" }, "C": { "7": "7", "8": "8" } } And we also have ...

Get rid of any empty space in the image preview icon

Is there a way to eliminate the white space that appears when mixing landscape and portrait images? I want the images to move up and fill the space, even if they don't align perfectly. Additionally, I would like the images to resize based on the scal ...

node index of data that has been posted

My HTML file is quite straightforward: <form method="post" action="http://localhost:3000/post"> <input name="name" type="text"/><br /> <input name="last_name" type="text"/><br /> <button id="submit" type="submit"& ...

Tips for retrieving the current value of an input using AJAX

Currently, I am in the process of developing an e-commerce website using Laravel. One issue I am facing is capturing the new value of an input when it changes. This input is located on the cart page where users have the option to adjust the quantity of a p ...

How to truncate text in a cell of a Vuetify data table using CSS styling

Is there a way to truncate text in a cell using CSS without it wrapping around or overflowing? The ideal CSS code should include: .truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } However, the current implementation caus ...

Tips for creating a cohesive group of HTML elements within an editable area

Imagine having a contenteditable area with some existing content: <div contenteditable="true"> <p>first paragraph</p> <p> <img width='63' src='https://developer.cdn.mozilla.net/media/img/mdn-logo-s ...

Presentation for a div's background image with the use of jQuery

My header contains a large div with text contents and boxes, along with a background image. Now I want to create a slideshow for this div's background. Does anyone know how to make a slideshow for a div's background image? I've searched ex ...

Scrolling through four limited list items automatically

Hey there! I am currently working on a design project. You can check it out here. I'm trying to replicate a section from another site, which you can see here. <div class="latest-winners-container"> <h3 class="header">Latest Winners< ...

filtering elements with selectable functionality in jQuery UI

I'm trying to exclude the <div> children from being selected within this list item and only select the entire <li>. The structure of the HTML is as follows: <ul id="selectable"> <li> <div id="1"></div> ...

Struggling to close the dropdown with jquery

Check out this code snippet: https://jsfiddle.net/rajat_bansal/rtapaew5/1/ The jQuery section below is causing some trouble: $(document).ready(function(e) { $(".sub-handle").click(function() { if(!$(this).hasClass('showing-sub&ap ...

"Enhance your HTML table by selecting and copying cell values with a simple click and CTRL +

I stumbled upon a fantastic script for highlighting HTML table rows and it's working perfectly: I decided to modify the onclick event to onmouseover and included additional code to select a cell by clicking on it. Now I can select, check which one is ...

Enhance user experience with HTML-tags in tooltips

Is there a way to display a tooltip in Twitter Bootstrap that includes HTML tags for formatting? Currently, I am using the following code: <a class="my-tooltip" rel="tooltip" href="#" data-original-title="CPR + O<sub>2</sub>.">First-Aid ...

Tips for creating a hidden tab that only appears when hovered over

When hovering over the link tag .cat-link a.navlink, each subcategory tab .subcategories div is displayed. However, I would like to hide all tabs initially and have them appear only when hovered over with the mouse. Once the mouse is removed, I want the t ...

Convert HTML files to .csv format

Is there a way to export a table from an HTML page directly to a .csv file without having to use additional code? Currently, I am using the following method: private void GenerateExcelFileOnType(string filePath1, ref DataTable dt) { if ...

Is there a way to incorporate text fields and input in BootstrapDialog?

Is it possible to include text fields and inputs within this modal using BootstrapDialog without modifying the library? Additionally, can I use $.ajax() inside? <button class="btn btn-primary" id="add_item">New item</button> jquery $('# ...

Unable to assign unique identifiers to elements within a user interface framework

I am having difficulty assigning an id to components. Scenario 1: - Trying to assign an id to an HTML component. <h1 id="demo-h1">Demo Heading</h1> Assigning id to HTML component Scenario 2: - Attempting to assign an id to a componen ...

Tips for managing the onloadedmetadata event

If a video file cannot be played by the browser due to format or codec issues, I want to notify the user about it. When the browser is unable to play a video (because of unsupported format or codec), the onloadedmetadata event does not occur. I have some ...

Enhance the functionality of various textareas by implementing bullets for easier organization and formatting

Is there a way to add bullets to hidden textareas that are created dynamically? Currently, I can add bullets to visible textareas but would like the functionality to extend to newly created ones as well. Additionally, is it possible for these new bullets t ...

What is the best way to implement a modal that can toggle dark mode using the Konami code, with the added functionality of a close button?

Recently, I attempted to create a Modal window that would activate when the Konami code (↑↑↓↓←→←→BA) is typed. As someone new to JavaScript, I'm still learning and open to feedback. While I have the coding part figured out, I need assi ...

Tips for sending an array of input field values through an Ajax request

In my current web form, there is a dynamic option that adds rows with the same attributes. These rows need to be submitted through an Ajax call to my PHP for insertion into the database. Access: <tr> <td><input type"text" name="access[nam ...