How to make a div with no content have a width?

前端 未结 11 1781
余生分开走
余生分开走 2020-12-12 20:13

I am trying to add a width to a div, but I seem to be running into a problem because it has no content.

Here is the CSS and HTML I have so far, but it is

相关标签:
11条回答
  • 2020-12-12 20:25

    You add to this DIV's CSS position: relative, it will do all the work.

    0 讨论(0)
  • 2020-12-12 20:26

    Use CSS to add a zero-width-space to your div. The content of the div will take no room but will force the div to display

    .test1::before{
       content: "\200B";
    }
    
    0 讨论(0)
  • 2020-12-12 20:27

    There are different methods to make the empty DIV with float: left or float: right visible.

    Here presents the ones I know:

    • set width(or min-width) with height (or min-height)
    • or set padding-top
    • or set padding-bottom
    • or set border-top
    • or set border-bottom
    • or use pseudo-elements: ::before or ::after with:
      • {content: "\200B";}
      • or {content: "."; visibility: hidden;}
    • or put   inside DIV (this sometimes can bring unexpected effects eg. in combination with text-decoration: underline;)
    0 讨论(0)
  • 2020-12-12 20:29

    Either use padding , height or &nbsp for width to take effect with empty div

    EDIT:

    Non zero min-height also works great

    0 讨论(0)
  • 2020-12-12 20:34

    Try to add display:block; to your test1

    0 讨论(0)
  • 2020-12-12 20:36

    a div usually needs at least a non-breaking space ( ) in order to have a width.

    0 讨论(0)
提交回复
热议问题