One inner DIV positioned in the center flanked by two others on either side

In this scenario, we encounter two different situations:
1. Divs A & B are placed side by side within a container
2. Div A is centered in the same container

For reference, you can view an example here

.container_one, .container_two {width:200px; height:100px; background-color:red;}
.container_one .inner_a {width:100px; background-color:green; float:left;}
.container_one .inner_b {width:100px; background-color:blue; float:right;}
.container_two .inner_a{width:100px; margin:0 auto; background-color:green;}

<div class="container_one">
    <div class="inner_a">
        a
    </div>
    <div class="inner_b">
        b
    </div>
</div>
<br>
<div class="container_two">
    <div class="inner_a">
        a
    </div>
</div>

If both containers have the same class (unlike the given example), and the number of inner divs varies between 1 or 2, it becomes challenging to create CSS that accommodates both situations seamlessly. Despite numerous attempts, achieving this has proven to be difficult.

Answer №1

Consider using inline-block instead of floats for better layout consistency. Apply the inner_a rules to both divs and eliminate the need for floats.

.container_one, .container_two {
  background-color: #ff0000;
  height: 100px;
  text-align: center;
  width: 200px;
}

.container_two .inner_a {
  background-color: #008000;
  display: inline-block;
  width: 100px;
}

Answer №2

This amazing code is both efficient and effective:

.inner_a  {
  background-color: green;
  display: table;
  margin: 0 auto;
  width: 100px;
}

.container_one .inner_a  {
    float:left;
}
    .container_one,  .container_two  {
    width:200px;
     height:100px;
     background-color:red;
}

.container_one .inner_b  {
    width:100px;
     background-color:blue;
     float:right;
}

Answer №3

If you're looking to achieve a different layout, consider using the CSS property display:inline-block instead of floating elements. You can then style the divs with the class .inner_a specifically when they are the last child within their parent container.

.container_one>div{
    display:inline-block;
    width:100px;
}
.inner_a{
    background-color:green;
}
.inner_a:last-child{
    margin:0 auto;
}
.inner_b{
    background-color:blue;
}

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

A guide on aligning text at the center of a List with an included image

I'm new to CSS and struggling to figure out how to vertically center text within an unordered list that also contains an image. I would like to add a logo to my navigation ul and have the text centered, but it is currently sticking to the bottom of t ...

Issue with color display inconsistency in webgrid across various segments

https://i.sstatic.net/UW17M.png My goal is to display section status colors in a webgrid and divide them percentage-wise, but I am facing issues with some of the rows. Here is the cshtml script for the webgrid section status: webGrid.Column(header: "Sec ...

With Jquery, my goal is to position a div outside of another div element

I need to place a div tag in a specific way, like this: <div class="abc"> <div class="mySlides fade"> <img src="img_nature_wide.jpg" style="width:100%"> </div> <div> Using jQuer ...

Ensure Bootstrap input maintains a fixed pixel width unless on a smaller screen, where it should expand to

With Bootstrap, I am trying to create form fields/inputs that are 200 pixels wide by default, but switch to 100% width when viewed on a smartphone with a screen width less than 768 pixels. <form> <div class="form-group responsive200"> &l ...

Enhance your iPhone web app with high-resolution retina images!

As I develop a mobile web application accessible on any smartphone, I have the index.html file available for review: The app includes 2 jQuery functions. One detects if the user is using an iPhone and displays a bubble with instructions to add it to the h ...

Ways to apply background to a div element

I attempted to place an image as the background of a div, but it seems to have vanished! I would like the background image to cover the entire page. Thank you for your assistance. <!DOCTYPE html> <html> <head> <title></title& ...

Incorporating PHP script within a stylesheet

Is it possible to include php code within a css file for adding conditions to css properties? styles.css <?php if($suserid == 2) { ?> .proverb { border:0px solid blue; margin-left:34px; width:250px !important; margin-top:6px; } <?php } e ...

What is the best way to retrieve the unique identifier of dynamically created divs and showcase a message based on that identifier?

Is it possible to retrieve the id of a dynamically generated div and show a unique message when each div is clicked in the code snippet found at this link? function get_random_color() { var letters = '0123456789ABCDEF'.split(''); ...

The fadeOut() method is currently only functioning on the initial item

Recently, I've been experimenting with jQuery and I decided to create a simple to-do list application. However, I encountered an issue with my code. When I check an item that is not the first item on the list, the fadeOut() function does not work prop ...

Comparing the positions of elements in Selenium WebDriver with PHP

One of the elements on my webpage serves as a button that reveals a drop-down menu. Due to various factors affecting the page layout, there were some alignment issues between the button and the menu until a few bugs were fixed. I am now looking for a way t ...

Rearranging Elements with Bootstrap 4 Grid System When Resizing Window

I need help with designing a responsive layout for my website. Currently, I have a simple layout with 3 elements that I want to display differently based on screen size. On larger screens, I want the elements to be arranged like the layout at the top in t ...

The Impact of Navbar Size across Different Screen Sizes

Looking to create a responsive navbar for my website, but struggling with button alignment on smaller screens. Any tips on keeping all navbar elements in line across different screen sizes? <!--Navigation with Button --> <?php if(session_status ...

When CSS width:auto expands beyond its intended length

I'm experiencing an issue when I set the width to auto, the div expands to full length rather than adjusting to the width of the inner divs. Here is the HTML code: <div class="vidswraper"> <div> <div class="videoimage"> <img src ...

Does the body element inherit the line height property?

Below is some simple HTML code consisting of a div and a span: <div id="container">Some text to affect the width</div> <span>A</span> I am currently using Eric Meyer's CSS Reset, which sets the line-height of the body to 1 an ...

When Ruby on Rails is combined with HTML and CSS and marked as selected, it will be displayed with

I'm currently in the process of developing my cookbook app and the next feature on my agenda is the grocery list functionality. Once on the recipe page, users will have the option to click on an "Add to Grocery List" link which will redirect them to a ...

Looking to adjust the positioning of text being inserted into a column - unable to utilize text-align or float properties

I'm struggling to position my text in a specific area within a column using CSS. Our app displays a price when a quantity is selected, but I can't seem to center the $14 under 'Item Total'. Even using float: center; hasn't worked f ...

Sass includes line-specific comments to indicate where a certain definition is located

There seems to be a strange occurrence when I save my file and it's generated by Compass. Each declaration is followed by a comment indicating the line of the sass file, like this: /* line 55, ../sass/app.scss */ In addition, my config.rb file conta ...

What could be causing the h4 tag to disappear when a clip path is applied to a sibling div?

Check out this codepen here on codepen where the h4 tag is not displaying fully above the pseudo element. I've attempted to apply a pseudo-selector, but so far nothing has been effective. Am I missing something here? Any assistance would be greatly a ...

I am looking to improve my form submission process by using JavaScript to only submit the form itself, rather than the

I have integrated JS and HTML code. The issue I am facing is that upon submission, the entire page is being submitted instead of just my form. function submit_by_id() { var name = document.getElementById("name").value; var email = document.getElementByI ...

What is the correct way to utilize Bootstrap grid offsets?

Recently, I have been working on a webpage that features a unique layout using Bootstrap grid rows. Each row consists of a paragraph of text and an image. The layout is designed to have the text occupy specific columns on large screens, with the image shif ...