What is the best way to position divs on opposite ends around another div?

I have experimented with this scenario so far,

http://jsfiddle.net/BXSJe/4/

My goal is to position two divs on the left and right sides within another div, using float:left and float:right resulted in them appearing on separate lines.

What I envision is something like this,

[[leftcap]................. TITLE ............... [rightcap]]

I apologize for not being able to provide a clearer representation.

HTML

<div id="shell">
    <div id="title">
        <div id='leftcap'>o</div>
        TITLE HERE
        <div id='rightcap'>x</div>
    </div>
    <div id="content">Content</div>
</div>

CSS

 #shell {
 width:500px;
 height:300px;
 background:lightGrey; 
}

#title {
 background:green;
 padding:5px;
 border-radius:25%;   
 text-align:center;
}
#content
{
    text-align:center;
    vertical-align:center;
}
#leftcap
{
    width:10%;
}
#rightcap
{
    width:10%;
}
#leftcap,#rightcap
{
    height:100%;
    width:10%;
    background:red;
}

UPDATE: I managed to address the issue by using the float property. I now have another query - how can I vertically center align text within a container div?

Answer №1

To achieve the desired layout, utilize CSS's float property to position elements either to the left or right.

View the revised code example here: http://jsfiddle.net/BXSJe/820/

I trust this information proves useful. :)

Answer №2

To implement this layout, you will need to structure your HTML and CSS code as follows:

<div id="mainDiv">
    <div id="floatLeft">Content floated to the left here</div>
    <div id="floatRight">Content floated to the right here</div>
</div>

For the styling, use the following CSS rules:

div {
    background-color: Red;
    height: 100px;
    width: 100%;
}

#floatLeft {
    width: 100px;
    float: left;
    background-color: Blue;
}

#floatRight {
    width: 100px;
    float: right;
    background-color: Gray;
}

You can view a live demo of this code here: http://jsfiddle.net/g6U8n/

I hope this explanation helps you achieve the desired layout!

Answer №3

To enhance your code, consider adding some additional properties. Take a look below:


<div id="shell">
    <div id="title">
        <div id='leftcap'>o</div>
        TITLE HERE
        <div id='rightcap'>x</div>
    </div>
    <div id="content">Content</div>
</div>

CSS


#shell {
    width:500px;
    height:300px;
    background:lightGrey; 
}

#title {
    background:green;
    padding:5px;
    border-radius:25%;   
    text-align:center;
}

#content {
    text-align:center;
    vertical-align:center;
}

#leftcap {
    width:10%;
    display:inline-block;
    float:left;
}

#rightcap {
    width:10%;
    display:inline-block;
    float:right;
}

#leftcap, #rightcap {
    height:100%;
    width:10%;
    background:red;
}

These additions should enhance your code. Good luck! :)

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

Is it possible to pass multiple IDs into document.getElementById?

I am working on a form that contains 45 dropdown lists and currently, I am using the following code for validation. Is there a way to modify this code so that I can use a single function to validate all 45 dropdown lists? Below is the Function: function ...

I'm just starting to dabble in Perl and am curious about a few things regarding regular expressions

I am currently teaching myself Perl and find that examples are the most effective way for me to learn. Right now, I am delving into a basic Perl script that scrapes a specific blog and have encountered some confusion regarding a few of the regex statements ...

Error Encountered: POST method not supported in ajax request using djangoIs this a

I am currently encountering an issue while trying to pass form data values through ajax. I keep getting a method not allowed error when attempting to add a comment on a blog post. The form below is located inside the blog_detail page: <form id="co ...

A method to apply a class to the third <li> element using javascript

Can someone help me figure out how to add a class to the third element using Javascript? Here is the structure I am working with: <ul class="products-grid row four-columns first"> <li class="item"></li> <li class="item"></li&g ...

Generating a table with two columns utilizing ng-repeat

I am currently dealing with an array of objects that I need to showcase in a two-column layout. The challenge arises because some columns have more data than others, requiring equi-height rows. Despite utilizing Bootstrap and clearfix in my attempts to di ...

Parent whose height is less than that of the child element

I'm working on creating a side menu similar to the one on this website. However, I'm facing an issue with the height of the #parent list element. I want it to match the exact same height as its content (the #child span - check out this jsfiddle). ...

The functionality of cloning in jQuery may encounter an issue where the text field remains enabled if the user selects an option labeled "other

Currently, I am working on a jQuery clone with my existing code and everything is functioning well. In the first scenario, if the user selects other from the dropdown menu, the text field becomes enabled. In the second scenario, when the user clicks ...

A guide to displaying the date retrieved from a JSON data file API request using JavaScript

How can I display the date from a JSON data file API call using JavaScript? I am facing difficulty in showing the date on the dashboard display page. I am utilizing JavaScript with async-await for calling the API, and Bootstrap 4 for design. The function ...

Using a variable with the :contains selector

function checkAndUpdateStatus(newName) { $('#myTable').children('tr').remove(":contains('" + newName + "')"); }; The function above is attempting to remove table rows in 'myTable' containing a string matching the ...

Automatically adjusting the top margin of the main content section depending on the variable height of a fixed header

Visit our mobile site here (mobile view) Currently, I have set the margin-top of .main-content based on sticky-animation, which keeps the header fixed at the top on mobile. However, there is a div with a close button (X) that alters its height dynamically ...

Looking for a jQuery plugin that helps with form alignment?

In a blog post comment, I came across an interesting jQuery form alignment plugin: jQuery.fn.autoWidth = function(options) { var settings = { limitWidth : false } if(options) { jQuery.extend(settings, options); }; ...

Display the spinner until the data is successfully updated in the DOM using Angular

Dealing with a large dataset displayed in a table (5000 rows), I am attempting to filter the data using column filters. When applying the filter, I have included a spinner to indicate that the filtering operation is in progress. However, due to the quick ...

Learn the steps to Toggle Javascript on window resize

Is there a way to toggle Javascript on and off when the window is resized? Currently, resizing the window causes the navigation bar to stick and remain visible above the content. <script> if ( $(window).width() <= 1200 ) { }else{ $('nav& ...

Why is the AJAX request not functioning properly after using jQuery's hide method?

It seems that the first AJAX call works fine when I hide the div, but subsequent requests are not going through. a) Clicking on the signup button triggers an AJAX request to display the details. b) Pressing the hide button hides everything within that di ...

Step-by-step guide to swapping an element with a textarea element using javascript

My current project involves creating a user profile that includes a text box where users can describe themselves. I've already implemented a separate page for editing the profile, but now I want to add a feature where users can hover over their descri ...

If a radio button is chosen, the input field must be filled out

Can someone assist me with this issue? I currently have 2 radio buttons and it is mandatory to select one of them. If the user chooses the first option and submits the form, the information gets sent successfully as intended. However, if the user select ...

How do you determine the dimensions of a background image?

My div is a perfect square, measuring 200 pixels by 200 pixels. The background image I've chosen to use is larger at 500 pixels by 500 pixels. I'm searching for a method to ensure that the entire background image fits neatly within the confines ...

Material UI - Clear all designs from the slate

Is it possible to completely override all default material-ui styles and implement your own custom style guide for components? This would involve removing all material-ui styles and starting fresh with customized html skeletons. Creating a new theme with m ...

Dynamic Loading of Hovercards using jQuery AJAX

I've been working on integrating jQuery hovercard into our web application, specifically to display HTML content retrieved from an AJAX page when a user hovers over a username link with the data attribute data-toggle="user". Here's what our code ...