Three methods that allow the parent element to be highly adaptive when the child element is floating (and solutions to the problem of simultaneous floating and absolute positioning of multiple subset elements)

Source: Internet
Author: User

For example:

<! DOCTYPE html>

Effect:


This is the problem that we often encounter, that is, the height of the parent element is collapsing when the child element floats ... but calm down, we can first think of setting a height for the parent element to solve it, but we have to adapt ourselves. Self adaptation. Self adaptation. Do not force the parent element to stand up, then, this time we have the following three ways to make the parent element consciously stand up for the child element prop up a piece of sky.

method One:

This method is more commonly used, that is, to add the parent element:

Overflow:hidden;


That's good, just fine.


Although this method can solve a highly adaptive problem, However, it would be awkward if the parent element had an absolute positioning at the same time as a subset element, and it was positioned outside the bounds of the parent element, because the absolutely positioned child element (part one) could not be found. , eh, are all overflow:hidden; Harm, was hidden.

Just like this:

<! DOCTYPE html>


This problem I still often encounter, I do not know whether you have this experience. However, there is always a solution.

Method Two:

The overflow:hidden of the parent element in the above code; To

Float:left;

(right also can)

Read it:


It's OK. Thus, the adaptive problem of the parent element is solved, and the absolute positioning problem of the child element is solved. Of course, due to the use of the parent element of the floating cause of its parent element if there is any problem, we need to be flexible according to the actual situation of the solution.


So it seems that the above two methods are still flawed, there is no one can be more beautiful (really beautiful) to solve this problem method. Yes, yes, we continue:

Method Three:

We do not set Overflow:hidden for the parent element; Also does not set float:left;. We add the following CSS for the parent element:

			#parent:: After {
				content: "";
				Display:block;
				Clear:both;
			}

Then, "Refresh" a little, haha, the effect came out (IBID.), where display set to block or Inline-block can be. And this method is also very beautiful, and then, like it, haha.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.